3 * This is the other half of the webserver. It handles the task of hooking
4 * up HTTP requests with the sessions they belong to, using HTTP cookies to
5 * keep track of things. If the HTTP request doesn't belong to any currently
6 * active session, a new session is started.
8 * Copyright (c) 1996-2012 by the citadel.org team
10 * This program is open source software. You can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include "webserver.h"
21 #include "modules_init.h"
23 /* Only one thread may manipulate SessionList at a time... */
24 pthread_mutex_t SessionListMutex;
26 wcsession *SessionList = NULL; /* Linked list of all webcit sessions */
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) {
43 pHdr = (OneHttpHeader *) V;
44 FreeStrBuf(&pHdr->Val);
48 void shutdown_sessions(void) {
51 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
56 void do_housekeeping(void) {
58 wcsession *sessions_to_kill = NULL;
62 * Lock the session list, moving any candidates for euthanasia into
66 CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
67 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
69 the_time = time(NULL);
70 /* Kill idle sessions */
71 if ((sptr->inuse == 0) && ((the_time - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT)) {
72 syslog(LOG_DEBUG, "Timeout session %d", sptr->wc_session);
76 /* Remove sessions flagged for kill */
79 /* remove session from linked list */
80 if (sptr == SessionList) {
81 SessionList = SessionList->next;
84 for (ss = SessionList; ss != NULL; ss = ss->next) {
85 if (ss->next == sptr) {
86 ss->next = ss->next->next;
90 sptr->next = sessions_to_kill;
91 sessions_to_kill = sptr;
94 CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
97 * Now free up and destroy the culled sessions.
99 while (sessions_to_kill != NULL) {
100 syslog(LOG_DEBUG, "Destroying session %d", sessions_to_kill->wc_session);
101 sptr = sessions_to_kill->next;
102 session_destroy_modules(&sessions_to_kill);
103 sessions_to_kill = sptr;
108 * Check the size of our thread pool. If all threads are executing, spawn another.
110 void check_thread_pool_size(void) {
112 return; /* don't expand the thread pool during shutdown */
114 begin_critical_section(S_SPAWNER); /* only one of these should run at a time */
115 if ((num_threads_executing >= num_threads_existing)
116 && (num_threads_existing < MAX_WORKER_THREADS)
118 syslog(LOG_DEBUG, "%d of %d threads are executing. Adding another worker thread.",
119 num_threads_executing, num_threads_existing);
120 spawn_another_worker_thread();
122 end_critical_section(S_SPAWNER);
127 * Wake up occasionally and clean house
129 void housekeeping_loop(void) {
131 sleeeeeeeeeep(HOUSEKEEPING);
138 * Create a Session id
139 * Generate a unique WebCit session ID (which is not the same thing as the
140 * Citadel session ID).
142 int GenerateSessionID(void) {
143 static int seq = (-1);
146 seq = (int) time(NULL);
152 wcsession *FindSession(wcsession ** wclist, ParsedHttpHdrs * Hdr, pthread_mutex_t * ListMutex) {
153 wcsession *sptr = NULL;
154 wcsession *TheSession = NULL;
156 if (Hdr->HR.got_auth == AUTH_BASIC) {
160 CtdlLogResult(pthread_mutex_lock(ListMutex));
161 for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
163 /* If HTTP-AUTH, look for a session with matching credentials */
164 switch (Hdr->HR.got_auth) {
166 if ((!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username)))
167 && (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password)))
168 && (sptr->killthis == 0)
171 syslog(LOG_DEBUG, "Matched a session with the same http-auth");
176 /* If cookie-session, look for a session with matching session ID */
177 if ((Hdr->HR.desired_session != 0)
178 && (sptr->wc_session == Hdr->HR.desired_session)
181 syslog(LOG_DEBUG, "Matched a session with the same cookie");
186 /* Any unbound session is a candidate */
187 if ((sptr->wc_session == 0) && (sptr->inuse == 0)) {
189 syslog(LOG_DEBUG, "Reusing an unbound session");
195 CtdlLogResult(pthread_mutex_unlock(ListMutex));
196 if (TheSession == NULL) {
197 syslog(LOG_DEBUG, "No existing session was matched");
202 wcsession *CreateSession(int Lockable, int Static, wcsession ** wclist, ParsedHttpHdrs * Hdr, pthread_mutex_t * ListMutex) {
203 wcsession *TheSession;
204 TheSession = (wcsession *) malloc(sizeof(wcsession));
205 memset(TheSession, 0, sizeof(wcsession));
206 TheSession->Hdr = Hdr;
207 TheSession->serv_sock = (-1);
208 TheSession->lastreq = time(NULL);;
210 pthread_setspecific(MyConKey, (void *) TheSession);
212 /* If we're recreating a session that expired, it's best to give it the same
213 * session number that it had before. The client browser ought to pick up
214 * the new session number and start using it, but in some rare situations it
215 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
218 if (Hdr->HR.desired_session == 0) {
219 TheSession->wc_session = GenerateSessionID();
220 syslog(LOG_DEBUG, "Created new session %d", TheSession->wc_session);
223 TheSession->wc_session = Hdr->HR.desired_session;
224 syslog(LOG_DEBUG, "Re-created session %d", TheSession->wc_session);
226 Hdr->HR.Static = Static;
227 session_new_modules(TheSession);
230 pthread_mutex_init(&TheSession->SessionMutex, NULL);
232 if (ListMutex != NULL)
233 CtdlLogResult(pthread_mutex_lock(ListMutex));
235 if (wclist != NULL) {
236 TheSession->nonce = rand();
237 TheSession->next = *wclist;
238 *wclist = TheSession;
240 if (ListMutex != NULL)
241 CtdlLogResult(pthread_mutex_unlock(ListMutex));
247 /* If it's a "force 404" situation then display the error and bail. */
249 hprintf("HTTP/1.1 404 Not found\r\n");
250 hprintf("Content-Type: text/plain\r\n");
251 wc_printf("Not found\r\n");
255 int ReadHttpSubject(ParsedHttpHdrs * Hdr, StrBuf * Line, StrBuf * Buf) {
257 void *vLine, *vHandler;
258 const char *Pos = NULL;
260 Hdr->HR.ReqLine = Line;
261 /* The requesttype... GET, POST... */
262 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
263 if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) && (vLine != NULL)) {
264 Hdr->HR.eReqType = *(long *) vLine;
267 Hdr->HR.eReqType = eGET;
270 StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
272 /* the HTTP Version... */
273 StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
274 StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
276 if (StrLength(Buf) == 0) {
277 Hdr->HR.eReqType = eGET;
281 StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
283 /* chop Filename / query arguments */
284 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
285 if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames. */
286 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
288 Args++; /* skip the ? */
289 StrBufPlain(Hdr->PlainArgs, Args, StrLength(Hdr->HR.ReqLine) - (Args - ChrPtr(Hdr->HR.ReqLine)));
290 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
291 } /* don't parse them yet, maybe we don't even care... */
293 /* now lookup what we are going to do with this... */
294 /* skip first slash */
295 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
297 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
299 GetHash(HandlerHash, SKEY(Buf), &vHandler), Hdr->HR.Handler = (WebcitHandler *) vHandler;
300 if (Hdr->HR.Handler == NULL)
303 * If the request is prefixed by "/webcit" then chop that off. This
304 * allows a front end web server to forward all /webcit requests to us
305 * while still using the same web server port for other things.
307 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
311 /* remove the handlername from the URL */
312 if ((Pos != NULL) && (Pos != StrBufNOTNULL)) {
313 StrBufCutLeft(Hdr->HR.ReqLine, Pos - ChrPtr(Hdr->HR.ReqLine));
316 if (Hdr->HR.Handler != NULL) {
317 if ((Hdr->HR.Handler->Flags & BOGUS) != 0) {
320 Hdr->HR.DontNeedAuth = (((Hdr->HR.Handler->Flags & ISSTATIC) != 0) || ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
324 /* If this is a "flat" request for the root, display the configured landing page. */
326 StrBuf *NewLine = NewStrBuf();
327 Hdr->HR.DontNeedAuth = 1;
328 StrBufAppendPrintf(NewLine, "GET /landing?go=%s?failvisibly=1 HTTP/1.0", ChrPtr(Buf));
330 syslog(LOG_DEBUG, "Replacing with: %s", ChrPtr(NewLine));
331 return_value = ReadHttpSubject(Hdr, NewLine, Buf);
332 FreeStrBuf(&NewLine);
339 int AnalyseHeaders(ParsedHttpHdrs * Hdr) {
344 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
346 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) && (vHdr != NULL)) {
347 pHdr = (OneHttpHeader *) vHdr;
348 if (pHdr->HaveEvaluator)
349 pHdr->H(pHdr->Val, Hdr);
358 * Read in the request
360 int ReadHTTPRequest(ParsedHttpHdrs * Hdr) {
361 const char *pch, *pchs, *pche;
363 StrBuf *Line, *LastLine, *HeaderName;
368 HeaderName = NewStrBuf();
372 Line = NewStrBufPlain(NULL, SIZ / 4);
374 if (ClientGetLine(Hdr, Line) < 0) {
380 if (StrLength(Line) == 0) {
385 Hdr->HTTPHeaders = NewHash(1, NULL);
386 pHdr = (OneHttpHeader *) malloc(sizeof(OneHttpHeader));
387 memset(pHdr, 0, sizeof(OneHttpHeader));
389 Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
390 if (verbose || strstr(ChrPtr(Line), "sslg") == NULL) {
391 syslog(LOG_DEBUG, "%s", ChrPtr(Line));
393 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
399 /* Do we need to Unfold? */
400 if ((LastLine != NULL) && (isspace(*ChrPtr(Line)))) {
401 pch = pchs = ChrPtr(Line);
402 pche = pchs + StrLength(Line);
403 while (isspace(*pch) && (pch < pche)) {
406 StrBufCutLeft(Line, pch - pchs);
407 StrBufAppendBuf(LastLine, Line, 0);
413 StrBufSanitizeAscii(Line, (char) 0xa7);
414 StrBufExtract_token(HeaderName, Line, 0, ':');
417 pche = pchs + StrLength(Line);
418 pch = pchs + StrLength(HeaderName) + 1;
419 pche = pchs + StrLength(Line);
420 while ((pch < pche) && isspace(*pch)) {
423 StrBufCutLeft(Line, pch - pchs);
424 StrBufUpCase(HeaderName);
426 pHdr = (OneHttpHeader *) malloc(sizeof(OneHttpHeader));
427 memset(pHdr, 0, sizeof(OneHttpHeader));
430 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) && (vF != NULL)) {
431 OneHttpHeader *FHdr = (OneHttpHeader *) vF;
433 pHdr->HaveEvaluator = 1;
435 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
437 } while (Line != NULL);
439 FreeStrBuf(&HeaderName);
444 void OverrideRequest(ParsedHttpHdrs * Hdr, const char *Line, long len) {
445 StrBuf *Buf = NewStrBuf();
447 if (Hdr->HR.ReqLine != NULL) {
448 FlushStrBuf(Hdr->HR.ReqLine);
449 StrBufPlain(Hdr->HR.ReqLine, Line, len);
452 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
454 ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
462 * This loop gets called once for every HTTP connection made to WebCit. At
463 * this entry point we have an HTTP socket with a browser allegedly on the
464 * other end, but we have not yet bound to a WebCit session.
466 * The job of this function is to locate the correct session and bind to it,
467 * or create a session if necessary and bind to it, then run the WebCit
468 * transaction loop. Afterwards, we unbind from the session. When this
469 * function returns, the worker thread is then free to handle another
472 void context_loop(ParsedHttpHdrs * Hdr) {
474 wcsession *TheSession;
475 struct timeval tx_start;
476 struct timeval tx_finish;
477 int session_may_be_reused = 1;
480 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
483 * Find out what it is that the web browser is asking for
485 isbogus = ReadHTTPRequest(Hdr);
487 Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
490 isbogus = AnalyseHeaders(Hdr);
494 || ((Hdr->HR.Handler != NULL)
495 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
498 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
500 syslog(LOG_WARNING, "HTTP: 404 [%ld.%06ld] %s %s",
501 ((tx_finish.tv_sec * 1000000 + tx_finish.tv_usec) -
502 (tx_start.tv_sec * 1000000 + tx_start.tv_usec)) / 1000000,
503 ((tx_finish.tv_sec * 1000000 + tx_finish.tv_usec) -
504 (tx_start.tv_sec * 1000000 + tx_start.tv_usec)) % 1000000, ReqStrs[Hdr->HR.eReqType], ChrPtr(Hdr->this_page)
506 session_detach_modules(Bogus);
507 session_destroy_modules(&Bogus);
511 if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0)) {
513 Static = CreateSession(0, 1, NULL, Hdr, NULL);
515 Hdr->HR.Handler->F();
517 /* How long did this transaction take? */
518 gettimeofday(&tx_finish, NULL);
521 syslog(LOG_DEBUG, "HTTP: 200 [%ld.%06ld] %s %s",
522 ((tx_finish.tv_sec * 1000000 + tx_finish.tv_usec) -
523 (tx_start.tv_sec * 1000000 + tx_start.tv_usec)) / 1000000,
524 ((tx_finish.tv_sec * 1000000 + tx_finish.tv_usec) -
525 (tx_start.tv_sec * 1000000 + tx_start.tv_usec)) % 1000000, ReqStrs[Hdr->HR.eReqType],
526 ChrPtr(Hdr->this_page)
528 session_detach_modules(Static);
529 session_destroy_modules(&Static);
533 if (Hdr->HR.got_auth == AUTH_BASIC) {
537 if (Hdr->HR.got_auth) {
538 session_may_be_reused = 0;
542 * See if there's an existing session open with any of:
543 * - The desired Session ID
544 * - A matching http-auth username and password
545 * - An unbound session flagged as reusable
547 TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
550 * If there were no qualifying sessions, then create a new one.
552 if ((TheSession == NULL) || (TheSession->killthis != 0)) {
553 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
557 * Reject transactions which require http-auth, if http-auth was not provided
559 if ((StrLength(Hdr->c_username) == 0)
560 && (!Hdr->HR.DontNeedAuth)
561 && (Hdr->HR.Handler != NULL)
562 && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
564 syslog(LOG_DEBUG, "http-auth required but not provided");
565 OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
566 Hdr->HR.prohibit_caching = 1;
570 * A future improvement might be to check the session integrity
571 * at this point before continuing.
575 * Bind to the session and perform the transaction
578 CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
579 pthread_setspecific(MyConKey, (void *) TheSession);
581 TheSession->inuse = 1; /* mark the session as bound */
582 TheSession->isFailure = 0; /* reset evntually existing error flags */
583 TheSession->lastreq = now; /* log */
584 TheSession->Hdr = Hdr;
587 * If a language was requested via a cookie, select that language now.
589 if (StrLength(Hdr->c_language) > 0) {
591 syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
593 set_selected_language(ChrPtr(Hdr->c_language));
594 go_selected_language();
600 session_attach_modules(TheSession);
603 /* How long did this transaction take? */
604 gettimeofday(&tx_finish, NULL);
606 if (verbose || strstr(ChrPtr(Hdr->this_page), "sslg") == NULL) {
607 syslog(LOG_INFO, "HTTP: 200 [%ld.%06ld] %s %s",
608 ((tx_finish.tv_sec * 1000000 + tx_finish.tv_usec) -
609 (tx_start.tv_sec * 1000000 + tx_start.tv_usec)) / 1000000,
610 ((tx_finish.tv_sec * 1000000 + tx_finish.tv_usec) -
611 (tx_start.tv_sec * 1000000 + tx_start.tv_usec)) % 1000000, ReqStrs[Hdr->HR.eReqType], ChrPtr(Hdr->this_page)
614 session_detach_modules(TheSession);
616 /* If *this* very transaction did not explicitly specify a session cookie,
617 * and it did not log in, we want to flag the session as a candidate for
618 * re-use by the next unbound client that comes along. This keeps our session
619 * table from getting bombarded with new sessions when, for example, a web
620 * spider crawls the site without using cookies.
622 if ((session_may_be_reused) && (!TheSession->logged_in)) {
623 TheSession->wc_session = 0; /* flag as available for re-use */
624 TheSession->selected_language = -1; /* clear any non-default language setting */
627 TheSession->Hdr = NULL;
628 TheSession->inuse = 0; /* mark the session as unbound */
629 CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
632 void tmplput_nonce(StrBuf * Target, WCTemplputParams * TP) {
634 StrBufAppendPrintf(Target, "%ld", (WCC != NULL) ? WCC->nonce : 0);
637 void tmplput_current_user(StrBuf * Target, WCTemplputParams * TP) {
638 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
641 void Header_HandleContentLength(StrBuf * Line, ParsedHttpHdrs * hdr) {
642 hdr->HR.ContentLength = StrToi(Line);
645 void Header_HandleContentType(StrBuf * Line, ParsedHttpHdrs * hdr) {
646 hdr->HR.ContentType = Line;
650 void Header_HandleHost(StrBuf * Line, ParsedHttpHdrs * hdr) {
651 if (hdr->HostHeader != NULL) {
652 FreeStrBuf(&hdr->HostHeader);
654 hdr->HostHeader = NewStrBuf();
655 StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http"));
656 StrBufAppendBuf(hdr->HostHeader, Line, 0);
659 void Header_HandleXFFHost(StrBuf * Line, ParsedHttpHdrs * hdr) {
663 if (hdr->HostHeader != NULL) {
664 FreeStrBuf(&hdr->HostHeader);
667 hdr->HostHeader = NewStrBuf();
668 StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
669 StrBufAppendBuf(hdr->HostHeader, Line, 0);
673 void Header_HandleXFF(StrBuf * Line, ParsedHttpHdrs * hdr) {
674 hdr->HR.browser_host = Line;
676 while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
677 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
679 StrBufTrim(hdr->HR.browser_host);
682 void Header_HandleIfModSince(StrBuf * Line, ParsedHttpHdrs * hdr) {
683 hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
686 void Header_HandleAcceptEncoding(StrBuf * Line, ParsedHttpHdrs * hdr) {
690 if (strstr(&ChrPtr(Line)[16], "gzip")) {
695 void Header_HandleContentRange(StrBuf * Line, ParsedHttpHdrs * hdr) {
696 const char *PRange = ChrPtr(Line);
698 while ((*PRange != '=') && (*PRange != '\0'))
702 if ((*PRange == '\0'))
705 hdr->RangeStart = atol(PRange);
707 while (isdigit(*PRange))
712 if ((*PRange == '\0'))
715 hdr->RangeTil = atol(PRange);
718 const char *ReqStrs[eNONE] = {
731 void ServerStartModule_CONTEXT(void) {
733 HttpReqTypes = NewHash(1, NULL);
734 HttpHeaderHandler = NewHash(1, NULL);
736 v = malloc(sizeof(long));
738 Put(HttpReqTypes, HKEY("GET"), v, NULL);
740 v = malloc(sizeof(long));
742 Put(HttpReqTypes, HKEY("POST"), v, NULL);
744 v = malloc(sizeof(long));
746 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
748 v = malloc(sizeof(long));
750 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
752 v = malloc(sizeof(long));
754 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
756 v = malloc(sizeof(long));
758 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
760 v = malloc(sizeof(long));
762 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
764 v = malloc(sizeof(long));
766 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
768 v = malloc(sizeof(long));
770 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
772 v = malloc(sizeof(long));
774 Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
777 void ServerShutdownModule_CONTEXT(void) {
778 DeleteHash(&HttpReqTypes);
779 DeleteHash(&HttpHeaderHandler);
782 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F) {
784 pHdr = (OneHttpHeader *) malloc(sizeof(OneHttpHeader));
785 memset(pHdr, 0, sizeof(OneHttpHeader));
787 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
791 void InitModule_CONTEXT(void) {
792 RegisterHeaderHandler(HKEY("RANGE"), Header_HandleContentRange);
793 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
794 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
795 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
796 RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
797 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
798 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
799 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
800 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
802 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
803 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
805 WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS | COOKIEUNNEEDED);
808 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
809 * Short-circuit these requests so we don't have to send them through the full processing loop.
811 WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS | BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
812 WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS | BOGUS); /* /winnt */
813 WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS | BOGUS);
814 WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS | BOGUS); /* Broken Microsoft DAV implementation */
815 WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS | BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
816 WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS | BOGUS); /* Exploit found in the wild January 2009 */
820 void HttpNewModule_CONTEXT(ParsedHttpHdrs * httpreq) {
821 httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
822 httpreq->this_page = NewStrBufPlain(NULL, SIZ);
825 void HttpDetachModule_CONTEXT(ParsedHttpHdrs * httpreq) {
826 FlushStrBuf(httpreq->PlainArgs);
827 FlushStrBuf(httpreq->HostHeader);
828 FlushStrBuf(httpreq->this_page);
829 FlushStrBuf(httpreq->PlainArgs);
830 DeleteHash(&httpreq->HTTPHeaders);
831 memset(&httpreq->HR, 0, sizeof(HdrRefs));
834 void HttpDestroyModule_CONTEXT(ParsedHttpHdrs * httpreq) {
835 FreeStrBuf(&httpreq->this_page);
836 FreeStrBuf(&httpreq->PlainArgs);
837 FreeStrBuf(&httpreq->this_page);
838 FreeStrBuf(&httpreq->PlainArgs);
839 FreeStrBuf(&httpreq->HostHeader);
840 DeleteHash(&httpreq->HTTPHeaders);