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";
22 StrBuf *csslocal = NULL;
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(WCC->ImportantMessage)) {
172 wprintf("<div id=\"important_message\">\n"
173 "<span class=\"imsg\">");
174 StrEscAppend(WCC->WBuf, NULL, WCC->ImportantMessage, 0, 0);
175 wprintf("</span><br />\n"
178 StrBufAppendBufPlain(WCC->trailing_javascript,
179 HKEY("setTimeout('hide_imsg_popup()', 5000); \n"),
182 WCC->ImportantMessage[0] = 0;
184 else if (StrLength(WCC->ImportantMsg) > 0) {
185 wprintf("<div id=\"important_message\">\n"
186 "<span class=\"imsg\">");
187 StrEscAppend(WCC->WBuf, WCC->ImportantMsg, NULL, 0, 0);
188 wprintf("</span><br />\n"
191 StrBufAppendBufPlain(WCC->trailing_javascript,
192 HKEY("setTimeout('hide_imsg_popup()', 5000); \n"),
195 FlushStrBuf(WCC->ImportantMsg);
197 if ( (WCC->logged_in) && (!unset_cookies) ) {
198 /*DoTemplate(HKEY("iconbar"), NULL, &NoCtx);*/
202 if (do_room_banner == 1) {
203 wprintf("<div id=\"banner\">\n");
204 embed_room_banner(NULL, navbar_default);
209 if (do_room_banner == 1) {
210 wprintf("<div id=\"content\">\n");
214 void output_custom_content_header(const char *ctype) {
215 hprintf("HTTP/1.1 200 OK\r\n");
216 hprintf("Content-type: %s; charset=utf-8\r\n",ctype);
217 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
218 hprintf("Connection: close\r\n");
223 * Generic function to do an HTTP redirect. Easy and fun.
225 void http_redirect(const char *whichpage) {
226 hprintf("HTTP/1.1 302 Moved Temporarily\n");
227 hprintf("Location: %s\r\n", whichpage);
228 hprintf("URI: %s\r\n", whichpage);
229 hprintf("Content-type: text/html; charset=utf-8\r\n");
230 wprintf("<html><body>");
231 wprintf("Go <a href=\"%s\">here</A>.", whichpage);
232 wprintf("</body></html>\n");
239 * Output a piece of content to the web browser using conformant HTTP and MIME semantics
241 void http_transmit_thing(const char *content_type,
245 lprintf(9, "http_transmit_thing(%s)%s\n",
247 (is_static ? " (static)" : "")
250 output_headers(0, 0, 0, 0, 0, is_static);
252 hprintf("Content-type: %s\r\n"
254 "Connection: close\r\n",
262 * print menu box like used in the floor view or admin interface.
263 * This function takes pair of strings as va_args,
264 * Title Title string of the box
265 * Class CSS Class for the box
266 * nLines How many string pairs should we print? (URL, UrlText)
267 * ... Pairs of URL Strings and their Names
269 void print_menu_box(char* Title, char *Class, int nLines, ...)
274 svput("BOXTITLE", WCS_STRING, Title);
275 do_template("beginboxx", NULL);
277 wprintf("<ul class=\"%s\">", Class);
279 va_start(arg_list, nLines);
280 for (i = 0; i < nLines; ++i)
282 wprintf("<li><a href=\"%s\">", va_arg(arg_list, char *));
283 wprintf((char *) va_arg(arg_list, char *));
284 wprintf("</a></li>\n");
288 wprintf("</a></li>\n");
292 do_template("endbox", NULL);
297 * dump out static pages from disk
299 void output_static(const char *what)
305 const char *content_type;
309 fd = open(what, O_RDONLY);
311 lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
312 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
313 hprintf("Content-Type: text/plain\r\n");
314 wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
318 content_type = GuessMimeByFilename(what, len);
320 if (fstat(fd, &statbuf) == -1) {
321 lprintf(9, "output_static('%s') -- FSTAT FAILED --\n", what);
322 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
323 hprintf("Content-Type: text/plain\r\n");
324 wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
330 bytes = statbuf.st_size;
332 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
334 if (fd > 0) close(fd);
335 lprintf(9, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
336 hprintf("HTTP/1.1 500 internal server error \r\n");
337 hprintf("Content-Type: text/plain\r\n");
345 lprintf(9, "output_static('%s') %s\n", what, content_type);
347 http_transmit_thing(content_type, 1);
349 if (yesbstr("force_close_session")) {
350 end_webcit_session();
356 * Convenience functions to display a page containing only a string
358 * titlebarcolor color of the titlebar of the frame
359 * titlebarmsg text to display in the title bar
360 * messagetext body of the box
362 void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext)
364 hprintf("HTTP/1.1 200 OK\n");
365 output_headers(1, 1, 2, 0, 0, 0);
366 wprintf("<div id=\"banner\">\n");
367 wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
368 wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
369 wprintf("</td></tr></table>\n");
370 wprintf("</div>\n<div id=\"content\">\n");
371 escputs(messagetext);
379 * Display a blank page.
381 void blank_page(void) {
382 output_headers(1, 1, 0, 0, 0, 0);
388 * A template has been requested
390 void url_do_template(void) {
391 const StrBuf *MimeType;
392 const StrBuf *Tmpl = sbstr("template");
394 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
395 http_transmit_thing(ChrPtr(MimeType), 0);
401 * convenience function to indicate success
403 void display_success(char *successmessage)
405 convenience_page("007700", "OK", successmessage);
410 * Authorization required page
411 * This is probably temporary and should be revisited
413 void authorization_required(const char *message)
415 hprintf("HTTP/1.1 401 Authorization Required\r\n");
416 hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", ChrPtr(WC->serv_info->serv_humannode));
417 hprintf("Content-Type: text/html\r\n");
419 wprintf(_("Authorization Required"));
420 wprintf("</h1>\r\n");
421 wprintf(_("The resource you requested requires a valid username and password. "
422 "You could not be logged in: %s\n"), message);
428 * Convenience functions to wrap around asynchronous ajax responses
430 void begin_ajax_response(void) {
433 FlushStrBuf(WCC->HBuf);
434 output_headers(0, 0, 0, 0, 0, 0);
436 hprintf("Content-type: text/html; charset=UTF-8\r\n"
438 "Connection: close\r\n"
445 * print ajax response footer
447 void end_ajax_response(void) {
452 * Wraps a Citadel server command in an AJAX transaction.
454 void ajax_servcmd(void)
462 begin_ajax_response();
464 serv_puts(bstr("g_cmd"));
465 StrBuf_ServGetln(Buf);
466 StrBufAppendBuf(WCC->WBuf, Buf, 0);
467 StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
469 switch (GetServerStatus(Buf, NULL)) {
471 serv_puts("\n\n000");
472 if ( (StrLength(Buf)==3) &&
473 !strcmp(ChrPtr(Buf), "000")) {
474 StrBufAppendBufPlain(WCC->WBuf, HKEY("\000"), 0);
479 StrBuf_ServGetln(Buf);
480 if ( (StrLength(Buf)==3) &&
481 !strcmp(ChrPtr(Buf), "000")) {
484 StrBufAppendBuf(WCC->WBuf, Buf, 0);
485 StrBufAppendBufPlain(WCC->WBuf, HKEY("\n"), 0);
489 text_to_server(bstr("g_input"));
493 len = atol(&ChrPtr(Buf)[4]);
494 StrBuf_ServGetBLOBBuffered(Buf, len);
497 len = atol(&ChrPtr(Buf)[4]);
499 memset(junk, 0, len);
500 serv_write(junk, len);
507 * This is kind of an ugly hack, but this is the only place it can go.
508 * If the command was GEXP, then the instant messenger window must be
509 * running, so reset the "last_pager_check" watchdog timer so
510 * that page_popup() doesn't try to open it a second time.
512 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
513 WCC->last_pager_check = time(NULL);
520 * Helper function for the asynchronous check to see if we need
521 * to open the instant messenger window.
523 void seconds_since_last_gexp(void)
527 if ( (time(NULL) - WC->last_pager_check) < 30) {
533 serv_getln(buf, sizeof buf);
544 * \brief Detects a 'mobile' user agent
546 int is_mobile_ua(char *user_agent) {
547 if (strstr(user_agent,"iPhone OS") != NULL) {
549 } else if (strstr(user_agent,"Windows CE") != NULL) {
551 } else if (strstr(user_agent,"SymbianOS") != NULL) {
553 } else if (strstr(user_agent, "Opera Mobi") != NULL) {
555 } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
556 /* For some reason a new install of Opera 9.51beta decided to spoof. */
564 * Entry point for WebCit transaction
566 void session_loop(StrBuf *ReqLine,
567 StrBuf *request_method,
572 const char *pch, *pchs, *pche;
579 int a, nBackDots, nEmpty;
580 int ContentLength = 0;
581 StrBuf *ContentType = NULL;
582 StrBuf *UrlLine = NULL;
583 StrBuf *content = NULL;
584 const char *content_end = NULL;
585 StrBuf *browser_host = NULL;
586 char user_agent[256];
592 WebcitHandler *Handler;
593 struct timeval tx_start;
594 struct timeval tx_finish;
597 * We stuff these with the values coming from the client cookies,
598 * so we can use them to reconnect a timed out session if we have to.
603 char c_httpauth_string[SIZ];
604 StrBuf *c_httpauth_user;
605 StrBuf *c_httpauth_pass;
608 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
611 c_username = NewStrBuf();
612 c_password = NewStrBuf();
613 c_roomname = NewStrBuf();
614 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
615 c_httpauth_user = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
616 c_httpauth_pass = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
620 WCC->upload_length = 0;
623 WCC->trailing_javascript = NewStrBuf();
624 WCC->nWildfireHeaders = 0;
626 /** Figure out the action */
628 sizes[0] = sizeof action;
632 sizes[a] = sizeof arg[a-1];
636 for ( a = 0; a < 9; ++a)
638 extract_token(index[a], ChrPtr(ReqLine), a + 1, '/', sizes[a]);
639 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
640 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
641 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
642 if ((index[a][0] == '.') && (index[a][1] == '.'))
644 if (index[a][0] == '\0')
649 if (GetHash(WCC->headers, HKEY("COOKIE"), &vLine) &&
651 cookie_to_stuff((StrBuf *)vLine, NULL,
656 if (GetHash(WCC->headers, HKEY("AUTHORIZATION"), &vLine) &&
658 StrBufDecodeBase64((StrBuf*)vLine);
659 StrBufExtract_token(c_httpauth_user, (StrBuf*)vLine, 0, ':');
660 StrBufExtract_token(c_httpauth_pass, (StrBuf*)vLine, 1, ':');
662 if (GetHash(WCC->headers, HKEY("CONTENT-LENGTH"), &vLine) &&
664 ContentLength = StrToi((StrBuf*)vLine);
666 if (GetHash(WCC->headers, HKEY("CONTENT-TYPE"), &vLine) &&
668 ContentType = (StrBuf*)vLine;
670 if (GetHash(WCC->headers, HKEY("USER-AGENT"), &vLine) &&
672 safestrncpy(user_agent, ChrPtr((StrBuf*)vLine), sizeof user_agent);
674 if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {
683 GetHash(WCC->headers, HKEY("X-FORWARDED-HOST"), &vLine) &&
685 WCC->http_host = (StrBuf*)vLine;
687 if ((StrLength(WCC->http_host) == 0) &&
688 GetHash(WCC->headers, HKEY("HOST"), &vLine) &&
690 WCC->http_host = (StrBuf*)vLine;
693 if (GetHash(WCC->headers, HKEY("X-FORWARDED-FOR"), &vLine) &&
695 browser_host = (StrBuf*) vLine;
697 while (StrBufNum_tokens(browser_host, ',') > 1) {
698 StrBufRemove_token(browser_host, 0, ',');
700 StrBufTrim(browser_host);
703 if (ContentLength > 0) {
704 content = NewStrBuf();
705 StrBufPrintf(content, "Content-type: %s\n"
706 "Content-length: %d\n\n",
707 ChrPtr(ContentType), ContentLength);
709 hprintf("Content-type: %s\n"
710 "Content-length: %d\n\n",
711 ContentType, ContentLength);
713 body_start = StrLength(content);
715 /** Read the entire input data at once. */
716 client_read_to(&WCC->http_sock,
722 if (!strncasecmp(ChrPtr(ContentType), "application/x-www-form-urlencoded", 33)) {
723 StrBufCutLeft(content, body_start);
724 ParseURLParams(content);
725 } else if (!strncasecmp(ChrPtr(ContentType), "multipart", 9)) {
726 content_end = ChrPtr(content) + ContentLength + body_start;
727 mime_parser(ChrPtr(content), content_end, *upload_handler, NULL, NULL, NULL, 0);
733 /* make a note of where we are in case the user wants to save it */
734 WCC->this_page = NewStrBufDup(ReqLine);
735 StrBufRemove_token(WCC->this_page, 2, ' ');
736 StrBufRemove_token(WCC->this_page, 0, ' ');
738 /* If there are variables in the URL, we must grab them now */
739 UrlLine = NewStrBufDup(ReqLine);
740 len = StrLength(UrlLine);
741 pch = pchs = ChrPtr(UrlLine);
744 if ((*pch == '?') || (*pch == '&')) {
745 StrBufCutLeft(UrlLine, pch - pchs + 1);
746 ParseURLParams(UrlLine);
751 FreeStrBuf(&UrlLine);
753 /* If it's a "force 404" situation then display the error and bail. */
754 if (!strcmp(action, "404")) {
755 hprintf("HTTP/1.1 404 Not found\r\n");
756 hprintf("Content-Type: text/plain\r\n");
757 wprintf("Not found\r\n");
759 goto SKIP_ALL_THIS_CRAP;
762 /* Static content can be sent without connecting to Citadel. */
764 for (a=0; a<ndirs && ! is_static; ++a) {
765 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
773 snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
774 static_dirs[n_static],
775 index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
776 for (a=0; a<8; ++a) {
777 if (buf[strlen(buf)-1] == '/') {
778 buf[strlen(buf)-1] = 0;
781 for (a = 0; a < strlen(buf); ++a) {
782 if (isspace(buf[a])) {
790 lprintf(9, "Suspicious request. Ignoring.");
791 hprintf("HTTP/1.1 404 Security check failed\r\n");
792 hprintf("Content-Type: text/plain\r\n\r\n");
793 wprintf("You have sent a malformed or invalid request.\r\n");
796 goto SKIP_ALL_THIS_CRAP; /* Don't try to connect */
799 /* If the client sent a nonce that is incorrect, kill the request. */
800 if (havebstr("nonce")) {
801 lprintf(9, "Comparing supplied nonce %s to session nonce %ld\n",
802 bstr("nonce"), WCC->nonce);
803 if (ibstr("nonce") != WCC->nonce) {
804 lprintf(9, "Ignoring request with mismatched nonce.\n");
805 hprintf("HTTP/1.1 404 Security check failed\r\n");
806 hprintf("Content-Type: text/plain\r\n\r\n");
807 wprintf("Security check failed.\r\n");
809 goto SKIP_ALL_THIS_CRAP;
814 * If we're not connected to a Citadel server, try to hook up the
817 if (!WCC->connected) {
818 if (WCC->ReadBuf == NULL)
819 WCC->ReadBuf = NewStrBuf();
820 if (!strcasecmp(ctdlhost, "uds")) {
821 /* unix domain socket */
822 snprintf(buf, SIZ, "%s/citadel.socket", ctdlport);
823 WCC->serv_sock = uds_connectsock(buf);
827 WCC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
830 if (WCC->serv_sock < 0) {
832 FreeStrBuf(&WCC->ReadBuf);
833 goto SKIP_ALL_THIS_CRAP;
837 serv_getln(buf, sizeof buf); /* get the server greeting */
839 /* Are there too many users already logged in? */
840 if (!strncmp(buf, "571", 3)) {
841 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."));
843 end_webcit_session();
844 goto SKIP_ALL_THIS_CRAP;
848 * From what host is our user connecting? Go with
849 * the host at the other end of the HTTP socket,
850 * unless we are following X-Forwarded-For: headers
851 * and such a header has already turned up something.
853 if ( (!follow_xff) || (StrLength(browser_host) == 0) ) {
854 if (browser_host == NULL) {
855 browser_host = NewStrBuf();
856 Put(WCC->headers, HKEY("FreeMeWithTheOtherHeaders"),
857 browser_host, HFreeStrBuf);
859 locate_host(browser_host, WCC->http_sock);
861 if (WCC->serv_info == NULL)
862 WCC->serv_info = get_serv_info(browser_host, user_agent);
863 if (WCC->serv_info == NULL){
865 wprintf(_("Received unexpected answer from Citadel "
866 "server; bailing out."));
867 hprintf("HTTP/1.1 200 OK\r\n");
868 hprintf("Content-type: text/plain; charset=utf-8\r\n");
870 end_webcit_session();
871 goto SKIP_ALL_THIS_CRAP;
873 if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
875 wprintf(_("You are connected to a Citadel "
876 "server running Citadel %d.%02d. \n"
877 "In order to run this version of WebCit "
878 "you must also have Citadel %d.%02d or"
880 WCC->serv_info->serv_rev_level / 100,
881 WCC->serv_info->serv_rev_level % 100,
882 MINIMUM_CIT_VERSION / 100,
883 MINIMUM_CIT_VERSION % 100
885 hprintf("HTTP/1.1 200 OK\r\n");
886 hprintf("Content-type: text/plain; charset=utf-8\r\n");
888 end_webcit_session();
889 goto SKIP_ALL_THIS_CRAP;
895 * Functions which can be performed without logging in
897 if (!strcasecmp(action, "listsub")) {
899 goto SKIP_ALL_THIS_CRAP;
901 if (!strcasecmp(action, "freebusy")) {
902 do_freebusy(ChrPtr(ReqLine));
903 goto SKIP_ALL_THIS_CRAP;
907 * If we're not logged in, but we have HTTP Authentication data,
908 * try logging in to Citadel using that.
910 if ((!WCC->logged_in)
911 && (StrLength(c_httpauth_user) > 0)
912 && (StrLength(c_httpauth_pass) > 0))
915 serv_printf("USER %s", ChrPtr(c_httpauth_user));
916 StrBuf_ServGetln(Buf);
917 if (GetServerStatus(Buf, NULL) == 3) {
918 serv_printf("PASS %s", ChrPtr(c_httpauth_pass));
919 StrBuf_ServGetln(Buf);
920 if (GetServerStatus(Buf, NULL) == 2) {
921 become_logged_in(c_httpauth_user,
922 c_httpauth_pass, Buf);
923 if (WCC->httpauth_user == NULL)
924 WCC->httpauth_user = NewStrBufDup(c_httpauth_user);
926 FlushStrBuf(WCC->httpauth_user);
927 StrBufAppendBuf(WCC->httpauth_user, c_httpauth_user, 0);
929 if (WCC->httpauth_pass == NULL)
930 WCC->httpauth_pass = NewStrBufDup(c_httpauth_pass);
932 FlushStrBuf(WCC->httpauth_pass);
933 StrBufAppendBuf(WCC->httpauth_pass, c_httpauth_pass, 0);
936 /* Should only display when password is wrong */
937 authorization_required(&buf[4]);
939 goto SKIP_ALL_THIS_CRAP;
944 /* This needs to run early */
946 if (!strcasecmp(action, "rss")) {
947 display_rss(sbstr("room"), request_method);
948 goto SKIP_ALL_THIS_CRAP;
953 * The GroupDAV stuff relies on HTTP authentication instead of
954 * our session's authentication.
956 if (!strncasecmp(action, "groupdav", 8)) {
957 groupdav_main(WCC->headers,
958 ReqLine, request_method,
959 ContentType, /* do GroupDAV methods */
960 ContentLength, content, body_start);
961 if (!WCC->logged_in) {
962 WCC->killthis = 1; /* If not logged in, don't */
963 } /* keep the session active */
964 goto SKIP_ALL_THIS_CRAP;
969 * Automatically send requests with any method other than GET or
970 * POST to the GroupDAV code as well.
972 if ((strcasecmp(ChrPtr(request_method), "GET")) && (strcasecmp(ChrPtr(request_method), "POST"))) {
973 groupdav_main(WCC->headers, ReqLine,
974 request_method, ContentType, /** do GroupDAV methods */
975 ContentLength, content, body_start);
976 if (!WCC->logged_in) {
977 WCC->killthis = 1; /** If not logged in, don't */
978 } /** keep the session active */
979 goto SKIP_ALL_THIS_CRAP;
983 * If we're not logged in, but we have username and password cookies
984 * supplied by the browser, try using them to log in.
986 if ((!WCC->logged_in)
987 && (StrLength(c_username)>0)
988 && (StrLength(c_password)>0)) {
989 serv_printf("USER %s", ChrPtr(c_username));
990 StrBuf_ServGetln(Buf);
991 if (GetServerStatus(Buf, NULL) == 3) {
992 serv_printf("PASS %s", ChrPtr(c_password));
993 StrBuf_ServGetln(Buf);
994 if (GetServerStatus(Buf, NULL) == 2) {
995 become_logged_in(c_username, c_password, Buf);
996 get_preference("default_header_charset", &WCC->DefaultCharset);
1002 * If a 'gotofirst' parameter has been specified, attempt to goto that room
1003 * prior to doing anything else.
1005 if (havebstr("gotofirst")) {
1006 gotoroom(sbstr("gotofirst")); /* do this quietly to avoid session output! */
1010 * If we don't have a current room, but a cookie specifying the
1011 * current room is supplied, make an effort to go there.
1013 if ((StrLength(WCC->wc_roomname) == 0) && (StrLength(c_roomname) > 0)) {
1014 serv_printf("GOTO %s", ChrPtr(c_roomname));
1015 StrBuf_ServGetln(Buf);
1016 if (GetServerStatus(Buf, NULL) == 2) {
1017 if (WCC->wc_roomname == NULL) {
1018 WCC->wc_roomname = NewStrBufDup(c_roomname);
1021 FlushStrBuf(WCC->wc_roomname);
1022 StrBufAppendBuf(WCC->wc_roomname, c_roomname, 0);
1027 GetHash(HandlerHash, action, strlen(action) /* TODO*/, &vHandler),
1028 Handler = (WebcitHandler*) vHandler;
1029 if (Handler != NULL) {
1030 if (!WCC->logged_in && ((Handler->Flags & ANONYMOUS) == 0)) {
1031 display_login(NULL);
1034 if((Handler->Flags & NEED_URL)) {
1035 if (WCC->UrlFragment1 == NULL)
1036 WCC->UrlFragment1 = NewStrBuf();
1037 if (WCC->UrlFragment2 == NULL)
1038 WCC->UrlFragment2 = NewStrBuf();
1039 if (WCC->UrlFragment3 == NULL)
1040 WCC->UrlFragment3 = NewStrBuf();
1041 if (WCC->UrlFragment4 == NULL)
1042 WCC->UrlFragment4 = NewStrBuf();
1043 StrBufPlain(WCC->UrlFragment1, index[0], -1);
1044 StrBufPlain(WCC->UrlFragment2, index[1], -1);
1045 StrBufPlain(WCC->UrlFragment3, index[2], -1);
1046 StrBufPlain(WCC->UrlFragment4, index[3], -1);
1048 if ((Handler->Flags & AJAX) != 0)
1049 begin_ajax_response();
1051 if ((Handler->Flags & AJAX) != 0)
1052 end_ajax_response();
1055 /* When all else fais, display the main menu. */
1057 if (!WCC->logged_in)
1058 display_login(NULL);
1060 display_main_menu();
1064 if (WCC->SavePrefsToServer) {
1066 WCC->SavePrefsToServer = 0;
1069 FreeStrBuf(&c_username);
1070 FreeStrBuf(&c_password);
1071 FreeStrBuf(&c_roomname);
1072 FreeStrBuf(&c_httpauth_user);
1073 FreeStrBuf(&c_httpauth_pass);
1074 FreeStrBuf(&WCC->this_page);
1076 if (content != NULL) {
1077 FreeStrBuf(&content);
1080 DeleteHash(&WCC->urlstrings);
1081 if (WCC->upload_length > 0) {
1083 WCC->upload_length = 0;
1085 FreeStrBuf(&WCC->trailing_javascript);
1086 WCC->http_host = NULL;
1088 /* How long did this transaction take? */
1089 gettimeofday(&tx_finish, NULL);
1091 lprintf(9, "Transaction completed in %ld.%06ld seconds.\n",
1092 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
1093 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
1099 * Replacement for sleep() that uses select() in order to avoid SIGALRM
1101 void sleeeeeeeeeep(int seconds)
1105 tv.tv_sec = seconds;
1107 select(0, NULL, NULL, NULL, &tv);
1111 int ConditionalImportantMesage(StrBuf *Target, WCTemplputParams *TP)
1113 wcsession *WCC = WC;
1115 return ((!IsEmptyStr(WCC->ImportantMessage)) ||
1116 (StrLength(WCC->ImportantMsg) > 0));
1121 void tmplput_importantmessage(StrBuf *Target, WCTemplputParams *TP)
1123 wcsession *WCC = WC;
1126 if (!IsEmptyStr(WCC->ImportantMessage)) {
1127 StrEscAppend(Target, NULL, WCC->ImportantMessage, 0, 0);
1128 WCC->ImportantMessage[0] = '\0';
1130 else if (StrLength(WCC->ImportantMsg) > 0) {
1131 StrEscAppend(Target, WCC->ImportantMsg, NULL, 0, 0);
1132 FlushStrBuf(WCC->ImportantMsg);
1137 void tmplput_trailing_javascript(StrBuf *Target, WCTemplputParams *TP)
1139 wcsession *WCC = WC;
1142 StrBufAppendTemplate(Target, TP, WCC->trailing_javascript, 0);
1145 void tmplput_csslocal(StrBuf *Target, WCTemplputParams *TP)
1147 StrBufAppendBuf(Target,
1151 extern char static_local_dir[PATH_MAX];
1158 WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS);
1159 WebcitAddUrlHandler(HKEY("do_template"), url_do_template, ANONYMOUS);
1160 WebcitAddUrlHandler(HKEY("sslg"), seconds_since_last_gexp, AJAX);
1161 WebcitAddUrlHandler(HKEY("ajax_servcmd"), ajax_servcmd, 0);
1163 RegisterConditional(HKEY("COND:IMPMSG"), 0, ConditionalImportantMesage, CTX_NONE);
1164 RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, CTX_NONE);
1165 RegisterNamespace("IMPORTANTMESSAGE", 0, 0, tmplput_importantmessage, CTX_NONE);
1166 RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, CTX_NONE);
1168 snprintf(dir, SIZ, "%s/static.local/webcit.css", static_local_dir);
1169 if (!access(dir, R_OK)) {
1170 lprintf(9, "Using local Stylesheet [%s]\n", dir);
1171 csslocal = NewStrBufPlain(HKEY("<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"));
1174 lprintf(9, "Didn't find site local Stylesheet [%s]\n", dir);
1179 ServerStartModule_WEBCIT
1182 HandlerHash = NewHash(1, NULL);
1187 ServerShutdownModule_WEBCIT
1190 FreeStrBuf(&csslocal);
1191 DeleteHash(&HandlerHash);
1197 SessionNewModule_WEBCIT
1200 sess->ImportantMsg = NewStrBuf();
1201 sess->WBuf = NewStrBuf();
1202 sess->HBuf = NewStrBuf();
1206 SessionDetachModule_WEBCIT
1209 if (StrLength(sess->WBuf) > SIZ * 30) /* Bigger than 120K? release. */
1211 FreeStrBuf(&sess->WBuf);
1212 sess->WBuf = NewStrBuf();
1215 FlushStrBuf(sess->WBuf);
1216 FlushStrBuf(sess->HBuf);
1220 SessionDestroyModule_WEBCIT
1223 FreeStrBuf(&sess->WBuf);
1224 FreeStrBuf(&sess->HBuf);
1225 FreeStrBuf(&sess->UrlFragment1);
1226 FreeStrBuf(&sess->UrlFragment2);
1227 FreeStrBuf(&sess->UrlFragment3);
1228 FreeStrBuf(&sess->UrlFragment4);
1229 FreeStrBuf(&sess->ImportantMsg);