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
68 CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
69 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
71 the_time = time(NULL);
72 /* Kill idle sessions */
73 if ((sptr->inuse == 0) &&
74 ((the_time - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT))
76 syslog(LOG_DEBUG, "Timeout session %d", sptr->wc_session);
80 /* Remove sessions flagged for kill */
83 /* remove session from linked list */
84 if (sptr == SessionList) {
85 SessionList = SessionList->next;
87 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
88 if (ss->next == sptr) {
89 ss->next = ss->next->next;
93 sptr->next = sessions_to_kill;
94 sessions_to_kill = sptr;
97 CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
100 * Now free up and destroy the culled sessions.
102 while (sessions_to_kill != NULL) {
103 syslog(LOG_DEBUG, "Destroying session %d", sessions_to_kill->wc_session);
104 sptr = sessions_to_kill->next;
105 session_destroy_modules(&sessions_to_kill);
106 sessions_to_kill = sptr;
111 * Check the size of our thread pool. If all threads are executing, spawn another.
113 void check_thread_pool_size(void)
115 if (time_to_die) return; /* don't expand the thread pool during shutdown */
117 begin_critical_section(S_SPAWNER); /* only one of these should run at a time */
119 (num_threads_executing >= num_threads_existing)
120 && (num_threads_existing < MAX_WORKER_THREADS)
122 syslog(LOG_DEBUG, "%d of %d threads are executing. Adding another worker thread.",
123 num_threads_executing,
126 spawn_another_worker_thread();
128 end_critical_section(S_SPAWNER);
133 * Wake up occasionally and clean house
135 void housekeeping_loop(void)
138 sleeeeeeeeeep(HOUSEKEEPING);
145 * Create a Session id
146 * Generate a unique WebCit session ID (which is not the same thing as the
147 * Citadel session ID).
149 int GenerateSessionID(void)
151 static int seq = (-1);
154 seq = (int) time(NULL);
160 wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
162 wcsession *sptr = NULL;
163 wcsession *TheSession = NULL;
165 if (Hdr->HR.got_auth == AUTH_BASIC) {
169 CtdlLogResult(pthread_mutex_lock(ListMutex));
170 for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
172 /* If HTTP-AUTH, look for a session with matching credentials */
173 switch (Hdr->HR.got_auth)
176 if ( (!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username)))
177 && (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password)))
178 && (sptr->killthis == 0)
180 syslog(LOG_DEBUG, "Matched a session with the same http-auth");
185 /* If cookie-session, look for a session with matching session ID */
186 if ( (Hdr->HR.desired_session != 0)
187 && (sptr->wc_session == Hdr->HR.desired_session)
189 syslog(LOG_DEBUG, "Matched a session with the same cookie");
194 /* Any unbound session is a candidate */
195 if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
196 syslog(LOG_DEBUG, "Reusing an unbound session");
202 CtdlLogResult(pthread_mutex_unlock(ListMutex));
203 if (TheSession == NULL) {
204 syslog(LOG_DEBUG, "No existing session was matched");
209 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
211 wcsession *TheSession;
212 TheSession = (wcsession *) malloc(sizeof(wcsession));
213 memset(TheSession, 0, sizeof(wcsession));
214 TheSession->Hdr = Hdr;
215 TheSession->serv_sock = (-1);
216 TheSession->lastreq = time(NULL);;
218 pthread_setspecific(MyConKey, (void *)TheSession);
220 /* If we're recreating a session that expired, it's best to give it the same
221 * session number that it had before. The client browser ought to pick up
222 * the new session number and start using it, but in some rare situations it
223 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
226 if (Hdr->HR.desired_session == 0) {
227 TheSession->wc_session = GenerateSessionID();
228 syslog(LOG_DEBUG, "Created new session %d", TheSession->wc_session);
231 TheSession->wc_session = Hdr->HR.desired_session;
232 syslog(LOG_DEBUG, "Re-created session %d", TheSession->wc_session);
234 Hdr->HR.Static = Static;
235 session_new_modules(TheSession);
238 pthread_mutex_init(&TheSession->SessionMutex, NULL);
240 if (ListMutex != NULL)
241 CtdlLogResult(pthread_mutex_lock(ListMutex));
243 if (wclist != NULL) {
244 TheSession->nonce = rand();
245 TheSession->next = *wclist;
246 *wclist = TheSession;
248 if (ListMutex != NULL)
249 CtdlLogResult(pthread_mutex_unlock(ListMutex));
255 /* If it's a "force 404" situation then display the error and bail. */
258 hprintf("HTTP/1.1 404 Not found\r\n");
259 hprintf("Content-Type: text/plain\r\n");
260 wc_printf("Not found\r\n");
264 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
267 void *vLine, *vHandler;
268 const char *Pos = NULL;
270 Hdr->HR.ReqLine = Line;
271 /* The requesttype... GET, POST... */
272 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
273 if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
276 Hdr->HR.eReqType = *(long*)vLine;
279 Hdr->HR.eReqType = eGET;
282 StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
284 /* the HTTP Version... */
285 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
286 StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
288 if (StrLength(Buf) == 0) {
289 Hdr->HR.eReqType = eGET;
293 StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
295 /* chop Filename / query arguments */
296 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
297 if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
298 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
300 Args ++; /* skip the ? */
301 StrBufPlain(Hdr->PlainArgs,
303 StrLength(Hdr->HR.ReqLine) -
304 (Args - ChrPtr(Hdr->HR.ReqLine)));
305 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
306 } /* don't parse them yet, maybe we don't even care... */
308 /* now lookup what we are going to do with this... */
309 /* skip first slash */
310 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
312 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
314 GetHash(HandlerHash, SKEY(Buf), &vHandler),
315 Hdr->HR.Handler = (WebcitHandler*) vHandler;
316 if (Hdr->HR.Handler == NULL)
319 * If the request is prefixed by "/webcit" then chop that off. This
320 * allows a front end web server to forward all /webcit requests to us
321 * while still using the same web server port for other things.
323 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
327 /* remove the handlername from the URL */
328 if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
329 StrBufCutLeft(Hdr->HR.ReqLine,
330 Pos - ChrPtr(Hdr->HR.ReqLine));
333 if (Hdr->HR.Handler != NULL) {
334 if ((Hdr->HR.Handler->Flags & BOGUS) != 0) {
337 Hdr->HR.DontNeedAuth = (
338 ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
339 ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
343 /* If this is a "flat" request for the root, display the configured landing page. */
345 StrBuf *NewLine = NewStrBuf();
346 Hdr->HR.DontNeedAuth = 1;
347 StrBufAppendPrintf(NewLine, "GET /landing?go=%s?failvisibly=1 HTTP/1.0", ChrPtr(Buf));
348 syslog(LOG_DEBUG, "Replacing with: %s", ChrPtr(NewLine));
349 return_value = ReadHttpSubject(Hdr, NewLine, Buf);
350 FreeStrBuf(&NewLine);
357 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
363 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
365 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
367 pHdr = (OneHttpHeader *)vHdr;
368 if (pHdr->HaveEvaluator)
369 pHdr->H(pHdr->Val, Hdr);
376 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
379 * Read in the request
381 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
383 const char *pch, *pchs, *pche;
385 StrBuf *Line, *LastLine, *HeaderName;
390 HeaderName = NewStrBuf();
394 Line = NewStrBufPlain(NULL, SIZ / 4);
396 if (ClientGetLine(Hdr, Line) < 0) return 1;
398 if (StrLength(Line) == 0) {
403 Hdr->HTTPHeaders = NewHash(1, NULL);
404 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
405 memset(pHdr, 0, sizeof(OneHttpHeader));
407 Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
408 syslog(LOG_DEBUG, "%s", ChrPtr(Line));
409 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
414 /* Do we need to Unfold? */
415 if ((LastLine != NULL) &&
416 (isspace(*ChrPtr(Line)))) {
417 pch = pchs = ChrPtr(Line);
418 pche = pchs + StrLength(Line);
419 while (isspace(*pch) && (pch < pche))
421 StrBufCutLeft(Line, pch - pchs);
422 StrBufAppendBuf(LastLine, Line, 0);
428 StrBufSanitizeAscii(Line, (char)0xa7);
429 StrBufExtract_token(HeaderName, Line, 0, ':');
432 pche = pchs + StrLength(Line);
433 pch = pchs + StrLength(HeaderName) + 1;
434 pche = pchs + StrLength(Line);
435 while ((pch < pche) && isspace(*pch))
437 StrBufCutLeft(Line, pch - pchs);
439 StrBufUpCase(HeaderName);
441 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
442 memset(pHdr, 0, sizeof(OneHttpHeader));
445 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
448 OneHttpHeader *FHdr = (OneHttpHeader*) vF;
450 pHdr->HaveEvaluator = 1;
452 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
454 } while (Line != NULL);
456 FreeStrBuf(&HeaderName);
461 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
463 StrBuf *Buf = NewStrBuf();
465 if (Hdr->HR.ReqLine != NULL) {
466 FlushStrBuf(Hdr->HR.ReqLine);
467 StrBufPlain(Hdr->HR.ReqLine, Line, len);
470 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
472 ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
480 * This loop gets called once for every HTTP connection made to WebCit. At
481 * this entry point we have an HTTP socket with a browser allegedly on the
482 * other end, but we have not yet bound to a WebCit session.
484 * The job of this function is to locate the correct session and bind to it,
485 * or create a session if necessary and bind to it, then run the WebCit
486 * transaction loop. Afterwards, we unbind from the session. When this
487 * function returns, the worker thread is then free to handle another
490 void context_loop(ParsedHttpHdrs *Hdr)
493 wcsession *TheSession;
494 struct timeval tx_start;
495 struct timeval tx_finish;
496 int session_may_be_reused = 1;
499 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
502 * Find out what it is that the web browser is asking for
504 isbogus = ReadHTTPRequest(Hdr);
506 Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
509 isbogus = AnalyseHeaders(Hdr);
513 || ((Hdr->HR.Handler != NULL)
514 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
517 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
519 syslog(LOG_WARNING, "HTTP: 404 [%ld.%06ld] %s %s",
520 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
521 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
522 ReqStrs[Hdr->HR.eReqType],
523 ChrPtr(Hdr->this_page)
525 session_detach_modules(Bogus);
526 session_destroy_modules(&Bogus);
530 if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
533 Static = CreateSession(0, 1, NULL, Hdr, NULL);
535 Hdr->HR.Handler->F();
537 /* How long did this transaction take? */
538 gettimeofday(&tx_finish, NULL);
540 syslog(LOG_DEBUG, "HTTP: 200 [%ld.%06ld] %s %s",
541 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
542 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
543 ReqStrs[Hdr->HR.eReqType],
544 ChrPtr(Hdr->this_page)
546 session_detach_modules(Static);
547 session_destroy_modules(&Static);
551 if (Hdr->HR.got_auth == AUTH_BASIC) {
555 if (Hdr->HR.got_auth) {
556 session_may_be_reused = 0;
560 * See if there's an existing session open with any of:
561 * - The desired Session ID
562 * - A matching http-auth username and password
563 * - An unbound session flagged as reusable
565 TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
568 * If there were no qualifying sessions, then create a new one.
570 if ((TheSession == NULL) || (TheSession->killthis != 0)) {
571 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
575 * Reject transactions which require http-auth, if http-auth was not provided
577 if ( (StrLength(Hdr->c_username) == 0)
578 && (!Hdr->HR.DontNeedAuth)
579 && (Hdr->HR.Handler != NULL)
580 && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
582 syslog(LOG_DEBUG, "http-auth required but not provided");
583 OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
584 Hdr->HR.prohibit_caching = 1;
588 * A future improvement might be to check the session integrity
589 * at this point before continuing.
593 * Bind to the session and perform the transaction
596 CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
597 pthread_setspecific(MyConKey, (void *)TheSession);
599 TheSession->inuse = 1; /* mark the session as bound */
600 TheSession->lastreq = now; /* log */
601 TheSession->Hdr = Hdr;
604 * If a language was requested via a cookie, select that language now.
606 if (StrLength(Hdr->c_language) > 0) {
607 syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
608 set_selected_language(ChrPtr(Hdr->c_language));
609 go_selected_language();
615 session_attach_modules(TheSession);
618 /* How long did this transaction take? */
619 gettimeofday(&tx_finish, NULL);
621 syslog(LOG_INFO, "HTTP: 200 [%ld.%06ld] %s %s",
622 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
623 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
624 ReqStrs[Hdr->HR.eReqType],
625 ChrPtr(Hdr->this_page)
628 session_detach_modules(TheSession);
630 /* If *this* very transaction did not explicitly specify a session cookie,
631 * and it did not log in, we want to flag the session as a candidate for
632 * re-use by the next unbound client that comes along. This keeps our session
633 * table from getting bombarded with new sessions when, for example, a web
634 * spider crawls the site without using cookies.
636 if ((session_may_be_reused) && (!TheSession->logged_in)) {
637 TheSession->wc_session = 0; /* flag as available for re-use */
638 TheSession->selected_language = -1; /* clear any non-default language setting */
641 TheSession->Hdr = NULL;
642 TheSession->inuse = 0; /* mark the session as unbound */
643 CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
646 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
649 StrBufAppendPrintf(Target, "%ld",
650 (WCC != NULL)? WCC->nonce:0);
653 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
655 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
658 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
660 hdr->HR.ContentLength = StrToi(Line);
663 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
665 hdr->HR.ContentType = Line;
669 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
671 if (hdr->HostHeader != NULL) {
672 FreeStrBuf(&hdr->HostHeader);
674 hdr->HostHeader = NewStrBuf();
675 StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
676 StrBufAppendBuf(hdr->HostHeader, Line, 0);
679 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
681 if (!follow_xff) return;
683 if (hdr->HostHeader != NULL) {
684 FreeStrBuf(&hdr->HostHeader);
687 hdr->HostHeader = NewStrBuf();
688 StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
689 StrBufAppendBuf(hdr->HostHeader, Line, 0);
693 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
695 hdr->HR.browser_host = Line;
697 while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
698 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
700 StrBufTrim(hdr->HR.browser_host);
703 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
705 hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
708 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
713 if (strstr(&ChrPtr(Line)[16], "gzip")) {
718 void Header_HandleContentRange(StrBuf *Line, ParsedHttpHdrs *hdr)
720 const char *PRange = ChrPtr(Line);
722 while ((*PRange != '=') && (*PRange != '\0'))
726 if ((*PRange == '\0'))
729 hdr->RangeStart = atol(PRange);
731 while (isdigit(*PRange))
736 if ((*PRange == '\0'))
739 hdr->RangeTil = atol(PRange);
742 const char *ReqStrs[eNONE] = {
756 ServerStartModule_CONTEXT
760 HttpReqTypes = NewHash(1, NULL);
761 HttpHeaderHandler = NewHash(1, NULL);
763 v = malloc(sizeof(long));
765 Put(HttpReqTypes, HKEY("GET"), v, NULL);
767 v = malloc(sizeof(long));
769 Put(HttpReqTypes, HKEY("POST"), v, NULL);
771 v = malloc(sizeof(long));
773 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
775 v = malloc(sizeof(long));
777 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
779 v = malloc(sizeof(long));
781 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
783 v = malloc(sizeof(long));
785 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
787 v = malloc(sizeof(long));
789 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
791 v = malloc(sizeof(long));
793 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
795 v = malloc(sizeof(long));
797 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
799 v = malloc(sizeof(long));
801 Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
805 ServerShutdownModule_CONTEXT
808 DeleteHash(&HttpReqTypes);
809 DeleteHash(&HttpHeaderHandler);
812 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
815 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
816 memset(pHdr, 0, sizeof(OneHttpHeader));
818 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
826 RegisterHeaderHandler(HKEY("RANGE"), Header_HandleContentRange);
827 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
828 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
829 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
830 RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
831 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
832 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
833 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
834 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
836 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
837 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
839 WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
841 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
842 * Short-circuit these requests so we don't have to send them through the full processing loop.
844 WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
845 WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS); /* /winnt */
846 WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
847 WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
848 WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
849 WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
854 HttpNewModule_CONTEXT
855 (ParsedHttpHdrs *httpreq)
857 httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
858 httpreq->this_page = NewStrBufPlain(NULL, SIZ);
862 HttpDetachModule_CONTEXT
863 (ParsedHttpHdrs *httpreq)
865 FlushStrBuf(httpreq->PlainArgs);
866 FlushStrBuf(httpreq->HostHeader);
867 FlushStrBuf(httpreq->this_page);
868 FlushStrBuf(httpreq->PlainArgs);
869 DeleteHash(&httpreq->HTTPHeaders);
870 memset(&httpreq->HR, 0, sizeof(HdrRefs));
874 HttpDestroyModule_CONTEXT
875 (ParsedHttpHdrs *httpreq)
877 FreeStrBuf(&httpreq->this_page);
878 FreeStrBuf(&httpreq->PlainArgs);
879 FreeStrBuf(&httpreq->this_page);
880 FreeStrBuf(&httpreq->PlainArgs);
881 FreeStrBuf(&httpreq->HostHeader);
882 DeleteHash(&httpreq->HTTPHeaders);