2 * This is the other half of the webserver. It handles the task of hooking
3 * up HTTP requests with the sessions they belong to, using HTTP cookies to
4 * keep track of things. If the HTTP request doesn't belong to any currently
5 * active session, a new session is started.
7 * Copyright (c) 1996-2012 by the citadel.org team
9 * This program is open source software. You can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include "webserver.h"
20 #include "modules_init.h"
22 /* Only one thread may manipulate SessionList at a time... */
23 pthread_mutex_t SessionListMutex;
25 wcsession *SessionList = NULL; /* Linked list of all webcit sessions */
27 pthread_key_t MyConKey; /* TSD key for MySession() */
28 HashList *HttpReqTypes = NULL;
29 HashList *HttpHeaderHandler = NULL;
30 extern HashList *HandlerHash;
32 /* the following two values start at 1 because the initial parent thread counts as one. */
33 int num_threads_existing = 1; /* Number of worker threads which exist. */
34 int num_threads_executing = 1; /* Number of worker threads currently executing. */
36 extern void session_loop(void);
37 void spawn_another_worker_thread(void);
40 void DestroyHttpHeaderHandler(void *V)
43 pHdr = (OneHttpHeader*) V;
44 FreeStrBuf(&pHdr->Val);
48 void shutdown_sessions(void)
52 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
57 void do_housekeeping(void)
60 wcsession *sessions_to_kill = NULL;
64 * Lock the session list, moving any candidates for euthanasia into
67 CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
68 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
70 the_time = time(NULL);
71 /* Kill idle sessions */
72 if ((sptr->inuse == 0) &&
73 ((the_time - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT))
75 syslog(3, "Timeout session %d", sptr->wc_session);
79 /* Remove sessions flagged for kill */
82 /* remove session from linked list */
83 if (sptr == SessionList) {
84 SessionList = SessionList->next;
86 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
87 if (ss->next == sptr) {
88 ss->next = ss->next->next;
92 sptr->next = sessions_to_kill;
93 sessions_to_kill = sptr;
96 CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
99 * Now free up and destroy the culled sessions.
101 while (sessions_to_kill != NULL) {
102 syslog(3, "Destroying session %d", sessions_to_kill->wc_session);
103 sptr = sessions_to_kill->next;
104 session_destroy_modules(&sessions_to_kill);
105 sessions_to_kill = sptr;
110 * Check the size of our thread pool. If all threads are executing, spawn another.
112 void check_thread_pool_size(void)
114 if (time_to_die) return; /* don't expand the thread pool during shutdown */
116 begin_critical_section(S_SPAWNER); /* only one of these should run at a time */
118 (num_threads_executing >= num_threads_existing)
119 && (num_threads_existing < MAX_WORKER_THREADS)
121 syslog(3, "%d of %d threads are executing. Adding another worker thread.",
122 num_threads_executing,
125 spawn_another_worker_thread();
127 end_critical_section(S_SPAWNER);
132 * Wake up occasionally and clean house
134 void housekeeping_loop(void)
137 sleeeeeeeeeep(HOUSEKEEPING);
144 * Create a Session id
145 * Generate a unique WebCit session ID (which is not the same thing as the
146 * Citadel session ID).
148 int GenerateSessionID(void)
150 static int seq = (-1);
153 seq = (int) time(NULL);
159 wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
161 wcsession *sptr = NULL;
162 wcsession *TheSession = NULL;
164 if (Hdr->HR.got_auth == AUTH_BASIC) {
168 CtdlLogResult(pthread_mutex_lock(ListMutex));
169 for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
171 /* If HTTP-AUTH, look for a session with matching credentials */
172 switch (Hdr->HR.got_auth)
175 if ( (!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username)))
176 && (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password)))
177 && (sptr->killthis == 0)
179 syslog(LOG_DEBUG, "Matched a session with the same http-auth");
184 /* If cookie-session, look for a session with matching session ID */
185 if ( (Hdr->HR.desired_session != 0)
186 && (sptr->wc_session == Hdr->HR.desired_session)
188 syslog(LOG_DEBUG, "Matched a session with the same cookie");
193 /* Any unbound session is a candidate */
194 if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
195 syslog(LOG_DEBUG, "Reusing an unbound session");
201 CtdlLogResult(pthread_mutex_unlock(ListMutex));
202 if (TheSession == NULL) {
203 syslog(LOG_DEBUG, "No existing session was matched");
208 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
210 wcsession *TheSession;
211 TheSession = (wcsession *) malloc(sizeof(wcsession));
212 memset(TheSession, 0, sizeof(wcsession));
213 TheSession->Hdr = Hdr;
214 TheSession->serv_sock = (-1);
216 pthread_setspecific(MyConKey, (void *)TheSession);
218 /* If we're recreating a session that expired, it's best to give it the same
219 * session number that it had before. The client browser ought to pick up
220 * the new session number and start using it, but in some rare situations it
221 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
224 if (Hdr->HR.desired_session == 0) {
225 TheSession->wc_session = GenerateSessionID();
226 syslog(3, "Created new session %d", TheSession->wc_session);
229 TheSession->wc_session = Hdr->HR.desired_session;
230 syslog(3, "Re-created session %d", TheSession->wc_session);
232 Hdr->HR.Static = Static;
233 session_new_modules(TheSession);
236 pthread_mutex_init(&TheSession->SessionMutex, NULL);
238 if (ListMutex != NULL)
239 CtdlLogResult(pthread_mutex_lock(ListMutex));
241 if (wclist != NULL) {
242 TheSession->nonce = rand();
243 TheSession->next = *wclist;
244 *wclist = TheSession;
246 if (ListMutex != NULL)
247 CtdlLogResult(pthread_mutex_unlock(ListMutex));
253 /* If it's a "force 404" situation then display the error and bail. */
256 hprintf("HTTP/1.1 404 Not found\r\n");
257 hprintf("Content-Type: text/plain\r\n");
258 wc_printf("Not found\r\n");
262 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
265 void *vLine, *vHandler;
266 const char *Pos = NULL;
268 Hdr->HR.ReqLine = Line;
269 /* The requesttype... GET, POST... */
270 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
271 if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
274 Hdr->HR.eReqType = *(long*)vLine;
277 Hdr->HR.eReqType = eGET;
280 StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
282 /* the HTTP Version... */
283 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
284 StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
286 if (StrLength(Buf) == 0) {
287 Hdr->HR.eReqType = eGET;
291 StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
293 /* chop Filename / query arguments */
294 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
295 if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
296 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
298 Args ++; /* skip the ? */
299 StrBufPlain(Hdr->PlainArgs,
301 StrLength(Hdr->HR.ReqLine) -
302 (Args - ChrPtr(Hdr->HR.ReqLine)));
303 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
304 } /* don't parse them yet, maybe we don't even care... */
306 /* now lookup what we are going to do with this... */
307 /* skip first slash */
308 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
310 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
312 GetHash(HandlerHash, SKEY(Buf), &vHandler),
313 Hdr->HR.Handler = (WebcitHandler*) vHandler;
314 if (Hdr->HR.Handler == NULL)
317 * If the request is prefixed by "/webcit" then chop that off. This
318 * allows a front end web server to forward all /webcit requests to us
319 * while still using the same web server port for other things.
321 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
325 /* remove the handlername from the URL */
326 if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
327 StrBufCutLeft(Hdr->HR.ReqLine,
328 Pos - ChrPtr(Hdr->HR.ReqLine));
331 if (Hdr->HR.Handler != NULL) {
332 if ((Hdr->HR.Handler->Flags & BOGUS) != 0) {
335 Hdr->HR.DontNeedAuth = (
336 ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
337 ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
341 /* If this is a "flat" request for the root, display the configured landing page. */
343 StrBuf *NewLine = NewStrBuf();
344 Hdr->HR.DontNeedAuth = 1;
345 StrBufAppendPrintf(NewLine, "GET /landing?go=%s HTTP/1.0", ChrPtr(Buf));
346 syslog(LOG_DEBUG, "Replacing with: %s", ChrPtr(NewLine));
347 return_value = ReadHttpSubject(Hdr, NewLine, Buf);
348 FreeStrBuf(&NewLine);
355 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
361 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
363 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
365 pHdr = (OneHttpHeader *)vHdr;
366 if (pHdr->HaveEvaluator)
367 pHdr->H(pHdr->Val, Hdr);
374 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
377 * Read in the request
379 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
381 const char *pch, *pchs, *pche;
383 StrBuf *Line, *LastLine, *HeaderName;
388 HeaderName = NewStrBuf();
392 Line = NewStrBufPlain(NULL, SIZ / 4);
394 if (ClientGetLine(Hdr, Line) < 0) return 1;
396 if (StrLength(Line) == 0) {
401 Hdr->HTTPHeaders = NewHash(1, NULL);
402 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
403 memset(pHdr, 0, sizeof(OneHttpHeader));
405 Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
406 syslog(9, "%s", ChrPtr(Line));
407 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
412 /* Do we need to Unfold? */
413 if ((LastLine != NULL) &&
414 (isspace(*ChrPtr(Line)))) {
415 pch = pchs = ChrPtr(Line);
416 pche = pchs + StrLength(Line);
417 while (isspace(*pch) && (pch < pche))
419 StrBufCutLeft(Line, pch - pchs);
420 StrBufAppendBuf(LastLine, Line, 0);
426 StrBufSanitizeAscii(Line, '§');
427 StrBufExtract_token(HeaderName, Line, 0, ':');
430 pche = pchs + StrLength(Line);
431 pch = pchs + StrLength(HeaderName) + 1;
432 pche = pchs + StrLength(Line);
433 while ((pch < pche) && isspace(*pch))
435 StrBufCutLeft(Line, pch - pchs);
437 StrBufUpCase(HeaderName);
439 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
440 memset(pHdr, 0, sizeof(OneHttpHeader));
443 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
446 OneHttpHeader *FHdr = (OneHttpHeader*) vF;
448 pHdr->HaveEvaluator = 1;
450 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
452 } while (Line != NULL);
454 FreeStrBuf(&HeaderName);
459 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
461 StrBuf *Buf = NewStrBuf();
463 if (Hdr->HR.ReqLine != NULL) {
464 FlushStrBuf(Hdr->HR.ReqLine);
465 StrBufPlain(Hdr->HR.ReqLine, Line, len);
468 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
470 ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
478 * This loop gets called once for every HTTP connection made to WebCit. At
479 * this entry point we have an HTTP socket with a browser allegedly on the
480 * other end, but we have not yet bound to a WebCit session.
482 * The job of this function is to locate the correct session and bind to it,
483 * or create a session if necessary and bind to it, then run the WebCit
484 * transaction loop. Afterwards, we unbind from the session. When this
485 * function returns, the worker thread is then free to handle another
488 void context_loop(ParsedHttpHdrs *Hdr)
491 wcsession *TheSession;
492 struct timeval tx_start;
493 struct timeval tx_finish;
494 int session_may_be_reused = 1;
496 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
499 * Find out what it is that the web browser is asking for
501 isbogus = ReadHTTPRequest(Hdr);
503 Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
506 isbogus = AnalyseHeaders(Hdr);
510 || ((Hdr->HR.Handler != NULL)
511 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
514 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
516 syslog(9, "HTTP: 404 [%ld.%06ld] %s %s",
517 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
518 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
519 ReqStrs[Hdr->HR.eReqType],
520 ChrPtr(Hdr->this_page)
522 session_detach_modules(Bogus);
523 session_destroy_modules(&Bogus);
527 if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
530 Static = CreateSession(0, 1, NULL, Hdr, NULL);
532 Hdr->HR.Handler->F();
534 /* How long did this transaction take? */
535 gettimeofday(&tx_finish, NULL);
537 syslog(9, "HTTP: 200 [%ld.%06ld] %s %s",
538 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
539 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
540 ReqStrs[Hdr->HR.eReqType],
541 ChrPtr(Hdr->this_page)
543 session_detach_modules(Static);
544 session_destroy_modules(&Static);
548 if (Hdr->HR.got_auth == AUTH_BASIC) {
552 if (Hdr->HR.got_auth) {
553 session_may_be_reused = 0;
557 * See if there's an existing session open with any of:
558 * - The desired Session ID
559 * - A matching http-auth username and password
560 * - An unbound session flagged as reusable
562 TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
565 * If there were no qualifying sessions, then create a new one.
567 if ((TheSession == NULL) || (TheSession->killthis != 0)) {
568 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
572 * Reject transactions which require http-auth, if http-auth was not provided
574 if ( (StrLength(Hdr->c_username) == 0)
575 && (!Hdr->HR.DontNeedAuth)
576 && (Hdr->HR.Handler != NULL)
577 && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
579 syslog(LOG_DEBUG, "http-auth required but not provided");
580 OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
581 Hdr->HR.prohibit_caching = 1;
585 * A future improvement might be to check the session integrity
586 * at this point before continuing.
590 * Bind to the session and perform the transaction
592 CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
593 pthread_setspecific(MyConKey, (void *)TheSession);
595 TheSession->inuse = 1; /* mark the session as bound */
596 TheSession->lastreq = time(NULL); /* log */
597 TheSession->Hdr = Hdr;
600 * If a language was requested via a cookie, select that language now.
602 if (StrLength(Hdr->c_language) > 0) {
603 syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
604 set_selected_language(ChrPtr(Hdr->c_language));
605 go_selected_language();
611 session_attach_modules(TheSession);
614 /* How long did this transaction take? */
615 gettimeofday(&tx_finish, NULL);
617 syslog(9, "HTTP: 200 [%ld.%06ld] %s %s",
618 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
619 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
620 ReqStrs[Hdr->HR.eReqType],
621 ChrPtr(Hdr->this_page)
624 session_detach_modules(TheSession);
626 /* If *this* very transaction did not explicitly specify a session cookie,
627 * and it did not log in, we want to flag the session as a candidate for
628 * re-use by the next unbound client that comes along. This keeps our session
629 * table from getting bombarded with new sessions when, for example, a web
630 * spider crawls the site without using cookies.
632 if ((session_may_be_reused) && (!WC->logged_in)) {
633 WC->wc_session = 0; /* flag as available for re-use */
634 TheSession->selected_language = 0; /* clear any non-default language setting */
637 TheSession->Hdr = NULL;
638 TheSession->inuse = 0; /* mark the session as unbound */
639 CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
642 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
645 StrBufAppendPrintf(Target, "%ld",
646 (WCC != NULL)? WCC->nonce:0);
649 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
651 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
654 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
656 hdr->HR.ContentLength = StrToi(Line);
659 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
661 hdr->HR.ContentType = Line;
665 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
667 if (hdr->HostHeader != NULL) {
668 FreeStrBuf(&hdr->HostHeader);
670 hdr->HostHeader = NewStrBuf();
671 StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
672 StrBufAppendBuf(hdr->HostHeader, Line, 0);
675 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
677 if (!follow_xff) return;
679 if (hdr->HostHeader != NULL) {
680 FreeStrBuf(&hdr->HostHeader);
683 hdr->HostHeader = NewStrBuf();
684 StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
685 StrBufAppendBuf(hdr->HostHeader, Line, 0);
689 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
691 hdr->HR.browser_host = Line;
693 while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
694 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
696 StrBufTrim(hdr->HR.browser_host);
699 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
701 hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
704 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
709 if (strstr(&ChrPtr(Line)[16], "gzip")) {
713 const char *ReqStrs[eNONE] = {
727 ServerStartModule_CONTEXT
731 HttpReqTypes = NewHash(1, NULL);
732 HttpHeaderHandler = NewHash(1, NULL);
734 v = malloc(sizeof(long));
736 Put(HttpReqTypes, HKEY("GET"), v, NULL);
738 v = malloc(sizeof(long));
740 Put(HttpReqTypes, HKEY("POST"), v, NULL);
742 v = malloc(sizeof(long));
744 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
746 v = malloc(sizeof(long));
748 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
750 v = malloc(sizeof(long));
752 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
754 v = malloc(sizeof(long));
756 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
758 v = malloc(sizeof(long));
760 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
762 v = malloc(sizeof(long));
764 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
766 v = malloc(sizeof(long));
768 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
770 v = malloc(sizeof(long));
772 Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
776 ServerShutdownModule_CONTEXT
779 DeleteHash(&HttpReqTypes);
780 DeleteHash(&HttpHeaderHandler);
783 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
786 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
787 memset(pHdr, 0, sizeof(OneHttpHeader));
789 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
797 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
798 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
799 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
800 RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
801 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
802 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
803 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
804 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
806 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
807 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
809 WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
811 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
812 * Short-circuit these requests so we don't have to send them through the full processing loop.
814 WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
815 WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS); /* /winnt */
816 WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
817 WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
818 WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
819 WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
824 HttpNewModule_CONTEXT
825 (ParsedHttpHdrs *httpreq)
827 httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
828 httpreq->this_page = NewStrBufPlain(NULL, SIZ);
832 HttpDetachModule_CONTEXT
833 (ParsedHttpHdrs *httpreq)
835 FlushStrBuf(httpreq->PlainArgs);
836 FlushStrBuf(httpreq->HostHeader);
837 FlushStrBuf(httpreq->this_page);
838 FlushStrBuf(httpreq->PlainArgs);
839 DeleteHash(&httpreq->HTTPHeaders);
840 memset(&httpreq->HR, 0, sizeof(HdrRefs));
844 HttpDestroyModule_CONTEXT
845 (ParsedHttpHdrs *httpreq)
847 FreeStrBuf(&httpreq->this_page);
848 FreeStrBuf(&httpreq->PlainArgs);
849 FreeStrBuf(&httpreq->this_page);
850 FreeStrBuf(&httpreq->PlainArgs);
851 FreeStrBuf(&httpreq->HostHeader);
852 DeleteHash(&httpreq->HTTPHeaders);