1 // This is the main transaction loop of the web service. It maintains a
2 // persistent session to the Citadel server, handling HTTP WebCit requests as
3 // they arrive and presenting a user interface.
5 // Copyright (c) 1996-2021 by the citadel.org team
7 // This program is open source software. You can redistribute it and/or
8 // modify it under the terms of the GNU General Public License, version 3.
11 #define SHOW_ME_VAPPEND_PRINTF
16 #include "webserver.h"
18 StrBuf *csslocal = NULL;
19 HashList *HandlerHash = NULL;
21 void stuff_to_cookie(int unset_cookie);
22 extern int GetConnected(void);
25 void PutRequestLocalMem(void *Data, DeleteHashDataFunc DeleteIt) {
28 n = GetCount(WC->Hdr->HTTPHeaders);
29 Put(WC->Hdr->HTTPHeaders, IKEY(n), Data, DeleteIt);
32 void DeleteWebcitHandler(void *vHandler) {
33 WebcitHandler *Handler = (WebcitHandler *) vHandler;
34 FreeStrBuf(&Handler->Name);
35 FreeStrBuf(&Handler->DisplayName);
39 void WebcitAddUrlHandler(const char *UrlString, long UrlSLen, const char *DisplayName, long dslen, WebcitHandlerFunc F, long Flags) {
40 WebcitHandler *NewHandler;
41 NewHandler = (WebcitHandler *) malloc(sizeof(WebcitHandler));
43 NewHandler->Flags = Flags;
44 NewHandler->Name = NewStrBufPlain(UrlString, UrlSLen);
45 StrBufShrinkToFit(NewHandler->Name, 1);
46 NewHandler->DisplayName = NewStrBufPlain(DisplayName, dslen);
47 StrBufShrinkToFit(NewHandler->DisplayName, 1);
48 Put(HandlerHash, UrlString, UrlSLen, NewHandler, DeleteWebcitHandler);
51 void tmplput_HANDLER_DISPLAYNAME(StrBuf * Target, WCTemplputParams * TP) {
52 if (WC->Hdr->HR.Handler != NULL)
53 StrBufAppendTemplate(Target, TP, WC->Hdr->HR.Handler->DisplayName, 0);
58 * web-printing funcion. uses our vsnprintf wrapper
60 #ifdef UBER_VERBOSE_DEBUGGING
61 void wcc_printf(const char *FILE, const char *FUNCTION, long LINE, const char *format, ...)
63 void wc_printf(const char *format, ...)
69 WC->WBuf = NewStrBuf();
70 #ifdef UBER_VERBOSE_DEBUGGING
71 StrBufAppendPrintf(WC->WBuf, "\n%s:%s:%d[", FILE, FUNCTION, LINE);
74 va_start(arg_ptr, format);
75 StrBufVAppendPrintf(WC->WBuf, format, arg_ptr);
77 #ifdef UBER_VERBOSE_DEBUGGING
78 StrBufAppendPrintf(WC->WBuf, "]\n");
83 * http-header-printing funcion. uses our vsnprintf wrapper
85 void hprintf(const char *format, ...) {
88 va_start(arg_ptr, format);
89 StrBufVAppendPrintf(WC->HBuf, format, arg_ptr);
96 * wrap up an HTTP session, closes tags, etc.
98 * print_standard_html_footer should be set to:
99 * 0 - to transmit only,
100 * nonzero - to append the closing tags
102 void wDumpContent(int print_standard_html_footer) {
103 if (print_standard_html_footer) {
104 wc_printf("</div> <!-- end of 'content' div -->\n");
105 do_template("trailing");
108 /* If we've been saving it all up for one big output burst,
109 * go ahead and do that now.
118 * Output HTTP headers and leading HTML for a page
120 void output_headers(int do_httpheaders, /* 1 = output HTTP headers */
121 int do_htmlhead, /* 1 = output HTML <head> section and <body> opener */
122 int do_room_banner, /* 1 = include the room banner and <div id="content"></div> */
123 int unset_cookies, /* 1 = session is terminating, so unset the cookies */
124 int suppress_check, /* 1 = suppress check for instant messages */
125 int cache /* 1 = allow browser to cache this page */
130 hprintf("HTTP/2.2 500 Internal Server Error");
131 else if (WC->Hdr->HaveRange > 1)
132 hprintf("HTTP/1.1 206 Partial Content\r\n");
134 hprintf("HTTP/1.1 200 OK\r\n");
136 http_datestring(httpnow, sizeof httpnow, time(NULL));
138 if (do_httpheaders) {
139 if (WC->serv_info != NULL)
140 hprintf("Content-type: text/html; charset=utf-8\r\n"
141 "Server: %s / %s\n" "Connection: close\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
143 hprintf("Content-type: text/html; charset=utf-8\r\n"
144 "Server: %s / [n/a]\n" "Connection: close\r\n", PACKAGE_STRING);
148 char httpTomorow[128];
150 http_datestring(httpTomorow, sizeof httpTomorow, time(NULL) + 60 * 60 * 24 * 2);
152 hprintf("Pragma: public\r\n"
153 "Cache-Control: max-age=3600, must-revalidate\r\n"
154 "Last-modified: %s\r\n" "Expires: %s\r\n", httpnow, httpTomorow);
157 hprintf("Pragma: no-cache\r\n" "Cache-Control: no-store\r\n" "Expires: -1\r\n");
161 stuff_to_cookie(unset_cookies);
166 if ((WC->logged_in) && (!unset_cookies)) {
167 DoTemplate(HKEY("paging"), NULL, &NoCtx);
169 if (do_room_banner) {
170 tmplput_roombanner(NULL, NULL);
174 if (do_room_banner) {
175 wc_printf("<div id=\"content\">\n");
179 void output_custom_content_header(const char *ctype) {
180 hprintf("HTTP/1.1 200 OK\r\n");
181 hprintf("Content-type: %s; charset=utf-8\r\n", ctype);
182 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
183 hprintf("Connection: close\r\n");
188 * Generic function to do an HTTP redirect. Easy and fun.
190 void http_redirect(const char *whichpage) {
191 hprintf("HTTP/1.1 302 Moved Temporarily\n");
192 hprintf("Location: %s\r\n", whichpage);
193 hprintf("URI: %s\r\n", whichpage);
194 hprintf("Content-type: text/html; charset=utf-8\r\n");
197 wc_printf("<html><body>");
198 wc_printf("Go <a href=\"%s\">here</A>.", whichpage);
199 wc_printf("</body></html>\n");
206 * Output a piece of content to the web browser using conformant HTTP and MIME semantics.
208 * If this function is called, it is expected that begin_burst() has already been called
209 * and some sort of content has been fed into the buffer. This function will transmit a
210 * bunch of headers to the client. end_burst() will add some headers of its own, and then
211 * transmit the buffered content to the client.
213 void http_transmit_thing(const char *content_type, int is_static) {
215 syslog(LOG_DEBUG, "http_transmit_thing(%s)%s", content_type, ((is_static > 0) ? " (static)" : ""));
216 output_headers(0, 0, 0, 0, 0, is_static);
218 hprintf("Content-type: %s\r\n" "Server: %s\r\n" "Connection: close\r\n", content_type, PACKAGE_STRING);
223 void http_transmit_headers(const char *content_type, int is_static, long is_chunked, int is_gzip) {
225 syslog(LOG_DEBUG, "http_transmit_thing(%s)%s", content_type, ((is_static > 0) ? " (static)" : ""));
226 output_headers(0, 0, 0, 0, 0, is_static);
229 hprintf("Content-encoding: gzip\r\n");
231 if (WC->Hdr->HaveRange)
232 hprintf("Accept-Ranges: bytes\r\n"
233 "Content-Range: bytes %ld-%ld/%ld\r\n", WC->Hdr->RangeStart, WC->Hdr->RangeTil, WC->Hdr->TotalBytes);
235 hprintf("Content-type: %s\r\n"
236 "Server: " PACKAGE_STRING "\r\n"
237 "%s" "Connection: close\r\n\r\n", content_type, (is_chunked) ? "Transfer-Encoding: chunked\r\n" : "");
242 * Convenience functions to display a page containing only a string
244 * titlebarcolor color of the titlebar of the frame
245 * titlebarmsg text to display in the title bar
246 * messagetext body of the box
248 void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext) {
249 hprintf("HTTP/1.1 200 OK\n");
250 output_headers(1, 1, 1, 0, 0, 0);
251 wc_printf("<div id=\"room_banner_override\">\n");
252 wc_printf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
253 wc_printf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
254 wc_printf("</td></tr></table>\n");
255 wc_printf("</div>\n<div id=\"content\">\n");
256 escputs(messagetext);
257 wc_printf("<hr />\n");
263 * Display a blank page.
265 void blank_page(void) {
266 output_headers(1, 1, 0, 0, 0, 0);
272 * A template has been requested
274 void url_do_template(void) {
275 const StrBuf *MimeType;
276 const StrBuf *Tmpl = sbstr("template");
278 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
279 http_transmit_thing(ChrPtr(MimeType), 0);
285 * convenience function to indicate success
287 void display_success(const char *successmessage) {
288 convenience_page("007700", "OK", successmessage);
293 * Authorization required page (sends a 401, causing the browser to request login credentials)
295 void authorization_required(void) {
296 const char *message = "";
298 hprintf("HTTP/1.1 401 Authorization Required\r\n");
299 hprintf("Server: %s / %s\r\n" "Connection: close\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
301 hprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n", ChrPtr(WC->serv_info->serv_humannode));
303 /* if this is a false cookie authentication, remove it to avoid endless loops. */
304 if (StrLength(WC->Hdr->HR.RawCookie) > 0)
307 hprintf("Content-Type: text/html\r\n");
310 wc_printf(_("Authorization Required"));
311 wc_printf("</h1>\r\n");
313 if (WC->ImportantMsg != NULL) {
314 message = ChrPtr(WC->ImportantMsg);
317 wc_printf(_("The resource you requested requires a valid username and password. "
318 "You could not be logged in: %s\n"), message);
324 * Convenience functions to wrap around asynchronous ajax responses
326 void begin_ajax_response(void) {
328 FlushStrBuf(WC->HBuf);
329 output_headers(0, 0, 0, 0, 0, 0);
331 hprintf("Content-type: text/html; charset=UTF-8\r\n" "Server: %s\r\n" "Connection: close\r\n", PACKAGE_STRING);
337 * print ajax response footer
339 void end_ajax_response(void) {
345 * Wraps a Citadel server command in an AJAX transaction.
347 void ajax_servcmd(void) {
354 syslog(LOG_DEBUG, "ajax_servcmd() g_cmd=\"%s\"", bstr("g_cmd"));
356 begin_ajax_response();
358 serv_puts(bstr("g_cmd"));
359 StrBuf_ServGetln(Buf);
360 StrBufAppendBuf(WC->WBuf, Buf, 0);
361 StrBufAppendBufPlain(WC->WBuf, HKEY("\n"), 0);
363 switch (GetServerStatus(Buf, NULL)) {
365 serv_puts("\n\n000");
366 if ((StrLength(Buf) == 3) && !strcmp(ChrPtr(Buf), "000")) {
367 StrBufAppendBufPlain(WC->WBuf, HKEY("\000"), 0);
372 if (StrBuf_ServGetln(Buf) < 0)
374 if ((StrLength(Buf) == 3) && !strcmp(ChrPtr(Buf), "000")) {
377 StrBufAppendBuf(WC->WBuf, Buf, 0);
378 StrBufAppendBufPlain(WC->WBuf, HKEY("\n"), 0);
382 text_to_server(bstr("g_input"));
386 len = atol(&ChrPtr(Buf)[4]);
387 StrBuf_ServGetBLOBBuffered(Buf, len);
390 len = atol(&ChrPtr(Buf)[4]);
392 memset(junk, 0, len);
393 serv_write(junk, len);
400 * This is kind of an ugly hack, but this is the only place it can go.
401 * If the command was GEXP, then the instant messenger window must be
402 * running, so reset the "last_pager_check" watchdog timer so
403 * that page_popup() doesn't try to open it a second time. TODO: page_popup isn't with us anymore.
405 if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
406 WC->last_pager_check = time(NULL);
413 * Helper function for the asynchronous check to see if we need
414 * to open the instant messenger window.
416 void seconds_since_last_gexp(void) {
419 if ((time(NULL) - WC->last_pager_check) < 30) {
425 serv_getln(buf, sizeof buf);
437 * Save a URL destination so we can go to it later
439 void push_destination(void) {
442 wc_printf("no session");
446 FreeStrBuf(&WC->PushedDestination);
447 WC->PushedDestination = NewStrBufDup(sbstr("url"));
449 syslog(LOG_DEBUG, "Push: %s", ChrPtr(WC->PushedDestination));
455 * Go to the URL saved by push_destination()
457 void pop_destination(void) {
459 * If we are in the middle of a new user signup, the server may request that
460 * we first pass through a registration screen.
462 if ((WC) && (WC->need_regi)) {
463 if ((WC->PushedDestination != NULL) && (StrLength(WC->PushedDestination) > 0)) {
464 /* Registering will take us to the My Citadel Config room, so save our place */
465 StrBufAppendBufPlain(WC->PushedDestination, HKEY("?go="), 0);
466 StrBufUrlescAppend(WC->PushedDestination, WC->CurRoom.name, NULL);
474 * Do something reasonable if we somehow ended up requesting a pop without
475 * having first done a push.
477 if ((!WC) || (WC->PushedDestination == NULL) || (StrLength(WC->PushedDestination) == 0)) {
483 * All righty then! We have a destination saved, so go there now.
486 syslog(LOG_DEBUG, "Pop: %s", ChrPtr(WC->PushedDestination));
488 http_redirect(ChrPtr(WC->PushedDestination));
492 int ReadPostData(void) {
494 int urlencoded_post = 0;
495 StrBuf *content = NULL;
497 urlencoded_post = (strncasecmp(ChrPtr(WC->Hdr->HR.ContentType), "application/x-www-form-urlencoded", 33) == 0);
499 content = NewStrBufPlain(NULL, WC->Hdr->HR.ContentLength + 256);
501 if (!urlencoded_post) {
502 StrBufPrintf(content,
504 "Content-length: %ld\n\n", ChrPtr(WC->Hdr->HR.ContentType), WC->Hdr->HR.ContentLength);
507 /* Read the entire input data at once. */
508 rc = client_read_to(WC->Hdr, content, WC->Hdr->HR.ContentLength, SLEEPING);
513 if (urlencoded_post) {
514 ParseURLParams(content);
516 else if (!strncasecmp(ChrPtr(WC->Hdr->HR.ContentType), "multipart", 9)) {
521 len = StrLength(content);
522 Buf = SmashStrBuf(&content);
524 mime_parser(Buf, BufEnd, *upload_handler, NULL, NULL, NULL, 0);
527 else if (WC->Hdr->HR.ContentLength > 0) {
528 WC->upload = content;
529 WC->upload_length = StrLength(WC->upload);
532 FreeStrBuf(&content);
537 int Conditional_REST_DEPTH(StrBuf * Target, WCTemplputParams * TP) {
541 if (WC->Hdr->HR.Handler != NULL)
543 Depth = GetTemplateTokenNumber(Target, TP, 2, 0);
544 IsDepth = GetCount(WC->Directory) + offset;
546 // LogTemplateError(Target, "bla", 1, TP, "REST_DEPTH: %ld : %ld\n", Depth, IsDepth);
549 return IsDepth > Depth;
552 return Depth == IsDepth;
558 * Entry point for WebCit transaction
560 void session_loop(void) {
565 * We stuff these with the values coming from the client cookies,
566 * so we can use them to reconnect a timed out session if we have to.
568 WC->upload_length = 0;
570 WC->Hdr->nWildfireHeaders = 0;
572 if (WC->Hdr->HR.ContentLength > 0) {
573 if (ReadPostData() < 0) {
579 WC->trailing_javascript = NewStrBuf();
581 /* Convert base64-encoded URL's back to plain text */
582 if (!strncmp(ChrPtr(WC->Hdr->this_page), "/B64", 4)) {
583 StrBufCutLeft(WC->Hdr->this_page, 4);
584 StrBufDecodeBase64(WC->Hdr->this_page);
585 http_redirect(ChrPtr(WC->Hdr->this_page));
586 goto SKIP_ALL_THIS_CRAP;
589 /* If there are variables in the URL, we must grab them now */
590 if (WC->Hdr->PlainArgs != NULL) {
591 ParseURLParams(WC->Hdr->PlainArgs);
594 /* If the client sent a nonce that is incorrect, kill the request. */
595 if (havebstr("nonce")) {
597 syslog(LOG_DEBUG, "Comparing supplied nonce %s to session nonce %d", bstr("nonce"), WC->nonce);
599 if (ibstr("nonce") != WC->nonce) {
600 syslog(LOG_INFO, "Ignoring request with mismatched nonce.");
601 hprintf("HTTP/1.1 404 Security check failed\r\n");
602 hprintf("Content-Type: text/plain\r\n");
604 wc_printf("Security check failed.\r\n");
606 goto SKIP_ALL_THIS_CRAP;
611 * If we're not connected to a Citadel server, try to hook up the connection now.
613 if (!WC->connected) {
614 if (GetConnected()) {
615 hprintf("HTTP/1.1 503 Service Unavailable\r\n");
616 hprintf("Content-Type: text/html\r\n");
618 wc_printf("<html><head><title>503 Service Unavailable</title></head><body>\n");
619 wc_printf(_("This program was unable to connect or stay "
620 "connected to the Citadel server. Please report " "this problem to your system administrator.")
623 wc_printf("<a href=\"http://www.citadel.org/doku.php/"
624 "faq:generalquestions:webcit_unable_to_connect\">%s</a>", _("Read More...")
626 wc_printf("</body></html>\n");
628 goto SKIP_ALL_THIS_CRAP;
633 * If we're not logged in, but we have authentication data (either from
634 * a cookie or from http-auth), try logging in to Citadel using that.
637 && (StrLength(WC->Hdr->c_username) > 0)
638 && (StrLength(WC->Hdr->c_password) > 0)
643 serv_printf("USER %s", ChrPtr(WC->Hdr->c_username));
644 StrBuf_ServGetln(Buf);
645 if (GetServerStatus(Buf, &Status) == 3) {
646 serv_printf("PASS %s", ChrPtr(WC->Hdr->c_password));
647 StrBuf_ServGetln(Buf);
648 if (GetServerStatus(Buf, NULL) == 2) {
649 become_logged_in(WC->Hdr->c_username, WC->Hdr->c_password, Buf);
652 /* Should only display when password is wrong */
653 WC->ImportantMsg = NewStrBufPlain(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
654 authorization_required();
656 goto SKIP_ALL_THIS_CRAP;
659 else if (Status == 541) {
664 xhttp = (WC->Hdr->HR.eReqType != eGET) && (WC->Hdr->HR.eReqType != ePOST) && (WC->Hdr->HR.eReqType != eHEAD);
667 * If a 'go' (or 'gotofirst') parameter has been specified, attempt to goto that room
668 * prior to doing anything else.
670 if (havebstr("go")) {
673 syslog(LOG_DEBUG, "Explicit room selection: %s", bstr("go"));
674 ret = gotoroom(sbstr("go")); /* do quietly to avoid session output! */
675 if ((ret / 100) != 2) {
677 syslog(LOG_DEBUG, "Unable to change to [%s]; Reason: %d", bstr("go"), ret);
680 else if (havebstr("gotofirst")) {
683 syslog(LOG_DEBUG, "Explicit room selection: %s", bstr("gotofirst"));
684 ret = gotoroom(sbstr("gotofirst")); /* do quietly to avoid session output! */
685 if ((ret / 100) != 2) {
686 syslog(LOG_INFO, "Unable to change to [%s]; Reason: %d", bstr("gotofirst"), ret);
691 * If we aren't in any room yet, but we have cookie data telling us where we're
692 * supposed to be, and 'go' was not specified, then go there.
694 else if ((StrLength(WC->CurRoom.name) == 0) && ((StrLength(WC->Hdr->c_roomname) > 0))) {
698 syslog(LOG_DEBUG, "We are in '%s' but cookie indicates '%s', going there...",
699 ChrPtr(WC->CurRoom.name), ChrPtr(WC->Hdr->c_roomname)
701 ret = gotoroom(WC->Hdr->c_roomname); /* do quietly to avoid session output! */
702 if ((ret / 100) != 2) {
704 syslog(LOG_DEBUG, "COOKIEGOTO: Unable to change to [%s]; Reason: %d",
705 ChrPtr(WC->Hdr->c_roomname), ret);
709 if (WC->Hdr->HR.Handler != NULL) {
711 && ((WC->Hdr->HR.Handler->Flags & ANONYMOUS) == 0)
712 && (WC->serv_info != NULL)
713 && (WC->serv_info->serv_supports_guest == 0)
718 if ((WC->Hdr->HR.Handler->Flags & AJAX) != 0) {
719 begin_ajax_response();
721 WC->Hdr->HR.Handler->F();
722 if ((WC->Hdr->HR.Handler->Flags & AJAX) != 0) {
727 /* When all else fails, display the default landing page or a main menu. */
730 * ordinary browser users get a nice login screen, DAV etc. requsets
731 * are given a 401 so they can handle it appropriate.
733 if (!WC->logged_in) {
735 authorization_required();
738 display_default_landing_page();
742 * Toplevel dav requests? or just a flat browser request?
762 * Display the appropriate landing page for this site.
764 void display_default_landing_page(void) {
765 if (WC && WC->serv_info && WC->serv_info->serv_supports_guest) {
768 if (havebstr("go")) {
770 syslog(LOG_DEBUG, "Explicit room selection: %s", bstr("go"));
771 smart_goto(sbstr("go"));
773 else if (default_landing_page) {
774 http_redirect(default_landing_page);
777 StrBuf *teh_lobby = NewStrBufPlain(HKEY("_BASEROOM_"));
778 smart_goto(teh_lobby);
779 FreeStrBuf(&teh_lobby);
789 * Replacement for sleep() that uses select() in order to avoid SIGALRM
791 void sleeeeeeeeeep(int seconds) {
796 select(0, NULL, NULL, NULL, &tv);
799 int Conditional_IS_HTTPS(StrBuf * Target, WCTemplputParams * TP) {
800 return is_https != 0;
803 void AppendImportantMessage(const char *pch, long len) {
805 if (StrLength(WC->ImportantMsg) > 0) {
806 StrBufAppendBufPlain(WC->ImportantMsg, HKEY("\n"), 0);
809 StrBufAppendBufPlain(WC->ImportantMsg, pch, len, 0);
812 int ConditionalImportantMesage(StrBuf * Target, WCTemplputParams * TP) {
814 return (StrLength(WC->ImportantMsg) > 0);
821 void tmplput_importantmessage(StrBuf * Target, WCTemplputParams * TP) {
824 if (StrLength(WC->ImportantMsg) > 0) {
825 StrBufAppendTemplate(Target, TP, WC->ImportantMsg, 0);
830 void tmplput_trailing_javascript(StrBuf * Target, WCTemplputParams * TP) {
833 StrBufAppendTemplate(Target, TP, WC->trailing_javascript, 0);
837 void tmplput_csslocal(StrBuf * Target, WCTemplputParams * TP) {
838 StrBufAppendBuf(Target, csslocal, 0);
841 void tmplput_packagestring(StrBuf * Target, WCTemplputParams * TP) {
842 StrBufAppendBufPlain(Target, HKEY(PACKAGE_STRING), 0);
845 extern char static_local_dir[PATH_MAX];
848 void InitModule_WEBCIT(void) {
850 WebcitAddUrlHandler(HKEY("blank"), "", 0, blank_page, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC);
851 WebcitAddUrlHandler(HKEY("landing"), "", 0, display_default_landing_page, ANONYMOUS | COOKIEUNNEEDED);
852 WebcitAddUrlHandler(HKEY("do_template"), "", 0, url_do_template, ANONYMOUS);
853 WebcitAddUrlHandler(HKEY("sslg"), "", 0, seconds_since_last_gexp, AJAX | LOGCHATTY);
854 WebcitAddUrlHandler(HKEY("ajax_servcmd"), "", 0, ajax_servcmd, 0);
855 WebcitAddUrlHandler(HKEY("webcit"), "", 0, blank_page, URLNAMESPACE);
856 WebcitAddUrlHandler(HKEY("push"), "", 0, push_destination, AJAX);
857 WebcitAddUrlHandler(HKEY("pop"), "", 0, pop_destination, 0);
859 WebcitAddUrlHandler(HKEY("401"), "", 0, authorization_required, ANONYMOUS | COOKIEUNNEEDED);
860 RegisterConditional("COND:IMPMSG", 0, ConditionalImportantMesage, CTX_NONE);
861 RegisterConditional("COND:REST:DEPTH", 0, Conditional_REST_DEPTH, CTX_NONE);
862 RegisterConditional("COND:IS_HTTPS", 0, Conditional_IS_HTTPS, CTX_NONE);
864 RegisterNamespace("CSSLOCAL", 0, 0, tmplput_csslocal, NULL, CTX_NONE);
865 RegisterNamespace("IMPORTANTMESSAGE", 0, 1, tmplput_importantmessage, NULL, CTX_NONE);
866 RegisterNamespace("TRAILING_JAVASCRIPT", 0, 0, tmplput_trailing_javascript, NULL, CTX_NONE);
867 RegisterNamespace("URL:DISPLAYNAME", 0, 1, tmplput_HANDLER_DISPLAYNAME, NULL, CTX_NONE);
868 RegisterNamespace("PACKAGESTRING", 0, 1, tmplput_packagestring, NULL, CTX_NONE);
871 snprintf(dir, SIZ, "%s/webcit.css", static_local_dir);
872 if (!access(dir, R_OK)) {
873 syslog(LOG_INFO, "Using local Stylesheet [%s]", dir);
874 csslocal = NewStrBufPlain(HKEY("<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\" />"));
877 syslog(LOG_INFO, "No Site-local Stylesheet [%s] installed.", dir);
881 void ServerStartModule_WEBCIT(void) {
882 HandlerHash = NewHash(1, NULL);
886 void ServerShutdownModule_WEBCIT(void) {
887 FreeStrBuf(&csslocal);
888 DeleteHash(&HandlerHash);
893 void SessionNewModule_WEBCIT(wcsession * sess) {
894 sess->ImportantMsg = NewStrBuf();
895 sess->WBuf = NewStrBufPlain(NULL, SIZ * 4);
896 sess->HBuf = NewStrBufPlain(NULL, SIZ / 4);
899 void SessionDetachModule_WEBCIT(wcsession * sess) {
900 DeleteHash(&sess->Directory);
902 FreeStrBuf(&sess->upload);
903 sess->upload_length = 0;
905 FreeStrBuf(&sess->trailing_javascript);
907 if (StrLength(sess->WBuf) > SIZ * 30) { /* Bigger than 120K? release. */
908 FreeStrBuf(&sess->WBuf);
909 sess->WBuf = NewStrBuf();
912 FlushStrBuf(sess->WBuf);
913 FlushStrBuf(sess->HBuf);
914 if (StrLength(sess->ImportantMsg) > 0) {
915 FlushStrBuf(sess->ImportantMsg);
921 SessionDestroyModule_WEBCIT(wcsession * sess) {
922 FreeStrBuf(&sess->WBuf);
923 FreeStrBuf(&sess->HBuf);
924 FreeStrBuf(&sess->ImportantMsg);
925 FreeStrBuf(&sess->PushedDestination);