5 * \defgroup MainServer This is the main transaction loop of the web service. It maintains a
6 * persistent session to the Citadel server, handling HTTP WebCit requests as
7 * they arrive and presenting a user interface.
8 * \ingroup WebcitHttpServer
13 #include "webserver.h"
14 #include "mime_parser.h"
17 * String to unset the cookie.
18 * Any date "in the past" will work, so I chose my birthday, right down to
19 * the exact minute. :)
21 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
24 * \brief remove escaped strings from i.e. the url string (like %20 for blanks)
25 * \param buf the buffer to examine
27 void unescape_input(char *buf)
32 while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
33 buf[strlen(buf) - 1] = 0;
35 for (a = 0; a < strlen(buf); ++a) {
43 sscanf(hex, "%02x", &b);
45 strcpy(&buf[a + 1], &buf[a + 3]);
52 * \brief Extract variables from the URL.
53 * \param url URL supplied by the HTTP parser
55 void addurls(char *url)
63 while (strlen(up) > 0) {
65 /** locate the = sign */
66 safestrncpy(buf, up, sizeof buf);
68 for (a = 255; a >= 0; --a)
75 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
76 u->next = WC->urlstrings;
78 safestrncpy(u->url_key, buf, sizeof u->url_key);
80 /** now chop that part off */
81 for (a = 0; a <= b; ++a)
84 /** locate "&" and "?" delimiters */
87 for (a = 0; a < strlen(up); ++a) {
88 if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
95 for (a = 0; a < b; ++a)
99 u->url_data = malloc(strlen(up) + 2);
100 safestrncpy(u->url_data, up, strlen(up) + 1);
102 unescape_input(u->url_data);
106 /* lprintf(9, "%s = %s\n", u->url_key, u->url_data); */
111 * \brief free urlstring memory
115 struct urlcontent *u;
117 while (WC->urlstrings != NULL) {
118 free(WC->urlstrings->url_data);
119 u = WC->urlstrings->next;
120 free(WC->urlstrings);
126 * \brief Diagnostic function to display the contents of all variables
130 struct urlcontent *u;
132 for (u = WC->urlstrings; u != NULL; u = u->next) {
133 wprintf("%38s = %s\n", u->url_key, u->url_data);
138 * \brief Return the value of a variable supplied to the current web page (from the url or a form)
139 * \param key The name of the variable we want
141 char *bstr(char *key)
143 struct urlcontent *u;
145 for (u = WC->urlstrings; u != NULL; u = u->next) {
146 if (!strcasecmp(u->url_key, key))
147 return (u->url_data);
153 * \brief web-printing funcion. uses our vsnprintf wrapper
154 * \param format printf format string
155 * \param ... the varargs to put into formatstring
157 void wprintf(const char *format,...)
162 va_start(arg_ptr, format);
163 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
166 client_write(wbuf, strlen(wbuf));
171 * \brief wrap up an HTTP session, closes tags, etc.
172 * \todo multiline params?
173 * \param print_standard_html_footer should be set to 0 to transmit only, 1 to
174 * append the main menu and closing tags, or 2 to
175 * append the closing tags only.
177 void wDumpContent(int print_standard_html_footer)
179 if (print_standard_html_footer) {
180 wprintf("</div>\n"); /* end of "text" div */
181 do_template("trailing");
184 /* If we've been saving it all up for one big output burst,
185 * go ahead and do that now.
192 * \brief Copy a string, escaping characters which have meaning in HTML.
193 * \param target target buffer
194 * \param strbuf source buffer
195 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
196 * \param nolinebreaks if set, linebreaks are removed from the string.
198 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
203 for (a = 0; a < strlen(strbuf); ++a) {
204 if (strbuf[a] == '<')
205 strcat(target, "<");
206 else if (strbuf[a] == '>')
207 strcat(target, ">");
208 else if (strbuf[a] == '&')
209 strcat(target, "&");
210 else if (strbuf[a] == '\"')
211 strcat(target, """);
212 else if (strbuf[a] == '\'')
213 strcat(target, "'");
214 else if (strbuf[a] == LB)
216 else if (strbuf[a] == RB)
218 else if (strbuf[a] == QU)
219 strcat(target, "\"");
220 else if ((strbuf[a] == 32) && (nbsp == 1))
221 strcat(target, " ");
222 else if ((strbuf[a] == '\n') && (nolinebreaks))
223 strcat(target, ""); /* nothing */
224 else if ((strbuf[a] == '\r') && (nolinebreaks))
225 strcat(target, ""); /* nothing */
227 strncat(target, &strbuf[a], 1);
233 * \param strbuf what???
234 * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
235 * \param nolinebreaks if set, linebreaks are removed from the string.
237 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
241 if (strbuf == NULL) return;
242 buf = malloc( (3 * strlen(strbuf)) + SIZ );
243 stresc(buf, strbuf, nbsp, nolinebreaks);
249 * \brief static wrapper for ecsputs1
250 * \param strbuf buffer to print escaped to client
252 void escputs(char *strbuf)
254 escputs1(strbuf, 0, 0);
258 * \brief Escape a string for feeding out as a URL.
259 * Returns a pointer to a buffer that must be freed by the caller!
260 * \param outbuf the output buffer
261 * \param strbuf the input buffer
263 void urlesc(char *outbuf, char *strbuf)
266 char *ec = " #&;`'|*?-~<>^()[]{}$\"\\";
270 for (a = 0; a < strlen(strbuf); ++a) {
272 for (b = 0; b < strlen(ec); ++b) {
273 if (strbuf[a] == ec[b])
278 sprintf(&outbuf[b], "%%%02x", strbuf[a]);
280 sprintf(&outbuf[b], "%c", strbuf[a]);
285 * \brief urlescape buffer and print it to the client
286 * \param strbuf buffer to urlescape
288 void urlescputs(char *strbuf)
292 urlesc(outbuf, strbuf);
293 wprintf("%s", outbuf);
298 * \brief Copy a string, escaping characters for JavaScript strings.
299 * \param target output string
300 * \param strbuf input string
302 void jsesc(char *target, char *strbuf)
307 for (a = 0; a < strlen(strbuf); ++a) {
308 if (strbuf[a] == '<')
310 else if (strbuf[a] == '>')
312 else if (strbuf[a] == '\"')
313 strcat(target, """);
314 else if (strbuf[a] == '&')
315 strcat(target, "&;");
316 else if (strbuf[a] == '\'')
317 strcat(target, "\\'");
319 strncat(target, &strbuf[a], 1);
325 * \brief escape and print java script
326 * \param strbuf the js code
328 void jsescputs(char *strbuf)
332 jsesc(outbuf, strbuf);
333 wprintf("%s", outbuf);
337 * \brief Copy a string, escaping characters for message text hold
338 * \param target target buffer
339 * \param strbuf source buffer
341 void msgesc(char *target, char *strbuf)
346 for (a = 0; a < strlen(strbuf); ++a) {
347 if (strbuf[a] == '\n')
349 else if (strbuf[a] == '\r')
351 else if (strbuf[a] == '\'')
352 strcat(target, "'");
354 strncat(target, &strbuf[a], 1);
360 * \brief print a string to the client after cleaning it with msgesc()
361 * \param strbuf string to be printed
363 void msgescputs(char *strbuf) {
366 if (strbuf == NULL) return;
367 outbuf = malloc( (3 * strlen(strbuf)) + SIZ);
368 msgesc(outbuf, strbuf);
369 wprintf("%s", outbuf);
377 * \brief Output all that important stuff that the browser will want to see
379 void output_headers( int do_httpheaders, /**< 1 = output HTTP headers */
380 int do_htmlhead, /**< 1 = output HTML <head> section and <body> opener */
382 int do_room_banner, /**< 0=no, 1=yes,
383 * 2 = I'm going to embed my own, so don't open the
384 * <div id="content"> either.
387 int unset_cookies, /**< 1 = session is terminating, so unset the cookies */
388 int suppress_check, /**< 1 = suppress check for instant messages */
389 int cache /**< 1 = allow browser to cache this page */
394 wprintf("HTTP/1.1 200 OK\n");
395 http_datestring(httpnow, sizeof httpnow, time(NULL));
397 if (do_httpheaders) {
398 wprintf("Content-type: text/html; charset=utf-8\r\n"
400 "Connection: close\r\n",
401 SERVER, serv_info.serv_software
406 wprintf("Pragma: public\r\n"
407 "Cache-Control: max-age=3600, must-revalidate\r\n"
408 "Last-modified: %s\r\n",
413 wprintf("Pragma: no-cache\r\n"
414 "Cache-Control: no-store\r\n"
418 stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
419 WC->wc_password, WC->wc_roomname);
422 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
424 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
425 if (server_cookie != NULL) {
426 wprintf("%s\n", server_cookie);
432 if (!access("static.local/webcit.css", R_OK)) {
433 svprintf("CSSLOCAL", WCS_STRING,
434 "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
444 /** check for ImportantMessages (these display in a div overlaying the main screen) */
445 if (strlen(WC->ImportantMessage) > 0) {
446 wprintf("<div id=\"important_message\">\n");
447 wprintf("<span class=\"imsg\">"
448 "%s</span><br />\n", WC->ImportantMessage);
450 wprintf("<script type=\"text/javascript\">\n"
451 " setTimeout('hide_imsg_popup()', 3000); \n"
453 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
456 if ( (WC->logged_in) && (!unset_cookies) ) {
457 wprintf("<div id=\"iconbar\">");
458 do_selected_iconbar();
459 /** check for instant messages (these display in a new window) */
464 if (do_room_banner == 1) {
465 wprintf("<div id=\"banner\">\n");
466 embed_room_banner(NULL, navbar_default);
471 if (do_room_banner == 1) {
472 wprintf("<div id=\"content\">\n");
478 * \brief Generic function to do an HTTP redirect. Easy and fun.
479 * \param whichpage target url to 302 to
481 void http_redirect(char *whichpage) {
482 wprintf("HTTP/1.1 302 Moved Temporarily\n");
483 wprintf("Location: %s\r\n", whichpage);
484 wprintf("URI: %s\r\n", whichpage);
485 wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
486 wprintf("<html><body>");
487 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
488 wprintf("</body></html>\n");
494 * \brief Output a piece of content to the web browser
496 void http_transmit_thing(char *thing, size_t length, char *content_type,
499 output_headers(0, 0, 0, 0, 0, is_static);
501 wprintf("Content-type: %s\r\n"
503 "Connection: close\r\n",
508 /** If we can send the data out compressed, please do so. */
510 char *compressed_data = NULL;
511 uLongf compressed_len;
513 compressed_len = (uLongf) ((length * 101) / 100) + 100;
514 compressed_data = malloc(compressed_len);
516 if (compress_gzip((Bytef *) compressed_data,
519 (uLongf) length, Z_BEST_SPEED) == Z_OK) {
520 wprintf("Content-encoding: gzip\r\n"
521 "Content-length: %ld\r\n"
523 (long) compressed_len
525 client_write(compressed_data, (size_t)compressed_len);
526 free(compressed_data);
532 /** No compression ... just send it out as-is */
533 wprintf("Content-length: %ld\r\n"
537 client_write(thing, (size_t)length);
543 * \brief dump out static pages from disk
544 * \param what the file urs to print
546 void output_static(char *what)
552 char content_type[128];
554 fp = fopen(what, "rb");
556 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
557 wprintf("HTTP/1.1 404 %s\n", strerror(errno));
558 wprintf("Content-Type: text/plain\r\n");
560 wprintf("Cannot open %s: %s\n", what, strerror(errno));
562 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
563 safestrncpy(content_type, "image/gif", sizeof content_type);
564 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
565 safestrncpy(content_type, "text/plain", sizeof content_type);
566 else if (!strncasecmp(&what[strlen(what) - 4], ".css", 4))
567 safestrncpy(content_type, "text/css", sizeof content_type);
568 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
569 safestrncpy(content_type, "image/jpeg", sizeof content_type);
570 else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
571 safestrncpy(content_type, "image/png", sizeof content_type);
572 else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
573 safestrncpy(content_type, "image/x-icon", sizeof content_type);
574 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
575 safestrncpy(content_type, "text/html", sizeof content_type);
576 else if (!strncasecmp(&what[strlen(what) - 4], ".htm", 4))
577 safestrncpy(content_type, "text/html", sizeof content_type);
578 else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
579 safestrncpy(content_type, "text/vnd.wap.wml", sizeof content_type);
580 else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
581 safestrncpy(content_type, "text/vnd.wap.wmlscript", sizeof content_type);
582 else if (!strncasecmp(&what[strlen(what) - 5], ".wmlc", 5))
583 safestrncpy(content_type, "application/vnd.wap.wmlc", sizeof content_type);
584 else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
585 safestrncpy(content_type, "application/vnd.wap.wmlscriptc", sizeof content_type);
586 else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
587 safestrncpy(content_type, "image/vnd.wap.wbmp", sizeof content_type);
588 else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
589 safestrncpy(content_type, "text/javascript", sizeof content_type);
591 safestrncpy(content_type, "application/octet-stream", sizeof content_type);
593 fstat(fileno(fp), &statbuf);
594 bytes = statbuf.st_size;
595 bigbuffer = malloc(bytes + 2);
596 fread(bigbuffer, bytes, 1, fp);
599 lprintf(9, "output_static('%s') %s\n", what, content_type);
600 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
603 if (!strcasecmp(bstr("force_close_session"), "yes")) {
604 end_webcit_session();
610 * \brief When the browser requests an image file from the Citadel server,
611 * this function is called to transmit it.
616 char *xferbuf = NULL;
619 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
620 serv_getln(buf, sizeof buf);
622 bytes = extract_long(&buf[4], 0);
623 xferbuf = malloc(bytes + 2);
625 /** Read it from the server */
626 read_server_binary(xferbuf, bytes);
628 serv_getln(buf, sizeof buf);
630 /** Write it to the browser */
631 http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
636 * Instead of an ugly 404, send a 1x1 transparent GIF
637 * when there's no such image on the server.
639 output_static("static/blank.gif");
647 * \brief Generic function to output an arbitrary MIME part from an arbitrary
648 * message number on the server.
650 * \param msgnum Number of the item on the citadel server
651 * \param partnum The MIME part to be output
652 * \param force_download Nonzero to force set the Content-Type: header
653 * to "application/octet-stream"
655 void mimepart(char *msgnum, char *partnum, int force_download)
659 char content_type[256];
660 char *content = NULL;
662 serv_printf("OPNA %s|%s", msgnum, partnum);
663 serv_getln(buf, sizeof buf);
665 bytes = extract_long(&buf[4], 0);
666 content = malloc(bytes + 2);
667 if (force_download) {
668 strcpy(content_type, "application/octet-stream");
671 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
673 output_headers(0, 0, 0, 0, 0, 0);
674 read_server_binary(content, bytes);
676 serv_getln(buf, sizeof buf);
677 http_transmit_thing(content, bytes, content_type, 0);
680 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
681 output_headers(0, 0, 0, 0, 0, 0);
682 wprintf("Content-Type: text/plain\r\n");
684 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
691 * \brief Read any MIME part of a message, from the server, into memory.
692 * \param msgnum number of the message on the citadel server
693 * \param partnum the MIME part to be loaded
695 char *load_mimepart(long msgnum, char *partnum)
699 char content_type[SIZ];
702 serv_printf("DLAT %ld|%s", msgnum, partnum);
703 serv_getln(buf, sizeof buf);
705 bytes = extract_long(&buf[4], 0);
706 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
708 content = malloc(bytes + 2);
709 serv_read(content, bytes);
711 content[bytes] = 0; /* null terminate for good measure */
722 * \brief Convenience functions to display a page containing only a string
723 * \param titlebarcolor color of the titlebar of the frame
724 * \param titlebarmsg text to display in the title bar
725 * \param messagetext body of the box
727 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
729 wprintf("HTTP/1.1 200 OK\n");
730 output_headers(1, 1, 2, 0, 0, 0);
731 wprintf("<div id=\"banner\">\n");
732 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
733 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
734 wprintf("</td></tr></table>\n");
735 wprintf("</div>\n<div id=\"content\">\n");
736 escputs(messagetext);
744 * \brief Display a blank page.
746 void blank_page(void) {
747 output_headers(1, 1, 0, 0, 0, 0);
753 * \brief A template has been requested
755 void url_do_template(void) {
756 do_template(bstr("template"));
762 * \brief Offer to make any page the user's "start page."
764 void offer_start_page(void) {
765 wprintf("<a href=\"change_start_page?startpage=");
766 urlescputs(WC->this_page);
767 wprintf("\"><font size=-2 color=\"#AAAAAA\">");
768 wprintf(_("Make this my start page"));
769 wprintf("</font></a>");
771 wprintf("<br/><a href=\"rss?room=");
772 urlescputs(WC->wc_roomname);
773 wprintf("\" title=\"RSS 2.0 feed for ");
774 escputs(WC->wc_roomname);
775 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
781 * \brief Change the user's start page
783 void change_start_page(void) {
785 if (bstr("startpage") == NULL) {
786 safestrncpy(WC->ImportantMessage,
787 _("You no longer have a start page selected."),
788 sizeof WC->ImportantMessage);
793 set_preference("startpage", bstr("startpage"), 1);
795 output_headers(1, 1, 0, 0, 0, 0);
796 do_template("newstartpage");
803 * \brief convenience function to indicate success
804 * \param successmessage the mesage itself
806 void display_success(char *successmessage)
808 convenience_page("007700", "OK", successmessage);
813 * \brief Authorization required page
814 * This is probably temporary and should be revisited
815 * \param message message to put in header
817 void authorization_required(const char *message)
819 wprintf("HTTP/1.1 401 Authorization Required\r\n");
820 wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
821 wprintf("Content-Type: text/html\r\n\r\n");
823 wprintf(_("Authorization Required"));
824 wprintf("</h1>\r\n");
825 wprintf(_("The resource you requested requires a valid username and password. "
826 "You could not be logged in: %s\n"), message);
831 * \brief This function is called by the MIME parser to handle data uploaded by
832 * the browser. Form data, uploaded files, and the data from HTTP PUT
833 * operations (such as those found in GroupDAV) all arrive this way.
835 * \param name Name of the item being uploaded
836 * \param filename Filename of the item being uploaded
837 * \param partnum MIME part identifier (not needed)
838 * \param disp MIME content disposition (not needed)
839 * \param content The actual data
840 * \param cbtype MIME content-type
841 * \param cbcharset Character set
842 * \param length Content length
843 * \param encoding MIME encoding type (not needed)
844 * \param userdata Not used here
846 void upload_handler(char *name, char *filename, char *partnum, char *disp,
847 void *content, char *cbtype, char *cbcharset,
848 size_t length, char *encoding, void *userdata)
850 struct urlcontent *u;
852 /* lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n",
853 name, cbtype, length); */
856 if ( (length > 0) && (strlen(cbtype) == 0) ) {
857 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
858 u->next = WC->urlstrings;
860 safestrncpy(u->url_key, name, sizeof(u->url_key));
861 u->url_data = malloc(length + 1);
862 memcpy(u->url_data, content, length);
863 u->url_data[length] = 0;
866 /** Uploaded files */
867 if ( (length > 0) && (strlen(cbtype) > 0) ) {
868 WC->upload = malloc(length);
869 if (WC->upload != NULL) {
870 WC->upload_length = length;
871 safestrncpy(WC->upload_filename, filename,
872 sizeof(WC->upload_filename));
873 safestrncpy(WC->upload_content_type, cbtype,
874 sizeof(WC->upload_content_type));
875 memcpy(WC->upload, content, length);
878 lprintf(3, "malloc() failed: %s\n", strerror(errno));
885 * \brief Convenience functions to wrap around asynchronous ajax responses
887 void begin_ajax_response(void) {
888 output_headers(0, 0, 0, 0, 0, 0);
890 wprintf("Content-type: text/html; charset=UTF-8\r\n"
892 "Connection: close\r\n"
893 "Pragma: no-cache\r\n"
894 "Cache-Control: no-cache\r\n",
900 * \brief print ajax response footer
902 void end_ajax_response(void) {
908 * \brief Wraps a Citadel server command in an AJAX transaction.
910 void ajax_servcmd(void)
917 begin_ajax_response();
919 serv_printf("%s", bstr("g_cmd"));
920 serv_getln(buf, sizeof buf);
921 wprintf("%s\n", buf);
924 serv_printf("\n\n000");
926 if ((buf[0] == '1') || (buf[0] == '8')) {
927 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
928 wprintf("%s\n", gcontent);
933 text_to_server(bstr("g_input"));
939 serv_read(junk, len);
945 memset(junk, 0, len);
946 serv_write(junk, len);
953 * This is kind of an ugly hack, but this is the only place it can go.
954 * If the command was GEXP, then the instant messenger window must be
955 * running, so reset the "last_pager_check" watchdog timer so
956 * that page_popup() doesn't try to open it a second time.
958 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
959 WC->last_pager_check = time(NULL);
965 * \brief Helper function for the asynchronous check to see if we need
966 * to open the instant messenger window.
968 void seconds_since_last_gexp(void)
972 begin_ajax_response();
973 if ( (time(NULL) - WC->last_pager_check) < 30) {
978 serv_getln(buf, sizeof buf);
993 * \brief Entry point for WebCit transaction
995 void session_loop(struct httprequest *req)
1003 char request_method[128];
1004 char pathname[1024];
1005 int a, b, nBackDots, nEmpty;
1006 int ContentLength = 0;
1008 char ContentType[512];
1009 char *content = NULL;
1010 char *content_end = NULL;
1011 struct httprequest *hptr;
1012 char browser_host[256];
1013 char user_agent[256];
1018 * We stuff these with the values coming from the client cookies,
1019 * so we can use them to reconnect a timed out session if we have to.
1021 char c_username[SIZ];
1022 char c_password[SIZ];
1023 char c_roomname[SIZ];
1024 char c_httpauth_string[SIZ];
1025 char c_httpauth_user[SIZ];
1026 char c_httpauth_pass[SIZ];
1029 safestrncpy(c_username, "", sizeof c_username);
1030 safestrncpy(c_password, "", sizeof c_password);
1031 safestrncpy(c_roomname, "", sizeof c_roomname);
1032 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1033 safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1034 safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1035 strcpy(browser_host, "");
1037 WC->upload_length = 0;
1043 if (hptr == NULL) return;
1045 safestrncpy(cmd, hptr->line, sizeof cmd);
1047 extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1048 extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1050 /** Figure out the action */
1052 sizes[0] = sizeof action;
1055 index[a] = arg[a-1];
1056 sizes[a] = sizeof arg[a-1];
1058 //// index[9] = &foo; todo
1061 for ( a = 0; a < 9; ++a)
1063 extract_token(index[a], pathname, a + 1, '/', sizes[a]);
1064 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
1065 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
1066 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
1067 if ((index[a][0] == '.') && (index[a][1] == '.'))
1069 if (index[a][0] == '\0')
1073 while (hptr != NULL) {
1074 safestrncpy(buf, hptr->line, sizeof buf);
1075 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1078 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1079 safestrncpy(cookie, &buf[15], sizeof cookie);
1080 cookie_to_stuff(cookie, NULL,
1081 c_username, sizeof c_username,
1082 c_password, sizeof c_password,
1083 c_roomname, sizeof c_roomname);
1085 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1086 CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1087 extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1088 extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1090 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1091 ContentLength = atoi(&buf[16]);
1093 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1094 safestrncpy(ContentType, &buf[14], sizeof ContentType);
1096 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1097 safestrncpy(user_agent, &buf[12], sizeof user_agent);
1099 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1101 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1104 else if (!strncasecmp(buf, "Host: ", 6)) {
1105 if (strlen(WC->http_host) == 0) {
1106 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1109 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1110 safestrncpy(browser_host, &buf[17], sizeof browser_host);
1111 while (num_tokens(browser_host, ',') > 1) {
1112 remove_token(browser_host, 0, ',');
1114 striplt(browser_host);
1116 /** Only WAP gateways explicitly name this content-type */
1117 else if (strstr(buf, "text/vnd.wap.wml")) {
1122 if (ContentLength > 0) {
1123 content = malloc(ContentLength + SIZ);
1124 memset(content, 0, ContentLength + SIZ);
1125 sprintf(content, "Content-type: %s\n"
1126 "Content-length: %d\n\n",
1127 ContentType, ContentLength);
1128 body_start = strlen(content);
1130 /** Read the entire input data at once. */
1131 client_read(WC->http_sock, &content[BytesRead+body_start],
1134 if (!strncasecmp(ContentType,
1135 "application/x-www-form-urlencoded", 33)) {
1136 addurls(&content[body_start]);
1137 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1138 content_end = content + ContentLength + body_start;
1139 mime_parser(content, content_end, *upload_handler,
1140 NULL, NULL, NULL, 0);
1146 /** make a note of where we are in case the user wants to save it */
1147 safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1148 remove_token(WC->this_page, 2, ' ');
1149 remove_token(WC->this_page, 0, ' ');
1151 /** If there are variables in the URL, we must grab them now */
1152 for (a = 0; a < strlen(cmd); ++a) {
1153 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1154 for (b = a; b < strlen(cmd); ++b)
1155 if (isspace(cmd[b]))
1157 addurls(&cmd[a + 1]);
1162 /** If it's a "force 404" situation then display the error and bail. */
1163 if (!strcmp(action, "404")) {
1164 wprintf("HTTP/1.1 404 Not found\r\n");
1165 wprintf("Content-Type: text/plain\r\n");
1167 wprintf("Not found\r\n");
1168 goto SKIP_ALL_THIS_CRAP;
1171 /** Static content can be sent without connecting to Citadel. */
1173 for (a=0; a<ndirs; ++a) {
1174 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1181 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1182 index[0], index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
1183 for (a=0; a<8; ++a) {
1184 if (buf[strlen(buf)-1] == '/') {
1185 buf[strlen(buf)-1] = 0;
1188 for (a = 0; a < strlen(buf); ++a) {
1189 if (isspace(buf[a])) {
1197 lprintf(9, "Suspicious request. Ignoring.");
1198 wprintf("HTTP/1.1 404 Not found. Don't try to Trick me DUDE!\r\n");
1199 wprintf("Content-Type: text/plain\r\n");
1201 wprintf("Not found. Don't play games on me!\r\n");
1203 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
1207 * If we're not connected to a Citadel server, try to hook up the
1210 if (!WC->connected) {
1211 if (!strcasecmp(ctdlhost, "uds")) {
1212 /* unix domain socket */
1213 sprintf(buf, "%s/citadel.socket", ctdlport);
1214 WC->serv_sock = uds_connectsock(buf);
1218 WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1221 if (WC->serv_sock < 0) {
1223 goto SKIP_ALL_THIS_CRAP;
1227 serv_getln(buf, sizeof buf); /** get the server welcome message */
1230 * From what host is our user connecting? Go with
1231 * the host at the other end of the HTTP socket,
1232 * unless we are following X-Forwarded-For: headers
1233 * and such a header has already turned up something.
1235 if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1236 locate_host(browser_host, WC->http_sock);
1239 get_serv_info(browser_host, user_agent);
1240 if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1241 wprintf(_("You are connected to a Citadel "
1242 "server running Citadel %d.%02d. \n"
1243 "In order to run this version of WebCit "
1244 "you must also have Citadel %d.%02d or"
1246 serv_info.serv_rev_level / 100,
1247 serv_info.serv_rev_level % 100,
1248 MINIMUM_CIT_VERSION / 100,
1249 MINIMUM_CIT_VERSION % 100
1251 end_webcit_session();
1252 goto SKIP_ALL_THIS_CRAP;
1258 * Functions which can be performed without logging in
1260 if (!strcasecmp(action, "listsub")) {
1262 goto SKIP_ALL_THIS_CRAP;
1264 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1265 if (!strcasecmp(action, "freebusy")) {
1267 goto SKIP_ALL_THIS_CRAP;
1272 * If we're not logged in, but we have HTTP Authentication data,
1273 * try logging in to Citadel using that.
1275 if ((!WC->logged_in)
1276 && (strlen(c_httpauth_user) > 0)
1277 && (strlen(c_httpauth_pass) > 0)) {
1278 serv_printf("USER %s", c_httpauth_user);
1279 serv_getln(buf, sizeof buf);
1280 if (buf[0] == '3') {
1281 serv_printf("PASS %s", c_httpauth_pass);
1282 serv_getln(buf, sizeof buf);
1283 if (buf[0] == '2') {
1284 become_logged_in(c_httpauth_user,
1285 c_httpauth_pass, buf);
1286 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1287 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1289 /** Should only display when password is wrong */
1290 authorization_required(&buf[4]);
1291 goto SKIP_ALL_THIS_CRAP;
1296 /** This needs to run early */
1297 if (!strcasecmp(action, "rss")) {
1298 display_rss(bstr("room"), request_method);
1299 goto SKIP_ALL_THIS_CRAP;
1303 * The GroupDAV stuff relies on HTTP authentication instead of
1304 * our session's authentication.
1306 if (!strncasecmp(action, "groupdav", 8)) {
1307 groupdav_main(req, ContentType, /* do GroupDAV methods */
1308 ContentLength, content+body_start);
1309 if (!WC->logged_in) {
1310 WC->killthis = 1; /* If not logged in, don't */
1311 } /* keep the session active */
1312 goto SKIP_ALL_THIS_CRAP;
1317 * Automatically send requests with any method other than GET or
1318 * POST to the GroupDAV code as well.
1320 if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1321 groupdav_main(req, ContentType, /** do GroupDAV methods */
1322 ContentLength, content+body_start);
1323 if (!WC->logged_in) {
1324 WC->killthis = 1; /** If not logged in, don't */
1325 } /** keep the session active */
1326 goto SKIP_ALL_THIS_CRAP;
1330 * If we're not logged in, but we have username and password cookies
1331 * supplied by the browser, try using them to log in.
1333 if ((!WC->logged_in)
1334 && (strlen(c_username) > 0)
1335 && (strlen(c_password) > 0)) {
1336 serv_printf("USER %s", c_username);
1337 serv_getln(buf, sizeof buf);
1338 if (buf[0] == '3') {
1339 serv_printf("PASS %s", c_password);
1340 serv_getln(buf, sizeof buf);
1341 if (buf[0] == '2') {
1342 become_logged_in(c_username, c_password, buf);
1347 * If we don't have a current room, but a cookie specifying the
1348 * current room is supplied, make an effort to go there.
1350 if ((strlen(WC->wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
1351 serv_printf("GOTO %s", c_roomname);
1352 serv_getln(buf, sizeof buf);
1353 if (buf[0] == '2') {
1354 safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1358 if (!strcasecmp(action, "image")) {
1362 * All functions handled below this point ... make sure we log in
1363 * before doing anything else!
1365 } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1367 } else if (!WC->logged_in) {
1368 display_login(NULL);
1372 * Various commands...
1375 else if (!strcasecmp(action, "do_welcome")) {
1377 } else if (!strcasecmp(action, "blank")) {
1379 } else if (!strcasecmp(action, "do_template")) {
1381 } else if (!strcasecmp(action, "display_aide_menu")) {
1382 display_aide_menu();
1383 } else if (!strcasecmp(action, "display_main_menu")) {
1384 display_main_menu();
1385 } else if (!strcasecmp(action, "who")) {
1387 } else if (!strcasecmp(action, "sslg")) {
1388 seconds_since_last_gexp();
1389 } else if (!strcasecmp(action, "who_inner_html")) {
1390 begin_ajax_response();
1392 end_ajax_response();
1393 } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1394 begin_ajax_response();
1396 end_ajax_response();
1397 } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1398 begin_ajax_response();
1399 do_iconbar_roomlist();
1400 end_ajax_response();
1401 } else if (!strcasecmp(action, "knrooms")) {
1403 } else if (!strcasecmp(action, "gotonext")) {
1406 } else if (!strcasecmp(action, "skip")) {
1408 } else if (!strcasecmp(action, "ungoto")) {
1410 } else if (!strcasecmp(action, "dotgoto")) {
1411 if (WC->wc_view != VIEW_MAILBOX) { /* dotgoto acts like dotskip when we're in a mailbox view */
1414 smart_goto(bstr("room"));
1415 } else if (!strcasecmp(action, "dotskip")) {
1416 smart_goto(bstr("room"));
1417 } else if (!strcasecmp(action, "termquit")) {
1419 } else if (!strcasecmp(action, "readnew")) {
1420 readloop("readnew");
1421 } else if (!strcasecmp(action, "readold")) {
1422 readloop("readold");
1423 } else if (!strcasecmp(action, "readfwd")) {
1424 readloop("readfwd");
1425 } else if (!strcasecmp(action, "headers")) {
1426 readloop("headers");
1427 } else if (!strcasecmp(action, "do_search")) {
1428 readloop("do_search");
1429 } else if (!strcasecmp(action, "msg")) {
1430 embed_message(index[1]);
1431 } else if (!strcasecmp(action, "printmsg")) {
1432 print_message(index[1]);
1433 } else if (!strcasecmp(action, "msgheaders")) {
1434 display_headers(index[1]);
1435 } else if (!strcasecmp(action, "wiki")) {
1436 display_wiki_page();
1437 } else if (!strcasecmp(action, "display_enter")) {
1439 } else if (!strcasecmp(action, "post")) {
1441 } else if (!strcasecmp(action, "move_msg")) {
1443 } else if (!strcasecmp(action, "delete_msg")) {
1445 } else if (!strcasecmp(action, "userlist")) {
1447 } else if (!strcasecmp(action, "showuser")) {
1449 } else if (!strcasecmp(action, "display_page")) {
1451 } else if (!strcasecmp(action, "page_user")) {
1453 } else if (!strcasecmp(action, "chat")) {
1455 } else if (!strcasecmp(action, "display_private")) {
1456 display_private("", 0);
1457 } else if (!strcasecmp(action, "goto_private")) {
1459 } else if (!strcasecmp(action, "zapped_list")) {
1461 } else if (!strcasecmp(action, "display_zap")) {
1463 } else if (!strcasecmp(action, "zap")) {
1465 } else if (!strcasecmp(action, "display_entroom")) {
1467 } else if (!strcasecmp(action, "entroom")) {
1469 } else if (!strcasecmp(action, "display_whok")) {
1471 } else if (!strcasecmp(action, "do_invt_kick")) {
1473 } else if (!strcasecmp(action, "display_editroom")) {
1475 } else if (!strcasecmp(action, "netedit")) {
1477 } else if (!strcasecmp(action, "editroom")) {
1479 } else if (!strcasecmp(action, "display_editinfo")) {
1480 display_edit(_("Room info"), "EINF 0", "RINF", "/editinfo", 1);
1481 } else if (!strcasecmp(action, "editinfo")) {
1482 save_edit(_("Room info"), "EINF 1", 1);
1483 } else if (!strcasecmp(action, "display_editbio")) {
1484 sprintf(buf, "RBIO %s", WC->wc_fullname);
1485 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1486 } else if (!strcasecmp(action, "editbio")) {
1487 save_edit(_("Your bio"), "EBIO", 0);
1488 } else if (!strcasecmp(action, "confirm_move_msg")) {
1490 } else if (!strcasecmp(action, "delete_room")) {
1492 } else if (!strcasecmp(action, "validate")) {
1494 } else if (!strcasecmp(action, "display_editpic")) {
1495 display_graphics_upload(_("your photo"),
1498 } else if (!strcasecmp(action, "editpic")) {
1499 do_graphics_upload("UIMG 1|_userpic_");
1500 } else if (!strcasecmp(action, "display_editroompic")) {
1501 display_graphics_upload(_("the icon for this room"),
1504 } else if (!strcasecmp(action, "editroompic")) {
1505 do_graphics_upload("UIMG 1|_roompic_");
1506 } else if (!strcasecmp(action, "delete_floor")) {
1508 } else if (!strcasecmp(action, "rename_floor")) {
1510 } else if (!strcasecmp(action, "create_floor")) {
1512 } else if (!strcasecmp(action, "display_editfloorpic")) {
1513 sprintf(buf, "UIMG 0|_floorpic_|%s",
1514 bstr("which_floor"));
1515 display_graphics_upload(_("the icon for this floor"),
1518 } else if (!strcasecmp(action, "editfloorpic")) {
1519 sprintf(buf, "UIMG 1|_floorpic_|%s",
1520 bstr("which_floor"));
1521 do_graphics_upload(buf);
1522 } else if (!strcasecmp(action, "display_reg")) {
1524 } else if (!strcasecmp(action, "display_changepw")) {
1526 } else if (!strcasecmp(action, "changepw")) {
1528 } else if (!strcasecmp(action, "display_edit_node")) {
1529 display_edit_node();
1530 } else if (!strcasecmp(action, "edit_node")) {
1532 } else if (!strcasecmp(action, "display_netconf")) {
1534 } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1535 display_confirm_delete_node();
1536 } else if (!strcasecmp(action, "delete_node")) {
1538 } else if (!strcasecmp(action, "display_add_node")) {
1540 } else if (!strcasecmp(action, "add_node")) {
1542 } else if (!strcasecmp(action, "terminate_session")) {
1544 terminate_session();
1545 } else if (!strcasecmp(action, "edit_me")) {
1547 } else if (!strcasecmp(action, "display_siteconfig")) {
1548 display_siteconfig();
1549 } else if (!strcasecmp(action, "chat_recv")) {
1551 } else if (!strcasecmp(action, "chat_send")) {
1553 } else if (!strcasecmp(action, "siteconfig")) {
1555 } else if (!strcasecmp(action, "display_generic")) {
1557 } else if (!strcasecmp(action, "do_generic")) {
1559 } else if (!strcasecmp(action, "ajax_servcmd")) {
1561 } else if (!strcasecmp(action, "display_menubar")) {
1563 } else if (!strcasecmp(action, "mimepart")) {
1564 mimepart(index[1], index[2], 0);
1565 } else if (!strcasecmp(action, "mimepart_download")) {
1566 mimepart(index[1], index[2], 1);
1567 } else if (!strcasecmp(action, "edit_vcard")) {
1569 } else if (!strcasecmp(action, "submit_vcard")) {
1571 } else if (!strcasecmp(action, "select_user_to_edit")) {
1572 select_user_to_edit(NULL, NULL);
1573 } else if (!strcasecmp(action, "display_edituser")) {
1574 display_edituser(NULL, 0);
1575 } else if (!strcasecmp(action, "edituser")) {
1577 } else if (!strcasecmp(action, "create_user")) {
1579 } else if (!strcasecmp(action, "changeview")) {
1581 } else if (!strcasecmp(action, "change_start_page")) {
1582 change_start_page();
1583 } else if (!strcasecmp(action, "display_floorconfig")) {
1584 display_floorconfig(NULL);
1585 } else if (!strcasecmp(action, "toggle_self_service")) {
1586 toggle_self_service();
1587 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1588 } else if (!strcasecmp(action, "display_edit_task")) {
1589 display_edit_task();
1590 } else if (!strcasecmp(action, "save_task")) {
1592 } else if (!strcasecmp(action, "display_edit_event")) {
1593 display_edit_event();
1594 } else if (!strcasecmp(action, "save_event")) {
1596 } else if (!strcasecmp(action, "respond_to_request")) {
1597 respond_to_request();
1598 } else if (!strcasecmp(action, "handle_rsvp")) {
1601 } else if (!strcasecmp(action, "summary")) {
1603 } else if (!strcasecmp(action, "summary_inner_div")) {
1604 begin_ajax_response();
1605 summary_inner_div();
1606 end_ajax_response();
1607 } else if (!strcasecmp(action, "display_customize_iconbar")) {
1608 display_customize_iconbar();
1609 } else if (!strcasecmp(action, "commit_iconbar")) {
1611 } else if (!strcasecmp(action, "set_room_policy")) {
1613 } else if (!strcasecmp(action, "display_inetconf")) {
1615 } else if (!strcasecmp(action, "save_inetconf")) {
1617 } else if (!strcasecmp(action, "display_smtpqueue")) {
1618 display_smtpqueue();
1619 } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1620 display_smtpqueue_inner_div();
1621 } else if (!strcasecmp(action, "display_sieve")) {
1623 } else if (!strcasecmp(action, "save_sieve")) {
1625 } else if (!strcasecmp(action, "display_add_remove_scripts")) {
1626 display_add_remove_scripts(NULL);
1627 } else if (!strcasecmp(action, "create_script")) {
1629 } else if (!strcasecmp(action, "delete_script")) {
1631 } else if (!strcasecmp(action, "setup_wizard")) {
1633 } else if (!strcasecmp(action, "display_preferences")) {
1634 display_preferences();
1635 } else if (!strcasecmp(action, "set_preferences")) {
1637 } else if (!strcasecmp(action, "recp_autocomplete")) {
1638 recp_autocomplete(bstr("recp"));
1639 } else if (!strcasecmp(action, "cc_autocomplete")) {
1640 recp_autocomplete(bstr("cc"));
1641 } else if (!strcasecmp(action, "bcc_autocomplete")) {
1642 recp_autocomplete(bstr("bcc"));
1643 } else if (!strcasecmp(action, "set_floordiv_expanded")) {
1644 set_floordiv_expanded(index[1]);
1645 } else if (!strcasecmp(action, "diagnostics")) {
1646 output_headers(1, 1, 1, 0, 0, 0);
1647 wprintf("Session: %d<hr />\n", WC->wc_session);
1648 wprintf("Command: <br /><PRE>\n");
1650 wprintf("</PRE><hr />\n");
1651 wprintf("Variables: <br /><PRE>\n");
1653 wprintf("</PRE><hr />\n");
1655 } else if (!strcasecmp(action, "updatenote")) {
1659 /** When all else fais, display the main menu. */
1661 display_main_menu();
1666 if (content != NULL) {
1671 if (WC->upload_length > 0) {
1673 WC->upload_length = 0;