]> 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 9b56b7376ca3416f25ff2ab5938a788f00d94e17..78646ffe03af7045f5c4e2025334e5086700902d 100644 (file)
@@ -8,45 +8,43 @@
  * $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"
-#include "mime_parser.h"
 
-int fake_frames = 0;
 
-int wc_session;
-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;
-int noframes = 0;
-int new_mail = 0;
-int need_vali = 0;
 
-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;
 
 static const char *defaulthost = DEFAULT_HOST;
 static const char *defaultport = DEFAULT_PORT;
 
-int upload_length = 0;
-char *upload;
 
 
 void unescape_input(char *buf)
@@ -165,72 +163,54 @@ char *bstr(char *key)
 void wprintf(const char *format,...)
 {
        va_list arg_ptr;
-       struct webcontent *wptr;
-
-       wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
-       wptr->next = NULL;
-       if (wlist == NULL) {
-               wlist = wptr;
-               wlast = wptr;
-       } else {
-               wlast->next = wptr;
-               wlast = wptr;
-       }
+       char wbuf[1024];
 
        va_start(arg_ptr, format);
-       vsprintf(wptr->w_data, format, arg_ptr);
+       vsprintf(wbuf, format, arg_ptr);
        va_end(arg_ptr);
-}
-
-int wContentLength(void)
-{
-       struct webcontent *wptr;
-       int len = 0;
 
-       for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
-               len = len + strlen(wptr->w_data);
-       }
-
-       return (len);
+       write(WC->http_sock, wbuf, strlen(wbuf));
 }
 
+
 /*
- * wDumpContent() takes all the stuff that's been queued up using
- * the wprintf() and escputs() functions, and sends it out to the browser.
- * By queuing instead of transmitting as it's generated, we're able to
- * calculate a Content-length: header.
+ * 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 (if in noframes mode) and closing tags, or 2 to
+ * append the main menu and closing tags, or 2 to
  * append the closing tags only.
  */
 void wDumpContent(int print_standard_html_footer)
 {
-       struct webcontent *wptr;
-
        if (fake_frames) {
-               wprintf("</TD></TR></TABLE></TABLE>\n");
+               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;
        }
 
        if (print_standard_html_footer) {
-               if ((noframes) && (print_standard_html_footer != 2)) {
+               if (print_standard_html_footer != 2) {
                        wprintf("<BR>");
-                       /* embed_main_menu(); */  /* not any more */
                }
                wprintf("</BODY></HTML>\n");
        }
-       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;
-       }
-       wlast = NULL;
+
+
 }
 
 
@@ -245,8 +225,10 @@ void escputs1(char *strbuf, int nbsp)
                        wprintf("&gt;");
                else if (strbuf[a] == '&')
                        wprintf("&amp;");
-               else if (strbuf[a] == 34)
+               else if (strbuf[a] == '\"')
                        wprintf("&quot;");
+               else if (strbuf[a] == '\'') 
+                       wprintf("&#39;");
                else if (strbuf[a] == LB)
                        wprintf("<");
                else if (strbuf[a] == RB)
@@ -299,42 +281,14 @@ void urlescputs(char *strbuf)
 
 
 
-/*
- * Get a line of text from the webserver (which originally came from the
- * user's browser), checking for sanity etc.
- */
-char *getz(char *buf)
-{
-       int e = 0;
-
-       bzero(buf, 256);
-
-       /* If fgets() fails, it's because the webserver crashed, so kill off
-        * the session too.
-        */
-       if (fgets(buf, 256, stdin) == NULL) {
-               e = errno;
-               fprintf(stderr, "webcit: exit code %d (%s)\n",
-                       e, strerror(e));
-               fflush(stderr);
-               exit(e);
-               
-       /* Otherwise, strip out nonprintables and resume our happy day.
-        */
-       } else {
-               while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
-                       buf[strlen(buf) - 1] = 0;
-               return buf;
-       }
-}
-
-
-
 /*
  * Output all that important stuff that the browser will want to see
  *
- * If print_standard_html_head is nonzero, we also get some standard HTML
- * headers.  If it's set to 2, the session is considered to be closing.
+ * 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(int print_standard_html_head)
 {
@@ -342,26 +296,31 @@ void output_headers(int print_standard_html_head)
        static char *unset = "; expires=28-May-1971 18:10:00 GMT";
        char cookie[256];
 
-       printf("Server: %s\n", SERVER);
-       printf("Connection: close\n");
+       wprintf("Content-type: text/html\n");
+       wprintf("Server: %s\n", SERVER);
+       wprintf("Connection: close\n");
 
        if (print_standard_html_head > 0) {
-               printf("Pragma: no-cache\n");
-               printf("Cache-Control: no-store\n");
+               wprintf("Pragma: no-cache\n");
+               wprintf("Cache-Control: no-store\n");
        }
-       stuff_to_cookie(cookie, wc_session, wc_username, wc_password,
-                       wc_roomname, noframes);
+       stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
+                       WC->wc_password, WC->wc_roomname);
        if (print_standard_html_head == 2) {
-               printf("X-WebCit-Session: close\n");
-               printf("Set-cookie: webcit=%s\n", unset);
+               wprintf("X-WebCit-Session: close\n");
+               wprintf("Set-cookie: webcit=%s\n", unset);
        } else {
-               printf("Set-cookie: webcit=%s\n", cookie);
+               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></HEAD>");
+               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");
@@ -370,6 +329,13 @@ void output_headers(int print_standard_html_head)
                        wprintf("\")\n");
                        wprintf(" }\n </SCRIPT>\n");
                }
+
+
+
+               /* JavaScript key-based navigation would go here if it
+                * were finished
+                */
+
                wprintf("<BODY ");
                if (ExpressMessages != NULL) {
                        wprintf("onload=\"ExpressMessage()\" ");
@@ -379,9 +345,10 @@ void output_headers(int print_standard_html_head)
                wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
        
        
-       if ((print_standard_html_head == 1) && (noframes == 1)) {
-               wprintf("<TABLE border=0 width=100%>");
-               wprintf("<TR VALIGN=TOP><TD>");
+       if (print_standard_html_head == 1) {
+               wprintf("<A NAME=\"TheTop\"></A>"
+                       "<TABLE border=0 width=100%>"
+                       "<TR VALIGN=TOP><TD>");
 
                display_menubar(0);
 
@@ -393,7 +360,6 @@ void output_headers(int print_standard_html_head)
 
                wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
                
-
                fake_frames = 1;
                }
        }
@@ -440,34 +406,32 @@ 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(0);
-               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);
+               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 {
-               printf("HTTP/1.0 200 OK\n");
+               wprintf("HTTP/1.0 200 OK\n");
                output_headers(0);
 
                if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
-                       printf("Content-type: image/gif\n");
+                       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))
-                       printf("Content-type: image/jpeg\n");
+                       wprintf("Content-type: image/jpeg\n");
                else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
-                       printf("Content-type: text/html\n");
+                       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);
+                       wprintf("%c", getc(fp) );
                }
-               fflush(stdout);
                fclose(fp);
        }
 }
@@ -485,11 +449,11 @@ void output_image()
        serv_gets(buf);
        if (buf[0] == '2') {
                bytes = extract_long(&buf[4], 0);
-               printf("HTTP/1.0 200 OK\n");
+               wprintf("HTTP/1.0 200 OK\n");
                output_headers(0);
-               printf("Content-type: image/gif\n");
-               printf("Content-length: %ld\n", (long) bytes);
-               printf("\n");
+               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);
@@ -500,7 +464,7 @@ void output_image()
                        if (buf[0] == '6')
                                thisblock = extract_long(&buf[4], 0);
                        serv_read(xferbuf, (int) thisblock);
-                       fwrite(xferbuf, thisblock, 1, stdout);
+                       write(WC->http_sock, xferbuf, thisblock);
                        bytes = bytes - thisblock;
                        accomplished = accomplished + thisblock;
                }
@@ -508,13 +472,11 @@ void output_image()
                serv_puts("CLOS");
                serv_gets(buf);
        } else {
-               printf("HTTP/1.0 404 %s\n", strerror(errno));
+               wprintf("HTTP/1.0 404 %s\n", strerror(errno));
                output_headers(0);
-               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);
+               wprintf("Content-Type: text/plain\n");
+               wprintf("\n");
+               wprintf("Error retrieving image\n");
        }
 
 }
@@ -525,7 +487,7 @@ void output_image()
  */
 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
 {
-       printf("HTTP/1.0 200 OK\n");
+       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\"");
@@ -533,10 +495,8 @@ void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
        wprintf("</FONT></TD></TR></TABLE><BR>\n");
        escputs(messagetext);
 
-       if (noframes) {
-               wprintf("<HR>\n");
-               embed_main_menu();
-       }
+       wprintf("<HR>\n");
+       embed_main_menu();
        wDumpContent(1);
 }
 
@@ -597,16 +557,19 @@ void upload_handler(char *name, char *filename, char *encoding,
        fprintf(stderr, "  length = %d\n", length);
 
        if (strlen(name) > 0) {
-               upload = malloc(length);
-               if (upload != NULL) {
-                       upload_length = length;
-                       memcpy(upload, content, length);
+               WC->upload = malloc(length);
+               if (WC->upload != NULL) {
+                       WC->upload_length = length;
+                       memcpy(WC->upload, content, length);
                }
        }
 }
 
 
-void session_loop(char *browser_host)
+/*
+ * Entry point for WebCit transaction
+ */
+void session_loop(struct httprequest *req)
 {
        char cmd[256];
        char action[256];
@@ -615,6 +578,9 @@ void session_loop(char *browser_host)
        int ContentLength = 0;
        char ContentType[512];
        char *content;
+       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.
@@ -632,36 +598,41 @@ void session_loop(char *browser_host)
        strcpy(c_password, "");
        strcpy(c_roomname, "");
 
-       upload_length = 0;
-       upload = NULL;
+       WC->upload_length = 0;
+       WC->upload = NULL;
+
+       hptr = req;
+       if (hptr == NULL) return;
 
-       if (getz(cmd) == NULL)
-               return;
+       strcpy(cmd, hptr->line);
+       hptr = hptr->next;
        extract_action(action, cmd);
 
-       do {
-               if (getz(buf) == NULL)
-                       return;
+       while (hptr != NULL) {
+               strcpy(buf, hptr->line);
+               hptr = hptr->next;
 
                if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
                        strcpy(cookie, &buf[15]);
                        cookie_to_stuff(cookie, NULL,
-                                     c_username, c_password, c_roomname,
-                                       &noframes);
+                                     c_username, c_password, c_roomname);
                }
-               if (!strncasecmp(buf, "Content-length: ", 16)) {
+               else if (!strncasecmp(buf, "Content-length: ", 16)) {
                        ContentLength = atoi(&buf[16]);
                }
-               if (!strncasecmp(buf, "Content-type: ", 14)) {
+               else if (!strncasecmp(buf, "Content-type: ", 14)) {
                        strcpy(ContentType, &buf[14]);
                }
-       } while (strlen(buf) > 0);
+               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);
+               read(WC->http_sock, content, ContentLength);
 
                content[ContentLength] = 0;
 
@@ -690,15 +661,20 @@ void session_loop(char *browser_host)
         * connection now.  Preference is given to the host and port specified
         * by browser cookies, if cookies have been supplied.
         */
-       if (!connected) {
+       if (!WC->connected) {
                if (strlen(bstr("host")) > 0)
                        strcpy(c_host, bstr("host"));
                if (strlen(bstr("port")) > 0)
                        strcpy(c_port, bstr("port"));
-               serv_sock = connectsock(c_host, c_port, "tcp");
-               connected = 1;
+               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 */
-               get_serv_info(browser_host);
+               locate_host(browser_host, WC->http_sock);
+               get_serv_info(browser_host, user_agent);
        }
        check_for_express_messages();
 
@@ -706,7 +682,7 @@ void session_loop(char *browser_host)
         * 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') {
@@ -721,11 +697,11 @@ void session_loop(char *browser_host)
         * 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);
+                       strcpy(WC->wc_roomname, c_roomname);
                }
        }
        if (!strcasecmp(action, "static")) {
@@ -736,9 +712,9 @@ void session_loop(char *browser_host)
                output_static(buf);
        } 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... */
@@ -816,7 +792,7 @@ void session_loop(char *browser_host)
        } else if (!strcasecmp(action, "editinfo")) {
                save_edit("Room info", "EINF 1", 1);
        } else if (!strcasecmp(action, "display_editbio")) {
-               sprintf(buf, "RBIO %s", wc_username);
+               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);
@@ -879,6 +855,7 @@ void session_loop(char *browser_host)
        } else if (!strcasecmp(action, "share")) {
                share();
        } else if (!strcasecmp(action, "terminate_session")) {
+               slrp_highest();
                terminate_session();
        } else if (!strcasecmp(action, "edit_me")) {
                edit_me();
@@ -892,14 +869,12 @@ void session_loop(char *browser_host)
                do_generic();
        } else if (!strcasecmp(action, "display_menubar")) {
                display_menubar(1);
-       }
-       /* When all else fails... */
-       else {
-               printf("HTTP/1.0 200 OK\n");
+       } else if (!strcasecmp(action, "diagnostics")) {
+               wprintf("HTTP/1.0 200 OK\n");
                output_headers(1);
 
                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");
@@ -908,6 +883,10 @@ void session_loop(char *browser_host)
                wprintf("</PRE><HR>\n");
                wDumpContent(1);
        }
+       /* When all else fais, display the main menu. */
+       else {
+               display_main_menu();
+       }
 
        fflush(stdout);
        if (content != NULL) {
@@ -915,47 +894,8 @@ void session_loop(char *browser_host)
                content = NULL;
        }
        free_urls();
-       if (upload_length > 0) {
-               free(upload);
-               upload_length = 0;
-       }
-}
-
-int main(int argc, char *argv[])
-{
-
-       char browser[256];
-       int bd;
-
-       if (argc != 6) {
-               fprintf(stderr,
-                       "webcit: usage error (argc must be 6, not %d)\n",
-                       argc);
-               return 1;
-       }
-
-       wc_session = atoi(argv[1]);
-       defaulthost = argv[2];
-       defaultport = argv[3];
-
-       strcpy(wc_username, "");
-       strcpy(wc_password, "");
-       strcpy(wc_roomname, "");
-
-       /* Clear out serv_info and temporarily set the value of serv_humannode
-        * to a default value, because it'll be used in HTML page titles
-        */
-       memset(&serv_info, 0, sizeof(serv_info));
-       strcpy(serv_info.serv_humannode, "WebCit");
-
-       strcpy(browser, argv[5]);
-       bd = browser_braindamage_check(browser);
-       if (bd == B_NO)
-               noframes = 1;
-       else
-               noframes = 0;
-
-       while (1) {
-               session_loop(argv[4]);
+       if (WC->upload_length > 0) {
+               free(WC->upload);
+               WC->upload_length = 0;
        }
 }