]> code.citadel.org Git - citadel.git/blobdiff - webcit/webcit.c
* Finished gzip compression of dynamic pages (when browser supports it)
[citadel.git] / webcit / webcit.c
index 6a20cf870b571cf0879c0a4b98113b6c42d7667f..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;
                }
@@ -768,7 +814,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
 /*
  * Entry point for WebCit transaction
  */
-void session_loop(struct httprequest *req)
+void session_loop(struct httprequest *req, int gzip)
 {
        char cmd[SIZ];
        char action[SIZ];
@@ -804,6 +850,13 @@ void session_loop(struct httprequest *req)
 
        WC->is_wap = 0;
 
+       if (gzip) {
+               WC->gzcompressed = 1;
+       }
+       else {
+               WC->gzcompressed = 0;
+       }
+
        hptr = req;
        if (hptr == NULL) return;
 
@@ -824,10 +877,13 @@ void session_loop(struct httprequest *req)
                        ContentLength = atoi(&buf[16]);
                }
                else if (!strncasecmp(buf, "Content-type: ", 14)) {
-                       strcpy(ContentType, &buf[14]);
+                       safestrncpy(ContentType, &buf[14], sizeof ContentType);
                }
                else if (!strncasecmp(buf, "User-agent: ", 12)) {
-                       strcpy(user_agent, &buf[12]);
+                       safestrncpy(user_agent, &buf[12], sizeof user_agent);
+               }
+               else if (!strncasecmp(buf, "Host: ", 6)) {
+                       safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
                }
                /* Only WAP gateways explicitly name this content-type */
                else if (strstr(buf, "text/vnd.wap.wml")) {
@@ -909,6 +965,14 @@ void session_loop(struct httprequest *req)
                }
        }
 
+       /*
+        * Functions which can be performed without logging in
+        */
+       if (!strcasecmp(action, "listsub")) {
+               do_listsub();
+               goto SKIP_ALL_THIS_CRAP;
+       }
+
        check_for_express_messages();
 
        /*
@@ -945,12 +1009,20 @@ void session_loop(struct httprequest *req)
                output_static(buf);
        } else if (!strcasecmp(action, "image")) {
                output_image();
+
+       /*
+        * All functions handled below this point ... make sure we log in
+        * before doing anything else!
+        */
        } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
                do_login();
        } else if (!WC->logged_in) {
                display_login(NULL);
        }
-       /* Various commands... */
+
+       /*
+        * Various commands...
+        */
 
        else if (!strcasecmp(action, "do_welcome")) {
                do_welcome();
@@ -1132,6 +1204,10 @@ void session_loop(struct httprequest *req)
                change_start_page();
        } else if (!strcasecmp(action, "display_floorconfig")) {
                display_floorconfig(NULL);
+       } else if (!strcasecmp(action, "toggle_self_service")) {
+               toggle_self_service();
+       } else if (!strcasecmp(action, "summary")) {
+               summary();
        } else if (!strcasecmp(action, "diagnostics")) {
                output_headers(1);
 
@@ -1160,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
 }