From 42d25facbd8e3dbaeda448095c5761768195df03 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Sat, 14 Sep 2002 04:02:36 +0000 Subject: [PATCH] * Finished gzip compression of dynamic pages (when browser supports it) --- webcit/ChangeLog | 4 ++- webcit/context_loop.c | 7 ++--- webcit/floors.c | 2 +- webcit/html2html.c | 2 +- webcit/webcit.c | 69 ++++++++++++++++++++++++++++++++++++++++--- webcit/webcit.h | 9 ++++++ 6 files changed, 81 insertions(+), 12 deletions(-) diff --git a/webcit/ChangeLog b/webcit/ChangeLog index f5efca216..8b03cdbcd 100644 --- a/webcit/ChangeLog +++ b/webcit/ChangeLog @@ -1,4 +1,7 @@ $Log$ +Revision 400.4 2002/09/14 04:02:36 ajc +* Finished gzip compression of dynamic pages (when browser supports it) + Revision 400.3 2002/09/13 20:40:53 ajc * Detect when the client browser is capable of gzip encoding (actual encoding is not implemented yet) @@ -947,4 +950,3 @@ Sun Dec 6 19:50:55 EST 1998 Art Cancro 1998-12-03 Nathan Bryant * webserver.c: warning fix - diff --git a/webcit/context_loop.c b/webcit/context_loop.c index 9b744f13f..3a9a29178 100644 --- a/webcit/context_loop.c +++ b/webcit/context_loop.c @@ -35,11 +35,6 @@ #include #include #include - -#ifdef WITH_ZLIB -#include -#endif - #include "webcit.h" #include "webserver.h" @@ -255,6 +250,7 @@ void context_loop(int sock) got_cookie = 1; } +#ifdef WITH_ZLIB if (!strncasecmp(buf, "Accept-encoding: ", 17)) { extract_token(encodings, &buf[17], 0, ';'); for (i=0; ihttp_sock, prepend_html, strlen(prepend_html)); + http_write(WC->http_sock, prepend_html, strlen(prepend_html)); } serv_printf("LFLR"); /* FIXME put a real test here */ diff --git a/webcit/html2html.c b/webcit/html2html.c index 023ab7c4a..a0a976ffc 100644 --- a/webcit/html2html.c +++ b/webcit/html2html.c @@ -166,7 +166,7 @@ void output_html(void) { } /* Output our big pile of markup */ - write(WC->http_sock, converted_msg, output_length); + http_write(WC->http_sock, converted_msg, output_length); /* A little trailing vertical whitespace... */ wprintf("

\n"); diff --git a/webcit/webcit.c b/webcit/webcit.c index 46623d1da..d4d9cacc4 100644 --- a/webcit/webcit.c +++ b/webcit/webcit.c @@ -152,6 +152,31 @@ char *bstr(char *key) } +#ifdef WITH_ZLIB + +ssize_t http_write(int fd, const void *buf, size_t count) { + + if (WC->gzfd) { + return gzwrite(WC->gzfd, buf, count); + } + else { + return write(fd, buf, count); + } + + +} + +#else + +ssize_t http_write(int fd, const void *buf, size_t count) { + return write(fd, buf, count); +} + +#endif + + + + void wprintf(const char *format,...) { va_list arg_ptr; @@ -161,7 +186,7 @@ void wprintf(const char *format,...) vsprintf(wbuf, format, arg_ptr); va_end(arg_ptr); - write(WC->http_sock, wbuf, strlen(wbuf)); + http_write(WC->http_sock, wbuf, strlen(wbuf)); } @@ -295,6 +320,9 @@ void output_headers(int controlcode) int suppress_check = 0; char httpnow[SIZ]; static int pageseq = 0; +#ifdef WITH_ZLIB + gzFile temp_gzfd = NULL; +#endif print_standard_html_head = controlcode & 0x03; refresh30 = ((controlcode & 0x04) >> 2); @@ -304,12 +332,23 @@ void output_headers(int controlcode) httpdate(httpnow, time(NULL)); +#ifdef WITH_ZLIB + if (WC->gzcompressed) { + temp_gzfd = gzdopen(WC->http_sock, "wb9"); + } +#endif + if (print_standard_html_head > 0) { wprintf("Content-type: text/html\n"); wprintf("Server: %s\n", SERVER); wprintf("Connection: close\n"); wprintf("Pragma: no-cache\n"); wprintf("Cache-Control: no-store\n"); +#ifdef WITH_ZLIB + if (temp_gzfd != NULL) { + wprintf("Content-Encoding: gzip\n"); + } +#endif } stuff_to_cookie(cookie, WC->wc_session, WC->wc_username, WC->wc_password, WC->wc_roomname); @@ -324,6 +363,13 @@ void output_headers(int controlcode) if (print_standard_html_head > 0) { wprintf("\n"); + +#ifdef WITH_ZLIB + if (temp_gzfd != NULL) { + WC->gzfd = temp_gzfd; + } +#endif + wprintf(""); escputs(serv_info.serv_humannode); wprintf("\n" @@ -452,7 +498,7 @@ void output_static(char *what) bigbuffer = malloc(bytes); fread(bigbuffer, bytes, 1, fp); fclose(fp); - write(WC->http_sock, bigbuffer, bytes); + http_write(WC->http_sock, bigbuffer, bytes); free(bigbuffer); } if (!strcasecmp(bstr("force_close_session"), "yes")) { @@ -497,7 +543,7 @@ void output_image() else { memset(xferbuf, 0, thisblock); } - write(WC->http_sock, xferbuf, thisblock); + http_write(WC->http_sock, xferbuf, thisblock); bytes = bytes - thisblock; accomplished = accomplished + thisblock; } @@ -548,7 +594,7 @@ void output_mimepart() else { memset(xferbuf, 0, thisblock); } - write(WC->http_sock, xferbuf, thisblock); + http_write(WC->http_sock, xferbuf, thisblock); bytes = bytes - thisblock; accomplished = accomplished + thisblock; } @@ -804,6 +850,13 @@ void session_loop(struct httprequest *req, int gzip) WC->is_wap = 0; + if (gzip) { + WC->gzcompressed = 1; + } + else { + WC->gzcompressed = 0; + } + hptr = req; if (hptr == NULL) return; @@ -1183,4 +1236,12 @@ SKIP_ALL_THIS_CRAP: free(WC->upload); WC->upload_length = 0; } + +#ifdef WITH_ZLIB + if (WC->gzfd) { + gzclose(WC->gzfd); + WC->gzfd = NULL; + WC->gzcompressed = 0; + } +#endif } diff --git a/webcit/webcit.h b/webcit/webcit.h index a442b725e..4aac82df1 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -1,5 +1,9 @@ /* $Id$ */ +#ifdef WITH_ZLIB +#include +#endif + #define SIZ 4096 /* generic buffer size */ #define TRACE fprintf(stderr, "Checkpoint: %s, %d\n", __FILE__, __LINE__) @@ -164,6 +168,10 @@ struct wcsession { char this_page[SIZ]; /* address of current page */ char http_host[SIZ]; /* HTTP Host: header */ char *preferences; +#ifdef WITH_ZLIB + int gzcompressed; /* nonzero if compressed output */ + gzFile gzfd; /* stream to send compressed */ +#endif }; #define extract(dest,source,parmnum) extract_token(dest,source,parmnum,'|') @@ -332,3 +340,4 @@ void rename_floor(void); void do_listsub(void); void toggle_self_service(void); void summary(void); +ssize_t write(int fd, const void *buf, size_t count); -- 2.30.2