From 072710cf56b3a60b9a614c17a6c76fb712d869e0 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Mon, 13 Feb 2006 17:49:01 +0000 Subject: [PATCH] webserver.c: don't call realloc() on every single call to client_write(). Instead, start with a 32K buffer and increase its size any time we find we've run out of space. --- webcit/webcit.h | 139 +++++++++++++++++++++++---------------------- webcit/webserver.c | 30 ++++++++-- 2 files changed, 94 insertions(+), 75 deletions(-) diff --git a/webcit/webcit.h b/webcit/webcit.h index da0959ff2..6c306050c 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -322,76 +322,77 @@ struct folder { * HTTP transactions are bound to on e at a time. */ struct wcsession { - struct wcsession *next; /**< Linked list */ - int wc_session; /**< WebCit session ID */ - char wc_username[128]; /**< ??? todo */ - char wc_fullname[128]; /**< ??? todo */ - char wc_password[128]; /**< ??? todo */ - char wc_roomname[256]; /**< ??? todo */ - int connected; /**< ??? todo */ - int logged_in; /**< ??? todo */ - int axlevel; /**< ??? todo */ - int is_aide; /**< ??? todo */ - int is_room_aide; /**< ??? todo */ - int http_sock; /**< ??? todo */ - int serv_sock; /**< ??? todo */ - int chat_sock; /**< ??? todo */ - unsigned room_flags; /**< ??? todo */ - int wc_view; /**< ??? todo */ - int wc_default_view; /**< ??? todo */ - int wc_is_trash; /**< ??? todo */ - int wc_floor; /**< ??? todo */ - char ugname[128]; /**< ??? todo */ - long uglsn; /**< ??? todo */ - int upload_length; /**< ??? todo */ - char *upload; /**< ??? todo */ - char upload_filename[PATH_MAX]; /**< ??? todo */ - char upload_content_type[256]; /**< ??? todo */ - int new_mail; /**< ??? todo */ - int remember_new_mail; /**< ??? todo */ - int need_regi; /**< This user needs to register. */ - int need_vali; /**< New users require validation. */ - char cs_inet_email[256]; /**< User's preferred Internet addr. */ - pthread_mutex_t SessionMutex; /**< mutex for exclusive access */ - time_t lastreq; /**< Timestamp of most recent HTTP */ - int killthis; /**< Nonzero == purge this session */ - struct march *march; /**< march mode room list */ - char reply_to[512]; /**< reply-to address */ - long msgarr[10000]; /**< for read operations */ - int num_summ; /**< ??? todo */ - struct message_summary *summ; /**< ??? todo */ - int is_wap; /**< Client is a WAP gateway */ - struct urlcontent *urlstrings; /**< ??? todo */ - struct wcsubst *vars; /**< ??? todo*/ - char this_page[512]; /**< address of current page */ - char http_host[512]; /**< HTTP Host: header */ - char *preferences; /**< ??? todo */ -#ifdef WEBCIT_WITH_CALENDAR_SERVICE - /** \brief ical???? */ - struct disp_cal { - icalcomponent *cal; /**< cal items for display */ - long cal_msgnum; /**< cal msgids for display */ - } *disp_cal; - int num_cal; /**< ??? todo */ + struct wcsession *next; /**< Linked list */ + int wc_session; /**< WebCit session ID */ + char wc_username[128]; /**< login name of current user */ + char wc_fullname[128]; /**< Screen name of current user */ + char wc_password[128]; /**< Password of current user */ + char wc_roomname[256]; /**< Room we are currently in */ + int connected; /**< nonzero == we are connected to Citadel */ + int logged_in; /**< nonzero == we are logged in */ + int axlevel; /**< this user's access level */ + int is_aide; /**< nonzero == this user is an Aide */ + int is_room_aide; /**< nonzero == this user is a Room Aide in this room */ + int http_sock; /**< HTTP server socket */ + int serv_sock; /**< Client socket to Citadel server */ + int chat_sock; /**< Client socket to Citadel server - for chat */ + unsigned room_flags; /**< flags associated with the current room */ + int wc_view; /**< view for the current room */ + int wc_default_view; /**< default view for the current room */ + int wc_is_trash; /**< nonzero == current room is a Trash folder */ + int wc_floor; /**< floor number of current room */ + char ugname[128]; /**< where does 'ungoto' take us */ + long uglsn; /**< last seen message number for ungoto */ + int upload_length; /**< content length of http-uploaded data */ + char *upload; /**< pointer to http-uploaded data */ + char upload_filename[PATH_MAX]; /**< filename of http-uploaded data */ + char upload_content_type[256]; /**< content type of http-uploaded data */ + int new_mail; /**< user has new mail waiting */ + int remember_new_mail; /**< last count of new mail messages */ + int need_regi; /**< This user needs to register. */ + int need_vali; /**< New users require validation. */ + char cs_inet_email[256]; /**< User's preferred Internet addr. */ + pthread_mutex_t SessionMutex; /**< mutex for exclusive access */ + time_t lastreq; /**< Timestamp of most recent HTTP */ + int killthis; /**< Nonzero == purge this session */ + struct march *march; /**< march mode room list */ + char reply_to[512]; /**< reply-to address */ + long msgarr[10000]; /**< for read operations */ + int num_summ; /**< number of messages in mailbox summary view */ + struct message_summary *summ; /**< array of messages for mailbox summary view */ + int is_wap; /**< Client is a WAP gateway */ + struct urlcontent *urlstrings; /**< variables passed to webcit in a URL */ + struct wcsubst *vars; /**< HTTP variable substitutions for this page */ + char this_page[512]; /**< URL of current page */ + char http_host[512]; /**< HTTP Host: header */ + char *preferences; /**< WebCit preferences for this user */ +#ifdef WEBCIT_WITH_CALENDAR_SERVICE + /** \brief ical???? */ + struct disp_cal { + icalcomponent *cal; /**< cal items for display */ + long cal_msgnum; /**< cal msgids for display */ + } *disp_cal; + int num_cal; /**< number of calendar items for display */ #endif - struct wc_attachment *first_attachment; /**< ??? todo */ - char last_chat_user[256]; /**< ??? todo */ - char ImportantMessage[SIZ]; /**< ??? todo */ - int ctdl_pid; /**< Session ID on the Citadel server */ - char httpauth_user[256]; /**< only for GroupDAV sessions */ - char httpauth_pass[256]; /**< only for GroupDAV sessions */ - size_t burst_len; /**< ??? todo */ - char *burst; /**< ??? todo */ - int gzip_ok; /**< Nonzero if Accept-encoding: gzip */ - int is_mailbox; /**< the current room is a private mailbox */ - struct folder *cache_fold; /**< cache the iconbar room list */ - int cache_max_folders; /**< ??? todo */ - int cache_num_floors; /**< ??? todo */ - time_t cache_timestamp; /**< ??? todo */ - int current_iconbar; /**< What's currently in the iconbar? */ - char floordiv_expanded[32]; /**< which floordiv currently expanded */ - int selected_language; /**< Language selected by user */ - time_t last_pager_check; /**< last time we polled for instant msgs */ + struct wc_attachment *first_attachment; /**< linked list of attachments for 'enter message' */ + char last_chat_user[256]; /**< ??? todo */ + char ImportantMessage[SIZ]; /**< ??? todo */ + int ctdl_pid; /**< Session ID on the Citadel server */ + char httpauth_user[256]; /**< only for GroupDAV sessions */ + char httpauth_pass[256]; /**< only for GroupDAV sessions */ + size_t burst_len; /** current_iconbar */ diff --git a/webcit/webserver.c b/webcit/webserver.c index 40b52a6f6..6c59f458d 100644 --- a/webcit/webserver.c +++ b/webcit/webserver.c @@ -201,13 +201,29 @@ int client_read_to(int sock, char *buf, int bytes, int timeout) */ ssize_t client_write(const void *buf, size_t count) { + char *newptr; + size_t newalloc; if (WC->burst != NULL) { - WC->burst = - realloc(WC->burst, (WC->burst_len + count + 2)); - memcpy(&WC->burst[WC->burst_len], buf, count); - WC->burst_len += count; - return (count); + if ((WC->burst_len + count) >= WC->burst_alloc) { + newalloc = (WC->burst_alloc * 2); + if ((WC->burst_len + count) >= newalloc) { + newalloc += count; + } + newptr = realloc(WC->burst, newalloc); + if (newptr != NULL) { + WC->burst = newptr; + WC->burst_alloc = newalloc; + } + } + if ((WC->burst_len + count) < WC->burst_alloc) { + memcpy(&WC->burst[WC->burst_len], buf, count); + WC->burst_len += count; + return (count); + } + else { + return(-1); + } } #ifdef HAVE_OPENSSL if (is_https) { @@ -233,7 +249,8 @@ void begin_burst(void) WC->burst = NULL; } WC->burst_len = 0; - WC->burst = malloc(SIZ); + WC->burst_alloc = 32768; + WC->burst = malloc(WC->burst_alloc); } @@ -316,6 +333,7 @@ void end_burst(void) the_data = WC->burst; WC->burst_len = 0; + WC->burst_alloc = 0; WC->burst = NULL; #ifdef HAVE_ZLIB -- 2.39.2