4 * This is the main transaction loop of the web service. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
12 #include "webserver.h"
13 #include "mime_parser.h"
16 * Subdirectories from which the client may request static content
18 char *static_content_dirs[] = {
23 * String to unset the cookie.
24 * Any date "in the past" will work, so I chose my birthday, right down to
25 * the exact minute. :)
27 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
29 void unescape_input(char *buf)
34 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
35 buf[strlen(buf) - 1] = 0;
37 for (a = 0; a < strlen(buf); ++a) {
45 sscanf(hex, "%02x", &b);
47 strcpy(&buf[a + 1], &buf[a + 3]);
54 void addurls(char *url)
62 while (strlen(up) > 0) {
64 /* locate the = sign */
65 safestrncpy(buf, up, sizeof buf);
67 for (a = 255; a >= 0; --a)
74 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
75 u->next = WC->urlstrings;
77 safestrncpy(u->url_key, buf, sizeof u->url_key);
79 /* now chop that part off */
80 for (a = 0; a <= b; ++a)
83 /* locate "&" and "?" delimiters */
86 for (a = 0; a < strlen(up); ++a) {
87 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
94 for (a = 0; a < b; ++a)
98 u->url_data = malloc(strlen(up) + 2);
99 safestrncpy(u->url_data, up, strlen(up) + 1);
101 unescape_input(u->url_data);
109 struct urlcontent *u;
111 while (WC->urlstrings != NULL) {
112 free(WC->urlstrings->url_data);
113 u = WC->urlstrings->next;
114 free(WC->urlstrings);
120 * Diagnostic function to display the contents of all variables
124 struct urlcontent *u;
126 for (u = WC->urlstrings; u != NULL; u = u->next) {
127 wprintf("%38s = %s\n", u->url_key, u->url_data);
131 char *bstr(char *key)
133 struct urlcontent *u;
135 for (u = WC->urlstrings; u != NULL; u = u->next) {
136 if (!strcasecmp(u->url_key, key))
137 return (u->url_data);
143 void wprintf(const char *format,...)
148 va_start(arg_ptr, format);
149 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
152 client_write(wbuf, strlen(wbuf));
157 * wDumpContent() wraps up an HTTP session, closes tags, etc.
159 * print_standard_html_footer should be set to 0 to transmit only, 1 to
160 * append the main menu and closing tags, or 2 to
161 * append the closing tags only.
163 void wDumpContent(int print_standard_html_footer)
165 if (print_standard_html_footer) {
166 wprintf("</div>\n"); /* end of "text" div */
167 do_template("trailing");
170 /* If we've been saving it all up for one big output burst,
171 * go ahead and do that now.
178 * Copy a string, escaping characters which have meaning in HTML. If
179 * nbsp is nonzero, spaces are converted to non-breaking spaces.
181 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
186 for (a = 0; a < strlen(strbuf); ++a) {
187 if (strbuf[a] == '<')
188 strcat(target, "<");
189 else if (strbuf[a] == '>')
190 strcat(target, ">");
191 else if (strbuf[a] == '&')
192 strcat(target, "&");
193 else if (strbuf[a] == '\"')
194 strcat(target, """);
195 else if (strbuf[a] == '\'')
196 strcat(target, "'");
197 else if (strbuf[a] == LB)
199 else if (strbuf[a] == RB)
201 else if (strbuf[a] == QU)
202 strcat(target, "\"");
203 else if ((strbuf[a] == 32) && (nbsp == 1))
204 strcat(target, " ");
205 else if ((strbuf[a] == '\n') && (nolinebreaks))
206 strcat(target, ""); /* nothing */
207 else if ((strbuf[a] == '\r') && (nolinebreaks))
208 strcat(target, ""); /* nothing */
210 strncat(target, &strbuf[a], 1);
214 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
218 if (strbuf == NULL) return;
219 buf = malloc( (3 * strlen(strbuf)) + SIZ );
220 stresc(buf, strbuf, nbsp, nolinebreaks);
225 void escputs(char *strbuf)
227 escputs1(strbuf, 0, 0);
231 * Escape a string for feeding out as a URL.
232 * Returns a pointer to a buffer that must be freed by the caller!
234 void urlesc(char *outbuf, char *strbuf)
237 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
241 for (a = 0; a < strlen(strbuf); ++a) {
243 for (b = 0; b < strlen(ec); ++b) {
244 if (strbuf[a] == ec[b])
249 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
251 sprintf(&outbuf[b], "%c", strbuf[a]);
255 void urlescputs(char *strbuf)
259 urlesc(outbuf, strbuf);
260 wprintf("%s", outbuf);
265 * Copy a string, escaping characters for JavaScript strings.
267 void jsesc(char *target, char *strbuf)
272 for (a = 0; a < strlen(strbuf); ++a) {
273 if (strbuf[a] == '<')
275 else if (strbuf[a] == '>')
277 else if (strbuf[a] == '\"')
278 strcat(target, """);
279 else if (strbuf[a] == '&')
280 strcat(target, "&;");
281 else if (strbuf[a] == '\'')
282 strcat(target, "\\'");
284 strncat(target, &strbuf[a], 1);
289 void jsescputs(char *strbuf)
293 jsesc(outbuf, strbuf);
294 wprintf("%s", outbuf);
298 * Copy a string, escaping characters for message text hold
300 void msgesc(char *target, char *strbuf)
305 for (a = 0; a < strlen(strbuf); ++a) {
306 if (strbuf[a] == '\'')
307 strcat(target, "\\'");
308 else if (strbuf[a] == '\n')
310 else if (strbuf[a] == '\r')
312 else if (strbuf[a] == '\'')
313 strcat(target, "'");
315 strncat(target, &strbuf[a], 1);
320 void msgescputs(char *strbuf) {
323 if (strbuf == NULL) return;
324 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
325 msgesc(outbuf, strbuf);
326 wprintf("%s", outbuf);
334 * Output all that important stuff that the browser will want to see
336 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
337 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
339 int do_room_banner, /* 0=no, 1=yes, */
340 /* 2 = I'm going to embed my own, so don't open the */
341 /* <div id="content"> either. */
343 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
344 int suppress_check, /* 1 = suppress check for instant messages */
345 int cache /* 1 = allow browser to cache this page */
350 wprintf("HTTP/1.1 200 OK\n");
351 httpdate(httpnow, time(NULL));
353 if (do_httpheaders) {
354 wprintf("Content-type: text/html; charset=utf-8\r\n"
356 "Connection: close\r\n",
357 SERVER, serv_info.serv_software
362 wprintf("Pragma: public\r\n"
363 "Cache-Control: max-age=3600, must-revalidate\r\n"
364 "Last-modified: %s\r\n",
369 wprintf("Pragma: no-cache\r\n"
370 "Cache-Control: no-store\r\n"
374 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
375 WC->wc_password, WC->wc_roomname);
378 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
380 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
381 if (server_cookie != NULL) {
382 wprintf("%s\n", server_cookie);
395 if (WC->HaveInstantMessages) {
396 wprintf("<div id=\"page_popup\">\n");
400 if (strlen(WC->ImportantMessage) > 0) {
401 wprintf("<div id=\"important_message\">\n");
402 wprintf("<SPAN CLASS=\"imsg\">"
403 "%s</SPAN><br />\n", WC->ImportantMessage);
405 wprintf("<script type=\"text/javascript\">\n"
406 " setTimeout('hide_imsg_popup()', 3000); \n"
408 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
410 if ( (WC->logged_in) && (!unset_cookies) ) {
411 wprintf("<div id=\"iconbar\">");
415 if (do_room_banner == 1) {
416 wprintf("<div id=\"banner\">\n");
417 embed_room_banner(NULL, navbar_default);
422 if (do_room_banner == 1) {
423 wprintf("<div id=\"content\">\n");
429 * Generic function to do an HTTP redirect. Easy and fun.
431 void http_redirect(char *whichpage) {
432 wprintf("HTTP/1.1 302 Moved Temporarily\n");
433 wprintf("Location: %s\r\n", whichpage);
434 wprintf("URI: %s\r\n", whichpage);
435 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
436 wprintf("<html><body>");
437 wprintf("Go <A HREF=\"%s\">here</A>.", whichpage);
438 wprintf("</body></html>\n");
443 void check_for_instant_messages()
448 serv_getln(buf, sizeof buf);
449 if (buf[3] == '*') WC->HaveInstantMessages = 1;
456 * Output a piece of content to the web browser
458 void http_transmit_thing(char *thing, size_t length, char *content_type,
461 output_headers(0, 0, 0, 0, 0, is_static);
463 wprintf("Content-type: %s\r\n"
465 "Connection: close\r\n",
470 /* If we can send the data out compressed, please do so. */
472 char *compressed_data = NULL;
473 uLongf compressed_len;
475 compressed_len = (uLongf) ((length * 101) / 100) + 100;
476 compressed_data = malloc(compressed_len);
478 if (compress_gzip((Bytef *) compressed_data,
481 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
482 wprintf("Content-encoding: gzip\r\n"
483 "Content-length: %ld\r\n"
485 (long) compressed_len
487 client_write(compressed_data, (size_t)compressed_len);
488 free(compressed_data);
494 /* No compression ... just send it out as-is */
495 wprintf("Content-length: %ld\r\n"
499 client_write(thing, (size_t)length);
505 void output_static(char *what)
511 char content_type[128];
513 fp = fopen(what, "rb");
515 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
516 wprintf("Content-Type: text/plain\r\n");
518 wprintf("Cannot open %s: %s\n", what, strerror(errno));
520 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
521 safestrncpy(content_type, "image/gif", sizeof content_type);
522 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
523 safestrncpy(content_type, "text/plain", sizeof content_type);
524 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
525 safestrncpy(content_type, "text/css", sizeof content_type);
526 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
527 safestrncpy(content_type, "image/jpeg", sizeof content_type);
528 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
529 safestrncpy(content_type, "image/png", sizeof content_type);
530 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
531 safestrncpy(content_type, "image/x-icon", sizeof content_type);
532 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
533 safestrncpy(content_type, "text/html", sizeof content_type);
534 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
535 safestrncpy(content_type, "text/html", sizeof content_type);
536 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
537 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
538 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
539 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
540 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
541 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
542 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
543 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
544 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
545 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
546 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
547 safestrncpy(content_type, "text/javascript", sizeof content_type);
549 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
551 fstat(fileno(fp), &statbuf);
552 bytes = statbuf.st_size;
553 bigbuffer = malloc(bytes + 2);
554 fread(bigbuffer, bytes, 1, fp);
557 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
560 if (!strcasecmp(bstr("force_close_session"), "yes")) {
561 end_webcit_session();
567 * When the browser requests an image file from the Citadel server,
568 * this function is called to transmit it.
573 char *xferbuf = NULL;
576 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
577 serv_getln(buf, sizeof buf);
579 bytes = extract_long(&buf[4], 0);
580 xferbuf = malloc(bytes + 2);
582 /* Read it from the server */
583 read_server_binary(xferbuf, bytes);
585 serv_getln(buf, sizeof buf);
587 /* Write it to the browser */
588 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
592 /* Instead of an ugly 404, send a 1x1 transparent GIF
593 * when there's no such image on the server.
595 output_static("static/blank.gif");
603 * Generic function to output an arbitrary MIME part from an arbitrary
604 * message number on the server.
606 void mimepart(char *msgnum, char *partnum)
610 char content_type[SIZ];
611 char *content = NULL;
613 serv_printf("OPNA %s|%s", msgnum, partnum);
614 serv_getln(buf, sizeof buf);
616 bytes = extract_long(&buf[4], 0);
617 content = malloc(bytes + 2);
618 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
619 output_headers(0, 0, 0, 0, 0, 0);
620 read_server_binary(content, bytes);
622 serv_getln(buf, sizeof buf);
623 http_transmit_thing(content, bytes, content_type, 0);
626 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
627 output_headers(0, 0, 0, 0, 0, 0);
628 wprintf("Content-Type: text/plain\r\n");
630 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
637 * Read any MIME part of a message, from the server, into memory.
639 char *load_mimepart(long msgnum, char *partnum)
643 char content_type[SIZ];
646 serv_printf("OPNA %ld|%s", msgnum, partnum);
647 serv_getln(buf, sizeof buf);
649 bytes = extract_long(&buf[4], 0);
650 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
652 content = malloc(bytes + 2);
653 read_server_binary(content, bytes);
656 serv_getln(buf, sizeof buf);
657 content[bytes] = 0; /* null terminate for good measure */
668 * Convenience functions to display a page containing only a string
670 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
672 wprintf("HTTP/1.1 200 OK\n");
673 output_headers(1, 1, 2, 0, 0, 0);
674 wprintf("<div id=\"banner\">\n");
675 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#%s\"><TR><TD>", titlebarcolor);
676 wprintf("<SPAN CLASS=\"titlebar\">%s</SPAN>\n", titlebarmsg);
677 wprintf("</TD></TR></TABLE>\n");
678 wprintf("</div>\n<div id=\"content\">\n");
679 escputs(messagetext);
687 * Display a blank page.
689 void blank_page(void) {
690 output_headers(1, 1, 0, 0, 0, 0);
696 * A template has been requested
698 void url_do_template(void) {
699 do_template(bstr("template"));
705 * Offer to make any page the user's "start page."
707 void offer_start_page(void) {
708 wprintf("<A HREF=\"/change_start_page?startpage=");
709 urlescputs(WC->this_page);
710 wprintf("\"><FONT SIZE=-2 COLOR=\"#AAAAAA\">");
711 wprintf(_("Make this my start page"));
712 wprintf("</FONT></A>");
714 wprintf("<br/><a href=\"/rss?room=");
715 urlescputs(WC->wc_roomname);
716 wprintf("\" title=\"RSS 2.0 feed for ");
717 escputs(WC->wc_roomname);
718 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"/static/xml_button.gif\"/></a>\n");
724 * Change the user's start page
726 void change_start_page(void) {
728 if (bstr("startpage") == NULL) {
729 safestrncpy(WC->ImportantMessage,
730 _("You no longer have a start page selected."),
731 sizeof WC->ImportantMessage);
736 set_preference("startpage", bstr("startpage"), 1);
738 output_headers(1, 1, 0, 0, 0, 0);
739 do_template("newstartpage");
746 void display_success(char *successmessage)
748 convenience_page("007700", "OK", successmessage);
752 /* Authorization required page */
753 /* This is probably temporary and should be revisited */
754 void authorization_required(const char *message)
756 wprintf("HTTP/1.1 401 Authorization Required\r\n");
757 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
758 wprintf("Content-Type: text/html\r\n\r\n");
760 wprintf(_("Authorization Required"));
761 wprintf("</h1>\r\n");
762 wprintf(_("The resource you requested requires a valid username and password. "
763 "You could not be logged in: %s\n"), message);
768 void upload_handler(char *name, char *filename, char *partnum, char *disp,
769 void *content, char *cbtype, char *cbcharset,
770 size_t length, char *encoding, void *userdata)
772 struct urlcontent *u;
774 lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
775 name, cbtype, length);
778 if ( (length > 0) && (strlen(cbtype) == 0) ) {
779 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
780 u->next = WC->urlstrings;
782 safestrncpy(u->url_key, name, sizeof(u->url_key));
783 u->url_data = malloc(length + 1);
784 memcpy(u->url_data, content, length);
785 u->url_data[length] = 0;
789 if ( (length > 0) && (strlen(cbtype) > 0) ) {
790 WC->upload = malloc(length);
791 if (WC->upload != NULL) {
792 WC->upload_length = length;
793 safestrncpy(WC->upload_filename, filename,
794 sizeof(WC->upload_filename));
795 safestrncpy(WC->upload_content_type, cbtype,
796 sizeof(WC->upload_content_type));
797 memcpy(WC->upload, content, length);
800 lprintf(3, "malloc() failed: %s\n", strerror(errno));
807 * Convenience functions to wrap around asynchronous ajax responses
809 void begin_ajax_response(void) {
810 output_headers(0, 0, 0, 0, 0, 0);
812 wprintf("Content-type: text/html; charset=UTF-8\r\n"
814 "Connection: close\r\n"
815 "Pragma: no-cache\r\n"
816 "Cache-Control: no-cache\r\n",
821 void end_ajax_response(void) {
829 * Entry point for WebCit transaction
831 void session_loop(struct httprequest *req)
841 char request_method[128];
844 int ContentLength = 0;
846 char ContentType[512];
847 char *content = NULL;
848 char *content_end = NULL;
849 struct httprequest *hptr;
850 char browser_host[SIZ];
851 char user_agent[SIZ];
855 /* We stuff these with the values coming from the client cookies,
856 * so we can use them to reconnect a timed out session if we have to.
858 char c_username[SIZ];
859 char c_password[SIZ];
860 char c_roomname[SIZ];
861 char c_httpauth_string[SIZ];
862 char c_httpauth_user[SIZ];
863 char c_httpauth_pass[SIZ];
866 safestrncpy(c_username, "", sizeof c_username);
867 safestrncpy(c_password, "", sizeof c_password);
868 safestrncpy(c_roomname, "", sizeof c_roomname);
869 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
870 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
871 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
873 WC->upload_length = 0;
880 if (hptr == NULL) return;
882 safestrncpy(cmd, hptr->line, sizeof cmd);
884 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
885 extract_token(pathname, cmd, 1, ' ', sizeof request_method);
887 /* Figure out the action */
888 extract_token(action, pathname, 1, '/', sizeof action);
889 if (strstr(action, "?")) *strstr(action, "?") = 0;
890 if (strstr(action, "&")) *strstr(action, "&") = 0;
891 if (strstr(action, " ")) *strstr(action, " ") = 0;
893 extract_token(arg1, pathname, 2, '/', sizeof arg1);
894 if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
895 if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
896 if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
898 extract_token(arg2, pathname, 3, '/', sizeof arg2);
899 if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
900 if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
901 if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
903 extract_token(arg3, pathname, 4, '/', sizeof arg3);
904 if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
905 if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
906 if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
908 extract_token(arg4, pathname, 4, '/', sizeof arg4);
909 if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
910 if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
911 if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
913 extract_token(arg5, pathname, 4, '/', sizeof arg5);
914 if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
915 if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
916 if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
918 while (hptr != NULL) {
919 safestrncpy(buf, hptr->line, sizeof buf);
922 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
923 safestrncpy(cookie, &buf[15], sizeof cookie);
924 cookie_to_stuff(cookie, NULL,
925 c_username, sizeof c_username,
926 c_password, sizeof c_password,
927 c_roomname, sizeof c_roomname);
929 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
930 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
931 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
932 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
934 else if (!strncasecmp(buf, "Content-length: ", 16)) {
935 ContentLength = atoi(&buf[16]);
937 else if (!strncasecmp(buf, "Content-type: ", 14)) {
938 safestrncpy(ContentType, &buf[14], sizeof ContentType);
940 else if (!strncasecmp(buf, "User-agent: ", 12)) {
941 safestrncpy(user_agent, &buf[12], sizeof user_agent);
943 else if (!strncasecmp(buf, "Host: ", 6)) {
944 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
946 /* Only WAP gateways explicitly name this content-type */
947 else if (strstr(buf, "text/vnd.wap.wml")) {
952 if (ContentLength > 0) {
953 content = malloc(ContentLength + SIZ);
954 memset(content, 0, ContentLength + SIZ);
955 sprintf(content, "Content-type: %s\n"
956 "Content-length: %d\n\n",
957 ContentType, ContentLength);
958 body_start = strlen(content);
960 /* Read the entire input data at once. */
961 client_read(WC->http_sock, &content[BytesRead+body_start],
964 if (!strncasecmp(ContentType,
965 "application/x-www-form-urlencoded", 33)) {
966 addurls(&content[body_start]);
967 } else if (!strncasecmp(ContentType, "multipart", 9)) {
968 content_end = content + ContentLength + body_start;
969 mime_parser(content, content_end, *upload_handler,
970 NULL, NULL, NULL, 0);
976 /* make a note of where we are in case the user wants to save it */
977 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
978 remove_token(WC->this_page, 2, ' ');
979 remove_token(WC->this_page, 0, ' ');
981 /* If there are variables in the URL, we must grab them now */
982 for (a = 0; a < strlen(cmd); ++a) {
983 if ((cmd[a] == '?') || (cmd[a] == '&')) {
984 for (b = a; b < strlen(cmd); ++b)
987 addurls(&cmd[a + 1]);
993 /* Static content can be sent without connecting to Citadel. */
995 for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
996 if (!strcasecmp(action, static_content_dirs[a])) {
1001 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s", action, arg1, arg2, arg3, arg4, arg5);
1002 for (a=0; a<4; ++a) {
1003 if (buf[strlen(buf)-1] == '/') {
1004 buf[strlen(buf)-1] = 0;
1007 for (a = 0; a < strlen(buf); ++a) {
1008 if (isspace(buf[a])) {
1013 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1017 * If we're not connected to a Citadel server, try to hook up the
1020 if (!WC->connected) {
1021 if (!strcasecmp(ctdlhost, "uds")) {
1022 /* unix domain socket */
1023 sprintf(buf, "%s/citadel.socket", ctdlport);
1024 WC->serv_sock = uds_connectsock(buf);
1028 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1031 if (WC->serv_sock < 0) {
1033 goto SKIP_ALL_THIS_CRAP;
1037 serv_getln(buf, sizeof buf); /* get the server welcome message */
1038 locate_host(browser_host, WC->http_sock);
1039 get_serv_info(browser_host, user_agent);
1040 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1041 wprintf(_("You are connected to a Citadel "
1042 "server running Citadel %d.%02d. \n"
1043 "In order to run this version of WebCit "
1044 "you must also have Citadel %d.%02d or"
1046 serv_info.serv_rev_level / 100,
1047 serv_info.serv_rev_level % 100,
1048 MINIMUM_CIT_VERSION / 100,
1049 MINIMUM_CIT_VERSION % 100
1051 end_webcit_session();
1052 goto SKIP_ALL_THIS_CRAP;
1058 * Functions which can be performed without logging in
1060 if (!strcasecmp(action, "listsub")) {
1062 goto SKIP_ALL_THIS_CRAP;
1064 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1065 if (!strcasecmp(action, "freebusy")) {
1067 goto SKIP_ALL_THIS_CRAP;
1072 * If we're not logged in, but we have HTTP Authentication data,
1073 * try logging in to Citadel using that.
1075 if ((!WC->logged_in)
1076 && (strlen(c_httpauth_user) > 0)
1077 && (strlen(c_httpauth_pass) > 0)) {
1078 serv_printf("USER %s", c_httpauth_user);
1079 serv_getln(buf, sizeof buf);
1080 if (buf[0] == '3') {
1081 serv_printf("PASS %s", c_httpauth_pass);
1082 serv_getln(buf, sizeof buf);
1083 if (buf[0] == '2') {
1084 become_logged_in(c_httpauth_user,
1085 c_httpauth_pass, buf);
1086 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1087 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1089 /* Should only display when password is wrong */
1090 authorization_required(&buf[4]);
1091 goto SKIP_ALL_THIS_CRAP;
1096 /* This needs to run early */
1097 if (!strcasecmp(action, "rss")) {
1098 display_rss(bstr("room"), request_method);
1099 goto SKIP_ALL_THIS_CRAP;
1103 * The GroupDAV stuff relies on HTTP authentication instead of
1104 * our session's authentication.
1106 if (!strncasecmp(action, "groupdav", 8)) {
1107 groupdav_main(req, ContentType, /* do GroupDAV methods */
1108 ContentLength, content+body_start);
1109 if (!WC->logged_in) {
1110 WC->killthis = 1; /* If not logged in, don't */
1111 } /* keep the session active */
1112 goto SKIP_ALL_THIS_CRAP;
1117 * Automatically send requests with any method other than GET or
1118 * POST to the GroupDAV code as well.
1120 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1121 groupdav_main(req, ContentType, /* do GroupDAV methods */
1122 ContentLength, content+body_start);
1123 if (!WC->logged_in) {
1124 WC->killthis = 1; /* If not logged in, don't */
1125 } /* keep the session active */
1126 goto SKIP_ALL_THIS_CRAP;
1130 * If we're not logged in, but we have username and password cookies
1131 * supplied by the browser, try using them to log in.
1133 if ((!WC->logged_in)
1134 && (strlen(c_username) > 0)
1135 && (strlen(c_password) > 0)) {
1136 serv_printf("USER %s", c_username);
1137 serv_getln(buf, sizeof buf);
1138 if (buf[0] == '3') {
1139 serv_printf("PASS %s", c_password);
1140 serv_getln(buf, sizeof buf);
1141 if (buf[0] == '2') {
1142 become_logged_in(c_username, c_password, buf);
1147 * If we don't have a current room, but a cookie specifying the
1148 * current room is supplied, make an effort to go there.
1150 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1151 serv_printf("GOTO %s", c_roomname);
1152 serv_getln(buf, sizeof buf);
1153 if (buf[0] == '2') {
1154 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1159 * If there are instant messages waiting, retrieve them for display.
1161 check_for_instant_messages();
1163 if (!strcasecmp(action, "image")) {
1167 * All functions handled below this point ... make sure we log in
1168 * before doing anything else!
1170 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1172 } else if (!WC->logged_in) {
1173 display_login(NULL);
1177 * Various commands...
1180 else if (!strcasecmp(action, "do_welcome")) {
1182 } else if (!strcasecmp(action, "blank")) {
1184 } else if (!strcasecmp(action, "do_template")) {
1186 } else if (!strcasecmp(action, "display_aide_menu")) {
1187 display_aide_menu();
1188 } else if (!strcasecmp(action, "display_main_menu")) {
1189 display_main_menu();
1190 } else if (!strcasecmp(action, "who")) {
1192 } else if (!strcasecmp(action, "who_inner_html")) {
1193 begin_ajax_response();
1195 end_ajax_response();
1196 } else if (!strcasecmp(action, "knrooms")) {
1198 } else if (!strcasecmp(action, "gotonext")) {
1201 } else if (!strcasecmp(action, "skip")) {
1203 } else if (!strcasecmp(action, "ungoto")) {
1205 } else if (!strcasecmp(action, "dotgoto")) {
1206 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1209 smart_goto(bstr("room"));
1210 } else if (!strcasecmp(action, "dotskip")) {
1211 smart_goto(bstr("room"));
1212 } else if (!strcasecmp(action, "termquit")) {
1214 } else if (!strcasecmp(action, "readnew")) {
1215 readloop("readnew");
1216 } else if (!strcasecmp(action, "readold")) {
1217 readloop("readold");
1218 } else if (!strcasecmp(action, "readfwd")) {
1219 readloop("readfwd");
1220 } else if (!strcasecmp(action, "headers")) {
1221 readloop("headers");
1222 } else if (!strcasecmp(action, "msg")) {
1224 } else if (!strcasecmp(action, "printmsg")) {
1226 } else if (!strcasecmp(action, "display_enter")) {
1228 } else if (!strcasecmp(action, "post")) {
1230 } else if (!strcasecmp(action, "move_msg")) {
1232 } else if (!strcasecmp(action, "delete_msg")) {
1234 } else if (!strcasecmp(action, "userlist")) {
1236 } else if (!strcasecmp(action, "showuser")) {
1238 } else if (!strcasecmp(action, "display_page")) {
1240 } else if (!strcasecmp(action, "page_user")) {
1242 } else if (!strcasecmp(action, "chat")) {
1244 } else if (!strcasecmp(action, "display_private")) {
1245 display_private("", 0);
1246 } else if (!strcasecmp(action, "goto_private")) {
1248 } else if (!strcasecmp(action, "zapped_list")) {
1250 } else if (!strcasecmp(action, "display_zap")) {
1252 } else if (!strcasecmp(action, "zap")) {
1254 } else if (!strcasecmp(action, "display_entroom")) {
1256 } else if (!strcasecmp(action, "entroom")) {
1258 } else if (!strcasecmp(action, "display_whok")) {
1260 } else if (!strcasecmp(action, "do_invt_kick")) {
1262 } else if (!strcasecmp(action, "display_editroom")) {
1264 } else if (!strcasecmp(action, "netedit")) {
1266 } else if (!strcasecmp(action, "editroom")) {
1268 } else if (!strcasecmp(action, "display_editinfo")) {
1269 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1270 } else if (!strcasecmp(action, "editinfo")) {
1271 save_edit(_("Room info"), "EINF 1", 1);
1272 } else if (!strcasecmp(action, "display_editbio")) {
1273 sprintf(buf, "RBIO %s", WC->wc_username);
1274 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1275 } else if (!strcasecmp(action, "editbio")) {
1276 save_edit(_("Your bio"), "EBIO", 0);
1277 } else if (!strcasecmp(action, "confirm_move_msg")) {
1279 } else if (!strcasecmp(action, "delete_room")) {
1281 } else if (!strcasecmp(action, "validate")) {
1283 } else if (!strcasecmp(action, "display_editpic")) {
1284 display_graphics_upload(_("your photo"),
1287 } else if (!strcasecmp(action, "editpic")) {
1288 do_graphics_upload("UIMG 1|_userpic_");
1289 } else if (!strcasecmp(action, "display_editroompic")) {
1290 display_graphics_upload(_("the icon for this room"),
1293 } else if (!strcasecmp(action, "editroompic")) {
1294 do_graphics_upload("UIMG 1|_roompic_");
1295 } else if (!strcasecmp(action, "delete_floor")) {
1297 } else if (!strcasecmp(action, "rename_floor")) {
1299 } else if (!strcasecmp(action, "create_floor")) {
1301 } else if (!strcasecmp(action, "display_editfloorpic")) {
1302 sprintf(buf, "UIMG 0|_floorpic_|%s",
1303 bstr("which_floor"));
1304 display_graphics_upload(_("the icon for this floor"),
1307 } else if (!strcasecmp(action, "editfloorpic")) {
1308 sprintf(buf, "UIMG 1|_floorpic_|%s",
1309 bstr("which_floor"));
1310 do_graphics_upload(buf);
1311 } else if (!strcasecmp(action, "display_reg")) {
1313 } else if (!strcasecmp(action, "display_changepw")) {
1315 } else if (!strcasecmp(action, "changepw")) {
1317 } else if (!strcasecmp(action, "display_edit_node")) {
1318 display_edit_node();
1319 } else if (!strcasecmp(action, "edit_node")) {
1321 } else if (!strcasecmp(action, "display_netconf")) {
1323 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1324 display_confirm_delete_node();
1325 } else if (!strcasecmp(action, "delete_node")) {
1327 } else if (!strcasecmp(action, "display_add_node")) {
1329 } else if (!strcasecmp(action, "add_node")) {
1331 } else if (!strcasecmp(action, "terminate_session")) {
1333 terminate_session();
1334 } else if (!strcasecmp(action, "edit_me")) {
1336 } else if (!strcasecmp(action, "display_siteconfig")) {
1337 display_siteconfig();
1338 } else if (!strcasecmp(action, "chat_recv")) {
1340 } else if (!strcasecmp(action, "chat_send")) {
1342 } else if (!strcasecmp(action, "siteconfig")) {
1344 } else if (!strcasecmp(action, "display_generic")) {
1346 } else if (!strcasecmp(action, "do_generic")) {
1348 } else if (!strcasecmp(action, "display_menubar")) {
1350 } else if (!strcasecmp(action, "mimepart")) {
1351 mimepart(arg1, arg2);
1352 } else if (!strcasecmp(action, "edit_vcard")) {
1354 } else if (!strcasecmp(action, "submit_vcard")) {
1356 } else if (!strcasecmp(action, "select_user_to_edit")) {
1357 select_user_to_edit(NULL, NULL);
1358 } else if (!strcasecmp(action, "display_edituser")) {
1359 display_edituser(NULL, 0);
1360 } else if (!strcasecmp(action, "edituser")) {
1362 } else if (!strcasecmp(action, "create_user")) {
1364 } else if (!strcasecmp(action, "changeview")) {
1366 } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
1368 } else if (!strcasecmp(action, "change_start_page")) {
1369 change_start_page();
1370 } else if (!strcasecmp(action, "display_floorconfig")) {
1371 display_floorconfig(NULL);
1372 } else if (!strcasecmp(action, "toggle_self_service")) {
1373 toggle_self_service();
1374 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1375 } else if (!strcasecmp(action, "display_edit_task")) {
1376 display_edit_task();
1377 } else if (!strcasecmp(action, "save_task")) {
1379 } else if (!strcasecmp(action, "display_edit_event")) {
1380 display_edit_event();
1381 } else if (!strcasecmp(action, "save_event")) {
1383 } else if (!strcasecmp(action, "respond_to_request")) {
1384 respond_to_request();
1385 } else if (!strcasecmp(action, "handle_rsvp")) {
1388 } else if (!strcasecmp(action, "summary")) {
1390 } else if (!strcasecmp(action, "summary_inner_div")) {
1391 begin_ajax_response();
1392 summary_inner_div();
1393 end_ajax_response();
1394 } else if (!strcasecmp(action, "iconbar")) {
1396 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1397 display_customize_iconbar();
1398 } else if (!strcasecmp(action, "commit_iconbar")) {
1400 } else if (!strcasecmp(action, "set_room_policy")) {
1402 } else if (!strcasecmp(action, "display_inetconf")) {
1404 } else if (!strcasecmp(action, "save_inetconf")) {
1406 } else if (!strcasecmp(action, "setup_wizard")) {
1408 } else if (!strcasecmp(action, "display_preferences")) {
1409 display_preferences();
1410 } else if (!strcasecmp(action, "set_preferences")) {
1412 } else if (!strcasecmp(action, "recp_autocomplete")) {
1413 recp_autocomplete(bstr("recp"));
1414 } else if (!strcasecmp(action, "cc_autocomplete")) {
1415 recp_autocomplete(bstr("cc"));
1416 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1417 recp_autocomplete(bstr("bcc"));
1418 } else if (!strcasecmp(action, "diagnostics")) {
1419 output_headers(1, 1, 1, 0, 0, 0);
1420 wprintf("Session: %d<hr />\n", WC->wc_session);
1421 wprintf("Command: <br /><PRE>\n");
1423 wprintf("</PRE><hr />\n");
1424 wprintf("Variables: <br /><PRE>\n");
1426 wprintf("</PRE><hr />\n");
1430 /* When all else fais, display the main menu. */
1432 display_main_menu();
1437 if (content != NULL) {
1442 if (WC->upload_length > 0) {
1444 WC->upload_length = 0;