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. */
37 extern void session_loop(void);
38 void spawn_another_worker_thread(void);
41 void DestroyHttpHeaderHandler(void *V)
44 pHdr = (OneHttpHeader*) V;
45 FreeStrBuf(&pHdr->Val);
49 void shutdown_sessions(void)
53 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
58 void do_housekeeping(void)
61 wcsession *sessions_to_kill = NULL;
65 * Lock the session list, moving any candidates for euthanasia into
69 CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
70 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
72 the_time = time(NULL);
73 /* Kill idle sessions */
74 if ((sptr->inuse == 0) &&
75 ((the_time - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT))
77 syslog(LOG_DEBUG, "Timeout session %d", sptr->wc_session);
81 /* Remove sessions flagged for kill */
84 /* remove session from linked list */
85 if (sptr == SessionList) {
86 SessionList = SessionList->next;
88 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
89 if (ss->next == sptr) {
90 ss->next = ss->next->next;
94 sptr->next = sessions_to_kill;
95 sessions_to_kill = sptr;
98 CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
101 * Now free up and destroy the culled sessions.
103 while (sessions_to_kill != NULL) {
104 syslog(LOG_DEBUG, "Destroying session %d", sessions_to_kill->wc_session);
105 sptr = sessions_to_kill->next;
106 session_destroy_modules(&sessions_to_kill);
107 sessions_to_kill = sptr;
112 * Check the size of our thread pool. If all threads are executing, spawn another.
114 void check_thread_pool_size(void)
116 if (time_to_die) return; /* don't expand the thread pool during shutdown */
118 begin_critical_section(S_SPAWNER); /* only one of these should run at a time */
120 (num_threads_executing >= num_threads_existing)
121 && (num_threads_existing < MAX_WORKER_THREADS)
123 syslog(LOG_DEBUG, "%d of %d threads are executing. Adding another worker thread.",
124 num_threads_executing,
127 spawn_another_worker_thread();
129 end_critical_section(S_SPAWNER);
134 * Wake up occasionally and clean house
136 void housekeeping_loop(void)
139 sleeeeeeeeeep(HOUSEKEEPING);
146 * Create a Session id
147 * Generate a unique WebCit session ID (which is not the same thing as the
148 * Citadel session ID).
150 int GenerateSessionID(void)
152 static int seq = (-1);
155 seq = (int) time(NULL);
161 wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
163 wcsession *sptr = NULL;
164 wcsession *TheSession = NULL;
166 if (Hdr->HR.got_auth == AUTH_BASIC) {
170 CtdlLogResult(pthread_mutex_lock(ListMutex));
171 for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
173 /* If HTTP-AUTH, look for a session with matching credentials */
174 switch (Hdr->HR.got_auth)
177 if ( (!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username)))
178 && (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password)))
179 && (sptr->killthis == 0)
182 syslog(LOG_DEBUG, "Matched a session with the same http-auth");
187 /* If cookie-session, look for a session with matching session ID */
188 if ( (Hdr->HR.desired_session != 0)
189 && (sptr->wc_session == Hdr->HR.desired_session)
192 syslog(LOG_DEBUG, "Matched a session with the same cookie");
197 /* Any unbound session is a candidate */
198 if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
200 syslog(LOG_DEBUG, "Reusing an unbound session");
206 CtdlLogResult(pthread_mutex_unlock(ListMutex));
207 if (TheSession == NULL) {
208 syslog(LOG_DEBUG, "No existing session was matched");
213 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
215 wcsession *TheSession;
216 TheSession = (wcsession *) malloc(sizeof(wcsession));
217 memset(TheSession, 0, sizeof(wcsession));
218 TheSession->Hdr = Hdr;
219 TheSession->serv_sock = (-1);
220 TheSession->lastreq = time(NULL);;
222 pthread_setspecific(MyConKey, (void *)TheSession);
224 /* If we're recreating a session that expired, it's best to give it the same
225 * session number that it had before. The client browser ought to pick up
226 * the new session number and start using it, but in some rare situations it
227 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
230 if (Hdr->HR.desired_session == 0) {
231 TheSession->wc_session = GenerateSessionID();
232 syslog(LOG_DEBUG, "Created new session %d", TheSession->wc_session);
235 TheSession->wc_session = Hdr->HR.desired_session;
236 syslog(LOG_DEBUG, "Re-created session %d", TheSession->wc_session);
238 Hdr->HR.Static = Static;
239 session_new_modules(TheSession);
242 pthread_mutex_init(&TheSession->SessionMutex, NULL);
244 if (ListMutex != NULL)
245 CtdlLogResult(pthread_mutex_lock(ListMutex));
247 if (wclist != NULL) {
248 TheSession->nonce = rand();
249 TheSession->next = *wclist;
250 *wclist = TheSession;
252 if (ListMutex != NULL)
253 CtdlLogResult(pthread_mutex_unlock(ListMutex));
259 /* If it's a "force 404" situation then display the error and bail. */
262 hprintf("HTTP/1.1 404 Not found\r\n");
263 hprintf("Content-Type: text/plain\r\n");
264 wc_printf("Not found\r\n");
268 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
271 void *vLine, *vHandler;
272 const char *Pos = NULL;
274 Hdr->HR.ReqLine = Line;
275 /* The requesttype... GET, POST... */
276 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
277 if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
280 Hdr->HR.eReqType = *(long*)vLine;
283 Hdr->HR.eReqType = eGET;
286 StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
288 /* the HTTP Version... */
289 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
290 StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
292 if (StrLength(Buf) == 0) {
293 Hdr->HR.eReqType = eGET;
297 StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
299 /* chop Filename / query arguments */
300 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
301 if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
302 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
304 Args ++; /* skip the ? */
305 StrBufPlain(Hdr->PlainArgs,
307 StrLength(Hdr->HR.ReqLine) -
308 (Args - ChrPtr(Hdr->HR.ReqLine)));
309 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
310 } /* don't parse them yet, maybe we don't even care... */
312 /* now lookup what we are going to do with this... */
313 /* skip first slash */
314 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
316 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
318 GetHash(HandlerHash, SKEY(Buf), &vHandler),
319 Hdr->HR.Handler = (WebcitHandler*) vHandler;
320 if (Hdr->HR.Handler == NULL)
323 * If the request is prefixed by "/webcit" then chop that off. This
324 * allows a front end web server to forward all /webcit requests to us
325 * while still using the same web server port for other things.
327 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
331 /* remove the handlername from the URL */
332 if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
333 StrBufCutLeft(Hdr->HR.ReqLine,
334 Pos - ChrPtr(Hdr->HR.ReqLine));
337 if (Hdr->HR.Handler != NULL) {
338 if ((Hdr->HR.Handler->Flags & BOGUS) != 0) {
341 Hdr->HR.DontNeedAuth = (
342 ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
343 ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
347 /* If this is a "flat" request for the root, display the configured landing page. */
349 StrBuf *NewLine = NewStrBuf();
350 Hdr->HR.DontNeedAuth = 1;
351 StrBufAppendPrintf(NewLine, "GET /landing?go=%s?failvisibly=1 HTTP/1.0", ChrPtr(Buf));
352 if (verbose) syslog(LOG_DEBUG, "Replacing with: %s", ChrPtr(NewLine));
353 return_value = ReadHttpSubject(Hdr, NewLine, Buf);
354 FreeStrBuf(&NewLine);
361 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
367 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
369 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
371 pHdr = (OneHttpHeader *)vHdr;
372 if (pHdr->HaveEvaluator)
373 pHdr->H(pHdr->Val, Hdr);
380 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
383 * Read in the request
385 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
387 const char *pch, *pchs, *pche;
389 StrBuf *Line, *LastLine, *HeaderName;
394 HeaderName = NewStrBuf();
398 Line = NewStrBufPlain(NULL, SIZ / 4);
400 if (ClientGetLine(Hdr, Line) < 0) {
406 if (StrLength(Line) == 0) {
411 Hdr->HTTPHeaders = NewHash(1, NULL);
412 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
413 memset(pHdr, 0, sizeof(OneHttpHeader));
415 Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
416 if (verbose || strstr(ChrPtr(Line), "sslg") == NULL)
417 syslog(LOG_DEBUG, "%s", ChrPtr(Line));
418 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
423 /* Do we need to Unfold? */
424 if ((LastLine != NULL) &&
425 (isspace(*ChrPtr(Line)))) {
426 pch = pchs = ChrPtr(Line);
427 pche = pchs + StrLength(Line);
428 while (isspace(*pch) && (pch < pche))
430 StrBufCutLeft(Line, pch - pchs);
431 StrBufAppendBuf(LastLine, Line, 0);
437 StrBufSanitizeAscii(Line, (char)0xa7);
438 StrBufExtract_token(HeaderName, Line, 0, ':');
441 pche = pchs + StrLength(Line);
442 pch = pchs + StrLength(HeaderName) + 1;
443 pche = pchs + StrLength(Line);
444 while ((pch < pche) && isspace(*pch))
446 StrBufCutLeft(Line, pch - pchs);
448 StrBufUpCase(HeaderName);
450 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
451 memset(pHdr, 0, sizeof(OneHttpHeader));
454 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
457 OneHttpHeader *FHdr = (OneHttpHeader*) vF;
459 pHdr->HaveEvaluator = 1;
461 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
463 } while (Line != NULL);
465 FreeStrBuf(&HeaderName);
470 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
472 StrBuf *Buf = NewStrBuf();
474 if (Hdr->HR.ReqLine != NULL) {
475 FlushStrBuf(Hdr->HR.ReqLine);
476 StrBufPlain(Hdr->HR.ReqLine, Line, len);
479 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
481 ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
489 * This loop gets called once for every HTTP connection made to WebCit. At
490 * this entry point we have an HTTP socket with a browser allegedly on the
491 * other end, but we have not yet bound to a WebCit session.
493 * The job of this function is to locate the correct session and bind to it,
494 * or create a session if necessary and bind to it, then run the WebCit
495 * transaction loop. Afterwards, we unbind from the session. When this
496 * function returns, the worker thread is then free to handle another
499 void context_loop(ParsedHttpHdrs *Hdr)
502 wcsession *TheSession;
503 struct timeval tx_start;
504 struct timeval tx_finish;
505 int session_may_be_reused = 1;
508 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
511 * Find out what it is that the web browser is asking for
513 isbogus = ReadHTTPRequest(Hdr);
515 Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
518 isbogus = AnalyseHeaders(Hdr);
522 || ((Hdr->HR.Handler != NULL)
523 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
526 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
528 syslog(LOG_WARNING, "HTTP: 404 [%ld.%06ld] %s %s",
529 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
530 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
531 ReqStrs[Hdr->HR.eReqType],
532 ChrPtr(Hdr->this_page)
534 session_detach_modules(Bogus);
535 session_destroy_modules(&Bogus);
539 if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
542 Static = CreateSession(0, 1, NULL, Hdr, NULL);
544 Hdr->HR.Handler->F();
546 /* How long did this transaction take? */
547 gettimeofday(&tx_finish, NULL);
550 syslog(LOG_DEBUG, "HTTP: 200 [%ld.%06ld] %s %s",
551 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
552 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
553 ReqStrs[Hdr->HR.eReqType],
554 ChrPtr(Hdr->this_page)
556 session_detach_modules(Static);
557 session_destroy_modules(&Static);
561 if (Hdr->HR.got_auth == AUTH_BASIC) {
565 if (Hdr->HR.got_auth) {
566 session_may_be_reused = 0;
570 * See if there's an existing session open with any of:
571 * - The desired Session ID
572 * - A matching http-auth username and password
573 * - An unbound session flagged as reusable
575 TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
578 * If there were no qualifying sessions, then create a new one.
580 if ((TheSession == NULL) || (TheSession->killthis != 0)) {
581 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
585 * Reject transactions which require http-auth, if http-auth was not provided
587 if ( (StrLength(Hdr->c_username) == 0)
588 && (!Hdr->HR.DontNeedAuth)
589 && (Hdr->HR.Handler != NULL)
590 && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
592 syslog(LOG_DEBUG, "http-auth required but not provided");
593 OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
594 Hdr->HR.prohibit_caching = 1;
598 * A future improvement might be to check the session integrity
599 * at this point before continuing.
603 * Bind to the session and perform the transaction
606 CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
607 pthread_setspecific(MyConKey, (void *)TheSession);
609 TheSession->inuse = 1; /* mark the session as bound */
610 TheSession->isFailure = 0; /* reset evntually existing error flags */
611 TheSession->lastreq = now; /* log */
612 TheSession->Hdr = Hdr;
615 * If a language was requested via a cookie, select that language now.
617 if (StrLength(Hdr->c_language) > 0) {
619 syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
620 set_selected_language(ChrPtr(Hdr->c_language));
621 go_selected_language();
627 session_attach_modules(TheSession);
630 /* How long did this transaction take? */
631 gettimeofday(&tx_finish, NULL);
633 if (verbose || strstr(ChrPtr(Hdr->this_page), "sslg") == NULL) {
634 syslog(LOG_INFO, "HTTP: 200 [%ld.%06ld] %s %s",
635 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
636 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
637 ReqStrs[Hdr->HR.eReqType],
638 ChrPtr(Hdr->this_page)
641 session_detach_modules(TheSession);
643 /* If *this* very transaction did not explicitly specify a session cookie,
644 * and it did not log in, we want to flag the session as a candidate for
645 * re-use by the next unbound client that comes along. This keeps our session
646 * table from getting bombarded with new sessions when, for example, a web
647 * spider crawls the site without using cookies.
649 if ((session_may_be_reused) && (!TheSession->logged_in)) {
650 TheSession->wc_session = 0; /* flag as available for re-use */
651 TheSession->selected_language = -1; /* clear any non-default language setting */
654 TheSession->Hdr = NULL;
655 TheSession->inuse = 0; /* mark the session as unbound */
656 CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
659 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
662 StrBufAppendPrintf(Target, "%ld",
663 (WCC != NULL)? WCC->nonce:0);
666 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
668 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
671 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
673 hdr->HR.ContentLength = StrToi(Line);
676 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
678 hdr->HR.ContentType = Line;
682 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
684 if (hdr->HostHeader != NULL) {
685 FreeStrBuf(&hdr->HostHeader);
687 hdr->HostHeader = NewStrBuf();
688 StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
689 StrBufAppendBuf(hdr->HostHeader, Line, 0);
692 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
694 if (!follow_xff) return;
696 if (hdr->HostHeader != NULL) {
697 FreeStrBuf(&hdr->HostHeader);
700 hdr->HostHeader = NewStrBuf();
701 StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
702 StrBufAppendBuf(hdr->HostHeader, Line, 0);
706 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
708 hdr->HR.browser_host = Line;
710 while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
711 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
713 StrBufTrim(hdr->HR.browser_host);
716 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
718 hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
721 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
726 if (strstr(&ChrPtr(Line)[16], "gzip")) {
731 void Header_HandleContentRange(StrBuf *Line, ParsedHttpHdrs *hdr)
733 const char *PRange = ChrPtr(Line);
735 while ((*PRange != '=') && (*PRange != '\0'))
739 if ((*PRange == '\0'))
742 hdr->RangeStart = atol(PRange);
744 while (isdigit(*PRange))
749 if ((*PRange == '\0'))
752 hdr->RangeTil = atol(PRange);
755 const char *ReqStrs[eNONE] = {
769 ServerStartModule_CONTEXT
773 HttpReqTypes = NewHash(1, NULL);
774 HttpHeaderHandler = NewHash(1, NULL);
776 v = malloc(sizeof(long));
778 Put(HttpReqTypes, HKEY("GET"), v, NULL);
780 v = malloc(sizeof(long));
782 Put(HttpReqTypes, HKEY("POST"), v, NULL);
784 v = malloc(sizeof(long));
786 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
788 v = malloc(sizeof(long));
790 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
792 v = malloc(sizeof(long));
794 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
796 v = malloc(sizeof(long));
798 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
800 v = malloc(sizeof(long));
802 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
804 v = malloc(sizeof(long));
806 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
808 v = malloc(sizeof(long));
810 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
812 v = malloc(sizeof(long));
814 Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
818 ServerShutdownModule_CONTEXT
821 DeleteHash(&HttpReqTypes);
822 DeleteHash(&HttpHeaderHandler);
825 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
828 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
829 memset(pHdr, 0, sizeof(OneHttpHeader));
831 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
839 RegisterHeaderHandler(HKEY("RANGE"), Header_HandleContentRange);
840 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
841 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
842 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
843 RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
844 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
845 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
846 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
847 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
849 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
850 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
852 WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
854 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
855 * Short-circuit these requests so we don't have to send them through the full processing loop.
857 WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
858 WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS); /* /winnt */
859 WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
860 WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
861 WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
862 WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
867 HttpNewModule_CONTEXT
868 (ParsedHttpHdrs *httpreq)
870 httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
871 httpreq->this_page = NewStrBufPlain(NULL, SIZ);
875 HttpDetachModule_CONTEXT
876 (ParsedHttpHdrs *httpreq)
878 FlushStrBuf(httpreq->PlainArgs);
879 FlushStrBuf(httpreq->HostHeader);
880 FlushStrBuf(httpreq->this_page);
881 FlushStrBuf(httpreq->PlainArgs);
882 DeleteHash(&httpreq->HTTPHeaders);
883 memset(&httpreq->HR, 0, sizeof(HdrRefs));
887 HttpDestroyModule_CONTEXT
888 (ParsedHttpHdrs *httpreq)
890 FreeStrBuf(&httpreq->this_page);
891 FreeStrBuf(&httpreq->PlainArgs);
892 FreeStrBuf(&httpreq->this_page);
893 FreeStrBuf(&httpreq->PlainArgs);
894 FreeStrBuf(&httpreq->HostHeader);
895 DeleteHash(&httpreq->HTTPHeaders);