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.
8 #define SHOW_ME_VAPPEND_PRINTF
13 #include "webserver.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";
23 HashList *HandlerHash = NULL;
25 void WebcitAddUrlHandler(const char * UrlString,
30 WebcitHandler *NewHandler;
32 if (HandlerHash == NULL)
33 HandlerHash = NewHash(1, NULL);
35 NewHandler = (WebcitHandler*) malloc(sizeof(WebcitHandler));
37 NewHandler->Flags = Flags;
38 Put(HandlerHash, UrlString, UrlSLen, NewHandler, NULL);
43 * web-printing funcion. uses our vsnprintf wrapper
45 void wprintf(const char *format,...)
50 if (WCC->WBuf == NULL)
51 WCC->WBuf = NewStrBuf();
53 va_start(arg_ptr, format);
54 StrBufVAppendPrintf(WCC->WBuf, format, arg_ptr);
59 * http-header-printing funcion. uses our vsnprintf wrapper
61 void hprintf(const char *format,...)
66 va_start(arg_ptr, format);
67 StrBufVAppendPrintf(WCC->HBuf, format, arg_ptr);
74 * wrap up an HTTP session, closes tags, etc.
76 * print_standard_html_footer should be set to:
77 * 0 - to transmit only,
78 * nonzero - to append the closing tags
80 void wDumpContent(int print_standard_html_footer)
82 if (print_standard_html_footer) {
83 wprintf("</div> <!-- end of 'content' div -->\n");
84 do_template("trailing", NULL);
87 /* If we've been saving it all up for one big output burst,
88 * go ahead and do that now.
97 * Output HTTP headers and leading HTML for a page
99 void output_headers( int do_httpheaders, /* 1 = output HTTP headers */
100 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
102 int do_room_banner, /* 0=no, 1=yes,
103 * 2 = I'm going to embed my own, so don't open the
104 * <div id="content"> either.
107 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
108 int suppress_check, /* 1 = suppress check for instant messages */
109 int cache /* 1 = allow browser to cache this page */
115 hprintf("HTTP/1.1 200 OK\n");
116 http_datestring(httpnow, sizeof httpnow, time(NULL));
118 if (do_httpheaders) {
119 if (WCC->serv_info != NULL)
120 hprintf("Content-type: text/html; charset=utf-8\r\n"
122 "Connection: close\r\n",
124 ChrPtr(WCC->serv_info->serv_software));
126 hprintf("Content-type: text/html; charset=utf-8\r\n"
127 "Server: %s / [n/a]\n"
128 "Connection: close\r\n",
133 char httpTomorow[128];
135 http_datestring(httpTomorow, sizeof httpTomorow,
136 time(NULL) + 60 * 60 * 24 * 2);
138 hprintf("Pragma: public\r\n"
139 "Cache-Control: max-age=3600, must-revalidate\r\n"
140 "Last-modified: %s\r\n"
147 hprintf("Pragma: no-cache\r\n"
148 "Cache-Control: no-store\r\n"
153 stuff_to_cookie(cookie, 1024,
154 WCC->wc_session, WCC->wc_username,
155 WCC->wc_password, WCC->wc_roomname);
158 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
160 hprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
161 if (server_cookie != NULL) {
162 hprintf("%s\n", server_cookie);
168 do_template("head", NULL);
170 /* check for ImportantMessages (these display in a div overlaying the main screen) */
171 if (!IsEmptyStr(WC->ImportantMessage)) {
172 wprintf("<div id=\"important_message\">\n"
173 "<span class=\"imsg\">");
174 escputs(WC->ImportantMessage);
175 wprintf("</span><br />\n"
178 StrBufAppendPrintf(WCC->trailing_javascript,
179 "setTimeout('hide_imsg_popup()', 5000); \n"
181 WCC->ImportantMessage[0] = 0;
184 if ( (WCC->logged_in) && (!unset_cookies) ) {
185 //DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
189 if (do_room_banner == 1) {
190 wprintf("<div id=\"banner\">\n");
191 embed_room_banner(NULL, navbar_default);
196 if (do_room_banner == 1) {
197 wprintf("<div id=\"content\">\n");
201 void output_custom_content_header(const char *ctype) {
202 hprintf("HTTP/1.1 200 OK\r\n");
203 hprintf("Content-type: %s; charset=utf-8\r\n",ctype);
204 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
205 hprintf("Connection: close\r\n");
210 * Generic function to do an HTTP redirect. Easy and fun.
212 void http_redirect(const char *whichpage) {
213 hprintf("HTTP/1.1 302 Moved Temporarily\n");
214 hprintf("Location: %s\r\n", whichpage);
215 hprintf("URI: %s\r\n", whichpage);
216 hprintf("Content-type: text/html; charset=utf-8\r\n");
217 wprintf("<html><body>");
218 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
219 wprintf("</body></html>\n");
226 * Output a piece of content to the web browser using conformant HTTP and MIME semantics
228 void http_transmit_thing(const char *content_type,
232 lprintf(9, "http_transmit_thing(%s)%s\n",
234 (is_static ? " (static)" : "")
237 output_headers(0, 0, 0, 0, 0, is_static);
239 hprintf("Content-type: %s\r\n"
241 "Connection: close\r\n",
249 * print menu box like used in the floor view or admin interface.
250 * This function takes pair of strings as va_args,
251 * Title Title string of the box
252 * Class CSS Class for the box
253 * nLines How many string pairs should we print? (URL, UrlText)
254 * ... Pairs of URL Strings and their Names
256 void print_menu_box(char* Title, char *Class, int nLines, ...)
261 svput("BOXTITLE", WCS_STRING, Title);
262 do_template("beginboxx", NULL);
264 wprintf("<ul class=\"%s\">", Class);
266 va_start(arg_list, nLines);
267 for (i = 0; i < nLines; ++i)
269 wprintf("<li><a href=\"%s\">", va_arg(arg_list, char *));
270 wprintf((char *) va_arg(arg_list, char *));
271 wprintf("</a></li>\n");
275 wprintf("</a></li>\n");
279 do_template("endbox", NULL);
284 * dump out static pages from disk
286 void output_static(const char *what)
292 const char *content_type;
296 fd = open(what, O_RDONLY);
298 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
299 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
300 hprintf("Content-Type: text/plain\r\n");
301 wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
305 content_type = GuessMimeByFilename(what, len);
307 if (fstat(fd, &statbuf) == -1) {
308 lprintf(9, "output_static('%s') -- FSTAT FAILED --\n", what);
309 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
310 hprintf("Content-Type: text/plain\r\n");
311 wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
317 bytes = statbuf.st_size;
319 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
321 if (fd > 0) close(fd);
322 lprintf(9, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
323 hprintf("HTTP/1.1 500 internal server error \r\n");
324 hprintf("Content-Type: text/plain\r\n");
332 lprintf(9, "output_static('%s') %s\n", what, content_type);
334 http_transmit_thing(content_type, 1);
336 if (yesbstr("force_close_session")) {
337 end_webcit_session();
343 * Convenience functions to display a page containing only a string
345 * titlebarcolor color of the titlebar of the frame
346 * titlebarmsg text to display in the title bar
347 * messagetext body of the box
349 void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext)
351 hprintf("HTTP/1.1 200 OK\n");
352 output_headers(1, 1, 2, 0, 0, 0);
353 wprintf("<div id=\"banner\">\n");
354 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
355 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
356 wprintf("</td></tr></table>\n");
357 wprintf("</div>\n<div id=\"content\">\n");
358 escputs(messagetext);
366 * Display a blank page.
368 void blank_page(void) {
369 output_headers(1, 1, 0, 0, 0, 0);
375 * A template has been requested
377 void url_do_template(void) {
378 const StrBuf *MimeType;
379 const StrBuf *Tmpl = sbstr("template");
381 output_headers(0, 0, 0, 0, 1, 0);
382 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
383 http_transmit_thing(ChrPtr(MimeType), 0);
389 * convenience function to indicate success
391 void display_success(char *successmessage)
393 convenience_page("007700", "OK", successmessage);
398 * Authorization required page
399 * This is probably temporary and should be revisited
401 void authorization_required(const char *message)
403 hprintf("HTTP/1.1 401 Authorization Required\r\n");
404 hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", ChrPtr(WC->serv_info->serv_humannode));
405 hprintf("Content-Type: text/html\r\n");
407 wprintf(_("Authorization Required"));
408 wprintf("</h1>\r\n");
409 wprintf(_("The resource you requested requires a valid username and password. "
410 "You could not be logged in: %s\n"), message);
416 * Convenience functions to wrap around asynchronous ajax responses
418 void begin_ajax_response(void) {
421 FlushStrBuf(WCC->HBuf);
422 output_headers(0, 0, 0, 0, 0, 0);
424 hprintf("Content-type: text/html; charset=UTF-8\r\n"
426 "Connection: close\r\n"
433 * print ajax response footer
435 void end_ajax_response(void) {
440 * Wraps a Citadel server command in an AJAX transaction.
442 void ajax_servcmd(void)
450 begin_ajax_response();
452 serv_puts(bstr("g_cmd"));
453 StrBuf_ServGetln(Buf);
454 StrBufAppendBuf(WCC->WBuf, Buf, 0);
455 StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
457 switch (GetServerStatus(Buf, NULL)) {
459 serv_puts("\n\n000");
460 if ( (StrLength(Buf)==3) &&
461 !strcmp(ChrPtr(Buf), "000")) {
462 StrBufAppendBufPlain(WCC->WBuf, HKEY("\000"), 0);
467 StrBuf_ServGetlnBuffered(Buf);
468 if ( (StrLength(Buf)==3) &&
469 !strcmp(ChrPtr(Buf), "000")) {
472 StrBufAppendBuf(WCC->WBuf, Buf, 0);
473 StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
477 text_to_server(bstr("g_input"));
481 len = atol(&ChrPtr(Buf)[4]);
482 StrBuf_ServGetBLOBBuffered(Buf, len);
485 len = atol(&ChrPtr(Buf)[4]);
487 memset(junk, 0, len);
488 serv_write(junk, len);
495 * This is kind of an ugly hack, but this is the only place it can go.
496 * If the command was GEXP, then the instant messenger window must be
497 * running, so reset the "last_pager_check" watchdog timer so
498 * that page_popup() doesn't try to open it a second time.
500 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
501 WCC->last_pager_check = time(NULL);
508 * Helper function for the asynchronous check to see if we need
509 * to open the instant messenger window.
511 void seconds_since_last_gexp(void)
515 if ( (time(NULL) - WC->last_pager_check) < 30) {
521 serv_getln(buf, sizeof buf);
532 * \brief Detects a 'mobile' user agent
534 int is_mobile_ua(char *user_agent) {
535 if (strstr(user_agent,"iPhone OS") != NULL) {
537 } else if (strstr(user_agent,"Windows CE") != NULL) {
539 } else if (strstr(user_agent,"SymbianOS") != NULL) {
541 } else if (strstr(user_agent, "Opera Mobi") != NULL) {
543 } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
544 /* For some reason a new install of Opera 9.51beta decided to spoof. */
552 * Entry point for WebCit transaction
554 void session_loop(HashList *HTTPHeaders,
556 StrBuf *request_method,
561 const char *pch, *pchs, *pche;
568 int a, nBackDots, nEmpty;
569 int ContentLength = 0;
570 StrBuf *ContentType = NULL;
571 StrBuf *UrlLine = NULL;
572 StrBuf *content = NULL;
573 const char *content_end = NULL;
574 StrBuf *browser_host = NULL;
575 char user_agent[256];
581 WebcitHandler *Handler;
582 struct timeval tx_start;
583 struct timeval tx_finish;
586 * We stuff these with the values coming from the client cookies,
587 * so we can use them to reconnect a timed out session if we have to.
592 char c_httpauth_string[SIZ];
593 StrBuf *c_httpauth_user;
594 StrBuf *c_httpauth_pass;
597 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
600 c_username = NewStrBuf();
601 c_password = NewStrBuf();
602 c_roomname = NewStrBuf();
603 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
604 c_httpauth_user = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
605 c_httpauth_pass = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
608 if (WCC->WBuf == NULL)
609 WC->WBuf = NewStrBufPlain(NULL, 32768);
610 FlushStrBuf(WCC->WBuf);
612 if (WCC->HBuf == NULL)
613 WCC->HBuf = NewStrBuf();
614 FlushStrBuf(WCC->HBuf);
616 WCC->upload_length = 0;
619 WCC->trailing_javascript = NewStrBuf();
620 WCC->nWildfireHeaders = 0;
622 /** Figure out the action */
624 sizes[0] = sizeof action;
628 sizes[a] = sizeof arg[a-1];
632 for ( a = 0; a < 9; ++a)
634 extract_token(index[a], ChrPtr(ReqLine), a + 1, '/', sizes[a]);
635 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
636 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
637 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
638 if ((index[a][0] == '.') && (index[a][1] == '.'))
640 if (index[a][0] == '\0')
645 if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) &&
647 cookie_to_stuff((StrBuf *)vLine, NULL,
652 if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) &&
654 StrBufDecodeBase64((StrBuf*)vLine);
655 StrBufExtract_token(c_httpauth_user, (StrBuf*)vLine, 0, ':');
656 StrBufExtract_token(c_httpauth_pass, (StrBuf*)vLine, 1, ':');
658 if (GetHash(HTTPHeaders, HKEY("CONTENT-LENGTH"), &vLine) &&
660 ContentLength = StrToi((StrBuf*)vLine);
662 if (GetHash(HTTPHeaders, HKEY("CONTENT-TYPE"), &vLine) &&
664 ContentType = (StrBuf*)vLine;
666 if (GetHash(HTTPHeaders, HKEY("USER-AGENT"), &vLine) &&
668 safestrncpy(user_agent, ChrPtr((StrBuf*)vLine), sizeof user_agent);
670 if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {
679 GetHash(HTTPHeaders, HKEY("X-FORWARDED-HOST"), &vLine) &&
681 WCC->http_host = (StrBuf*)vLine;
683 if ((StrLength(WCC->http_host) == 0) &&
684 GetHash(HTTPHeaders, HKEY("HOST"), &vLine) &&
686 WCC->http_host = (StrBuf*)vLine;
689 if (GetHash(HTTPHeaders, HKEY("X-FORWARDED-FOR"), &vLine) &&
691 browser_host = (StrBuf*) vLine;
693 while (StrBufNum_tokens(browser_host, ',') > 1) {
694 StrBufRemove_token(browser_host, 0, ',');
696 StrBufTrim(browser_host);
699 if (ContentLength > 0) {
700 content = NewStrBuf();
701 StrBufPrintf(content, "Content-type: %s\n"
702 "Content-length: %d\n\n",
703 ChrPtr(ContentType), ContentLength);
705 hprintf("Content-type: %s\n"
706 "Content-length: %d\n\n",
707 ContentType, ContentLength);
709 body_start = StrLength(content);
711 /** Read the entire input data at once. */
712 client_read_to(&WCC->http_sock,
718 if (!strncasecmp(ChrPtr(ContentType), "application/x-www-form-urlencoded", 33)) {
719 StrBufCutLeft(content, body_start);
720 ParseURLParams(content);
721 } else if (!strncasecmp(ChrPtr(ContentType), "multipart", 9)) {
722 content_end = ChrPtr(content) + ContentLength + body_start;
723 mime_parser(ChrPtr(content), content_end, *upload_handler, NULL, NULL, NULL, 0);
729 /* make a note of where we are in case the user wants to save it */
730 WCC->this_page = NewStrBufDup(ReqLine);
731 StrBufRemove_token(WCC->this_page, 2, ' ');
732 StrBufRemove_token(WCC->this_page, 0, ' ');
734 /* If there are variables in the URL, we must grab them now */
735 UrlLine = NewStrBufDup(ReqLine);
736 len = StrLength(UrlLine);
737 pch = pchs = ChrPtr(UrlLine);
740 if ((*pch == '?') || (*pch == '&')) {
741 StrBufCutLeft(UrlLine, pch - pchs + 1);
742 ParseURLParams(UrlLine);
747 FreeStrBuf(&UrlLine);
749 /* If it's a "force 404" situation then display the error and bail. */
750 if (!strcmp(action, "404")) {
751 hprintf("HTTP/1.1 404 Not found\r\n");
752 hprintf("Content-Type: text/plain\r\n");
753 wprintf("Not found\r\n");
755 goto SKIP_ALL_THIS_CRAP;
758 /* Static content can be sent without connecting to Citadel. */
760 for (a=0; a<ndirs && ! is_static; ++a) {
761 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
769 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
770 static_dirs[n_static],
771 index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
772 for (a=0; a<8; ++a) {
773 if (buf[strlen(buf)-1] == '/') {
774 buf[strlen(buf)-1] = 0;
777 for (a = 0; a < strlen(buf); ++a) {
778 if (isspace(buf[a])) {
786 lprintf(9, "Suspicious request. Ignoring.");
787 hprintf("HTTP/1.1 404 Security check failed\r\n");
788 hprintf("Content-Type: text/plain\r\n\r\n");
789 wprintf("You have sent a malformed or invalid request.\r\n");
792 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
795 /* If the client sent a nonce that is incorrect, kill the request. */
796 if (strlen(bstr("nonce")) > 0) {
797 lprintf(9, "Comparing supplied nonce %s to session nonce %ld\n",
798 bstr("nonce"), WCC->nonce);
799 if (ibstr("nonce") != WCC->nonce) {
800 lprintf(9, "Ignoring request with mismatched nonce.\n");
801 hprintf("HTTP/1.1 404 Security check failed\r\n");
802 hprintf("Content-Type: text/plain\r\n\r\n");
803 wprintf("Security check failed.\r\n");
805 goto SKIP_ALL_THIS_CRAP;
810 * If we're not connected to a Citadel server, try to hook up the
813 if (!WCC->connected) {
814 if (WCC->ReadBuf == NULL)
815 WCC->ReadBuf = NewStrBuf();
816 if (!strcasecmp(ctdlhost, "uds")) {
817 /* unix domain socket */
818 snprintf(buf, SIZ, "%s/citadel.socket", ctdlport);
819 WCC->serv_sock = uds_connectsock(buf);
823 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
826 if (WCC->serv_sock < 0) {
828 FreeStrBuf(&WCC->ReadBuf);
829 goto SKIP_ALL_THIS_CRAP;
833 serv_getln(buf, sizeof buf); /* get the server greeting */
835 /* Are there too many users already logged in? */
836 if (!strncmp(buf, "571", 3)) {
837 wprintf(_("This server is already serving its maximum number of users and cannot accept any additional logins at this time. Please try again later or contact your system administrator."));
839 end_webcit_session();
840 goto SKIP_ALL_THIS_CRAP;
844 * From what host is our user connecting? Go with
845 * the host at the other end of the HTTP socket,
846 * unless we are following X-Forwarded-For: headers
847 * and such a header has already turned up something.
849 if ( (!follow_xff) || (StrLength(browser_host) == 0) ) {
850 if (browser_host == NULL) {
851 browser_host = NewStrBuf();
852 Put(HTTPHeaders, HKEY("FreeMeWithTheOtherHeaders"),
853 browser_host, HFreeStrBuf);
855 locate_host(browser_host, WCC->http_sock);
857 if (WCC->serv_info == NULL)
858 WCC->serv_info = get_serv_info(browser_host, user_agent);
859 if (WCC->serv_info == NULL){
861 wprintf(_("Received unexpected answer from Citadel "
862 "server; bailing out."));
863 hprintf("HTTP/1.1 200 OK\r\n");
864 hprintf("Content-type: text/plain; charset=utf-8\r\n");
866 end_webcit_session();
867 goto SKIP_ALL_THIS_CRAP;
869 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
871 wprintf(_("You are connected to a Citadel "
872 "server running Citadel %d.%02d. \n"
873 "In order to run this version of WebCit "
874 "you must also have Citadel %d.%02d or"
876 WCC->serv_info->serv_rev_level / 100,
877 WCC->serv_info->serv_rev_level % 100,
878 MINIMUM_CIT_VERSION / 100,
879 MINIMUM_CIT_VERSION % 100
881 hprintf("HTTP/1.1 200 OK\r\n");
882 hprintf("Content-type: text/plain; charset=utf-8\r\n");
884 end_webcit_session();
885 goto SKIP_ALL_THIS_CRAP;
891 * Functions which can be performed without logging in
893 if (!strcasecmp(action, "listsub")) {
895 goto SKIP_ALL_THIS_CRAP;
897 if (!strcasecmp(action, "freebusy")) {
898 do_freebusy(ChrPtr(ReqLine));
899 goto SKIP_ALL_THIS_CRAP;
903 * If we're not logged in, but we have HTTP Authentication data,
904 * try logging in to Citadel using that.
906 if ((!WCC->logged_in)
907 && (StrLength(c_httpauth_user) > 0)
908 && (StrLength(c_httpauth_pass) > 0))
911 serv_printf("USER %s", ChrPtr(c_httpauth_user));
912 StrBuf_ServGetln(Buf);
913 if (GetServerStatus(Buf, NULL) == 3) {
914 serv_printf("PASS %s", ChrPtr(c_httpauth_pass));
915 StrBuf_ServGetln(Buf);
916 if (GetServerStatus(Buf, NULL) == 2) {
917 become_logged_in(c_httpauth_user,
918 c_httpauth_pass, Buf);
919 if (WCC->httpauth_user == NULL)
920 WCC->httpauth_user = NewStrBufDup(c_httpauth_user);
922 FlushStrBuf(WCC->httpauth_user);
923 StrBufAppendBuf(WCC->httpauth_user, c_httpauth_user, 0);
925 if (WCC->httpauth_pass == NULL)
926 WCC->httpauth_pass = NewStrBufDup(c_httpauth_pass);
928 FlushStrBuf(WCC->httpauth_pass);
929 StrBufAppendBuf(WCC->httpauth_pass, c_httpauth_pass, 0);
932 /* Should only display when password is wrong */
933 authorization_required(&buf[4]);
935 goto SKIP_ALL_THIS_CRAP;
940 /* This needs to run early */
942 if (!strcasecmp(action, "rss")) {
943 display_rss(sbstr("room"), request_method);
944 goto SKIP_ALL_THIS_CRAP;
949 * The GroupDAV stuff relies on HTTP authentication instead of
950 * our session's authentication.
952 if (!strncasecmp(action, "groupdav", 8)) {
953 groupdav_main(HTTPHeaders,
954 ReqLine, request_method,
955 ContentType, /* do GroupDAV methods */
956 ContentLength, content, body_start);
957 if (!WCC->logged_in) {
958 WCC->killthis = 1; /* If not logged in, don't */
959 } /* keep the session active */
960 goto SKIP_ALL_THIS_CRAP;
965 * Automatically send requests with any method other than GET or
966 * POST to the GroupDAV code as well.
968 if ((strcasecmp(ChrPtr(request_method), "GET")) && (strcasecmp(ChrPtr(request_method), "POST"))) {
969 groupdav_main(HTTPHeaders, ReqLine,
970 request_method, ContentType, /** do GroupDAV methods */
971 ContentLength, content, body_start);
972 if (!WCC->logged_in) {
973 WCC->killthis = 1; /** If not logged in, don't */
974 } /** keep the session active */
975 goto SKIP_ALL_THIS_CRAP;
979 * If we're not logged in, but we have username and password cookies
980 * supplied by the browser, try using them to log in.
982 if ((!WCC->logged_in)
983 && (StrLength(c_username)>0)
984 && (StrLength(c_password)>0)) {
985 serv_printf("USER %s", ChrPtr(c_username));
986 StrBuf_ServGetln(Buf);
987 if (GetServerStatus(Buf, NULL) == 3) {
988 serv_printf("PASS %s", ChrPtr(c_password));
989 StrBuf_ServGetln(Buf);
990 if (GetServerStatus(Buf, NULL) == 2) {
991 become_logged_in(c_username, c_password, Buf);
992 get_preference("default_header_charset", &WCC->DefaultCharset);
998 * If a 'gotofirst' parameter has been specified, attempt to goto that room
999 * prior to doing anything else.
1001 if (havebstr("gotofirst")) {
1002 gotoroom(sbstr("gotofirst")); /* do this quietly to avoid session output! */
1006 * If we don't have a current room, but a cookie specifying the
1007 * current room is supplied, make an effort to go there.
1009 if ((StrLength(WCC->wc_roomname) == 0) && (StrLength(c_roomname) > 0)) {
1010 serv_printf("GOTO %s", ChrPtr(c_roomname));
1011 StrBuf_ServGetln(Buf);
1012 if (GetServerStatus(Buf, NULL) == 2) {
1013 if (WCC->wc_roomname == NULL) {
1014 WCC->wc_roomname = NewStrBufDup(c_roomname);
1017 FlushStrBuf(WCC->wc_roomname);
1018 StrBufAppendBuf(WCC->wc_roomname, c_roomname, 0);
1023 GetHash(HandlerHash, action, strlen(action) /* TODO*/, &vHandler),
1024 Handler = (WebcitHandler*) vHandler;
1025 if (Handler != NULL) {
1026 if (!WCC->logged_in && ((Handler->Flags & ANONYMOUS) == 0)) {
1027 display_login(NULL);
1030 if((Handler->Flags & NEED_URL)) {
1031 if (WCC->UrlFragment1 == NULL)
1032 WCC->UrlFragment1 = NewStrBuf();
1033 if (WCC->UrlFragment2 == NULL)
1034 WCC->UrlFragment2 = NewStrBuf();
1035 if (WCC->UrlFragment3 == NULL)
1036 WCC->UrlFragment3 = NewStrBuf();
1037 if (WCC->UrlFragment4 == NULL)
1038 WCC->UrlFragment4 = NewStrBuf();
1039 StrBufPlain(WCC->UrlFragment1, index[0], -1);
1040 StrBufPlain(WCC->UrlFragment2, index[1], -1);
1041 StrBufPlain(WCC->UrlFragment3, index[2], -1);
1042 StrBufPlain(WCC->UrlFragment4, index[3], -1);
1044 if ((Handler->Flags & AJAX) != 0)
1045 begin_ajax_response();
1047 if ((Handler->Flags & AJAX) != 0)
1048 end_ajax_response();
1051 /* When all else fais, display the main menu. */
1053 if (!WCC->logged_in)
1054 display_login(NULL);
1056 display_main_menu();
1060 if (WCC->SavePrefsToServer) {
1062 WCC->SavePrefsToServer = 0;
1065 FreeStrBuf(&c_username);
1066 FreeStrBuf(&c_password);
1067 FreeStrBuf(&c_roomname);
1068 FreeStrBuf(&c_httpauth_user);
1069 FreeStrBuf(&c_httpauth_pass);
1070 FreeStrBuf(&WCC->this_page);
1072 if (content != NULL) {
1073 FreeStrBuf(&content);
1076 DeleteHash(&WCC->urlstrings);
1077 if (WCC->upload_length > 0) {
1079 WCC->upload_length = 0;
1081 FreeStrBuf(&WCC->trailing_javascript);
1082 WCC->http_host = NULL;
1084 /* How long did this transaction take? */
1085 gettimeofday(&tx_finish, NULL);
1087 lprintf(9, "Transaction completed in %ld.%06ld seconds.\n",
1088 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
1089 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
1095 * Replacement for sleep() that uses select() in order to avoid SIGALRM
1097 void sleeeeeeeeeep(int seconds)
1101 tv.tv_sec = seconds;
1103 select(0, NULL, NULL, NULL, &tv);
1107 int ConditionalImportantMesage(StrBuf *Target, WCTemplputParams *TP)
1109 wcsession *WCC = WC;
1111 return (!IsEmptyStr(WCC->ImportantMessage));
1116 void tmplput_importantmessage(StrBuf *Target, WCTemplputParams *TP)
1118 wcsession *WCC = WC;
1122 StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType,
1123 WCC->ImportantMessage, 0);
1125 StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
1126 WCC->ImportantMessage[0] = '\0';
1130 void tmplput_trailing_javascript(StrBuf *Target, WCTemplputParams *TP)
1132 wcsession *WCC = WC;
1135 StrBufAppendTemplate(Target, TP, WCC->trailing_javascript, 0);
1138 void tmplput_csslocal(StrBuf *Target, WCTemplputParams *TP)
1140 extern StrBuf *csslocal;
1141 StrBufAppendBuf(Target,
1152 WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS);
1153 WebcitAddUrlHandler(HKEY("do_template"), url_do_template, ANONYMOUS);
1154 WebcitAddUrlHandler(HKEY("sslg"), seconds_since_last_gexp, AJAX);
1155 WebcitAddUrlHandler(HKEY("ajax_servcmd"), ajax_servcmd, 0);
1157 RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
1158 RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
1159 RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
1160 RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, CTX_NONE);