* Finished gzip compression of dynamic pages (when browser supports it)
authorArt Cancro <ajc@citadel.org>
Sat, 14 Sep 2002 04:02:36 +0000 (04:02 +0000)
committerArt Cancro <ajc@citadel.org>
Sat, 14 Sep 2002 04:02:36 +0000 (04:02 +0000)
webcit/ChangeLog
webcit/context_loop.c
webcit/floors.c
webcit/html2html.c
webcit/webcit.c
webcit/webcit.h

index f5efca2168e0826d0ef009f0b6d7d9350a49897e..8b03cdbcd8ff5d1a5cf533baf172bc39df1e4101 100644 (file)
@@ -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 <ajc@uncnsrd.mt-kisco.ny.us>
 
 1998-12-03 Nathan Bryant <bryant@cs.usm.maine.edu>
        * webserver.c: warning fix
-
index 9b744f13f12708675abade12df20fa94ff8f489d..3a9a29178d9dc1fdc604139a72d738c889fdeb5e 100644 (file)
 #include <stdarg.h>
 #include <pthread.h>
 #include <signal.h>
-
-#ifdef WITH_ZLIB
-#include <zlib.h>
-#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; i<num_tokens(encodings, ','); ++i) {
@@ -264,6 +260,7 @@ void context_loop(int sock)
                                }
                        }
                }
+#endif
 
                hptr = (struct httprequest *)
                        malloc(sizeof(struct httprequest));
index 48cca50f51fd88a7cf00154a4594b52871eccfc5..511a5907ea7c6281153a6acc9687c8572a7db4e2 100644 (file)
@@ -43,7 +43,7 @@ void display_floorconfig(char *prepend_html)
        output_headers(3);
 
        if (prepend_html != NULL) {
-               write(WC->http_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 */
index 023ab7c4af8184dfc9193003d28079a8d3964f38..a0a976ffc3d58cf776af23fbd93f2bcd787afe32 100644 (file)
@@ -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("<BR><BR>\n");
index 46623d1dafbd7630d76ee4b42e553011f4631808..d4d9cacc41f01bc46355aebc550ebc4298015749 100644 (file)
@@ -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("<HTML><HEAD><TITLE>");
                escputs(serv_info.serv_humannode);
                wprintf("</TITLE>\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
 }
index a442b725e810e958937c5bf8cb595609f4382583..4aac82df13adc3984ea2b00c16c34bbf7ebd91f0 100644 (file)
@@ -1,5 +1,9 @@
 /* $Id$ */
 
+#ifdef WITH_ZLIB
+#include <zlib.h>
+#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);