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 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)
366 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
368 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
370 pHdr = (OneHttpHeader *)vHdr;
371 if (pHdr->HaveEvaluator)
372 pHdr->H(pHdr->Val, Hdr);
379 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
382 * Read in the request
384 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
386 const char *pch, *pchs, *pche;
388 StrBuf *Line, *LastLine, *HeaderName;
393 HeaderName = NewStrBuf();
397 Line = NewStrBufPlain(NULL, SIZ / 4);
399 if (ClientGetLine(Hdr, Line) < 0) {
405 if (StrLength(Line) == 0) {
410 Hdr->HTTPHeaders = NewHash(1, NULL);
411 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
412 memset(pHdr, 0, sizeof(OneHttpHeader));
414 Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
415 if (verbose || strstr(ChrPtr(Line), "sslg") == NULL)
416 syslog(LOG_DEBUG, "%s", ChrPtr(Line));
417 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
422 /* Do we need to Unfold? */
423 if ((LastLine != NULL) &&
424 (isspace(*ChrPtr(Line)))) {
425 pch = pchs = ChrPtr(Line);
426 pche = pchs + StrLength(Line);
427 while (isspace(*pch) && (pch < pche))
429 StrBufCutLeft(Line, pch - pchs);
430 StrBufAppendBuf(LastLine, Line, 0);
436 StrBufSanitizeAscii(Line, (char)0xa7);
437 StrBufExtract_token(HeaderName, Line, 0, ':');
440 pche = pchs + StrLength(Line);
441 pch = pchs + StrLength(HeaderName) + 1;
442 pche = pchs + StrLength(Line);
443 while ((pch < pche) && isspace(*pch))
445 StrBufCutLeft(Line, pch - pchs);
447 StrBufUpCase(HeaderName);
449 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
450 memset(pHdr, 0, sizeof(OneHttpHeader));
453 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
456 OneHttpHeader *FHdr = (OneHttpHeader*) vF;
458 pHdr->HaveEvaluator = 1;
460 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
462 } while (Line != NULL);
464 FreeStrBuf(&HeaderName);
469 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
471 StrBuf *Buf = NewStrBuf();
473 if (Hdr->HR.ReqLine != NULL) {
474 FlushStrBuf(Hdr->HR.ReqLine);
475 StrBufPlain(Hdr->HR.ReqLine, Line, len);
478 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
480 ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
488 * This loop gets called once for every HTTP connection made to WebCit. At
489 * this entry point we have an HTTP socket with a browser allegedly on the
490 * other end, but we have not yet bound to a WebCit session.
492 * The job of this function is to locate the correct session and bind to it,
493 * or create a session if necessary and bind to it, then run the WebCit
494 * transaction loop. Afterwards, we unbind from the session. When this
495 * function returns, the worker thread is then free to handle another
498 void context_loop(ParsedHttpHdrs *Hdr)
501 wcsession *TheSession;
502 struct timeval tx_start;
503 struct timeval tx_finish;
504 int session_may_be_reused = 1;
507 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
510 * Find out what it is that the web browser is asking for
512 isbogus = ReadHTTPRequest(Hdr);
514 Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
517 isbogus = AnalyseHeaders(Hdr);
521 || ((Hdr->HR.Handler != NULL)
522 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
525 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
527 syslog(LOG_WARNING, "HTTP: 404 [%ld.%06ld] %s %s",
528 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
529 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
530 ReqStrs[Hdr->HR.eReqType],
531 ChrPtr(Hdr->this_page)
533 session_detach_modules(Bogus);
534 session_destroy_modules(&Bogus);
538 if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
541 Static = CreateSession(0, 1, NULL, Hdr, NULL);
543 Hdr->HR.Handler->F();
545 /* How long did this transaction take? */
546 gettimeofday(&tx_finish, NULL);
549 syslog(LOG_DEBUG, "HTTP: 200 [%ld.%06ld] %s %s",
550 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
551 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
552 ReqStrs[Hdr->HR.eReqType],
553 ChrPtr(Hdr->this_page)
555 session_detach_modules(Static);
556 session_destroy_modules(&Static);
560 if (Hdr->HR.got_auth == AUTH_BASIC) {
564 if (Hdr->HR.got_auth) {
565 session_may_be_reused = 0;
569 * See if there's an existing session open with any of:
570 * - The desired Session ID
571 * - A matching http-auth username and password
572 * - An unbound session flagged as reusable
574 TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
577 * If there were no qualifying sessions, then create a new one.
579 if ((TheSession == NULL) || (TheSession->killthis != 0)) {
580 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
584 * Reject transactions which require http-auth, if http-auth was not provided
586 if ( (StrLength(Hdr->c_username) == 0)
587 && (!Hdr->HR.DontNeedAuth)
588 && (Hdr->HR.Handler != NULL)
589 && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
591 syslog(LOG_DEBUG, "http-auth required but not provided");
592 OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
593 Hdr->HR.prohibit_caching = 1;
597 * A future improvement might be to check the session integrity
598 * at this point before continuing.
602 * Bind to the session and perform the transaction
605 CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
606 pthread_setspecific(MyConKey, (void *)TheSession);
608 TheSession->inuse = 1; /* mark the session as bound */
609 TheSession->isFailure = 0; /* reset evntually existing error flags */
610 TheSession->lastreq = now; /* log */
611 TheSession->Hdr = Hdr;
614 * If a language was requested via a cookie, select that language now.
616 if (StrLength(Hdr->c_language) > 0) {
618 syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
619 set_selected_language(ChrPtr(Hdr->c_language));
620 go_selected_language();
626 session_attach_modules(TheSession);
629 /* How long did this transaction take? */
630 gettimeofday(&tx_finish, NULL);
632 if (verbose || strstr(ChrPtr(Hdr->this_page), "sslg") == NULL) {
633 syslog(LOG_INFO, "HTTP: 200 [%ld.%06ld] %s %s",
634 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
635 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
636 ReqStrs[Hdr->HR.eReqType],
637 ChrPtr(Hdr->this_page)
640 session_detach_modules(TheSession);
642 /* If *this* very transaction did not explicitly specify a session cookie,
643 * and it did not log in, we want to flag the session as a candidate for
644 * re-use by the next unbound client that comes along. This keeps our session
645 * table from getting bombarded with new sessions when, for example, a web
646 * spider crawls the site without using cookies.
648 if ((session_may_be_reused) && (!TheSession->logged_in)) {
649 TheSession->wc_session = 0; /* flag as available for re-use */
650 TheSession->selected_language = -1; /* clear any non-default language setting */
653 TheSession->Hdr = NULL;
654 TheSession->inuse = 0; /* mark the session as unbound */
655 CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
658 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
661 StrBufAppendPrintf(Target, "%ld",
662 (WCC != NULL)? WCC->nonce:0);
665 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
667 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
670 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
672 hdr->HR.ContentLength = StrToi(Line);
675 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
677 hdr->HR.ContentType = Line;
681 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
683 if (hdr->HostHeader != NULL) {
684 FreeStrBuf(&hdr->HostHeader);
686 hdr->HostHeader = NewStrBuf();
687 StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
688 StrBufAppendBuf(hdr->HostHeader, Line, 0);
691 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
693 if (!follow_xff) return;
695 if (hdr->HostHeader != NULL) {
696 FreeStrBuf(&hdr->HostHeader);
699 hdr->HostHeader = NewStrBuf();
700 StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
701 StrBufAppendBuf(hdr->HostHeader, Line, 0);
705 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
707 hdr->HR.browser_host = Line;
709 while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
710 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
712 StrBufTrim(hdr->HR.browser_host);
715 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
717 hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
720 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
725 if (strstr(&ChrPtr(Line)[16], "gzip")) {
730 void Header_HandleContentRange(StrBuf *Line, ParsedHttpHdrs *hdr)
732 const char *PRange = ChrPtr(Line);
734 while ((*PRange != '=') && (*PRange != '\0'))
738 if ((*PRange == '\0'))
741 hdr->RangeStart = atol(PRange);
743 while (isdigit(*PRange))
748 if ((*PRange == '\0'))
751 hdr->RangeTil = atol(PRange);
754 const char *ReqStrs[eNONE] = {
768 ServerStartModule_CONTEXT
772 HttpReqTypes = NewHash(1, NULL);
773 HttpHeaderHandler = NewHash(1, NULL);
775 v = malloc(sizeof(long));
777 Put(HttpReqTypes, HKEY("GET"), v, NULL);
779 v = malloc(sizeof(long));
781 Put(HttpReqTypes, HKEY("POST"), v, NULL);
783 v = malloc(sizeof(long));
785 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
787 v = malloc(sizeof(long));
789 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
791 v = malloc(sizeof(long));
793 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
795 v = malloc(sizeof(long));
797 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
799 v = malloc(sizeof(long));
801 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
803 v = malloc(sizeof(long));
805 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
807 v = malloc(sizeof(long));
809 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
811 v = malloc(sizeof(long));
813 Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
817 ServerShutdownModule_CONTEXT
820 DeleteHash(&HttpReqTypes);
821 DeleteHash(&HttpHeaderHandler);
824 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
827 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
828 memset(pHdr, 0, sizeof(OneHttpHeader));
830 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
838 RegisterHeaderHandler(HKEY("RANGE"), Header_HandleContentRange);
839 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
840 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
841 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
842 RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
843 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
844 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
845 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
846 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
848 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
849 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
851 WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
853 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
854 * Short-circuit these requests so we don't have to send them through the full processing loop.
856 WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
857 WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS); /* /winnt */
858 WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
859 WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
860 WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
861 WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
866 HttpNewModule_CONTEXT
867 (ParsedHttpHdrs *httpreq)
869 httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
870 httpreq->this_page = NewStrBufPlain(NULL, SIZ);
874 HttpDetachModule_CONTEXT
875 (ParsedHttpHdrs *httpreq)
877 FlushStrBuf(httpreq->PlainArgs);
878 FlushStrBuf(httpreq->HostHeader);
879 FlushStrBuf(httpreq->this_page);
880 FlushStrBuf(httpreq->PlainArgs);
881 DeleteHash(&httpreq->HTTPHeaders);
882 memset(&httpreq->HR, 0, sizeof(HdrRefs));
886 HttpDestroyModule_CONTEXT
887 (ParsedHttpHdrs *httpreq)
889 FreeStrBuf(&httpreq->this_page);
890 FreeStrBuf(&httpreq->PlainArgs);
891 FreeStrBuf(&httpreq->this_page);
892 FreeStrBuf(&httpreq->PlainArgs);
893 FreeStrBuf(&httpreq->HostHeader);
894 DeleteHash(&httpreq->HTTPHeaders);