]> code.citadel.org Git - citadel.git/blobdiff - webcit/webcit.c
* Rewrote the HTTP engine and application coupling to run in a worker thread
[citadel.git] / webcit / webcit.c
index e0c731829afb5e9b355339ff6c750a29b19ee67f..78646ffe03af7045f5c4e2025334e5086700902d 100644 (file)
@@ -8,32 +8,37 @@
  * $Id$
  */
 
+#include <ctype.h>
 #include <stdlib.h>
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 #include <stdio.h>
-#include <ctype.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <limits.h>
+#include <netinet/in.h>
+#include <netdb.h>
 #include <string.h>
+#include <pwd.h>
 #include <errno.h>
-#include <sys/stat.h>
 #include <stdarg.h>
+#include <pthread.h>
+#include <signal.h>
 #include "webcit.h"
-#include "child.h"
-
-int wc_session;
-char wc_host[256];
-char wc_port[256];
-char wc_username[256];
-char wc_password[256];
-char wc_roomname[256];
+
+
+
 int TransactionCount = 0;
-int connected = 0;
-int logged_in = 0;
-int axlevel;
+char *ExpressMessages = NULL;
 
-struct webcontent *wlist = NULL;
-struct webcontent *wlast = NULL;
+/* This variable is set to 1 if the room banner and menubar have been
+ * displayed, and we need to close the <TABLE> tags.
+ */
+int fake_frames = 0;
 
 struct urlcontent *urlstrings = NULL;
 
@@ -41,77 +46,86 @@ static const char *defaulthost = DEFAULT_HOST;
 static const char *defaultport = DEFAULT_PORT;
 
 
+
 void unescape_input(char *buf)
 {
-       int a,b;
+       int a, b;
        char hex[3];
 
-       while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
-               buf[strlen(buf)-1] = 0;
-
-       for (a=0; a<strlen(buf); ++a) {
-               if (buf[a]=='+') buf[a]=' ';    
-               if (buf[a]=='%') {
-                       hex[0]=buf[a+1];
-                       hex[1]=buf[a+2];
-                       hex[2]=0;
-                       sscanf(hex,"%02x",&b);
+       while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
+               buf[strlen(buf) - 1] = 0;
+
+       for (a = 0; a < strlen(buf); ++a) {
+               if (buf[a] == '+')
+                       buf[a] = ' ';
+               if (buf[a] == '%') {
+                       hex[0] = buf[a + 1];
+                       hex[1] = buf[a + 2];
+                       hex[2] = 0;
+                       sscanf(hex, "%02x", &b);
                        buf[a] = (char) b;
-                       strcpy(&buf[a+1],&buf[a+3]);
-                       }
+                       strcpy(&buf[a + 1], &buf[a + 3]);
                }
-
        }
 
+}
+
 
-void addurls(char *url) {
+void addurls(char *url)
+{
        char *up, *ptr;
        char buf[256];
-       int a,b;
+       int a, b;
        struct urlcontent *u;
 
        up = url;
-       while (strlen(up)>0) {
-               
+       while (strlen(up) > 0) {
+
                /* locate the = sign */
-               strncpy(buf,up,255);
+               strncpy(buf, up, 255);
                b = (-1);
-               for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
-               if (b<0) return;
-               buf[b]=0;
-       
-               u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
+               for (a = 255; a >= 0; --a)
+                       if (buf[a] == '=')
+                               b = a;
+               if (b < 0)
+                       return;
+               buf[b] = 0;
+
+               u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
                u->next = urlstrings;
                urlstrings = u;
                strcpy(u->url_key, buf);
-       
+
                /* now chop that part off */
-               for (a=0; a<=b; ++a) ++up;
-       
+               for (a = 0; a <= b; ++a)
+                       ++up;
+
                /* locate the & sign */
                ptr = up;
                b = strlen(up);
-               for (a=0; a<strlen(up); ++a) {
-                       if (!strncmp(ptr,"&",1)) {
-                               b=a;
+               for (a = 0; a < strlen(up); ++a) {
+                       if (!strncmp(ptr, "&", 1)) {
+                               b = a;
                                break;
-                               }
-                       ++ptr;
                        }
+                       ++ptr;
+               }
                ptr = up;
-               for (a=0; a<b; ++a) ++ptr;
-               strcpy(ptr,"");
-               
-               u->url_data = malloc(strlen(up));
+               for (a = 0; a < b; ++a)
+                       ++ptr;
+               strcpy(ptr, "");
+
+               u->url_data = malloc(strlen(up) + 1);
                strcpy(u->url_data, up);
+               u->url_data[b] = 0;
                unescape_input(u->url_data);
-
                up = ptr;
                ++up;
-               }
        }
+}
 
-void free_urls(void) {
+void free_urls(void)
+{
        struct urlcontent *u;
 
        while (urlstrings != NULL) {
@@ -119,171 +133,271 @@ void free_urls(void) {
                u = urlstrings->next;
                free(urlstrings);
                urlstrings = u;
-               }
        }
+}
 
 /*
  * Diagnostic function to display the contents of all variables
  */
-void dump_vars(void) {
+void dump_vars(void)
+{
        struct urlcontent *u;
 
        for (u = urlstrings; u != NULL; u = u->next) {
                wprintf("%38s = %s\n", u->url_key, u->url_data);
-               }
        }
+}
 
-char *bstr(char *key) {
+char *bstr(char *key)
+{
        struct urlcontent *u;
 
        for (u = urlstrings; u != NULL; u = u->next) {
-               if (!strcasecmp(u->url_key, key)) return(u->url_data);
-               }
-       return("");
+               if (!strcasecmp(u->url_key, key))
+                       return (u->url_data);
        }
+       return ("");
+}
 
 
-void wprintf(const char *format, ...) {   
-        va_list arg_ptr;   
-       struct webcontent *wptr;
+void wprintf(const char *format,...)
+{
+       va_list arg_ptr;
+       char wbuf[1024];
 
-       wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
-       wptr->next = NULL;
-       if (wlist == NULL) {
-               wlist = wptr;
-               wlast = wptr;
-               }
-       else {
-               wlast->next = wptr;
-               wlast = wptr;
-               }
-  
-               va_start(arg_ptr, format);   
-               vsprintf(wptr->w_data, format, arg_ptr);   
-               va_end(arg_ptr);   
-       }
+       va_start(arg_ptr, format);
+       vsprintf(wbuf, format, arg_ptr);
+       va_end(arg_ptr);
 
-int wContentLength(void) {
-       struct webcontent *wptr;
-       int len = 0;
+       write(WC->http_sock, wbuf, strlen(wbuf));
+}
 
-       for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
-               len = len + strlen(wptr->w_data);
-               }
 
-       return(len);
+/*
+ * wDumpContent() wraps up an HTTP session, closes tags, etc.
+ *
+ * print_standard_html_footer should be set to 0 to transmit only, 1 to
+ * append the main menu and closing tags, or 2 to
+ * append the closing tags only.
+ */
+void wDumpContent(int print_standard_html_footer)
+{
+       if (fake_frames) {
+               wprintf("<CENTER><FONT SIZE=-1>"
+                       "<TABLE border=0 width=100%><TR>"
+                       "<TD><A HREF=\"/ungoto\">"
+                       "<IMG SRC=\"/static/back.gif\" BORDER=0>"
+                       "Ungoto</A></TD>");
+               wprintf("<TD><A HREF=\"#TheTop\">"
+                       "<IMG SRC=\"/static/up.gif\" BORDER=0>"
+                       "Top of page</A></TD>");
+               wprintf("<TD><A HREF=\"/display_enter\">"
+                       "<IMG SRC=\"/static/enter.gif\" BORDER=0>"
+                       "Enter a message</A></TD>");
+               wprintf("<TD><A HREF=\"/gotonext\">"
+                       "Goto next room"
+                       "<IMG SRC=\"/static/forward.gif\" BORDER=0></A></TD>"
+                       "</TR></TABLE>"
+                       "</FONT>\n"
+                       "</TD></TR></TABLE></TABLE>\n");
+               fake_frames = 0;
        }
 
-void wDumpContent(void) {
-       struct webcontent *wptr;
-
-       printf("Content-type: text/html\n");
-       printf("Content-length: %d\n", wContentLength());
-       printf("\n");
-
-       while (wlist != NULL) {
-               fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
-               wptr = wlist->next;
-               free(wlist);
-               wlist = wptr;
+       if (print_standard_html_footer) {
+               if (print_standard_html_footer != 2) {
+                       wprintf("<BR>");
                }
-       wlast = NULL;
+               wprintf("</BODY></HTML>\n");
        }
 
 
+}
+
+
 void escputs1(char *strbuf, int nbsp)
 {
        int a;
 
-       for (a=0; a<strlen(strbuf); ++a) {
-               if (strbuf[a]=='<') wprintf("&lt;");
-               else if (strbuf[a]=='>') wprintf("&gt;");
-               else if (strbuf[a]=='&') wprintf("&amp;");
-               else if (strbuf[a]==34) wprintf("&quot;");
-               else if (strbuf[a]==LB) wprintf("<");
-               else if (strbuf[a]==RB) wprintf(">");
-               else if (strbuf[a]==QU) wprintf("\"");
-               else if ((strbuf[a]==32)&&(nbsp==1)) {
+       for (a = 0; a < strlen(strbuf); ++a) {
+               if (strbuf[a] == '<')
+                       wprintf("&lt;");
+               else if (strbuf[a] == '>')
+                       wprintf("&gt;");
+               else if (strbuf[a] == '&')
+                       wprintf("&amp;");
+               else if (strbuf[a] == '\"')
+                       wprintf("&quot;");
+               else if (strbuf[a] == '\'') 
+                       wprintf("&#39;");
+               else if (strbuf[a] == LB)
+                       wprintf("<");
+               else if (strbuf[a] == RB)
+                       wprintf(">");
+               else if (strbuf[a] == QU)
+                       wprintf("\"");
+               else if ((strbuf[a] == 32) && (nbsp == 1)) {
                        wprintf("&nbsp;");
-                       }
-               else {
+               } else {
                        wprintf("%c", strbuf[a]);
-                       }
                }
        }
+}
 
 void escputs(char *strbuf)
 {
-       escputs1(strbuf,0);
-       }
+       escputs1(strbuf, 0);
+}
 
 
 
 char *urlesc(char *strbuf)
 {
-       int a,b,c;
-        char *ec = " #&;`'|*?-~<>^()[]{}$\\";
+       int a, b, c;
+       char *ec = " #&;`'|*?-~<>^()[]{}$\\";
        static char outbuf[512];
-       
-       strcpy(outbuf,"");
 
-       for (a=0; a<strlen(strbuf); ++a) {
+       strcpy(outbuf, "");
+
+       for (a = 0; a < strlen(strbuf); ++a) {
                c = 0;
-               for (b=0; b<strlen(ec); ++b) {
-                       if (strbuf[a]==ec[b]) c=1;
-                       }
-               b = strlen(outbuf);
-               if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
-               else sprintf(&outbuf[b],"%c",strbuf[a]);
+               for (b = 0; b < strlen(ec); ++b) {
+                       if (strbuf[a] == ec[b])
+                               c = 1;
                }
-       return(outbuf);
+               b = strlen(outbuf);
+               if (c == 1)
+                       sprintf(&outbuf[b], "%%%02x", strbuf[a]);
+               else
+                       sprintf(&outbuf[b], "%c", strbuf[a]);
        }
+       return (outbuf);
+}
 
 void urlescputs(char *strbuf)
 {
-       wprintf("%s",urlesc(strbuf));
-       }
+       wprintf("%s", urlesc(strbuf));
+}
+
 
 
-void getz(char *buf) {
-       if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
-       else {
-               while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
-                       buf[strlen(buf)-1] = 0;
-               }
-       }
 
 /*
  * Output all that important stuff that the browser will want to see
+ *
+ * print_standard_html_head values:
+ * 0 = Nothing.  Do not display any leading HTTP or HTML.
+ * 1 = HTTP headers plus the "fake frames" found in most windows.
+ * 2 = HTTP headers required to terminate the session (unset cookies)
+ * 3 = HTTP headers only.
  */
-void output_headers(void) {
+void output_headers(int print_standard_html_head)
+{
 
        static char *unset = "; expires=28-May-1971 18:10:00 GMT";
+       char cookie[256];
+
+       wprintf("Content-type: text/html\n");
+       wprintf("Server: %s\n", SERVER);
+       wprintf("Connection: close\n");
+
+       if (print_standard_html_head > 0) {
+               wprintf("Pragma: no-cache\n");
+               wprintf("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("X-WebCit-Session: close\n");
+               wprintf("Set-cookie: webcit=%s\n", unset);
+       } else {
+               wprintf("Set-cookie: webcit=%s\n", cookie);
+       }
+
+       wprintf("\n");
+
+       if (print_standard_html_head > 0) {
+               wprintf("<HTML><HEAD><TITLE>");
+               escputs(serv_info.serv_humannode);
+               wprintf("</TITLE>\n"
+                       "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
+                       "</HEAD>\n");
+               if (ExpressMessages != NULL) {
+                       wprintf("<SCRIPT language=\"javascript\">\n");
+                       wprintf("function ExpressMessage() {\n");
+                       wprintf(" alert(\"");
+                       escputs(ExpressMessages);
+                       wprintf("\")\n");
+                       wprintf(" }\n </SCRIPT>\n");
+               }
+
+
 
-       printf("Server: %s\n", SERVER);
-       printf("Connection: close\n");
-       printf("Set-cookie: wc_session=%d\n", wc_session);
+               /* JavaScript key-based navigation would go here if it
+                * were finished
+                */
 
-       if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
-       else printf("Set-cookie: wc_host=%s\n", unset);
+               wprintf("<BODY ");
+               if (ExpressMessages != NULL) {
+                       wprintf("onload=\"ExpressMessage()\" ");
+                       free(ExpressMessages);
+                       ExpressMessages = NULL;
+               }
+               wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
+       
+       
+       if (print_standard_html_head == 1) {
+               wprintf("<A NAME=\"TheTop\"></A>"
+                       "<TABLE border=0 width=100%>"
+                       "<TR VALIGN=TOP><TD>");
 
-       if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
-       else printf("Set-cookie: wc_port=%s\n", unset);
+               display_menubar(0);
 
-       if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
-               wc_username);
-       else printf("Set-cookie: wc_username=%s\n", unset);
+               wprintf("</TD><TD VALIGN=TOP>"
+                       "<TABLE border=0 width=100%><TR VALIGN=TOP>"
+                       "<TD>\n");
 
-       if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
-               wc_password);
-       else printf("Set-cookie: wc_password=%s\n", unset);
+               embed_room_banner(NULL);
 
-       if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
-               wc_roomname);
-       else printf("Set-cookie: wc_roomname=%s\n", unset);
+               wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
+               
+               fake_frames = 1;
+               }
        }
+}
+
+
 
-void output_static(char *what) {
+void ExpressMessageCat(char *buf) {
+       if (ExpressMessages == NULL) {
+               ExpressMessages = malloc(strlen(buf) + 4);
+               strcpy(ExpressMessages, "");
+       } else {
+               ExpressMessages = realloc(ExpressMessages,
+                       (strlen(ExpressMessages) + strlen(buf) + 4));
+       }
+       strcat(ExpressMessages, buf);
+       strcat(ExpressMessages, "\\n");
+}
+
+
+void check_for_express_messages()
+{
+       char buf[256];
+
+       serv_puts("PEXP");
+       serv_gets(buf);
+       if (buf[0] == '1') {
+               while (serv_gets(buf), strcmp(buf, "000")) {
+                       ExpressMessageCat(buf);
+               }
+       }
+}
+
+
+
+
+void output_static(char *what)
+{
        char buf[256];
        FILE *fp;
        struct stat statbuf;
@@ -292,38 +406,38 @@ void output_static(char *what) {
        sprintf(buf, "static/%s", what);
        fp = fopen(buf, "rb");
        if (fp == NULL) {
-               printf("HTTP/1.0 404 %s\n", strerror(errno));
-               output_headers();
-               printf("Content-Type: text/plain\n");
-               sprintf(buf, "%s: %s\n", what, strerror(errno));
-               printf("Content-length: %d\n", strlen(buf));
-               printf("\n");
-               fwrite(buf, strlen(buf), 1, stdout);
-               }
-       else {
-               printf("HTTP/1.0 200 OK\n");
-               output_headers();
-
-               if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
-                       printf("Content-type: image/gif\n");
-               else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
-                       printf("Content-type: text/html\n");
+               wprintf("HTTP/1.0 404 %s\n", strerror(errno));
+               wprintf("Content-Type: text/plain\n");
+               wprintf("\n");
+               wprintf("Cannot open %s: %s\n", what, strerror(errno));
+       } else {
+               wprintf("HTTP/1.0 200 OK\n");
+               output_headers(0);
+
+               if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
+                       wprintf("Content-type: image/gif\n");
+               else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
+                       wprintf("Content-type: text/plain\n");
+               else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
+                       wprintf("Content-type: image/jpeg\n");
+               else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
+                       wprintf("Content-type: text/html\n");
                else
-                       printf("Content-type: application/octet-stream\n");
+                       wprintf("Content-type: application/octet-stream\n");
 
                fstat(fileno(fp), &statbuf);
                bytes = statbuf.st_size;
-               printf("Content-length: %ld\n", (long)bytes);
+               wprintf("Content-length: %ld\n", (long) bytes);
                printf("\n");
                while (bytes--) {
-                       putc(getc(fp), stdout);
-                       }
-               fflush(stdout);
-               fclose(fp);
+                       wprintf("%c", getc(fp) );
                }
+               fclose(fp);
        }
+}
 
-void output_image() {
+void output_image()
+{
        char buf[256];
        char xferbuf[4096];
        off_t bytes;
@@ -333,66 +447,140 @@ void output_image() {
 
        serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
        serv_gets(buf);
-       if (buf[0]=='2') {
+       if (buf[0] == '2') {
                bytes = extract_long(&buf[4], 0);
-               printf("HTTP/1.0 200 OK\n");
-               output_headers();
-               printf("Content-type: image/gif\n");
-               printf("Content-length: %ld\n", bytes);
-               printf("\n");
-
-               while (bytes > (off_t)0) {
-                       thisblock = (off_t)sizeof(xferbuf);
-                       if (thisblock > bytes) thisblock = bytes;
+               wprintf("HTTP/1.0 200 OK\n");
+               output_headers(0);
+               wprintf("Content-type: image/gif\n");
+               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);
-                       fwrite(xferbuf, thisblock, 1, stdout);
+                       if (buf[0] == '6')
+                               thisblock = extract_long(&buf[4], 0);
+                       serv_read(xferbuf, (int) thisblock);
+                       write(WC->http_sock, xferbuf, thisblock);
                        bytes = bytes - thisblock;
                        accomplished = accomplished + thisblock;
-                       }
+               }
                fflush(stdout);
                serv_puts("CLOS");
                serv_gets(buf);
-               }
-       else {
-               printf("HTTP/1.0 404 %s\n", strerror(errno));
-               output_headers();
-               printf("Content-Type: text/plain\n");
-               sprintf(buf, "Error retrieving image\n");
-               printf("Content-length: %d\n", strlen(buf));
-               printf("\n");
-               fwrite(buf, strlen(buf), 1, stdout);
-               }
-
+       } else {
+               wprintf("HTTP/1.0 404 %s\n", strerror(errno));
+               output_headers(0);
+               wprintf("Content-Type: text/plain\n");
+               wprintf("\n");
+               wprintf("Error retrieving image\n");
        }
 
-void extract_action(char *actbuf, char *cmdbuf) {
+}
+
+
+/*
+ * Convenience functions to display a page containing only a string
+ */
+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");
+       escputs(messagetext);
+
+       wprintf("<HR>\n");
+       embed_main_menu();
+       wDumpContent(1);
+}
+
+void display_error(char *errormessage)
+{
+       convenience_page("770000", "Error", errormessage);
+}
+
+void display_success(char *successmessage)
+{
+       convenience_page("007700", "OK", successmessage);
+}
+
+
+
+void extract_action(char *actbuf, char *cmdbuf)
+{
        int i;
 
        strcpy(actbuf, cmdbuf);
-       if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
-       if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
-       if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
-
-       for (i=0; i<strlen(actbuf); ++i) {
-               if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
-               if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
-               if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
-               if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
+       if (!strncasecmp(actbuf, "GET /", 5))
+               strcpy(actbuf, &actbuf[5]);
+       if (!strncasecmp(actbuf, "PUT /", 5))
+               strcpy(actbuf, &actbuf[5]);
+       if (!strncasecmp(actbuf, "POST /", 6))
+               strcpy(actbuf, &actbuf[6]);
+
+       for (i = 0; i < strlen(actbuf); ++i) {
+               if (actbuf[i] == ' ') {
+                       actbuf[i] = 0;
+                       i = 0;
+               }
+               if (actbuf[i] == '/') {
+                       actbuf[i] = 0;
+                       i = 0;
+               }
+               if (actbuf[i] == '?') {
+                       actbuf[i] = 0;
+                       i = 0;
+               }
+               if (actbuf[i] == '&') {
+                       actbuf[i] = 0;
+                       i = 0;
+               }
+       }
+}
+
+
+void upload_handler(char *name, char *filename, char *encoding,
+                   void *content, char *cbtype, size_t length)
+{
+
+       fprintf(stderr, "UPLOAD HANDLER CALLED\n");
+       fprintf(stderr, "    name = %s\n", name);
+       fprintf(stderr, "filename = %s\n", filename);
+       fprintf(stderr, "encoding = %s\n", encoding);
+       fprintf(stderr, "    type = %s\n", cbtype);
+       fprintf(stderr, "  length = %d\n", length);
+
+       if (strlen(name) > 0) {
+               WC->upload = malloc(length);
+               if (WC->upload != NULL) {
+                       WC->upload_length = length;
+                       memcpy(WC->upload, content, length);
                }
        }
+}
 
 
-void session_loop(void) {
+/*
+ * Entry point for WebCit transaction
+ */
+void session_loop(struct httprequest *req)
+{
        char cmd[256];
        char action[256];
        char buf[256];
        int a, b;
        int ContentLength = 0;
+       char ContentType[512];
        char *content;
-FILE *fp;
+       struct httprequest *hptr;
+       char browser_host[256];
+       char user_agent[256];
 
        /* 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.
@@ -402,6 +590,7 @@ FILE *fp;
        char c_username[256];
        char c_password[256];
        char c_roomname[256];
+       char cookie[256];
 
        strcpy(c_host, defaulthost);
        strcpy(c_port, defaultport);
@@ -409,217 +598,304 @@ FILE *fp;
        strcpy(c_password, "");
        strcpy(c_roomname, "");
 
-       getz(cmd);
+       WC->upload_length = 0;
+       WC->upload = NULL;
+
+       hptr = req;
+       if (hptr == NULL) return;
+
+       strcpy(cmd, hptr->line);
+       hptr = hptr->next;
        extract_action(action, cmd);
 
-       do {
-               getz(buf);
-
-               if (!strncasecmp(buf, "Cookie: wc_host=", 16))
-                       strcpy(c_host, &buf[16]);
-               if (!strncasecmp(buf, "Cookie: wc_port=", 16))
-                       strcpy(c_port, &buf[16]);
-               if (!strncasecmp(buf, "Cookie: wc_username=", 20))
-                       strcpy(c_username, &buf[20]);
-               if (!strncasecmp(buf, "Cookie: wc_password=", 20))
-                       strcpy(c_password, &buf[20]);
-               if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
-                       strcpy(c_roomname, &buf[20]);
-
-               if (!strncasecmp(buf, "Content-length: ", 16)) {
-                       ContentLength = atoi(&buf[16]);
-                       }
+       while (hptr != NULL) {
+               strcpy(buf, hptr->line);
+               hptr = hptr->next;
 
-               } while(strlen(buf)>0);
+               if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
+                       strcpy(cookie, &buf[15]);
+                       cookie_to_stuff(cookie, NULL,
+                                     c_username, c_password, c_roomname);
+               }
+               else if (!strncasecmp(buf, "Content-length: ", 16)) {
+                       ContentLength = atoi(&buf[16]);
+               }
+               else if (!strncasecmp(buf, "Content-type: ", 14)) {
+                       strcpy(ContentType, &buf[14]);
+               }
+               else if (!strncasecmp(buf, "User-agent: ", 12)) {
+                       strcpy(user_agent, &buf[12]);
+               }
+       }
 
        ++TransactionCount;
 
        if (ContentLength > 0) {
-               content = malloc(ContentLength+1);
-               fread(content, ContentLength, 1, stdin);
-fp = fopen("content", "wb");
-fwrite(content, ContentLength, 1, fp);
-fclose(fp);
+               content = malloc(ContentLength + 1);
+               read(WC->http_sock, content, ContentLength);
+
                content[ContentLength] = 0;
-               addurls(content);
+
+               if (!strncasecmp(ContentType,
+                             "application/x-www-form-urlencoded", 33)) {
+                       addurls(content);
+               } else if (!strncasecmp(ContentType, "multipart", 9)) {
+                       mime_parser(content, ContentLength, ContentType,
+                                   *upload_handler);
                }
-       else {
+       else {
                content = NULL;
-               }
+       }
 
+       /* If there are variables in the URL, we must grab them now */
+       for (a = 0; a < strlen(cmd); ++a)
+               if ((cmd[a] == '?') || (cmd[a] == '&')) {
+                       for (b = a; b < strlen(cmd); ++b)
+                               if (isspace(cmd[b]))
+                                       cmd[b] = 0;
+                       addurls(&cmd[a + 1]);
+                       cmd[a] = 0;
+               }
        /*
         * 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.
         */
-       if (!connected) {
-               serv_sock = connectsock(c_host, c_port, "tcp");
-               connected = 1;
-               serv_gets(buf); /* get the server welcome message */
-               strcpy(wc_host, c_host);
-               strcpy(wc_port, c_port);
-               get_serv_info();
+       if (!WC->connected) {
+               if (strlen(bstr("host")) > 0)
+                       strcpy(c_host, bstr("host"));
+               if (strlen(bstr("port")) > 0)
+                       strcpy(c_port, bstr("port"));
+               WC->serv_sock = connectsock(c_host, c_port, "tcp");
+               if (WC->serv_sock < 0) {
+                       do_logout();
                }
 
+               WC->connected = 1;
+               serv_gets(buf); /* get the server welcome message */
+               locate_host(browser_host, WC->http_sock);
+               get_serv_info(browser_host, user_agent);
+       }
+       check_for_express_messages();
 
        /*
         * If we're not logged in, but we have username and password cookies
         * supplied by the browser, try using them to log in.
         */
-       if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
+       if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
                serv_printf("USER %s", c_username);
                serv_gets(buf);
-               if (buf[0]=='3') {
+               if (buf[0] == '3') {
                        serv_printf("PASS %s", c_password);
                        serv_gets(buf);
-                       if (buf[0]=='2') {
+                       if (buf[0] == '2') {
                                become_logged_in(c_username, c_password, buf);
-                               }
                        }
                }
-
+       }
        /*
         * If we don't have a current room, but a cookie specifying the
         * current room is supplied, make an effort to go there.
         */
-       if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
+       if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
                serv_printf("GOTO %s", c_roomname);
                serv_gets(buf);
-               if (buf[0]=='2') {
-                       strcpy(wc_roomname, c_roomname);
-                       }
+               if (buf[0] == '2') {
+                       strcpy(WC->wc_roomname, c_roomname);
                }
-
-       /* If there are variables in the URL, we must grab them now */  
-       for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
-               for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
-               addurls(&cmd[a+1]);
-               cmd[a] = 0;
-               }
-
+       }
        if (!strcasecmp(action, "static")) {
                strcpy(buf, &cmd[12]);
-               for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
+               for (a = 0; a < strlen(buf); ++a)
+                       if (isspace(buf[a]))
+                               buf[a] = 0;
                output_static(buf);
-               }
-
-       else if (!strcasecmp(action, "image")) {
+       } else if (!strcasecmp(action, "image")) {
                output_image();
-               }
-
-       else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
+       } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
                do_login();
-               }
-
-       else if (!logged_in) {
+       } else if (!WC->logged_in) {
                display_login(NULL);
-               }
-
+       }
        /* Various commands... */
-       
+
        else if (!strcasecmp(action, "do_welcome")) {
                do_welcome();
-               }
-
-       else if (!strcasecmp(action, "display_main_menu")) {
+       } else if (!strcasecmp(action, "display_main_menu")) {
                display_main_menu();
-               }
-
-       else if (!strcasecmp(action, "advanced")) {
+       } else if (!strcasecmp(action, "advanced")) {
                display_advanced_menu();
-               }
-
-       else if (!strcasecmp(action, "whobbs")) {
+       } else if (!strcasecmp(action, "whobbs")) {
                whobbs();
-               }
-
-       else if (!strcasecmp(action, "knrooms")) {
+       } else if (!strcasecmp(action, "knrooms")) {
                list_all_rooms_by_floor();
-               }
-
-       else if (!strcasecmp(action, "gotonext")) {
+       } else if (!strcasecmp(action, "gotonext")) {
                slrp_highest();
                gotonext();
-               }
-
-       else if (!strcasecmp(action, "skip")) {
+       } else if (!strcasecmp(action, "skip")) {
                gotonext();
-               }
-
-       else if (!strcasecmp(action, "ungoto")) {
+       } else if (!strcasecmp(action, "ungoto")) {
                ungoto();
-               }
-
-       else if (!strcasecmp(action, "dotgoto")) {
+       } else if (!strcasecmp(action, "dotgoto")) {
                slrp_highest();
-               dotgoto();
-               }
-
-       else if (!strcasecmp(action, "termquit")) {
+               smart_goto(bstr("room"));
+       } else if (!strcasecmp(action, "termquit")) {
                do_logout();
-               }
-
-       else if (!strcasecmp(action, "readnew")) {
+       } else if (!strcasecmp(action, "readnew")) {
                readloop("readnew");
-               }
-
-       else if (!strcasecmp(action, "readold")) {
+       } else if (!strcasecmp(action, "readold")) {
                readloop("readold");
-               }
-
-       else if (!strcasecmp(action, "readfwd")) {
+       } else if (!strcasecmp(action, "readfwd")) {
                readloop("readfwd");
-               }
+       } else if (!strcasecmp(action, "display_enter")) {
+               display_enter();
+       } else if (!strcasecmp(action, "post")) {
+               post_message();
+       } else if (!strcasecmp(action, "confirm_delete_msg")) {
+               confirm_delete_msg();
+       } else if (!strcasecmp(action, "delete_msg")) {
+               delete_msg();
+       } else if (!strcasecmp(action, "confirm_move_msg")) {
+               confirm_move_msg();
+       } else if (!strcasecmp(action, "move_msg")) {
+               move_msg();
+       } else if (!strcasecmp(action, "userlist")) {
+               userlist();
+       } else if (!strcasecmp(action, "showuser")) {
+               showuser();
+       } else if (!strcasecmp(action, "display_page")) {
+               display_page();
+       } else if (!strcasecmp(action, "page_user")) {
+               page_user();
+       } else if (!strcasecmp(action, "chat")) {
+               do_chat();
+       } else if (!strcasecmp(action, "display_private")) {
+               display_private("", 0);
+       } else if (!strcasecmp(action, "goto_private")) {
+               goto_private();
+       } else if (!strcasecmp(action, "zapped_list")) {
+               zapped_list();
+       } else if (!strcasecmp(action, "display_zap")) {
+               display_zap();
+       } else if (!strcasecmp(action, "zap")) {
+               zap();
+       } else if (!strcasecmp(action, "display_entroom")) {
+               display_entroom();
+       } else if (!strcasecmp(action, "entroom")) {
+               entroom();
+       } else if (!strcasecmp(action, "display_editroom")) {
+               display_editroom();
+       } else if (!strcasecmp(action, "editroom")) {
+               editroom();
+       } else if (!strcasecmp(action, "display_editinfo")) {
+               display_edit("Room info", "EINF 0", "RINF", "/editinfo");
+       } 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");
+       } else if (!strcasecmp(action, "editbio")) {
+               save_edit("Your bio", "EBIO", 0);
+       } else if (!strcasecmp(action, "confirm_delete_room")) {
+               confirm_delete_room();
+       } else if (!strcasecmp(action, "delete_room")) {
+               delete_room();
+       } else if (!strcasecmp(action, "validate")) {
+               validate();
+       } else if (!strcasecmp(action, "display_editpic")) {
+               display_graphics_upload("your photo",
+                                       "UIMG 0|_userpic_",
+                                       "/editpic");
+       } else if (!strcasecmp(action, "editpic")) {
+               do_graphics_upload("UIMG 1|_userpic_");
+       } else if (!strcasecmp(action, "display_editroompic")) {
+               display_graphics_upload("the graphic for this room",
+                                       "UIMG 0|_roompic_",
+                                       "/editroompic");
+       } else if (!strcasecmp(action, "editroompic")) {
+               do_graphics_upload("UIMG 1|_roompic_");
+       } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
+               select_floor_to_edit_pic();
+       } else if (!strcasecmp(action, "display_editfloorpic")) {
+               sprintf(buf, "UIMG 0|_floorpic_|%s",
+                       bstr("which_floor"));
+               display_graphics_upload("the graphic for this floor",
+                                       buf,
+                                       "/editfloorpic");
+       } else if (!strcasecmp(action, "editfloorpic")) {
+               sprintf(buf, "UIMG 1|_floorpic_|%s",
+                       bstr("which_floor"));
+               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")) {
+               changepw();
+       } else if (!strcasecmp(action, "display_edit_node")) {
+               display_edit_node();
+       } else if (!strcasecmp(action, "display_netconf")) {
+               display_netconf();
+       } else if (!strcasecmp(action, "display_confirm_unshare")) {
+               display_confirm_unshare();
+       } else if (!strcasecmp(action, "display_confirm_delete_node")) {
+               display_confirm_delete_node();
+       } else if (!strcasecmp(action, "delete_node")) {
+               delete_node();
+       } else if (!strcasecmp(action, "unshare")) {
+               unshare();
+       } else if (!strcasecmp(action, "display_add_node")) {
+               display_add_node();
+       } else if (!strcasecmp(action, "add_node")) {
+               add_node();
+       } else if (!strcasecmp(action, "display_share")) {
+               display_share();
+       } else if (!strcasecmp(action, "share")) {
+               share();
+       } else if (!strcasecmp(action, "terminate_session")) {
+               slrp_highest();
+               terminate_session();
+       } else if (!strcasecmp(action, "edit_me")) {
+               edit_me();
+       } else if (!strcasecmp(action, "display_siteconfig")) {
+               display_siteconfig();
+       } else if (!strcasecmp(action, "siteconfig")) {
+               siteconfig();
+       } else if (!strcasecmp(action, "display_generic")) {
+               display_generic();
+       } else if (!strcasecmp(action, "do_generic")) {
+               do_generic();
+       } else if (!strcasecmp(action, "display_menubar")) {
+               display_menubar(1);
+       } else if (!strcasecmp(action, "diagnostics")) {
+               wprintf("HTTP/1.0 200 OK\n");
+               output_headers(1);
 
-       /* When all else fails... */
-       else {
-               printf("HTTP/1.0 200 OK\n");
-               output_headers();
-       
-               wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
                wprintf("TransactionCount is %d<BR>\n", TransactionCount);
-               wprintf("You're in session %d<HR>\n", wc_session);
+               wprintf("You're in session %d<HR>\n", WC->wc_session);
                wprintf("Command: <BR><PRE>\n");
                escputs(cmd);
                wprintf("</PRE><HR>\n");
                wprintf("Variables: <BR><PRE>\n");
                dump_vars();
                wprintf("</PRE><HR>\n");
-               wprintf("</BODY></HTML>\n");
-               wDumpContent();
-               }
+               wDumpContent(1);
+       }
+       /* When all else fais, display the main menu. */
+       else {
+               display_main_menu();
+       }
 
        fflush(stdout);
        if (content != NULL) {
                free(content);
                content = NULL;
-               }
-       free_urls();
        }
-
-int main(int argc, char *argv[]) {
-
-       if (argc < 2 || argc > 4) {
-               fprintf(stderr,
-                       "webcit: usage: webcit <session_id> [host [port]]\n");
-               return 1;
-               }
-
-       wc_session = atoi(argv[1]);
-
-       if (argc > 2) {
-               defaulthost = argv[2];
-               if (argc > 3)
-                       defaultport = argv[3];
-               }
-
-       strcpy(wc_host, "");
-       strcpy(wc_port, "");
-       strcpy(wc_username, "");
-       strcpy(wc_password, "");
-       strcpy(wc_roomname, "");
-
-       while (1) {
-               session_loop();
-               }
+       free_urls();
+       if (WC->upload_length > 0) {
+               free(WC->upload);
+               WC->upload_length = 0;
        }
+}