]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* When editing another user's vCard, do a "transient goto" to their config
[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[1024];
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 (WC->fake_frames) {
178                 wprintf("</TABLE>\n");
179                 WC->fake_frames = 0;
180         }
181
182         if (print_standard_html_footer) {
183                 if (print_standard_html_footer != 2) {
184                         wprintf("<BR>");
185                 }
186                 wprintf("</BODY></HTML>\n");
187         }
188
189
190 }
191
192
193 /*
194  * Copy a string, escaping characters which have meaning in HTML.  If
195  * nbsp is nonzero, spaces are converted to non-breaking spaces.
196  */
197 void stresc(char *target, char *strbuf, int nbsp)
198 {
199         int a;
200         strcpy(target, "");
201
202         for (a = 0; a < strlen(strbuf); ++a) {
203                 if (strbuf[a] == '<')
204                         strcat(target, "&lt;");
205                 else if (strbuf[a] == '>')
206                         strcat(target, "&gt;");
207                 else if (strbuf[a] == '&')
208                         strcat(target, "&amp;");
209                 else if (strbuf[a] == '\"')
210                         strcat(target, "&quot;");
211                 else if (strbuf[a] == '\'') 
212                         strcat(target, "&#39;");
213                 else if (strbuf[a] == LB)
214                         strcat(target, "<");
215                 else if (strbuf[a] == RB)
216                         strcat(target, ">");
217                 else if (strbuf[a] == QU)
218                         strcat(target, "\"");
219                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
220                         strcat(target, "&nbsp;");
221                 } else {
222                         strncat(target, &strbuf[a], 1);
223                 }
224         }
225 }
226
227 void escputs1(char *strbuf, int nbsp)
228 {
229         char buf[1024];
230         stresc(buf, strbuf, nbsp);
231         wprintf("%s", buf);
232 }
233
234 void escputs(char *strbuf)
235 {
236         escputs1(strbuf, 0);
237 }
238
239 /*
240  * Escape a string for feeding out as a URL.
241  * Returns a pointer to a buffer that must be freed by the caller!
242  */
243 void urlesc(char *outbuf, char *strbuf)
244 {
245         int a, b, c;
246         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
247
248         strcpy(outbuf, "");
249
250         for (a = 0; a < strlen(strbuf); ++a) {
251                 c = 0;
252                 for (b = 0; b < strlen(ec); ++b) {
253                         if (strbuf[a] == ec[b])
254                                 c = 1;
255                 }
256                 b = strlen(outbuf);
257                 if (c == 1)
258                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
259                 else
260                         sprintf(&outbuf[b], "%c", strbuf[a]);
261         }
262 }
263
264 void urlescputs(char *strbuf)
265 {
266         char outbuf[SIZ];
267         
268         urlesc(outbuf, strbuf);
269         wprintf("%s", outbuf);
270 }
271
272
273
274
275 /*
276  * Output all that important stuff that the browser will want to see
277  *
278  * control codes:
279  * 
280  * Bits 0 and 1:
281  * 0 = Nothing.  Do not display any leading HTTP or HTML.
282  * 1 = HTTP headers plus the room banner
283  * 2 = HTTP headers required to terminate the session (unset cookies)
284  * 3 = HTTP and HTML headers, but no room banner
285  *
286  * Bit 2: Set to 1 to auto-refresh page every 30 seconds
287  *
288  * Bit 3: suppress check for express messages
289  */
290 void output_headers(int controlcode)
291 {
292         char cookie[SIZ];
293         int print_standard_html_head = 0;
294         int refresh30 = 0;
295         int suppress_check = 0;
296         char httpnow[SIZ];
297         static int pageseq = 0;
298
299         print_standard_html_head        =       controlcode & 0x03;
300         refresh30                       =       ((controlcode & 0x04) >> 2);
301         suppress_check                  =       ((controlcode & 0x08) >> 3);
302
303         wprintf("HTTP/1.0 200 OK\n");
304
305         httpdate(httpnow, time(NULL));
306
307         if (print_standard_html_head > 0) {
308                 wprintf("Content-type: text/html\n");
309                 wprintf("Server: %s\n", SERVER);
310                 wprintf("Connection: close\n");
311                 wprintf("Pragma: no-cache\n");
312                 wprintf("Cache-Control: no-store\n");
313         }
314         stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
315                         WC->wc_password, WC->wc_roomname);
316         if (print_standard_html_head == 2) {
317                 wprintf("Set-cookie: webcit=%s\n", unset);
318         } else {
319                 wprintf("Set-cookie: webcit=%s\n", cookie);
320                 if (server_cookie != NULL) {
321                         wprintf("%s\n", server_cookie);
322                 }
323         }
324
325         if (print_standard_html_head > 0) {
326                 wprintf("\n");
327                 wprintf("<HTML><HEAD><TITLE>");
328                 escputs(serv_info.serv_humannode);
329                 wprintf("</TITLE>\n"
330                         "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n"
331                         "<META HTTP-EQUIV=\"expired\" CONTENT=\"28-May-1971 18:10:00 GMT\">\n"
332                         "<META NAME=\"MSSmartTagsPreventParsing\" CONTENT=\"TRUE\">\n");
333                 if (refresh30) wprintf(
334                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"30\">\n");
335                 else wprintf(
336                         "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
337                 wprintf("</HEAD>\n");
338
339                 /* script for checking for pages (not always launched) */
340                 wprintf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
341                 wprintf("function launch_page_popup() {\n");
342                 wprintf("pwin = window.open('/page_popup', 'CitaPage%d', "
343                         "'toolbar=no,location=no,copyhistory=no,status=no,"
344                         "scrollbars=yes,resizable=no,height=250,width=400');\n",
345                         ++pageseq);
346                 wprintf("}\n");
347                 wprintf("</SCRIPT>\n");
348                 /* end script */
349
350                 /* (JavaScript keyboard-based navigation would go here) */
351
352                 if (!suppress_check) if (WC->HaveExpressMessages) {
353                         svprintf("extrabodyparms", WCS_STRING, "%s", 
354                                 "onload=\"launch_page_popup()\" ");
355                         WC->HaveExpressMessages = 0;
356                 }
357                 do_template("background");
358                 clear_local_substs();
359
360         if (print_standard_html_head == 1) {
361                 wprintf("<A NAME=\"TheTop\"></A>");
362
363                 wprintf("<TABLE border=0 width=100%%><TR VALIGN=TOP>"
364                         "<TD>\n");
365
366                 embed_room_banner(NULL);
367
368                 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
369                 
370                 WC->fake_frames = 1;
371                 }
372         }
373 }
374
375
376 /*
377  *
378  */
379 void http_redirect(char *whichpage) {
380         wprintf("HTTP/1.0 302 Moved Temporarily\n");
381         wprintf("Location: %s\n", whichpage);
382         wprintf("URI: %s\n", whichpage);
383         wprintf("Content-type: text/html\n\n");
384         wprintf("<html><body>\n");
385         wprintf("you really want to be <A HREF=\"%s\">here</A> now\n",
386                 whichpage);
387         wprintf("</body></html>\n");
388 }
389
390
391
392 void check_for_express_messages()
393 {
394         char buf[SIZ];
395
396         serv_puts("NOOP");
397         serv_gets(buf);
398         if (buf[3] == '*') WC->HaveExpressMessages = 1;
399 }
400
401
402
403
404 void output_static(char *what)
405 {
406         char buf[4096];
407         FILE *fp;
408         struct stat statbuf;
409         off_t bytes;
410         char *bigbuffer;
411
412         sprintf(buf, "static/%s", what);
413         fp = fopen(buf, "rb");
414         if (fp == NULL) {
415                 wprintf("HTTP/1.0 404 %s\n", strerror(errno));
416                 wprintf("Content-Type: text/plain\n");
417                 wprintf("\n");
418                 wprintf("Cannot open %s: %s\n", what, strerror(errno));
419         } else {
420                 output_headers(0);
421
422                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
423                         wprintf("Content-type: image/gif\n");
424                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
425                         wprintf("Content-type: text/plain\n");
426                 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
427                         wprintf("Content-type: text/css\n");
428                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
429                         wprintf("Content-type: image/jpeg\n");
430                 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
431                         wprintf("Content-type: image/png\n");
432                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
433                         wprintf("Content-type: text/html\n");
434                 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
435                         wprintf("Content-type: text/vnd.wap.wml\n");
436                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
437                         wprintf("Content-type: text/vnd.wap.wmlscript\n");
438                 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
439                         wprintf("Content-type: application/vnd.wap.wmlc\n");
440                 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
441                         wprintf("Content-type: application/vnd.wap.wmlscriptc\n");
442                 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
443                         wprintf("Content-type: image/vnd.wap.wbmp\n");
444                 else
445                         wprintf("Content-type: application/octet-stream\n");
446
447                 fstat(fileno(fp), &statbuf);
448                 bytes = statbuf.st_size;
449                 lprintf(3, "Static: %s, %ld bytes\n", what, bytes);
450                 wprintf("Content-length: %ld\n", (long) bytes);
451                 wprintf("\n");
452                 bigbuffer = malloc(bytes);
453                 fread(bigbuffer, bytes, 1, fp);
454                 fclose(fp);
455                 write(WC->http_sock, bigbuffer, bytes);
456                 free(bigbuffer);
457         }
458         if (!strcasecmp(bstr("force_close_session"), "yes")) {
459                 end_webcit_session();
460         }
461 }
462
463
464 /*
465  * When the browser requests an image file from the Citadel server,
466  * this function is called to transmit it.
467  */
468 void output_image()
469 {
470         char buf[SIZ];
471         char xferbuf[4096];
472         off_t bytes;
473         off_t thisblock;
474         off_t accomplished = 0L;
475
476         lprintf(5, "output_image() called\n");
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                 output_headers(0);
482                 wprintf("Content-type: image/gif\n");
483                 wprintf("Content-length: %ld\n", (long) bytes);
484                 wprintf("\n");
485
486                 while (bytes > (off_t) 0) {
487                         thisblock = (off_t) sizeof(xferbuf);
488                         if (thisblock > bytes) {
489                                 thisblock = bytes;
490                         }
491                         serv_printf("READ %ld|%ld", accomplished, thisblock);
492                         serv_gets(buf);
493                         if (buf[0] == '6') {
494                                 thisblock = extract_long(&buf[4], 0);
495                                 serv_read(xferbuf, (int) thisblock);
496                         }
497                         else {
498                                 memset(xferbuf, 0, thisblock);
499                         }
500                         write(WC->http_sock, xferbuf, thisblock);
501                         bytes = bytes - thisblock;
502                         accomplished = accomplished + thisblock;
503                 }
504                 serv_puts("CLOS");
505                 serv_gets(buf);
506         } else {
507                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
508                 output_headers(0);
509                 wprintf("Content-Type: text/plain\n");
510                 wprintf("\n");
511                 wprintf("Error retrieving image: %s\n", &buf[4]);
512         }
513
514 }
515
516 /*
517  */
518 void output_mimepart()
519 {
520         char buf[SIZ];
521         char xferbuf[4096];
522         off_t bytes;
523         off_t thisblock;
524         off_t accomplished = 0L;
525         char content_type[SIZ];
526         
527         serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
528         serv_gets(buf);
529         if (buf[0] == '2') {
530                 bytes = extract_long(&buf[4], 0);
531                 extract(content_type, &buf[4], 3);
532                 output_headers(0);
533                 wprintf("Content-type: %s\n", content_type);
534                 wprintf("Content-length: %ld\n", (long) bytes);
535                 wprintf("\n");
536
537                 while (bytes > (off_t) 0) {
538                         thisblock = (off_t) sizeof(xferbuf);
539                         if (thisblock > bytes) {
540                                 thisblock = bytes;
541                         }
542                         serv_printf("READ %ld|%ld", accomplished, thisblock);
543                         serv_gets(buf);
544                         if (buf[0] == '6') {
545                                 thisblock = extract_long(&buf[4], 0);
546                                 serv_read(xferbuf, (int) thisblock);
547                         }
548                         else {
549                                 memset(xferbuf, 0, thisblock);
550                         }
551                         write(WC->http_sock, xferbuf, thisblock);
552                         bytes = bytes - thisblock;
553                         accomplished = accomplished + thisblock;
554                 }
555                 serv_puts("CLOS");
556                 serv_gets(buf);
557         } else {
558                 wprintf("HTTP/1.0 404 %s\n", &buf[4]);
559                 output_headers(0);
560                 wprintf("Content-Type: text/plain\n");
561                 wprintf("\n");
562                 wprintf("Error retrieving part: %s\n", &buf[4]);
563         }
564
565 }
566
567
568 /*
569  */
570 char *load_mimepart(long msgnum, char *partnum)
571 {
572         char buf[SIZ];
573         off_t bytes;
574         off_t thisblock;
575         off_t accomplished = 0L;
576         char content_type[SIZ];
577         char *content;
578         
579         serv_printf("OPNA %ld|%s", msgnum, partnum);
580         serv_gets(buf);
581         if (buf[0] == '2') {
582                 bytes = extract_long(&buf[4], 0);
583                 extract(content_type, &buf[4], 3);
584
585                 content = malloc(bytes + 1);
586
587                 while (bytes > (off_t) 0) {
588                         thisblock = bytes;
589                         if (thisblock > 4096L) {
590                                 thisblock = 4096L;
591                         }
592                         if (thisblock > bytes) {
593                                 thisblock = bytes;
594                         }
595                         serv_printf("READ %ld|%ld", accomplished, thisblock);
596                         serv_gets(buf);
597                         if (buf[0] == '6') {
598                                 thisblock = extract_long(&buf[4], 0);
599                                 serv_read(&content[accomplished], (int) thisblock);
600                         }
601                         else {
602                                 memset(&content[accomplished], 0, thisblock);
603                         }
604                         bytes = bytes - thisblock;
605                         accomplished = accomplished + thisblock;
606                 }
607                 serv_puts("CLOS");
608                 serv_gets(buf);
609                 content[accomplished] = 0;      /* null terminate for good measure */
610                 return(content);
611         }
612         else {
613                 return(NULL);
614         }
615
616 }
617
618
619 /*
620  * Convenience functions to display a page containing only a string
621  */
622 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
623 {
624         wprintf("HTTP/1.0 200 OK\n");
625         output_headers(1);
626         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
627         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
628         wprintf("<B>%s</B>\n", titlebarmsg);
629         wprintf("</FONT></TD></TR></TABLE><BR>\n");
630         escputs(messagetext);
631
632         wprintf("<HR>\n");
633         wDumpContent(1);
634 }
635
636
637 /*
638  * Display a blank page.
639  */
640 void blank_page(void) {
641         output_headers(7);
642         wDumpContent(2);
643 }
644
645
646 void display_error(char *errormessage)
647 {
648         convenience_page("770000", "Error", errormessage);
649 }
650
651 void display_success(char *successmessage)
652 {
653         convenience_page("007700", "OK", successmessage);
654 }
655
656
657
658 void extract_action(char *actbuf, char *cmdbuf)
659 {
660         int i;
661
662         strcpy(actbuf, cmdbuf);
663         if (!strncasecmp(actbuf, "GET /", 5))
664                 strcpy(actbuf, &actbuf[5]);
665         if (!strncasecmp(actbuf, "PUT /", 5))
666                 strcpy(actbuf, &actbuf[5]);
667         if (!strncasecmp(actbuf, "POST /", 6))
668                 strcpy(actbuf, &actbuf[6]);
669
670         for (i = 0; i < strlen(actbuf); ++i) {
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                 if (actbuf[i] == '?') {
680                         actbuf[i] = 0;
681                         i = 0;
682                 }
683                 if (actbuf[i] == '&') {
684                         actbuf[i] = 0;
685                         i = 0;
686                 }
687         }
688 }
689
690
691 void upload_handler(char *name, char *filename, char *partnum, char *disp,
692                         void *content, char *cbtype, size_t length,
693                         char *encoding, void *userdata)
694 {
695
696         lprintf(5, "UPLOAD HANDLER CALLED\n");
697         lprintf(5, "    name = %s\n", name);
698         lprintf(5, "filename = %s\n", filename);
699         lprintf(5, "encoding = %s\n", encoding);
700         lprintf(5, "    type = %s\n", cbtype);
701         lprintf(5, "  length = %ld\n", (long)length);
702
703         if (length > 0) {
704                 WC->upload = malloc(length);
705                 if (WC->upload != NULL) {
706                         WC->upload_length = length;
707                         memcpy(WC->upload, content, length);
708                 }
709                 else {
710                         lprintf(9, "malloc() failed: %s\n",
711                                 strerror(errno));
712                 }
713         }
714 }
715
716
717 /*
718  * Entry point for WebCit transaction
719  */
720 void session_loop(struct httprequest *req)
721 {
722         char cmd[SIZ];
723         char action[SIZ];
724         char buf[SIZ];
725         int a, b;
726         int ContentLength = 0;
727         int BytesRead;
728         char ContentType[512];
729         char *content;
730         char *content_end;
731         struct httprequest *hptr;
732         char browser_host[SIZ];
733         char user_agent[SIZ];
734
735         /* We stuff these with the values coming from the client cookies,
736          * so we can use them to reconnect a timed out session if we have to.
737          */
738         char c_host[SIZ];
739         char c_port[SIZ];
740         char c_username[SIZ];
741         char c_password[SIZ];
742         char c_roomname[SIZ];
743         char cookie[SIZ];
744
745         strcpy(c_host, defaulthost);
746         strcpy(c_port, defaultport);
747         strcpy(c_username, "");
748         strcpy(c_password, "");
749         strcpy(c_roomname, "");
750
751         WC->upload_length = 0;
752         WC->upload = NULL;
753
754         WC->is_wap = 0;
755
756         hptr = req;
757         if (hptr == NULL) return;
758
759         strcpy(cmd, hptr->line);
760         hptr = hptr->next;
761         extract_action(action, cmd);
762
763         while (hptr != NULL) {
764                 strcpy(buf, hptr->line);
765                 hptr = hptr->next;
766
767                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
768                         strcpy(cookie, &buf[15]);
769                         cookie_to_stuff(cookie, NULL,
770                                       c_username, c_password, c_roomname);
771                 }
772                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
773                         ContentLength = atoi(&buf[16]);
774                 }
775                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
776                         strcpy(ContentType, &buf[14]);
777                 }
778                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
779                         strcpy(user_agent, &buf[12]);
780                 }
781                 /* Only WAP gateways explicitly name this content-type */
782                 else if (strstr(buf, "text/vnd.wap.wml")) {
783                         WC->is_wap = 1;
784                 }
785         }
786
787         if (ContentLength > 0) {
788                 lprintf(5, "Content length: %d\n", ContentLength);
789                 content = malloc(ContentLength + 1);
790                 memset(content, 0, ContentLength+1);
791                 BytesRead = 0;
792
793                 while (BytesRead < ContentLength) {
794                         a=read(WC->http_sock, &content[BytesRead],
795                                 ContentLength - BytesRead);
796                         if (a <= 0) BytesRead = ContentLength;
797                         else BytesRead += a;
798                 }
799
800                 if (!strncasecmp(ContentType,
801                               "application/x-www-form-urlencoded", 33)) {
802                         addurls(content);
803                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
804                         content_end = content + ContentLength;
805                         mime_parser(content, content_end, *upload_handler,
806                                         NULL, NULL, NULL, 0);
807                 }
808         } else {
809                 content = NULL;
810         }
811
812         /* If there are variables in the URL, we must grab them now */
813         for (a = 0; a < strlen(cmd); ++a)
814                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
815                         for (b = a; b < strlen(cmd); ++b)
816                                 if (isspace(cmd[b]))
817                                         cmd[b] = 0;
818                         addurls(&cmd[a + 1]);
819                         cmd[a] = 0;
820                 }
821         /*
822          * If we're not connected to a Citadel server, try to hook up the
823          * connection now.  Preference is given to the host and port specified
824          * by browser cookies, if cookies have been supplied.
825          */
826         if (!WC->connected) {
827                 if (strlen(bstr("host")) > 0)
828                         strcpy(c_host, bstr("host"));
829                 if (strlen(bstr("port")) > 0)
830                         strcpy(c_port, bstr("port"));
831
832                 if (!strcasecmp(c_host, "uds")) {
833                         /* unix domain socket */
834                         sprintf(buf, "%s/citadel.socket", c_port);
835                         WC->serv_sock = uds_connectsock(buf);
836                 }
837                 else {
838                         /* tcp socket */
839                         WC->serv_sock = tcp_connectsock(c_host, c_port);
840                 }
841
842                 if (WC->serv_sock < 0) {
843                         do_logout();
844                         goto SKIP_ALL_THIS_CRAP;
845                 }
846                 else {
847                         WC->connected = 1;
848                         serv_gets(buf); /* get the server welcome message */
849                         locate_host(browser_host, WC->http_sock);
850                         get_serv_info(browser_host, user_agent);
851                 }
852         }
853
854         check_for_express_messages();
855
856         /*
857          * If we're not logged in, but we have username and password cookies
858          * supplied by the browser, try using them to log in.
859          */
860         if ((!WC->logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
861                 serv_printf("USER %s", c_username);
862                 serv_gets(buf);
863                 if (buf[0] == '3') {
864                         serv_printf("PASS %s", c_password);
865                         serv_gets(buf);
866                         if (buf[0] == '2') {
867                                 become_logged_in(c_username, c_password, buf);
868                         }
869                 }
870         }
871         /*
872          * If we don't have a current room, but a cookie specifying the
873          * current room is supplied, make an effort to go there.
874          */
875         if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
876                 serv_printf("GOTO %s", c_roomname);
877                 serv_gets(buf);
878                 if (buf[0] == '2') {
879                         strcpy(WC->wc_roomname, c_roomname);
880                 }
881         }
882         if (!strcasecmp(action, "static")) {
883                 strcpy(buf, &cmd[12]);
884                 for (a = 0; a < strlen(buf); ++a)
885                         if (isspace(buf[a]))
886                                 buf[a] = 0;
887                 output_static(buf);
888         } else if (!strcasecmp(action, "image")) {
889                 output_image();
890         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
891                 do_login();
892         } else if (!WC->logged_in) {
893                 display_login(NULL);
894         }
895         /* Various commands... */
896
897         else if (!strcasecmp(action, "do_welcome")) {
898                 do_welcome();
899         } else if (!strcasecmp(action, "blank")) {
900                 blank_page();
901         } else if (!strcasecmp(action, "display_main_menu")) {
902                 display_main_menu();
903         } else if (!strcasecmp(action, "whobbs")) {
904                 whobbs();
905         } else if (!strcasecmp(action, "knrooms")) {
906                 knrooms();
907         } else if (!strcasecmp(action, "gotonext")) {
908                 slrp_highest();
909                 gotonext();
910         } else if (!strcasecmp(action, "skip")) {
911                 gotonext();
912         } else if (!strcasecmp(action, "ungoto")) {
913                 ungoto();
914         } else if (!strcasecmp(action, "dotgoto")) {
915                 slrp_highest();
916                 smart_goto(bstr("room"));
917         } else if (!strcasecmp(action, "dotskip")) {
918                 smart_goto(bstr("room"));
919         } else if (!strcasecmp(action, "termquit")) {
920                 do_logout();
921         } else if (!strcasecmp(action, "readnew")) {
922                 readloop("readnew");
923         } else if (!strcasecmp(action, "readold")) {
924                 readloop("readold");
925         } else if (!strcasecmp(action, "readfwd")) {
926                 readloop("readfwd");
927         } else if (!strcasecmp(action, "headers")) {
928                 readloop("headers");
929         } else if (!strcasecmp(action, "display_enter")) {
930                 display_enter();
931         } else if (!strcasecmp(action, "post")) {
932                 post_message();
933         } else if (!strcasecmp(action, "delete_msg")) {
934                 delete_msg();
935         } else if (!strcasecmp(action, "confirm_move_msg")) {
936                 confirm_move_msg();
937         } else if (!strcasecmp(action, "move_msg")) {
938                 move_msg();
939         } else if (!strcasecmp(action, "userlist")) {
940                 userlist();
941         } else if (!strcasecmp(action, "showuser")) {
942                 showuser();
943         } else if (!strcasecmp(action, "display_page")) {
944                 display_page();
945         } else if (!strcasecmp(action, "page_user")) {
946                 page_user();
947         } else if (!strcasecmp(action, "chat")) {
948                 do_chat();
949         } else if (!strcasecmp(action, "display_private")) {
950                 display_private("", 0);
951         } else if (!strcasecmp(action, "goto_private")) {
952                 goto_private();
953         } else if (!strcasecmp(action, "zapped_list")) {
954                 zapped_list();
955         } else if (!strcasecmp(action, "display_zap")) {
956                 display_zap();
957         } else if (!strcasecmp(action, "zap")) {
958                 zap();
959         } else if (!strcasecmp(action, "display_entroom")) {
960                 display_entroom();
961         } else if (!strcasecmp(action, "entroom")) {
962                 entroom();
963         } else if (!strcasecmp(action, "display_editroom")) {
964                 display_editroom();
965         } else if (!strcasecmp(action, "netedit")) {
966                 netedit();
967         } else if (!strcasecmp(action, "editroom")) {
968                 editroom();
969         } else if (!strcasecmp(action, "display_whok")) {
970                 display_whok();
971         } else if (!strcasecmp(action, "display_editinfo")) {
972                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
973         } else if (!strcasecmp(action, "editinfo")) {
974                 save_edit("Room info", "EINF 1", 1);
975         } else if (!strcasecmp(action, "display_editbio")) {
976                 sprintf(buf, "RBIO %s", WC->wc_username);
977                 display_edit("Your bio", "NOOP", buf, "editbio");
978         } else if (!strcasecmp(action, "editbio")) {
979                 save_edit("Your bio", "EBIO", 0);
980         } else if (!strcasecmp(action, "confirm_delete_room")) {
981                 confirm_delete_room();
982         } else if (!strcasecmp(action, "delete_room")) {
983                 delete_room();
984         } else if (!strcasecmp(action, "validate")) {
985                 validate();
986         } else if (!strcasecmp(action, "display_editpic")) {
987                 display_graphics_upload("your photo",
988                                         "UIMG 0|_userpic_",
989                                         "/editpic");
990         } else if (!strcasecmp(action, "editpic")) {
991                 do_graphics_upload("UIMG 1|_userpic_");
992         } else if (!strcasecmp(action, "display_editroompic")) {
993                 display_graphics_upload("the graphic for this room",
994                                         "UIMG 0|_roompic_",
995                                         "/editroompic");
996         } else if (!strcasecmp(action, "editroompic")) {
997                 do_graphics_upload("UIMG 1|_roompic_");
998         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
999                 select_floor_to_edit_pic();
1000         } else if (!strcasecmp(action, "display_editfloorpic")) {
1001                 sprintf(buf, "UIMG 0|_floorpic_|%s",
1002                         bstr("which_floor"));
1003                 display_graphics_upload("the graphic for this floor",
1004                                         buf,
1005                                         "/editfloorpic");
1006         } else if (!strcasecmp(action, "editfloorpic")) {
1007                 sprintf(buf, "UIMG 1|_floorpic_|%s",
1008                         bstr("which_floor"));
1009                 do_graphics_upload(buf);
1010         } else if (!strcasecmp(action, "display_reg")) {
1011                 display_reg(0);
1012         } else if (!strcasecmp(action, "register")) {
1013                 register_user();
1014         } else if (!strcasecmp(action, "display_changepw")) {
1015                 display_changepw();
1016         } else if (!strcasecmp(action, "changepw")) {
1017                 changepw();
1018         } else if (!strcasecmp(action, "display_edit_node")) {
1019                 display_edit_node();
1020         } else if (!strcasecmp(action, "edit_node")) {
1021                 edit_node();
1022         } else if (!strcasecmp(action, "display_netconf")) {
1023                 display_netconf();
1024         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1025                 display_confirm_delete_node();
1026         } else if (!strcasecmp(action, "delete_node")) {
1027                 delete_node();
1028         } else if (!strcasecmp(action, "display_add_node")) {
1029                 display_add_node();
1030         } else if (!strcasecmp(action, "add_node")) {
1031                 add_node();
1032         } else if (!strcasecmp(action, "terminate_session")) {
1033                 slrp_highest();
1034                 terminate_session();
1035         } else if (!strcasecmp(action, "edit_me")) {
1036                 edit_me();
1037         } else if (!strcasecmp(action, "display_siteconfig")) {
1038                 display_siteconfig();
1039         } else if (!strcasecmp(action, "page_popup")) {
1040                 page_popup();
1041         } else if (!strcasecmp(action, "siteconfig")) {
1042                 siteconfig();
1043         } else if (!strcasecmp(action, "display_generic")) {
1044                 display_generic();
1045         } else if (!strcasecmp(action, "do_generic")) {
1046                 do_generic();
1047         } else if (!strcasecmp(action, "display_menubar")) {
1048                 display_menubar(1);
1049         } else if (!strcasecmp(action, "output_mimepart")) {
1050                 output_mimepart();
1051         } else if (!strcasecmp(action, "edit_vcard")) {
1052                 edit_vcard();
1053         } else if (!strcasecmp(action, "submit_vcard")) {
1054                 submit_vcard();
1055         } else if (!strcasecmp(action, "select_user_to_edit")) {
1056                 select_user_to_edit(NULL, NULL);
1057         } else if (!strcasecmp(action, "display_edituser")) {
1058                 display_edituser(NULL);
1059         } else if (!strcasecmp(action, "edituser")) {
1060                 edituser();
1061         } else if (!strcasecmp(action, "create_user")) {
1062                 create_user();
1063         } else if (!strcasecmp(action, "changeview")) {
1064                 change_view();
1065         } else if (!strcasecmp(action, "folders")) {
1066                 folders();
1067         } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1068                 do_stuff_to_msgs();
1069         } else if (!strcasecmp(action, "diagnostics")) {
1070                 output_headers(1);
1071
1072                 wprintf("You're in session %d<HR>\n", WC->wc_session);
1073                 wprintf("Command: <BR><PRE>\n");
1074                 escputs(cmd);
1075                 wprintf("</PRE><HR>\n");
1076                 wprintf("Variables: <BR><PRE>\n");
1077                 dump_vars();
1078                 wprintf("</PRE><HR>\n");
1079                 wDumpContent(1);
1080         }
1081         /* When all else fais, display the main menu. */
1082         else {
1083                 display_main_menu();
1084         }
1085
1086 SKIP_ALL_THIS_CRAP:
1087         fflush(stdout);
1088         if (content != NULL) {
1089                 free(content);
1090                 content = NULL;
1091         }
1092         free_urls();
1093         if (WC->upload_length > 0) {
1094                 free(WC->upload);
1095                 WC->upload_length = 0;
1096         }
1097 }