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.
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 HashList *HttpReqTypes = NULL;
28 HashList *HttpHeaderHandler = NULL;
29 extern HashList *HandlerHash;
31 /* the following two values start at 1 because the initial parent thread counts as one. */
32 int num_threads_existing = 1; /* Number of worker threads which exist. */
33 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)
181 syslog(LOG_DEBUG, "Matched a session with the same http-auth");
186 /* If cookie-session, look for a session with matching session ID */
187 if ( (Hdr->HR.desired_session != 0)
188 && (sptr->wc_session == Hdr->HR.desired_session)
191 syslog(LOG_DEBUG, "Matched a session with the same cookie");
196 /* Any unbound session is a candidate */
197 if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
199 syslog(LOG_DEBUG, "Reusing an unbound session");
205 CtdlLogResult(pthread_mutex_unlock(ListMutex));
206 if (TheSession == NULL) {
207 syslog(LOG_DEBUG, "No existing session was matched");
212 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
214 wcsession *TheSession;
215 TheSession = (wcsession *) malloc(sizeof(wcsession));
216 memset(TheSession, 0, sizeof(wcsession));
217 TheSession->Hdr = Hdr;
218 TheSession->serv_sock = (-1);
219 TheSession->lastreq = time(NULL);;
221 pthread_setspecific(MyConKey, (void *)TheSession);
223 /* If we're recreating a session that expired, it's best to give it the same
224 * session number that it had before. The client browser ought to pick up
225 * the new session number and start using it, but in some rare situations it
226 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
229 if (Hdr->HR.desired_session == 0) {
230 TheSession->wc_session = GenerateSessionID();
231 syslog(LOG_DEBUG, "Created new session %d", TheSession->wc_session);
234 TheSession->wc_session = Hdr->HR.desired_session;
235 syslog(LOG_DEBUG, "Re-created session %d", TheSession->wc_session);
237 Hdr->HR.Static = Static;
238 session_new_modules(TheSession);
241 pthread_mutex_init(&TheSession->SessionMutex, NULL);
243 if (ListMutex != NULL)
244 CtdlLogResult(pthread_mutex_lock(ListMutex));
246 if (wclist != NULL) {
247 TheSession->nonce = rand();
248 TheSession->next = *wclist;
249 *wclist = TheSession;
251 if (ListMutex != NULL)
252 CtdlLogResult(pthread_mutex_unlock(ListMutex));
258 /* If it's a "force 404" situation then display the error and bail. */
261 hprintf("HTTP/1.1 404 Not found\r\n");
262 hprintf("Content-Type: text/plain\r\n");
263 wc_printf("Not found\r\n");
267 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
270 void *vLine, *vHandler;
271 const char *Pos = NULL;
273 Hdr->HR.ReqLine = Line;
274 /* The requesttype... GET, POST... */
275 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
276 if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
279 Hdr->HR.eReqType = *(long*)vLine;
282 Hdr->HR.eReqType = eGET;
285 StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
287 /* the HTTP Version... */
288 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
289 StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
291 if (StrLength(Buf) == 0) {
292 Hdr->HR.eReqType = eGET;
296 StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
298 /* chop Filename / query arguments */
299 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
300 if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
301 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
303 Args ++; /* skip the ? */
304 StrBufPlain(Hdr->PlainArgs,
306 StrLength(Hdr->HR.ReqLine) -
307 (Args - ChrPtr(Hdr->HR.ReqLine)));
308 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
309 } /* don't parse them yet, maybe we don't even care... */
311 /* now lookup what we are going to do with this... */
312 /* skip first slash */
313 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
315 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
317 GetHash(HandlerHash, SKEY(Buf), &vHandler),
318 Hdr->HR.Handler = (WebcitHandler*) vHandler;
319 if (Hdr->HR.Handler == NULL)
322 * If the request is prefixed by "/webcit" then chop that off. This
323 * allows a front end web server to forward all /webcit requests to us
324 * while still using the same web server port for other things.
326 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
330 /* remove the handlername from the URL */
331 if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
332 StrBufCutLeft(Hdr->HR.ReqLine,
333 Pos - ChrPtr(Hdr->HR.ReqLine));
336 if (Hdr->HR.Handler != NULL) {
337 if ((Hdr->HR.Handler->Flags & BOGUS) != 0) {
340 Hdr->HR.DontNeedAuth = (
341 ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
342 ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
346 /* If this is a "flat" request for the root, display the configured landing page. */
348 StrBuf *NewLine = NewStrBuf();
349 Hdr->HR.DontNeedAuth = 1;
350 StrBufAppendPrintf(NewLine, "GET /landing?go=%s?failvisibly=1 HTTP/1.0", ChrPtr(Buf));
351 if (verbose) syslog(LOG_DEBUG, "Replacing with: %s", ChrPtr(NewLine));
352 return_value = ReadHttpSubject(Hdr, NewLine, Buf);
353 FreeStrBuf(&NewLine);
360 int AnalyseHeaders(ParsedHttpHdrs *Hdr) {
365 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
367 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
369 pHdr = (OneHttpHeader *)vHdr;
370 if (pHdr->HaveEvaluator)
371 pHdr->H(pHdr->Val, Hdr);
380 * Read in the request
382 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) {
402 if (StrLength(Line) == 0) {
407 Hdr->HTTPHeaders = NewHash(1, NULL);
408 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
409 memset(pHdr, 0, sizeof(OneHttpHeader));
411 Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
412 if (verbose || strstr(ChrPtr(Line), "sslg") == NULL) {
413 syslog(LOG_DEBUG, "%s", ChrPtr(Line));
415 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
420 /* Do we need to Unfold? */
421 if ((LastLine != NULL) && (isspace(*ChrPtr(Line)))) {
422 pch = pchs = ChrPtr(Line);
423 pche = pchs + StrLength(Line);
424 while (isspace(*pch) && (pch < pche)) {
427 StrBufCutLeft(Line, pch - pchs);
428 StrBufAppendBuf(LastLine, Line, 0);
434 StrBufSanitizeAscii(Line, (char)0xa7);
435 StrBufExtract_token(HeaderName, Line, 0, ':');
438 pche = pchs + StrLength(Line);
439 pch = pchs + StrLength(HeaderName) + 1;
440 pche = pchs + StrLength(Line);
441 while ((pch < pche) && isspace(*pch)) {
444 StrBufCutLeft(Line, pch - pchs);
445 StrBufUpCase(HeaderName);
447 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
448 memset(pHdr, 0, sizeof(OneHttpHeader));
451 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) && (vF != NULL)) {
452 OneHttpHeader *FHdr = (OneHttpHeader*) vF;
454 pHdr->HaveEvaluator = 1;
456 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
458 } while (Line != NULL);
460 FreeStrBuf(&HeaderName);
465 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
467 StrBuf *Buf = NewStrBuf();
469 if (Hdr->HR.ReqLine != NULL) {
470 FlushStrBuf(Hdr->HR.ReqLine);
471 StrBufPlain(Hdr->HR.ReqLine, Line, len);
474 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
476 ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
484 * This loop gets called once for every HTTP connection made to WebCit. At
485 * this entry point we have an HTTP socket with a browser allegedly on the
486 * other end, but we have not yet bound to a WebCit session.
488 * The job of this function is to locate the correct session and bind to it,
489 * or create a session if necessary and bind to it, then run the WebCit
490 * transaction loop. Afterwards, we unbind from the session. When this
491 * function returns, the worker thread is then free to handle another
494 void context_loop(ParsedHttpHdrs *Hdr) {
496 wcsession *TheSession;
497 struct timeval tx_start;
498 struct timeval tx_finish;
499 int session_may_be_reused = 1;
502 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
505 * Find out what it is that the web browser is asking for
507 isbogus = ReadHTTPRequest(Hdr);
509 Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
512 isbogus = AnalyseHeaders(Hdr);
516 || ((Hdr->HR.Handler != NULL)
517 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
520 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
522 syslog(LOG_WARNING, "HTTP: 404 [%ld.%06ld] %s %s",
523 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
524 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
525 ReqStrs[Hdr->HR.eReqType],
526 ChrPtr(Hdr->this_page)
528 session_detach_modules(Bogus);
529 session_destroy_modules(&Bogus);
533 if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0)) {
535 Static = CreateSession(0, 1, NULL, Hdr, NULL);
537 Hdr->HR.Handler->F();
539 /* How long did this transaction take? */
540 gettimeofday(&tx_finish, NULL);
543 syslog(LOG_DEBUG, "HTTP: 200 [%ld.%06ld] %s %s",
544 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
545 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
546 ReqStrs[Hdr->HR.eReqType],
547 ChrPtr(Hdr->this_page)
549 session_detach_modules(Static);
550 session_destroy_modules(&Static);
554 if (Hdr->HR.got_auth == AUTH_BASIC) {
558 if (Hdr->HR.got_auth) {
559 session_may_be_reused = 0;
563 * See if there's an existing session open with any of:
564 * - The desired Session ID
565 * - A matching http-auth username and password
566 * - An unbound session flagged as reusable
568 TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
571 * If there were no qualifying sessions, then create a new one.
573 if ((TheSession == NULL) || (TheSession->killthis != 0)) {
574 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
578 * Reject transactions which require http-auth, if http-auth was not provided
580 if ( (StrLength(Hdr->c_username) == 0)
581 && (!Hdr->HR.DontNeedAuth)
582 && (Hdr->HR.Handler != NULL)
583 && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
585 syslog(LOG_DEBUG, "http-auth required but not provided");
586 OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
587 Hdr->HR.prohibit_caching = 1;
591 * A future improvement might be to check the session integrity
592 * at this point before continuing.
596 * Bind to the session and perform the transaction
599 CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
600 pthread_setspecific(MyConKey, (void *)TheSession);
602 TheSession->inuse = 1; /* mark the session as bound */
603 TheSession->isFailure = 0; /* reset evntually existing error flags */
604 TheSession->lastreq = now; /* log */
605 TheSession->Hdr = Hdr;
608 * If a language was requested via a cookie, select that language now.
610 if (StrLength(Hdr->c_language) > 0) {
612 syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
614 set_selected_language(ChrPtr(Hdr->c_language));
615 go_selected_language();
621 session_attach_modules(TheSession);
624 /* How long did this transaction take? */
625 gettimeofday(&tx_finish, NULL);
627 if (verbose || strstr(ChrPtr(Hdr->this_page), "sslg") == NULL) {
628 syslog(LOG_INFO, "HTTP: 200 [%ld.%06ld] %s %s",
629 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
630 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
631 ReqStrs[Hdr->HR.eReqType],
632 ChrPtr(Hdr->this_page)
635 session_detach_modules(TheSession);
637 /* If *this* very transaction did not explicitly specify a session cookie,
638 * and it did not log in, we want to flag the session as a candidate for
639 * re-use by the next unbound client that comes along. This keeps our session
640 * table from getting bombarded with new sessions when, for example, a web
641 * spider crawls the site without using cookies.
643 if ((session_may_be_reused) && (!TheSession->logged_in)) {
644 TheSession->wc_session = 0; /* flag as available for re-use */
645 TheSession->selected_language = -1; /* clear any non-default language setting */
648 TheSession->Hdr = NULL;
649 TheSession->inuse = 0; /* mark the session as unbound */
650 CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
653 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
656 StrBufAppendPrintf(Target, "%ld",
657 (WCC != NULL)? WCC->nonce:0);
660 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
662 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
665 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
667 hdr->HR.ContentLength = StrToi(Line);
670 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
672 hdr->HR.ContentType = Line;
676 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
678 if (hdr->HostHeader != NULL) {
679 FreeStrBuf(&hdr->HostHeader);
681 hdr->HostHeader = NewStrBuf();
682 StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
683 StrBufAppendBuf(hdr->HostHeader, Line, 0);
686 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
688 if (!follow_xff) return;
690 if (hdr->HostHeader != NULL) {
691 FreeStrBuf(&hdr->HostHeader);
694 hdr->HostHeader = NewStrBuf();
695 StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
696 StrBufAppendBuf(hdr->HostHeader, Line, 0);
700 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
702 hdr->HR.browser_host = Line;
704 while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
705 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
707 StrBufTrim(hdr->HR.browser_host);
710 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
712 hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
715 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
720 if (strstr(&ChrPtr(Line)[16], "gzip")) {
725 void Header_HandleContentRange(StrBuf *Line, ParsedHttpHdrs *hdr)
727 const char *PRange = ChrPtr(Line);
729 while ((*PRange != '=') && (*PRange != '\0'))
733 if ((*PRange == '\0'))
736 hdr->RangeStart = atol(PRange);
738 while (isdigit(*PRange))
743 if ((*PRange == '\0'))
746 hdr->RangeTil = atol(PRange);
749 const char *ReqStrs[eNONE] = {
763 ServerStartModule_CONTEXT
767 HttpReqTypes = NewHash(1, NULL);
768 HttpHeaderHandler = NewHash(1, NULL);
770 v = malloc(sizeof(long));
772 Put(HttpReqTypes, HKEY("GET"), v, NULL);
774 v = malloc(sizeof(long));
776 Put(HttpReqTypes, HKEY("POST"), v, NULL);
778 v = malloc(sizeof(long));
780 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
782 v = malloc(sizeof(long));
784 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
786 v = malloc(sizeof(long));
788 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
790 v = malloc(sizeof(long));
792 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
794 v = malloc(sizeof(long));
796 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
798 v = malloc(sizeof(long));
800 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
802 v = malloc(sizeof(long));
804 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
806 v = malloc(sizeof(long));
808 Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
812 ServerShutdownModule_CONTEXT
815 DeleteHash(&HttpReqTypes);
816 DeleteHash(&HttpHeaderHandler);
819 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
822 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
823 memset(pHdr, 0, sizeof(OneHttpHeader));
825 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
833 RegisterHeaderHandler(HKEY("RANGE"), Header_HandleContentRange);
834 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
835 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
836 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
837 RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
838 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
839 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
840 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
841 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
843 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
844 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
846 WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
848 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
849 * Short-circuit these requests so we don't have to send them through the full processing loop.
851 WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
852 WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS); /* /winnt */
853 WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
854 WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
855 WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
856 WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
861 HttpNewModule_CONTEXT
862 (ParsedHttpHdrs *httpreq)
864 httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
865 httpreq->this_page = NewStrBufPlain(NULL, SIZ);
869 HttpDetachModule_CONTEXT
870 (ParsedHttpHdrs *httpreq)
872 FlushStrBuf(httpreq->PlainArgs);
873 FlushStrBuf(httpreq->HostHeader);
874 FlushStrBuf(httpreq->this_page);
875 FlushStrBuf(httpreq->PlainArgs);
876 DeleteHash(&httpreq->HTTPHeaders);
877 memset(&httpreq->HR, 0, sizeof(HdrRefs));
881 HttpDestroyModule_CONTEXT
882 (ParsedHttpHdrs *httpreq)
884 FreeStrBuf(&httpreq->this_page);
885 FreeStrBuf(&httpreq->PlainArgs);
886 FreeStrBuf(&httpreq->this_page);
887 FreeStrBuf(&httpreq->PlainArgs);
888 FreeStrBuf(&httpreq->HostHeader);
889 DeleteHash(&httpreq->HTTPHeaders);