]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* When static content is requested, fetch it without trying to connect
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
4  * This is the actual program called by the webserver.  It maintains a
5  * persistent session to the Citadel server, handling HTTP WebCit requests as
6  * they arrive and presenting a user interface.
7  *
8  * $Id$
9  */
10
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <sys/socket.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <limits.h>
23 #include <netinet/in.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include "webcit.h"
32 #include "webserver.h"
33 #include "mime_parser.h"
34
35 /*
36  * String to unset the cookie.
37  * Any date "in the past" will work, so I chose my birthday, right down to
38  * the exact minute.  :)
39  */
40 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
41
42 void unescape_input(char *buf)
43 {
44         int a, b;
45         char hex[3];
46
47         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
48                 buf[strlen(buf) - 1] = 0;
49
50         for (a = 0; a < strlen(buf); ++a) {
51                 if (buf[a] == '+')
52                         buf[a] = ' ';
53                 if (buf[a] == '%') {
54                         hex[0] = buf[a + 1];
55                         hex[1] = buf[a + 2];
56                         hex[2] = 0;
57                         sscanf(hex, "%02x", &b);
58                         buf[a] = (char) b;
59                         strcpy(&buf[a + 1], &buf[a + 3]);
60                 }
61         }
62
63 }
64
65
66 void addurls(char *url)
67 {
68         char *up, *ptr;
69         char buf[SIZ];
70         int a, b;
71         struct urlcontent *u;
72
73         up = url;
74         while (strlen(up) > 0) {
75
76                 /* locate the = sign */
77                 safestrncpy(buf, up, sizeof buf);
78                 b = (-1);
79                 for (a = 255; a >= 0; --a)
80                         if (buf[a] == '=')
81                                 b = a;
82                 if (b < 0)
83                         return;
84                 buf[b] = 0;
85
86                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
87                 u->next = WC->urlstrings;
88                 WC->urlstrings = u;
89                 strcpy(u->url_key, buf);
90
91                 /* now chop that part off */
92                 for (a = 0; a <= b; ++a)
93                         ++up;
94
95                 /* locate the & sign */
96                 ptr = up;
97                 b = strlen(up);
98                 for (a = 0; a < strlen(up); ++a) {
99                         if (!strncmp(ptr, "&", 1)) {
100                                 b = a;
101                                 break;
102                         }
103                         ++ptr;
104                 }
105                 ptr = up;
106                 for (a = 0; a < b; ++a)
107                         ++ptr;
108                 strcpy(ptr, "");
109
110                 u->url_data = malloc(strlen(up) + 1);
111                 strcpy(u->url_data, up);
112                 u->url_data[b] = 0;
113                 unescape_input(u->url_data);
114                 up = ptr;
115                 ++up;
116         }
117 }
118
119 void free_urls(void)
120 {
121         struct urlcontent *u;
122
123         while (WC->urlstrings != NULL) {
124                 free(WC->urlstrings->url_data);
125                 u = WC->urlstrings->next;
126                 free(WC->urlstrings);
127                 WC->urlstrings = u;
128         }
129 }
130
131 /*
132  * Diagnostic function to display the contents of all variables
133  */
134 void dump_vars(void)
135 {
136         struct urlcontent *u;
137
138         for (u = WC->urlstrings; u != NULL; u = u->next) {
139                 wprintf("%38s = %s\n", u->url_key, u->url_data);
140         }
141 }
142
143 char *bstr(char *key)
144 {
145         struct urlcontent *u;
146
147         for (u = WC->urlstrings; u != NULL; u = u->next) {
148                 if (!strcasecmp(u->url_key, key))
149                         return (u->url_data);
150         }
151         return ("");
152 }
153
154
155 void wprintf(const char *format,...)
156 {
157         va_list arg_ptr;
158         char wbuf[4096];
159
160         va_start(arg_ptr, format);
161         vsprintf(wbuf, format, arg_ptr);
162         va_end(arg_ptr);
163
164         write(WC->http_sock, wbuf, strlen(wbuf));
165 }
166
167
168 /*
169  * wDumpContent() wraps up an HTTP session, closes tags, etc.
170  *
171  * print_standard_html_footer should be set to 0 to transmit only, 1 to
172  * append the main menu and closing tags, or 2 to
173  * append the closing tags only.
174  */
175 void wDumpContent(int print_standard_html_footer)
176 {
177         if (print_standard_html_footer) {
178                 do_template("trailing");
179         }
180
181
182 }
183
184
185 /*
186  * Copy a string, escaping characters which have meaning in HTML.  If
187  * nbsp is nonzero, spaces are converted to non-breaking spaces.
188  */
189 void stresc(char *target, char *strbuf, int nbsp)
190 {
191         int a;
192         strcpy(target, "");
193
194         for (a = 0; a < strlen(strbuf); ++a) {
195                 if (strbuf[a] == '<')
196                         strcat(target, "&lt;");
197                 else if (strbuf[a] == '>')
198                         strcat(target, "&gt;");
199                 else if (strbuf[a] == '&')
200                         strcat(target, "&amp;");
201                 else if (strbuf[a] == '\"')
202                         strcat(target, "&quot;");
203                 else if (strbuf[a] == '\'') 
204                         strcat(target, "&#39;");
205                 else if (strbuf[a] == LB)
206                         strcat(target, "<");
207                 else if (strbuf[a] == RB)
208                         strcat(target, ">");
209                 else if (strbuf[a] == QU)
210                         strcat(target, "\"");
211                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
212                         strcat(target, "&nbsp;");
213                 } else {
214                         strncat(target, &strbuf[a], 1);
215                 }
216         }
217 }
218
219 void escputs1(char *strbuf, int nbsp)
220 {
221         char buf[1024];
222         stresc(buf, strbuf, nbsp);
223         wprintf("%s", buf);
224 }
225
226 void escputs(char *strbuf)
227 {
228         escputs1(strbuf, 0);
229 }
230
231 /*
232  * Escape a string for feeding out as a URL.
233  * Returns a pointer to a buffer that must be freed by the caller!
234  */
235 void urlesc(char *outbuf, char *strbuf)
236 {
237         int a, b, c;
238         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
239
240         strcpy(outbuf, "");
241
242         for (a = 0; a < strlen(strbuf); ++a) {
243                 c = 0;
244                 for (b = 0; b < strlen(ec); ++b) {
245                         if (strbuf[a] == ec[b])
246                                 c = 1;
247                 }
248                 b = strlen(outbuf);
249                 if (c == 1)
250                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
251                 else
252                         sprintf(&outbuf[b], "%c", strbuf[a]);
253         }
254 }
255
256 void urlescputs(char *strbuf)
257 {
258         char outbuf[SIZ];
259         
260         urlesc(outbuf, strbuf);
261         wprintf("%s", outbuf);
262 }
263
264
265
266
267 /*
268  * Output all that important stuff that the browser will want to see
269  *
270  * control codes:
271  * 
272  * Bits 0 and 1:
273  * 0 = Nothing.  Do not display any leading HTTP or HTML.
274  * 1 = HTTP headers plus the room banner
275  * 2 = HTTP headers required to terminate the session (unset cookies)
276  * 3 = HTTP and HTML headers, but no room banner
277  *
278  * Bit 2: Set to 1 to auto-refresh page every 30 seconds
279  *
280  * Bit 3: suppress check for express messages
281  */
282 void output_headers(int controlcode)
283 {
284         char cookie[SIZ];
285         int print_standard_html_head = 0;
286         int refresh30 = 0;
287         int suppress_check = 0;
288         char httpnow[SIZ];
289         static int pageseq = 0;
290         print_standard_html_head        =       controlcode & 0x03;
291         refresh30                       =       ((controlcode & 0x04) >> 2);
292         suppress_check                  =       ((controlcode & 0x08) >> 3);
293
294         wprintf("HTTP/1.0 200 OK\n");
295
296         httpdate(httpnow, time(NULL));
297
298         if (print_standard_html_head > 0) {
299                 wprintf("Content-type: text/html\n"
300                         "Server: %s\n", SERVER
301                 );
302                 wprintf("Connection: close\n"
303                         "Pragma: no-cache\n"
304                         "Cache-Control: no-store\n"
305                 );
306         }
307         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
308                         WC->wc_password, WC->wc_roomname);
309         if (print_standard_html_head == 2) {
310                 wprintf("Set-cookie: webcit=%s\n", unset);
311         } else {
312                 wprintf("Set-cookie: webcit=%s\n", cookie);
313                 if (server_cookie != NULL) {
314                         wprintf("%s\n", server_cookie);
315                 }
316         }
317
318         if (print_standard_html_head > 0) {
319                 wprintf("\n");
320
321                 if (refresh30) svprintf("REFRESHTAG", WCS_STRING,
322                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
323                 else svprintf("REFRESHTAG", WCS_STRING,
324                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
325                 /* script for checking for pages (not always launched) */
326                 svprintf("PAGERSCRIPT", WCS_STRING,
327                         "<SCRIPT LANGUAGE=\"JavaScript\">\n"
328                         "function launch_page_popup() {\n"
329                         "pwin = window.open('/page_popup', 'CitaPage%d', "
330                         "'toolbar=no,location=no,copyhistory=no,status=no,"
331                         "scrollbars=yes,resizable=no,height=250,width=400');\n"
332                         "}\n"
333                         "</SCRIPT>\n",
334                         ++pageseq
335                 );
336                 /* end script */
337
338                 do_template("head");
339                 clear_local_substs();
340
341                 if (!suppress_check) if (WC->HaveExpressMessages) {
342                         svprintf("extrabodyparms", WCS_STRING, "%s", 
343                                 "onload=\"launch_page_popup()\" ");
344                         WC->HaveExpressMessages = 0;
345                 }
346
347                 do_template("background");
348                 clear_local_substs();
349
350         if (print_standard_html_head == 1) {
351                 wprintf("<A NAME=\"TheTop\"></A>");
352
353                 embed_room_banner(NULL);
354
355                 }
356         }
357 }
358
359
360 /*
361  *
362  */
363 void http_redirect(char *whichpage) {
364         wprintf("HTTP/1.0 302 Moved Temporarily\n");
365         wprintf("Location: %s\n", whichpage);
366         wprintf("URI: %s\n", whichpage);
367         wprintf("Content-type: text/html\n\n");
368         wprintf("<html><body>\n");
369         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
370                 whichpage);
371         wprintf("</body></html>\n");
372 }
373
374
375
376 void check_for_express_messages()
377 {
378         char buf[SIZ];
379
380         serv_puts("NOOP");
381         serv_gets(buf);
382         if (buf[3] == '*') WC->HaveExpressMessages = 1;
383 }
384
385
386
387
388 /* 
389  * Output a piece of content to the web browser
390  */
391 void http_transmit_thing(char *thing, size_t length, char *content_type) {
392         output_headers(0);
393         wprintf("Content-type: %s\n"
394                 "Content-length: %ld\n"
395                 "Server: %s\n"
396                 "Connection: close\n"
397                 "\n",
398                 content_type,
399                 (long) length,
400                 SERVER
401         );
402         write(WC->http_sock, thing, (size_t)length);
403 }
404
405
406
407
408 void output_static(char *what)
409 {
410         char buf[4096];
411         FILE *fp;
412         struct stat statbuf;
413         off_t bytes;
414         char *bigbuffer;
415         char content_type[SIZ];
416
417         lprintf(9, "output_static(%s)\n", what);
418         sprintf(buf, "static/%s", what);
419         fp = fopen(buf, "rb");
420         if (fp == NULL) {
421                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
422                 wprintf("Content-Type: text/plain\n");
423                 wprintf("\n");
424                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
425         } else {
426                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
427                         strcpy(content_type, "image/gif");
428                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
429                         strcpy(content_type, "text/plain");
430                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
431                         strcpy(content_type, "text/css");
432                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
433                         strcpy(content_type, "image/jpeg");
434                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
435                         strcpy(content_type, "image/png");
436                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
437                         strcpy(content_type, "text/html");
438                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
439                         strcpy(content_type, "text/vnd.wap.wml");
440                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
441                         strcpy(content_type, "text/vnd.wap.wmlscript");
442                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
443                         strcpy(content_type, "application/vnd.wap.wmlc");
444                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
445                         strcpy(content_type, "application/vnd.wap.wmlscriptc");
446                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
447                         wprintf("Content-type: image/vnd.wap.wbmp");
448                 else
449                         wprintf("Content-type: application/octet-stream");
450
451                 fstat(fileno(fp), &statbuf);
452                 bytes = statbuf.st_size;
453                 lprintf(3, "Static: %s, %ld bytes\n", what, bytes);
454                 bigbuffer = malloc(bytes);
455                 fread(bigbuffer, bytes, 1, fp);
456                 fclose(fp);
457
458                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type);
459                 free(bigbuffer);
460         }
461         if (!strcasecmp(bstr("force_close_session"), "yes")) {
462                 end_webcit_session();
463         }
464 }
465
466
467 /*
468  * When the browser requests an image file from the Citadel server,
469  * this function is called to transmit it.
470  */
471 void output_image()
472 {
473         char buf[SIZ];
474         char *xferbuf = NULL;
475         off_t bytes;
476
477         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
478         serv_gets(buf);
479         if (buf[0] == '2') {
480                 bytes = extract_long(&buf[4], 0);
481                 xferbuf = malloc(bytes);
482
483                 /* Read it from the server */
484                 read_server_binary(xferbuf, bytes);
485                 serv_puts("CLOS");
486                 serv_gets(buf);
487
488                 /* Write it to the browser */
489                 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif");
490                 free(xferbuf);
491
492         } else {
493
494                 /* Instead of an ugly 404, send a 1x1 transparent GIF
495                  * when there's no such image on the server.
496                  */
497                 output_static("blank.gif");
498
499                 /*
500                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
501                 output_headers(0);
502                 wprintf("Content-Type: text/plain\n"
503                         "\n"
504                         "Error retrieving image: %s\n",
505                         &buf[4]
506                 );
507                 */
508
509         }
510
511
512
513 }
514
515 /*
516  */
517 void output_mimepart()
518 {
519         char buf[SIZ];
520         off_t bytes;
521         char content_type[SIZ];
522         char *content = NULL;
523         
524         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
525         serv_gets(buf);
526         if (buf[0] == '2') {
527                 bytes = extract_long(&buf[4], 0);
528                 content = malloc(bytes);
529                 extract(content_type, &buf[4], 3);
530                 output_headers(0);
531                 read_server_binary(content, bytes);
532                 serv_puts("CLOS");
533                 serv_gets(buf);
534                 http_transmit_thing(content, bytes, content_type);
535                 free(content);
536         } else {
537                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
538                 output_headers(0);
539                 wprintf("Content-Type: text/plain\n");
540                 wprintf("\n");
541                 wprintf("Error retrieving part: %s\n", &buf[4]);
542         }
543
544 }
545
546
547 /*
548  */
549 char *load_mimepart(long msgnum, char *partnum)
550 {
551         char buf[SIZ];
552         off_t bytes;
553         char content_type[SIZ];
554         char *content;
555         
556         serv_printf("OPNA %ld|%s", msgnum, partnum);
557         serv_gets(buf);
558         if (buf[0] == '2') {
559                 bytes = extract_long(&buf[4], 0);
560                 extract(content_type, &buf[4], 3);
561
562                 content = malloc(bytes + 1);
563                 read_server_binary(content, bytes);
564
565                 serv_puts("CLOS");
566                 serv_gets(buf);
567                 content[bytes] = 0;     /* null terminate for good measure */
568                 return(content);
569         }
570         else {
571                 return(NULL);
572         }
573
574 }
575
576
577 /*
578  * Convenience functions to display a page containing only a string
579  */
580 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
581 {
582         wprintf("HTTP/1.0 200 OK\n");
583         output_headers(1);
584         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
585         wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
586         wprintf("</TD></TR></TABLE><BR>\n");
587         escputs(messagetext);
588
589         wprintf("<HR>\n");
590         wDumpContent(1);
591 }
592
593
594 /*
595  * Display a blank page.
596  */
597 void blank_page(void) {
598         output_headers(7);
599         wDumpContent(2);
600 }
601
602
603
604
605 /*
606  * Offer to make any page the user's "start page."
607  */
608 void offer_start_page(void) {
609         wprintf("<A HREF=\"/change_start_page?startpage=");
610         urlescputs(WC->this_page);
611         wprintf("\">"
612                 "<FONT SIZE=-2 COLOR=\"#AAAAAA\">Make this my start page</FONT>"
613                 "</A>"
614         );
615 }
616
617
618 /* 
619  * Change the user's start page
620  */
621 void change_start_page(void) {
622
623         if (bstr("startpage") == NULL) {
624                 display_error("startpage set to null");
625                 return;
626         }
627
628         set_preference("startpage", bstr("startpage"));
629
630         output_headers(3);
631         do_template("newstartpage");
632         wDumpContent(1);
633 }
634
635
636
637
638 void display_error(char *errormessage)
639 {
640         convenience_page("770000", "Error", errormessage);
641 }
642
643 void display_success(char *successmessage)
644 {
645         convenience_page("007700", "OK", successmessage);
646 }
647
648
649
650 void extract_action(char *actbuf, char *cmdbuf)
651 {
652         int i;
653
654         strcpy(actbuf, cmdbuf);
655         if (!strncasecmp(actbuf, "GET /", 5))
656                 strcpy(actbuf, &actbuf[5]);
657         if (!strncasecmp(actbuf, "PUT /", 5))
658                 strcpy(actbuf, &actbuf[5]);
659         if (!strncasecmp(actbuf, "POST /", 6))
660                 strcpy(actbuf, &actbuf[6]);
661
662         for (i = 0; i < strlen(actbuf); ++i) {
663                 if (actbuf[i] == ' ') {
664                         actbuf[i] = 0;
665                         i = 0;
666                 }
667                 if (actbuf[i] == '/') {
668                         actbuf[i] = 0;
669                         i = 0;
670                 }
671                 if (actbuf[i] == '?') {
672                         actbuf[i] = 0;
673                         i = 0;
674                 }
675                 if (actbuf[i] == '&') {
676                         actbuf[i] = 0;
677                         i = 0;
678                 }
679         }
680 }
681
682
683 void upload_handler(char *name, char *filename, char *partnum, char *disp,
684                         void *content, char *cbtype, size_t length,
685                         char *encoding, void *userdata)
686 {
687         struct urlcontent *u;
688
689         lprintf(5, "UPLOAD HANDLER CALLED\n");
690         lprintf(5, "       name = %s\n", name);
691         lprintf(5, "   filename = %s\n", filename);
692         lprintf(5, "   encoding = %s\n", encoding);
693         lprintf(5, "       type = %s\n", cbtype);
694         lprintf(5, "     length = %ld\n", (long)length);
695         lprintf(5, "disposition = %s\n", disp);
696
697         /* Form fields */
698         if ( (length > 0) && (strlen(cbtype) == 0) ) {
699                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
700                 u->next = WC->urlstrings;
701                 WC->urlstrings = u;
702                 safestrncpy(u->url_key, name, sizeof(u->url_key));
703                 u->url_data = malloc(length + 1);
704                 memcpy(u->url_data, content, length);
705                 u->url_data[length] = 0;
706         }
707
708         /* Uploaded files */
709         if ( (length > 0) && (strlen(cbtype) > 0) ) {
710                 WC->upload = malloc(length);
711                 if (WC->upload != NULL) {
712                         WC->upload_length = length;
713                         safestrncpy(WC->upload_filename, filename,
714                                         sizeof(WC->upload_filename));
715                         safestrncpy(WC->upload_content_type, cbtype,
716                                         sizeof(WC->upload_content_type));
717                         memcpy(WC->upload, content, length);
718                 }
719                 else {
720                         lprintf(9, "malloc() failed: %s\n",
721                                 strerror(errno));
722                 }
723         }
724
725 }
726
727
728
729
730 /*
731  * Entry point for WebCit transaction
732  */
733 void session_loop(struct httprequest *req)
734 {
735         char cmd[SIZ];
736         char action[SIZ];
737         char buf[SIZ];
738         int a, b;
739         int ContentLength = 0;
740         int BytesRead;
741         char ContentType[512];
742         char *content;
743         char *content_end;
744         struct httprequest *hptr;
745         char browser_host[SIZ];
746         char user_agent[SIZ];
747         int body_start;
748
749         /* We stuff these with the values coming from the client cookies,
750          * so we can use them to reconnect a timed out session if we have to.
751          */
752         char c_username[SIZ];
753         char c_password[SIZ];
754         char c_roomname[SIZ];
755         char cookie[SIZ];
756
757         strcpy(c_username, "");
758         strcpy(c_password, "");
759         strcpy(c_roomname, "");
760
761         WC->upload_length = 0;
762         WC->upload = NULL;
763
764         WC->is_wap = 0;
765
766         hptr = req;
767         if (hptr == NULL) return;
768
769         strcpy(cmd, hptr->line);
770         hptr = hptr->next;
771         extract_action(action, cmd);
772
773         while (hptr != NULL) {
774                 strcpy(buf, hptr->line);
775                 hptr = hptr->next;
776
777                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
778                         strcpy(cookie, &buf[15]);
779                         cookie_to_stuff(cookie, NULL,
780                                       c_username, c_password, c_roomname);
781                 }
782                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
783                         ContentLength = atoi(&buf[16]);
784                 }
785                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
786                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
787                 }
788                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
789                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
790                 }
791                 else if (!strncasecmp(buf, "Host: ", 6)) {
792                         safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
793                 }
794                 /* Only WAP gateways explicitly name this content-type */
795                 else if (strstr(buf, "text/vnd.wap.wml")) {
796                         WC->is_wap = 1;
797                 }
798         }
799
800         if (ContentLength > 0) {
801                 lprintf(5, "Content length: %d\n", ContentLength);
802                 content = malloc(ContentLength + SIZ);
803                 memset(content, 0, ContentLength + SIZ);
804                 sprintf(content, "Content-type: %s\n"
805                                 "Content-length: %d\n\n",
806                                 ContentType, ContentLength);
807                 body_start = strlen(content);
808
809                 BytesRead = 0;
810                 while (BytesRead < ContentLength) {
811                         a=read(WC->http_sock, &content[BytesRead+body_start],
812                                 ContentLength - BytesRead);
813                         if (a <= 0) BytesRead = ContentLength;
814                         else BytesRead += a;
815                 }
816
817                 if (!strncasecmp(ContentType,
818                               "application/x-www-form-urlencoded", 33)) {
819                         addurls(&content[body_start]);
820                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
821                         content_end = content + ContentLength;
822                         mime_parser(content, content_end, *upload_handler,
823                                         NULL, NULL, NULL, 0);
824                 }
825         } else {
826                 content = NULL;
827         }
828
829         /* make a note of where we are in case the user wants to save it */
830         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
831         remove_token(WC->this_page, 2, ' ');
832         remove_token(WC->this_page, 0, ' ');
833
834         /* If there are variables in the URL, we must grab them now */
835         for (a = 0; a < strlen(cmd); ++a) {
836                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
837                         for (b = a; b < strlen(cmd); ++b)
838                                 if (isspace(cmd[b]))
839                                         cmd[b] = 0;
840                         addurls(&cmd[a + 1]);
841                         cmd[a] = 0;
842                 }
843         }
844
845         /* Static content can be sent without connecting to Citadel. */
846         if (!strcasecmp(action, "static")) {
847                 strcpy(buf, &cmd[12]);
848                 for (a = 0; a < strlen(buf); ++a)
849                         if (isspace(buf[a]))
850                                 buf[a] = 0;
851                 output_static(buf);
852                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
853         }
854
855         /*
856          * If we're not connected to a Citadel server, try to hook up the
857          * connection now.
858          */
859         if (!WC->connected) {
860                 if (!strcasecmp(ctdlhost, "uds")) {
861                         /* unix domain socket */
862                         sprintf(buf, "%s/citadel.socket", ctdlport);
863                         WC->serv_sock = uds_connectsock(buf);
864                 }
865                 else {
866                         /* tcp socket */
867                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
868                 }
869
870                 if (WC->serv_sock < 0) {
871                         do_logout();
872                         goto SKIP_ALL_THIS_CRAP;
873                 }
874                 else {
875                         WC->connected = 1;
876                         serv_gets(buf); /* get the server welcome message */
877                         locate_host(browser_host, WC->http_sock);
878                         get_serv_info(browser_host, user_agent);
879                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
880                                 wprintf("You are connected to a Citadel "
881                                         "server running Citadel %d.%02d;\nin "
882                                         "order to run this version of WebCit "
883                                         "you must also have Citadel %d.%02d or"
884                                         " newer.\n\n\n",
885                                                 serv_info.serv_rev_level / 100,
886                                                 serv_info.serv_rev_level % 100,
887                                                 MINIMUM_CIT_VERSION / 100,
888                                                 MINIMUM_CIT_VERSION % 100
889                                         );
890                                 end_webcit_session();
891                                 goto SKIP_ALL_THIS_CRAP;
892                         }
893                 }
894         }
895
896         /*
897          * Functions which can be performed without logging in
898          */
899         if (!strcasecmp(action, "listsub")) {
900                 do_listsub();
901                 goto SKIP_ALL_THIS_CRAP;
902         }
903
904         check_for_express_messages();
905
906         /*
907          * If we're not logged in, but we have username and password cookies
908          * supplied by the browser, try using them to log in.
909          */
910         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
911                 serv_printf("USER %s", c_username);
912                 serv_gets(buf);
913                 if (buf[0] == '3') {
914                         serv_printf("PASS %s", c_password);
915                         serv_gets(buf);
916                         if (buf[0] == '2') {
917                                 become_logged_in(c_username, c_password, buf);
918                         }
919                 }
920         }
921         /*
922          * If we don't have a current room, but a cookie specifying the
923          * current room is supplied, make an effort to go there.
924          */
925         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
926                 serv_printf("GOTO %s", c_roomname);
927                 serv_gets(buf);
928                 if (buf[0] == '2') {
929                         strcpy(WC->wc_roomname, c_roomname);
930                 }
931         }
932
933         if (!strcasecmp(action, "image")) {
934                 output_image();
935
936         /*
937          * All functions handled below this point ... make sure we log in
938          * before doing anything else!
939          */
940         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
941                 do_login();
942         } else if (!WC->logged_in) {
943                 display_login(NULL);
944         }
945
946         /*
947          * Various commands...
948          */
949
950         else if (!strcasecmp(action, "do_welcome")) {
951                 do_welcome();
952         } else if (!strcasecmp(action, "blank")) {
953                 blank_page();
954         } else if (!strcasecmp(action, "display_main_menu")) {
955                 display_main_menu();
956         } else if (!strcasecmp(action, "whobbs")) {
957                 whobbs();
958         } else if (!strcasecmp(action, "knrooms")) {
959                 knrooms();
960         } else if (!strcasecmp(action, "gotonext")) {
961                 slrp_highest();
962                 gotonext();
963         } else if (!strcasecmp(action, "skip")) {
964                 gotonext();
965         } else if (!strcasecmp(action, "ungoto")) {
966                 ungoto();
967         } else if (!strcasecmp(action, "dotgoto")) {
968                 slrp_highest();
969                 smart_goto(bstr("room"));
970         } else if (!strcasecmp(action, "dotskip")) {
971                 smart_goto(bstr("room"));
972         } else if (!strcasecmp(action, "termquit")) {
973                 do_logout();
974         } else if (!strcasecmp(action, "readnew")) {
975                 readloop("readnew");
976         } else if (!strcasecmp(action, "readold")) {
977                 readloop("readold");
978         } else if (!strcasecmp(action, "readfwd")) {
979                 readloop("readfwd");
980         } else if (!strcasecmp(action, "headers")) {
981                 readloop("headers");
982         } else if (!strcasecmp(action, "display_enter")) {
983                 display_enter();
984         } else if (!strcasecmp(action, "post")) {
985                 post_message();
986         } else if (!strcasecmp(action, "delete_msg")) {
987                 delete_msg();
988         } else if (!strcasecmp(action, "confirm_move_msg")) {
989                 confirm_move_msg();
990         } else if (!strcasecmp(action, "move_msg")) {
991                 move_msg();
992         } else if (!strcasecmp(action, "userlist")) {
993                 userlist();
994         } else if (!strcasecmp(action, "showuser")) {
995                 showuser();
996         } else if (!strcasecmp(action, "display_page")) {
997                 display_page();
998         } else if (!strcasecmp(action, "page_user")) {
999                 page_user();
1000         } else if (!strcasecmp(action, "chat")) {
1001                 do_chat();
1002         } else if (!strcasecmp(action, "display_private")) {
1003                 display_private("", 0);
1004         } else if (!strcasecmp(action, "goto_private")) {
1005                 goto_private();
1006         } else if (!strcasecmp(action, "zapped_list")) {
1007                 zapped_list();
1008         } else if (!strcasecmp(action, "display_zap")) {
1009                 display_zap();
1010         } else if (!strcasecmp(action, "zap")) {
1011                 zap();
1012         } else if (!strcasecmp(action, "display_entroom")) {
1013                 display_entroom();
1014         } else if (!strcasecmp(action, "entroom")) {
1015                 entroom();
1016         } else if (!strcasecmp(action, "display_editroom")) {
1017                 display_editroom();
1018         } else if (!strcasecmp(action, "netedit")) {
1019                 netedit();
1020         } else if (!strcasecmp(action, "editroom")) {
1021                 editroom();
1022         } else if (!strcasecmp(action, "display_whok")) {
1023                 display_whok();
1024         } else if (!strcasecmp(action, "display_editinfo")) {
1025                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
1026         } else if (!strcasecmp(action, "editinfo")) {
1027                 save_edit("Room info", "EINF 1", 1);
1028         } else if (!strcasecmp(action, "display_editbio")) {
1029                 sprintf(buf, "RBIO %s", WC->wc_username);
1030                 display_edit("Your bio", "NOOP", buf, "editbio");
1031         } else if (!strcasecmp(action, "editbio")) {
1032                 save_edit("Your bio", "EBIO", 0);
1033         } else if (!strcasecmp(action, "confirm_delete_room")) {
1034                 confirm_delete_room();
1035         } else if (!strcasecmp(action, "delete_room")) {
1036                 delete_room();
1037         } else if (!strcasecmp(action, "validate")) {
1038                 validate();
1039         } else if (!strcasecmp(action, "display_editpic")) {
1040                 display_graphics_upload("your photo",
1041                                         "UIMG 0|_userpic_",
1042                                         "/editpic");
1043         } else if (!strcasecmp(action, "editpic")) {
1044                 do_graphics_upload("UIMG 1|_userpic_");
1045         } else if (!strcasecmp(action, "display_editroompic")) {
1046                 display_graphics_upload("the graphic for this room",
1047                                         "UIMG 0|_roompic_",
1048                                         "/editroompic");
1049         } else if (!strcasecmp(action, "editroompic")) {
1050                 do_graphics_upload("UIMG 1|_roompic_");
1051         } else if (!strcasecmp(action, "delete_floor")) {
1052                 delete_floor();
1053         } else if (!strcasecmp(action, "rename_floor")) {
1054                 rename_floor();
1055         } else if (!strcasecmp(action, "create_floor")) {
1056                 create_floor();
1057         } else if (!strcasecmp(action, "display_editfloorpic")) {
1058                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1059                         bstr("which_floor"));
1060                 display_graphics_upload("the graphic for this floor",
1061                                         buf,
1062                                         "/editfloorpic");
1063         } else if (!strcasecmp(action, "editfloorpic")) {
1064                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1065                         bstr("which_floor"));
1066                 do_graphics_upload(buf);
1067         } else if (!strcasecmp(action, "display_reg")) {
1068                 display_reg(0);
1069         } else if (!strcasecmp(action, "display_changepw")) {
1070                 display_changepw();
1071         } else if (!strcasecmp(action, "changepw")) {
1072                 changepw();
1073         } else if (!strcasecmp(action, "display_edit_node")) {
1074                 display_edit_node();
1075         } else if (!strcasecmp(action, "edit_node")) {
1076                 edit_node();
1077         } else if (!strcasecmp(action, "display_netconf")) {
1078                 display_netconf();
1079         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1080                 display_confirm_delete_node();
1081         } else if (!strcasecmp(action, "delete_node")) {
1082                 delete_node();
1083         } else if (!strcasecmp(action, "display_add_node")) {
1084                 display_add_node();
1085         } else if (!strcasecmp(action, "add_node")) {
1086                 add_node();
1087         } else if (!strcasecmp(action, "terminate_session")) {
1088                 slrp_highest();
1089                 terminate_session();
1090         } else if (!strcasecmp(action, "edit_me")) {
1091                 edit_me();
1092         } else if (!strcasecmp(action, "display_siteconfig")) {
1093                 display_siteconfig();
1094         } else if (!strcasecmp(action, "page_popup")) {
1095                 page_popup();
1096         } else if (!strcasecmp(action, "siteconfig")) {
1097                 siteconfig();
1098         } else if (!strcasecmp(action, "display_generic")) {
1099                 display_generic();
1100         } else if (!strcasecmp(action, "do_generic")) {
1101                 do_generic();
1102         } else if (!strcasecmp(action, "display_menubar")) {
1103                 display_menubar(1);
1104         } else if (!strcasecmp(action, "output_mimepart")) {
1105                 output_mimepart();
1106         } else if (!strcasecmp(action, "edit_vcard")) {
1107                 edit_vcard();
1108         } else if (!strcasecmp(action, "submit_vcard")) {
1109                 submit_vcard();
1110         } else if (!strcasecmp(action, "select_user_to_edit")) {
1111                 select_user_to_edit(NULL, NULL);
1112         } else if (!strcasecmp(action, "display_edituser")) {
1113                 display_edituser(NULL);
1114         } else if (!strcasecmp(action, "edituser")) {
1115                 edituser();
1116         } else if (!strcasecmp(action, "create_user")) {
1117                 create_user();
1118         } else if (!strcasecmp(action, "changeview")) {
1119                 change_view();
1120         } else if (!strcasecmp(action, "folders")) {
1121                 folders();
1122         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1123                 do_stuff_to_msgs();
1124         } else if (!strcasecmp(action, "change_start_page")) {
1125                 change_start_page();
1126         } else if (!strcasecmp(action, "display_floorconfig")) {
1127                 display_floorconfig(NULL);
1128         } else if (!strcasecmp(action, "toggle_self_service")) {
1129                 toggle_self_service();
1130 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1131         } else if (!strcasecmp(action, "display_edit_task")) {
1132                 display_edit_task();
1133         } else if (!strcasecmp(action, "save_task")) {
1134                 save_task();
1135         } else if (!strcasecmp(action, "display_edit_event")) {
1136                 display_edit_event();
1137         } else if (!strcasecmp(action, "save_event")) {
1138                 save_event();
1139         } else if (!strcasecmp(action, "respond_to_request")) {
1140                 respond_to_request();
1141         } else if (!strcasecmp(action, "handle_rsvp")) {
1142                 handle_rsvp();
1143 #endif
1144         } else if (!strcasecmp(action, "summary")) {
1145                 summary();
1146         } else if (!strcasecmp(action, "diagnostics")) {
1147                 output_headers(1);
1148
1149                 wprintf("You're in session %d<HR>\n", WC->wc_session);
1150                 wprintf("Command: <BR><PRE>\n");
1151                 escputs(cmd);
1152                 wprintf("</PRE><HR>\n");
1153                 wprintf("Variables: <BR><PRE>\n");
1154                 dump_vars();
1155                 wprintf("</PRE><HR>\n");
1156                 wDumpContent(1);
1157         }
1158         /* When all else fais, display the main menu. */
1159         else {
1160                 display_main_menu();
1161         }
1162
1163 SKIP_ALL_THIS_CRAP:
1164         fflush(stdout);
1165         if (content != NULL) {
1166                 free(content);
1167                 content = NULL;
1168         }
1169         free_urls();
1170         if (WC->upload_length > 0) {
1171                 free(WC->upload);
1172                 WC->upload_length = 0;
1173         }
1174
1175 }