* inetconf.c: added. Not finished yet.
[citadel.git] / webcit / webcit.c
index 277ca76a04d4590329765b9c78fe2776309dff78..fa577a5be5e12ad0f0a17fb5ac17a34d9422aabd 100644 (file)
@@ -92,11 +92,11 @@ void addurls(char *url)
                for (a = 0; a <= b; ++a)
                        ++up;
 
-               /* locate the & sign */
+               /* locate "&" and "?" delimiters */
                ptr = up;
                b = strlen(up);
                for (a = 0; a < strlen(up); ++a) {
-                       if (!strncmp(ptr, "&", 1)) {
+                       if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
                                b = a;
                                break;
                        }
@@ -152,41 +152,16 @@ char *bstr(char *key)
 }
 
 
-#ifdef WITH_ZLIB
-
-ssize_t http_write(int fd, 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;
-       char wbuf[1024];
+       char wbuf[4096];
 
        va_start(arg_ptr, format);
        vsprintf(wbuf, format, arg_ptr);
        va_end(arg_ptr);
 
-       http_write(WC->http_sock, wbuf, strlen(wbuf));
+       client_write(wbuf, strlen(wbuf));
 }
 
 
@@ -199,16 +174,8 @@ void wprintf(const char *format,...)
  */
 void wDumpContent(int print_standard_html_footer)
 {
-       if (WC->fake_frames) {
-               wprintf("</TABLE>\n");
-               WC->fake_frames = 0;
-       }
-
        if (print_standard_html_footer) {
-               if (print_standard_html_footer != 2) {
-                       wprintf("<BR>");
-               }
-               wprintf("</BODY></HTML>\n");
+               do_template("trailing");
        }
 
 
@@ -219,7 +186,7 @@ void wDumpContent(int print_standard_html_footer)
  * Copy a string, escaping characters which have meaning in HTML.  If
  * nbsp is nonzero, spaces are converted to non-breaking spaces.
  */
-void stresc(char *target, char *strbuf, int nbsp)
+void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
 {
        int a;
        strcpy(target, "");
@@ -241,24 +208,31 @@ void stresc(char *target, char *strbuf, int nbsp)
                        strcat(target, ">");
                else if (strbuf[a] == QU)
                        strcat(target, "\"");
-               else if ((strbuf[a] == 32) && (nbsp == 1)) {
+               else if ((strbuf[a] == 32) && (nbsp == 1))
                        strcat(target, "&nbsp;");
-               } else {
+               else if ((strbuf[a] == '\n') && (nolinebreaks))
+                       strcat(target, "");     /* nothing */
+               else if ((strbuf[a] == '\r') && (nolinebreaks))
+                       strcat(target, "");     /* nothing */
+               else
                        strncat(target, &strbuf[a], 1);
-               }
        }
 }
 
-void escputs1(char *strbuf, int nbsp)
+void escputs1(char *strbuf, int nbsp, int nolinebreaks)
 {
-       char buf[1024];
-       stresc(buf, strbuf, nbsp);
+       char *buf;
+
+       if (strbuf == NULL) return;
+       buf = malloc(2 * strlen(strbuf));
+       stresc(buf, strbuf, nbsp, nolinebreaks);
        wprintf("%s", buf);
+       free(buf);
 }
 
 void escputs(char *strbuf)
 {
-       escputs1(strbuf, 0);
+       escputs1(strbuf, 0, 0);
 }
 
 /*
@@ -295,6 +269,71 @@ void urlescputs(char *strbuf)
 }
 
 
+/*
+ * Copy a string, escaping characters for JavaScript strings.
+ */
+void jsesc(char *target, char *strbuf)
+{
+       int a;
+       strcpy(target, "");
+
+       for (a = 0; a < strlen(strbuf); ++a) {
+               if (strbuf[a] == '<')
+                       strcat(target, "[");
+               else if (strbuf[a] == '>')
+                       strcat(target, "]");
+               else if (strbuf[a] == '\"')
+                       strcat(target, "&quot;");
+               else if (strbuf[a] == '&')
+                       strcat(target, "&amp;;");
+               else if (strbuf[a] == '\'') 
+                       strcat(target, "\\'");
+               else {
+                       strncat(target, &strbuf[a], 1);
+               }
+       }
+}
+
+void jsescputs(char *strbuf)
+{
+       char outbuf[SIZ];
+       
+       jsesc(outbuf, strbuf);
+       wprintf("%s", outbuf);
+}
+
+/*
+ * Copy a string, escaping characters for message text hold
+ */
+void msgesc(char *target, char *strbuf)
+{
+       int a;
+       strcpy(target, "");
+
+       for (a = 0; a < strlen(strbuf); ++a) {
+               if (strbuf[a] == '\'') 
+                       strcat(target, "\\'");
+               else if (strbuf[a] == '\n')
+                       strcat(target, " ");
+               else if (strbuf[a] == '\r')
+                       strcat(target, " ");
+               else {
+                       strncat(target, &strbuf[a], 1);
+               }
+       }
+}
+
+void msgescputs(char *strbuf) {
+       char *outbuf;
+
+       if (strbuf == NULL) return;
+       outbuf = malloc(2 * strlen(strbuf));
+       msgesc(outbuf, strbuf);
+       wprintf("%s", outbuf);
+       free(outbuf);
+}
+
+
 
 
 /*
@@ -309,8 +348,9 @@ void urlescputs(char *strbuf)
  * 3 = HTTP and HTML headers, but no room banner
  *
  * Bit 2: Set to 1 to auto-refresh page every 30 seconds
- *
  * Bit 3: suppress check for express messages
+ * Bit 4: Allow browser to cache this document
+ *
  */
 void output_headers(int controlcode)
 {
@@ -318,42 +358,33 @@ void output_headers(int controlcode)
        int print_standard_html_head = 0;
        int refresh30 = 0;
        int suppress_check = 0;
+       int cache = 0;
        char httpnow[SIZ];
+       char onload_fcn[SIZ];
        static int pageseq = 0;
-#ifdef WITH_ZLIB
-       gzFile temp_gzfd = NULL;
-#endif
-
        print_standard_html_head        =       controlcode & 0x03;
        refresh30                       =       ((controlcode & 0x04) >> 2);
        suppress_check                  =       ((controlcode & 0x08) >> 3);
+       cache                           =       ((controlcode & 0x10) >> 4);
 
        wprintf("HTTP/1.0 200 OK\n");
 
        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"
                        "Server: %s\n", SERVER
                );
-               wprintf("Connection: close\n"
-                       "Pragma: no-cache\n"
-                       "Cache-Control: no-store\n"
-               );
-#ifdef WITH_ZLIB
-               if (temp_gzfd != NULL) {
-                       wprintf("Content-Encoding: gzip\n");
-               }
-#endif
+               if (!cache)
+                       wprintf("Connection: close\n"
+                               "Pragma: no-cache\n"
+                               "Cache-Control: no-store\n"
+                       );
        }
+
        stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
                        WC->wc_password, WC->wc_roomname);
+
        if (print_standard_html_head == 2) {
                wprintf("Set-cookie: webcit=%s\n", unset);
        } else {
@@ -366,58 +397,64 @@ 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"
-                       "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
-                       "<META HTTP-EQUIV=\"expired\" CONTENT=\"28-May-1971 18:10:00 GMT\">\n"
-                       "<META NAME=\"MSSmartTagsPreventParsing\" CONTENT=\"TRUE\">\n");
-               if (refresh30) wprintf(
+               if (refresh30) svprintf("REFRESHTAG", WCS_STRING,
                        "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
-               else wprintf(
+               else svprintf("REFRESHTAG", WCS_STRING,
                        "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
-               wprintf("</HEAD>\n");
-
                /* script for checking for pages (not always launched) */
-               wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
-               wprintf("function launch_page_popup() {\n");
-               wprintf("pwin = window.open('/page_popup', 'CitaPage%d', "
-                       "'toolbar=no,location=no,copyhistory=no,status=no,"
-                       "scrollbars=yes,resizable=no,height=250,width=400');\n",
-                       ++pageseq);
-               wprintf("}\n");
-               wprintf("</SCRIPT>\n");
-               /* end script */
-
-               /* (JavaScript keyboard-based navigation would go here) */
 
+               sprintf(onload_fcn, "function onload_fcn() { \n");
+               if (!WC->outside_frameset_allowed) {
+                       strcat(onload_fcn, "  force_frameset();  \n");
+               }
                if (!suppress_check) if (WC->HaveExpressMessages) {
-                       svprintf("extrabodyparms", WCS_STRING, "%s", 
-                               "onload=\"launch_page_popup()\" ");
+                       strcat(onload_fcn, "  launch_page_popup();  \n");
                        WC->HaveExpressMessages = 0;
                }
+               strcat(onload_fcn, "} \n");
+
+               svprintf("PAGERSCRIPT", WCS_STRING,
+                       "<SCRIPT LANGUAGE=\"JavaScript\">\n"
+                       "function launch_page_popup() {\n"
+                       "pwin = window.open('/page_popup', 'CitaPage%d', "
+                       "'toolbar=no,location=no,copyhistory=no,status=no,"
+                       "scrollbars=yes,resizable=no,height=250,width=400');\n"
+                       "}\n"
+                       "function force_frameset() { \n"
+                       " if (top.frames.length == 0) { \n"
+                       "  top.location.replace('/do_welcome'); \n"
+                       " } \n"
+                       "} \n"
+                       "%s\n"
+                       "</SCRIPT>\n",
+                       ++pageseq,
+                       onload_fcn
+               );
+               /* end script */
+
+
+               do_template("head");
+               clear_local_substs();
+
+               svprintf("extrabodyparms", WCS_STRING, "%s", 
+                       "onload='onload_fcn();' ");
+
                do_template("background");
                clear_local_substs();
+       }
 
        if (print_standard_html_head == 1) {
                wprintf("<A NAME=\"TheTop\"></A>");
-
-               wprintf("<TABLE border=0 width=100%%><TR VALIGN=TOP>"
-                       "<TD>\n");
-
                embed_room_banner(NULL);
-
-               wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
-               
-               WC->fake_frames = 1;
-               }
        }
+
+       if (strlen(WC->ImportantMessage) > 0) {
+               do_template("beginbox_nt");
+               wprintf("<SPAN CLASS=\"errormsg\">"
+                       "%s</SPAN><BR>\n", WC->ImportantMessage);
+               do_template("endbox");
+               strcpy(WC->ImportantMessage, "");
+       }       
 }
 
 
@@ -452,8 +489,9 @@ void check_for_express_messages()
 /* 
  * Output a piece of content to the web browser
  */
-void http_transmit_thing(char *thing, size_t length, char *content_type) {
-       output_headers(0);
+void http_transmit_thing(char *thing, size_t length, char *content_type,
+                        int is_static) {
+       output_headers(is_static ? 0x10 : 0x00);
        wprintf("Content-type: %s\n"
                "Content-length: %ld\n"
                "Server: %s\n"
@@ -463,7 +501,7 @@ void http_transmit_thing(char *thing, size_t length, char *content_type) {
                (long) length,
                SERVER
        );
-       http_write(WC->http_sock, thing, (size_t)length);
+       client_write(thing, (size_t)length);
 }
 
 
@@ -478,6 +516,7 @@ void output_static(char *what)
        char *bigbuffer;
        char content_type[SIZ];
 
+       lprintf(9, "output_static(%s)\n", what);
        sprintf(buf, "static/%s", what);
        fp = fopen(buf, "rb");
        if (fp == NULL) {
@@ -496,8 +535,12 @@ void output_static(char *what)
                        strcpy(content_type, "image/jpeg");
                else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
                        strcpy(content_type, "image/png");
+               else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
+                       strcpy(content_type, "image/x-icon");
                else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
                        strcpy(content_type, "text/html");
+               else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
+                       strcpy(content_type, "text/html");
                else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
                        strcpy(content_type, "text/vnd.wap.wml");
                else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
@@ -507,9 +550,11 @@ void output_static(char *what)
                else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
                        strcpy(content_type, "application/vnd.wap.wmlscriptc");
                else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
-                       wprintf("Content-type: image/vnd.wap.wbmp");
+                       strcpy(content_type, "image/vnd.wap.wbmp");
+               else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
+                       strcpy(content_type, "text/javascript");
                else
-                       wprintf("Content-type: application/octet-stream");
+                       strcpy(content_type, "application/octet-stream");
 
                fstat(fileno(fp), &statbuf);
                bytes = statbuf.st_size;
@@ -518,7 +563,7 @@ void output_static(char *what)
                fread(bigbuffer, bytes, 1, fp);
                fclose(fp);
 
-               http_transmit_thing(bigbuffer, (size_t)bytes, content_type);
+               http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
                free(bigbuffer);
        }
        if (!strcasecmp(bstr("force_close_session"), "yes")) {
@@ -536,8 +581,6 @@ void output_image()
        char buf[SIZ];
        char *xferbuf = NULL;
        off_t bytes;
-       off_t thisblock;
-       off_t accomplished = 0L;
 
        serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
        serv_gets(buf);
@@ -546,32 +589,22 @@ void output_image()
                xferbuf = malloc(bytes);
 
                /* Read it from the server */
-               while (bytes > (off_t) 0) {
-                       thisblock = 4096;
-                       if (thisblock > bytes) {
-                               thisblock = bytes;
-                       }
-                       serv_printf("READ %ld|%ld", accomplished, thisblock);
-                       serv_gets(buf);
-                       if (buf[0] == '6') {
-                               thisblock = extract_long(&buf[4], 0);
-                               serv_read(&xferbuf[accomplished],
-                                       (int) thisblock);
-                       }
-                       else {
-                               memset(&xferbuf[accomplished], 0, thisblock);
-                       }
-                       bytes = bytes - thisblock;
-                       accomplished += thisblock;
-               }
+               read_server_binary(xferbuf, bytes);
                serv_puts("CLOS");
                serv_gets(buf);
 
                /* Write it to the browser */
-               http_transmit_thing(xferbuf, (size_t)accomplished,
-                                       "image/gif");
+               http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
+               free(xferbuf);
 
        } else {
+
+               /* Instead of an ugly 404, send a 1x1 transparent GIF
+                * when there's no such image on the server.
+                */
+               output_static("blank.gif");
+
+               /*
                wprintf("HTTP/1.0 404 %s\n", &buf[4]);
                output_headers(0);
                wprintf("Content-Type: text/plain\n"
@@ -579,13 +612,12 @@ void output_image()
                        "Error retrieving image: %s\n",
                        &buf[4]
                );
-       }
+               */
 
-       if (xferbuf) {
-               free(xferbuf);
        }
 
 
+
 }
 
 /*
@@ -593,42 +625,22 @@ void output_image()
 void output_mimepart()
 {
        char buf[SIZ];
-       char xferbuf[4096];
        off_t bytes;
-       off_t thisblock;
-       off_t accomplished = 0L;
        char content_type[SIZ];
+       char *content = NULL;
        
        serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
        serv_gets(buf);
        if (buf[0] == '2') {
                bytes = extract_long(&buf[4], 0);
+               content = malloc(bytes);
                extract(content_type, &buf[4], 3);
                output_headers(0);
-               wprintf("Content-type: %s\n", content_type);
-               wprintf("Content-length: %ld\n", (long) bytes);
-               wprintf("\n");
-
-               while (bytes > (off_t) 0) {
-                       thisblock = (off_t) sizeof(xferbuf);
-                       if (thisblock > bytes) {
-                               thisblock = bytes;
-                       }
-                       serv_printf("READ %ld|%ld", accomplished, thisblock);
-                       serv_gets(buf);
-                       if (buf[0] == '6') {
-                               thisblock = extract_long(&buf[4], 0);
-                               serv_read(xferbuf, (int) thisblock);
-                       }
-                       else {
-                               memset(xferbuf, 0, thisblock);
-                       }
-                       http_write(WC->http_sock, xferbuf, thisblock);
-                       bytes = bytes - thisblock;
-                       accomplished = accomplished + thisblock;
-               }
+               read_server_binary(content, bytes);
                serv_puts("CLOS");
                serv_gets(buf);
+               http_transmit_thing(content, bytes, content_type, 0);
+               free(content);
        } else {
                wprintf("HTTP/1.0 404 %s\n", &buf[4]);
                output_headers(0);
@@ -646,8 +658,6 @@ char *load_mimepart(long msgnum, char *partnum)
 {
        char buf[SIZ];
        off_t bytes;
-       off_t thisblock;
-       off_t accomplished = 0L;
        char content_type[SIZ];
        char *content;
        
@@ -658,30 +668,11 @@ char *load_mimepart(long msgnum, char *partnum)
                extract(content_type, &buf[4], 3);
 
                content = malloc(bytes + 1);
+               read_server_binary(content, bytes);
 
-               while (bytes > (off_t) 0) {
-                       thisblock = bytes;
-                       if (thisblock > 4096L) {
-                               thisblock = 4096L;
-                       }
-                       if (thisblock > bytes) {
-                               thisblock = bytes;
-                       }
-                       serv_printf("READ %ld|%ld", accomplished, thisblock);
-                       serv_gets(buf);
-                       if (buf[0] == '6') {
-                               thisblock = extract_long(&buf[4], 0);
-                               serv_read(&content[accomplished], (int) thisblock);
-                       }
-                       else {
-                               memset(&content[accomplished], 0, thisblock);
-                       }
-                       bytes = bytes - thisblock;
-                       accomplished = accomplished + thisblock;
-               }
                serv_puts("CLOS");
                serv_gets(buf);
-               content[accomplished] = 0;      /* null terminate for good measure */
+               content[bytes] = 0;     /* null terminate for good measure */
                return(content);
        }
        else {
@@ -698,10 +689,9 @@ void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
 {
        wprintf("HTTP/1.0 200 OK\n");
        output_headers(1);
-       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>%s</B>\n", titlebarmsg);
-       wprintf("</FONT></TD></TR></TABLE><BR>\n");
+       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
+       wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
+       wprintf("</TD></TR></TABLE><BR>\n");
        escputs(messagetext);
 
        wprintf("<HR>\n");
@@ -718,6 +708,13 @@ void blank_page(void) {
 }
 
 
+/*
+ * A template has been requested
+ */
+void url_do_template(void) {
+       do_template(bstr("template"));
+}
+
 
 
 /*
@@ -727,7 +724,7 @@ void offer_start_page(void) {
        wprintf("<A HREF=\"/change_start_page?startpage=");
        urlescputs(WC->this_page);
        wprintf("\">"
-               "<FONT SIZE=-2 COLOR=#AAAAAA>Make this my start page</FONT>"
+               "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
                "</A>"
        );
 }
@@ -739,39 +736,22 @@ void offer_start_page(void) {
 void change_start_page(void) {
 
        if (bstr("startpage") == NULL) {
-               display_error("startpage set to null");
+               strcpy(WC->ImportantMessage,
+                       "startpage set to null");
+               display_main_menu();
                return;
        }
 
        set_preference("startpage", bstr("startpage"));
 
        output_headers(3);
-       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=000077><TR><TD>");
-       wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
-       wprintf("<B>New start page</B>\n");
-       wprintf("</FONT></TD></TR></TABLE>\n");
-
-       wprintf("<CENTER>"
-               "<font size=+2>Your start page has been changed.</font>"
-               "<BR><BR>\n"
-               "<I>(Note: this does not change your browser's home page. "
-               "It changes the page you begin on when you log on to ");
-       escputs(serv_info.serv_humannode);
-       wprintf(".)</I><BR><BR>"
-               "<a href = \"javascript:history.back()\">Back...</a>"
-               "</CENTER>");
-
+       do_template("newstartpage");
        wDumpContent(1);
 }
 
 
 
 
-void display_error(char *errormessage)
-{
-       convenience_page("770000", "Error", errormessage);
-}
-
 void display_success(char *successmessage)
 {
        convenience_page("007700", "OK", successmessage);
@@ -816,25 +796,35 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        void *content, char *cbtype, size_t length,
                        char *encoding, void *userdata)
 {
+       struct urlcontent *u;
 
-       lprintf(5, "UPLOAD HANDLER CALLED\n");
-       lprintf(5, "    name = %s\n", name);
-       lprintf(5, "filename = %s\n", filename);
-       lprintf(5, "encoding = %s\n", encoding);
-       lprintf(5, "    type = %s\n", cbtype);
-       lprintf(5, "  length = %ld\n", (long)length);
+       /* Form fields */
+       if ( (length > 0) && (strlen(cbtype) == 0) ) {
+               u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
+               u->next = WC->urlstrings;
+               WC->urlstrings = u;
+               safestrncpy(u->url_key, name, sizeof(u->url_key));
+               u->url_data = malloc(length + 1);
+               memcpy(u->url_data, content, length);
+               u->url_data[length] = 0;
+       }
 
-       if (length > 0) {
+       /* Uploaded files */
+       if ( (length > 0) && (strlen(cbtype) > 0) ) {
                WC->upload = malloc(length);
                if (WC->upload != NULL) {
                        WC->upload_length = length;
+                       safestrncpy(WC->upload_filename, filename,
+                                       sizeof(WC->upload_filename));
+                       safestrncpy(WC->upload_content_type, cbtype,
+                                       sizeof(WC->upload_content_type));
                        memcpy(WC->upload, content, length);
                }
                else {
-                       lprintf(9, "malloc() failed: %s\n",
-                               strerror(errno));
+                       lprintf(3, "malloc() failed: %s\n", strerror(errno));
                }
        }
+
 }
 
 
@@ -843,33 +833,30 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
 /*
  * Entry point for WebCit transaction
  */
-void session_loop(struct httprequest *req, int gzip)
+void session_loop(struct httprequest *req)
 {
        char cmd[SIZ];
        char action[SIZ];
        char buf[SIZ];
        int a, b;
        int ContentLength = 0;
-       int BytesRead;
+       int BytesRead = 0;
        char ContentType[512];
        char *content;
        char *content_end;
        struct httprequest *hptr;
        char browser_host[SIZ];
        char user_agent[SIZ];
+       int body_start;
 
        /* We stuff these with the values coming from the client cookies,
         * so we can use them to reconnect a timed out session if we have to.
         */
-       char c_host[SIZ];
-       char c_port[SIZ];
        char c_username[SIZ];
        char c_password[SIZ];
        char c_roomname[SIZ];
        char cookie[SIZ];
 
-       strcpy(c_host, defaulthost);
-       strcpy(c_port, defaultport);
        strcpy(c_username, "");
        strcpy(c_password, "");
        strcpy(c_roomname, "");
@@ -879,15 +866,6 @@ void session_loop(struct httprequest *req, int gzip)
 
        WC->is_wap = 0;
 
-#ifdef WITH_ZLIB
-       if (gzip) {
-               WC->gzcompressed = 1;
-       }
-       else {
-               WC->gzcompressed = 0;
-       }
-#endif
-
        hptr = req;
        if (hptr == NULL) return;
 
@@ -923,23 +901,31 @@ void session_loop(struct httprequest *req, int gzip)
        }
 
        if (ContentLength > 0) {
-               lprintf(5, "Content length: %d\n", ContentLength);
-               content = malloc(ContentLength + 1);
-               memset(content, 0, ContentLength+1);
+               content = malloc(ContentLength + SIZ);
+               memset(content, 0, ContentLength + SIZ);
+               sprintf(content, "Content-type: %s\n"
+                               "Content-length: %d\n\n",
+                               ContentType, ContentLength);
+               body_start = strlen(content);
+
+/***** old version
                BytesRead = 0;
-
                while (BytesRead < ContentLength) {
-                       a=read(WC->http_sock, &content[BytesRead],
+                       a=read(WC->http_sock, &content[BytesRead+body_start],
                                ContentLength - BytesRead);
                        if (a <= 0) BytesRead = ContentLength;
                        else BytesRead += a;
                }
+*******/
+
+               /* Now we're daring and read it all at once. */
+               client_read(WC->http_sock, &content[BytesRead+body_start], ContentLength);
 
                if (!strncasecmp(ContentType,
                              "application/x-www-form-urlencoded", 33)) {
-                       addurls(content);
+                       addurls(&content[body_start]);
                } else if (!strncasecmp(ContentType, "multipart", 9)) {
-                       content_end = content + ContentLength;
+                       content_end = content + ContentLength + body_start;
                        mime_parser(content, content_end, *upload_handler,
                                        NULL, NULL, NULL, 0);
                }
@@ -963,25 +949,29 @@ void session_loop(struct httprequest *req, int gzip)
                }
        }
 
+       /* Static content can be sent without connecting to Citadel. */
+       if (!strcasecmp(action, "static")) {
+               strcpy(buf, &cmd[12]);
+               for (a = 0; a < strlen(buf); ++a)
+                       if (isspace(buf[a]))
+                               buf[a] = 0;
+               output_static(buf);
+               goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
+       }
+
        /*
         * If we're not connected to a Citadel server, try to hook up the
-        * connection now.  Preference is given to the host and port specified
-        * by browser cookies, if cookies have been supplied.
+        * connection now.
         */
        if (!WC->connected) {
-               if (strlen(bstr("host")) > 0)
-                       strcpy(c_host, bstr("host"));
-               if (strlen(bstr("port")) > 0)
-                       strcpy(c_port, bstr("port"));
-
-               if (!strcasecmp(c_host, "uds")) {
+               if (!strcasecmp(ctdlhost, "uds")) {
                        /* unix domain socket */
-                       sprintf(buf, "%s/citadel.socket", c_port);
+                       sprintf(buf, "%s/citadel.socket", ctdlport);
                        WC->serv_sock = uds_connectsock(buf);
                }
                else {
                        /* tcp socket */
-                       WC->serv_sock = tcp_connectsock(c_host, c_port);
+                       WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
                }
 
                if (WC->serv_sock < 0) {
@@ -993,6 +983,20 @@ void session_loop(struct httprequest *req, int gzip)
                        serv_gets(buf); /* get the server welcome message */
                        locate_host(browser_host, WC->http_sock);
                        get_serv_info(browser_host, user_agent);
+                       if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
+                               wprintf("You are connected to a Citadel "
+                                       "server running Citadel %d.%02d;\nin "
+                                       "order to run this version of WebCit "
+                                       "you must also have Citadel %d.%02d or"
+                                       " newer.\n\n\n",
+                                               serv_info.serv_rev_level / 100,
+                                               serv_info.serv_rev_level % 100,
+                                               MINIMUM_CIT_VERSION / 100,
+                                               MINIMUM_CIT_VERSION % 100
+                                       );
+                               end_webcit_session();
+                               goto SKIP_ALL_THIS_CRAP;
+                       }
                }
        }
 
@@ -1003,6 +1007,12 @@ void session_loop(struct httprequest *req, int gzip)
                do_listsub();
                goto SKIP_ALL_THIS_CRAP;
        }
+#ifdef WEBCIT_WITH_CALENDAR_SERVICE
+       if (!strcasecmp(action, "freebusy")) {
+               do_freebusy(cmd);
+               goto SKIP_ALL_THIS_CRAP;
+       }
+#endif
 
        check_for_express_messages();
 
@@ -1032,13 +1042,8 @@ void session_loop(struct httprequest *req, int gzip)
                        strcpy(WC->wc_roomname, c_roomname);
                }
        }
-       if (!strcasecmp(action, "static")) {
-               strcpy(buf, &cmd[12]);
-               for (a = 0; a < strlen(buf); ++a)
-                       if (isspace(buf[a]))
-                               buf[a] = 0;
-               output_static(buf);
-       } else if (!strcasecmp(action, "image")) {
+
+       if (!strcasecmp(action, "image")) {
                output_image();
 
        /*
@@ -1059,6 +1064,8 @@ void session_loop(struct httprequest *req, int gzip)
                do_welcome();
        } else if (!strcasecmp(action, "blank")) {
                blank_page();
+       } else if (!strcasecmp(action, "do_template")) {
+               url_do_template();
        } else if (!strcasecmp(action, "display_main_menu")) {
                display_main_menu();
        } else if (!strcasecmp(action, "whobbs")) {
@@ -1130,12 +1137,12 @@ void session_loop(struct httprequest *req, int gzip)
         } else if (!strcasecmp(action, "display_whok")) {
                 display_whok();
        } else if (!strcasecmp(action, "display_editinfo")) {
-               display_edit("Room info", "EINF 0", "RINF", "/editinfo");
+               display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
        } else if (!strcasecmp(action, "editinfo")) {
                save_edit("Room info", "EINF 1", 1);
        } else if (!strcasecmp(action, "display_editbio")) {
                sprintf(buf, "RBIO %s", WC->wc_username);
-               display_edit("Your bio", "NOOP", buf, "editbio");
+               display_edit("Your bio", "NOOP", buf, "editbio", 3);
        } else if (!strcasecmp(action, "editbio")) {
                save_edit("Your bio", "EBIO", 0);
        } else if (!strcasecmp(action, "confirm_delete_room")) {
@@ -1174,8 +1181,6 @@ void session_loop(struct httprequest *req, int gzip)
                do_graphics_upload(buf);
        } else if (!strcasecmp(action, "display_reg")) {
                display_reg(0);
-       } else if (!strcasecmp(action, "register")) {
-               register_user();
        } else if (!strcasecmp(action, "display_changepw")) {
                display_changepw();
        } else if (!strcasecmp(action, "changepw")) {
@@ -1201,6 +1206,10 @@ void session_loop(struct httprequest *req, int gzip)
                edit_me();
        } else if (!strcasecmp(action, "display_siteconfig")) {
                display_siteconfig();
+       } else if (!strcasecmp(action, "chat_recv")) {
+               chat_recv();
+       } else if (!strcasecmp(action, "chat_send")) {
+               chat_send();
        } else if (!strcasecmp(action, "page_popup")) {
                page_popup();
        } else if (!strcasecmp(action, "siteconfig")) {
@@ -1220,15 +1229,13 @@ void session_loop(struct httprequest *req, int gzip)
        } else if (!strcasecmp(action, "select_user_to_edit")) {
                select_user_to_edit(NULL, NULL);
        } else if (!strcasecmp(action, "display_edituser")) {
-               display_edituser(NULL);
+               display_edituser(NULL, 0);
        } else if (!strcasecmp(action, "edituser")) {
                edituser();
        } else if (!strcasecmp(action, "create_user")) {
                create_user();
        } else if (!strcasecmp(action, "changeview")) {
                change_view();
-       } else if (!strcasecmp(action, "folders")) {
-               folders();
        } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
                do_stuff_to_msgs();
        } else if (!strcasecmp(action, "change_start_page")) {
@@ -1237,8 +1244,34 @@ void session_loop(struct httprequest *req, int gzip)
                display_floorconfig(NULL);
        } else if (!strcasecmp(action, "toggle_self_service")) {
                toggle_self_service();
+#ifdef WEBCIT_WITH_CALENDAR_SERVICE
+       } else if (!strcasecmp(action, "display_edit_task")) {
+               display_edit_task();
+       } else if (!strcasecmp(action, "save_task")) {
+               save_task();
+       } else if (!strcasecmp(action, "display_edit_event")) {
+               display_edit_event();
+       } else if (!strcasecmp(action, "save_event")) {
+               save_event();
+       } else if (!strcasecmp(action, "respond_to_request")) {
+               respond_to_request();
+       } else if (!strcasecmp(action, "handle_rsvp")) {
+               handle_rsvp();
+#endif
        } else if (!strcasecmp(action, "summary")) {
                summary();
+       } else if (!strcasecmp(action, "iconbar")) {
+               do_iconbar();
+       } else if (!strcasecmp(action, "display_customize_iconbar")) {
+               display_customize_iconbar();
+       } else if (!strcasecmp(action, "commit_iconbar")) {
+               commit_iconbar();
+       } else if (!strcasecmp(action, "set_room_policy")) {
+               set_room_policy();
+       } else if (!strcasecmp(action, "display_inetconf")) {
+               display_inetconf();
+       } else if (!strcasecmp(action, "save_inetconf")) {
+               save_inetconf();
        } else if (!strcasecmp(action, "diagnostics")) {
                output_headers(1);
 
@@ -1268,11 +1301,4 @@ SKIP_ALL_THIS_CRAP:
                WC->upload_length = 0;
        }
 
-#ifdef WITH_ZLIB
-       if (WC->gzfd) {
-               gzclose(WC->gzfd);
-               WC->gzfd = NULL;
-               WC->gzcompressed = 0;
-       }
-#endif
 }