35e6ee7b34e5afcae4d83571c5c29f306171b4f5
[citadel.git] / webcit / context_loop.c
1 /*
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.
6  *
7  * Copyright (c) 1996-2011 by the citadel.org team
8  *
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 as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include "webcit.h"
25 #include "webserver.h"
26 #include "modules_init.h"
27
28 /* Only one thread may manipulate SessionList at a time... */
29 pthread_mutex_t SessionListMutex;
30
31 wcsession *SessionList = NULL;  /* Linked list of all webcit sessions */
32
33 pthread_key_t MyConKey;         /* TSD key for MySession() */
34 HashList *HttpReqTypes = NULL;
35 HashList *HttpHeaderHandler = NULL;
36 extern HashList *HandlerHash;
37
38 /* the following two values start at 1 because the initial parent thread counts as one. */
39 int num_threads_existing = 1;           /* Number of worker threads which exist. */
40 int num_threads_executing = 1;          /* Number of worker threads currently executing. */
41
42 void DestroyHttpHeaderHandler(void *V)
43 {
44         OneHttpHeader *pHdr;
45         pHdr = (OneHttpHeader*) V;
46         FreeStrBuf(&pHdr->Val);
47         free(pHdr);
48 }
49
50 void shutdown_sessions(void)
51 {
52         wcsession *sptr;
53         
54         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
55                         sptr->killthis = 1;
56         }
57 }
58
59 void do_housekeeping(void)
60 {
61         wcsession *sptr, *ss;
62         wcsession *sessions_to_kill = NULL;
63
64         /*
65          * Lock the session list, moving any candidates for euthanasia into
66          * a separate list.
67          */
68         CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
69         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
70
71                 /* Kill idle sessions */
72                 if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
73                         sptr->killthis = 1;
74                 }
75
76                 /* Remove sessions flagged for kill */
77                 if (sptr->killthis) {
78
79                         /** remove session from linked list */
80                         if (sptr == SessionList) {
81                                 SessionList = SessionList->next;
82                         }
83                         else for (ss=SessionList;ss!=NULL;ss=ss->next) {
84                                 if (ss->next == sptr) {
85                                         ss->next = ss->next->next;
86                                 }
87                         }
88
89                         sptr->next = sessions_to_kill;
90                         sessions_to_kill = sptr;
91                 }
92         }
93         CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
94
95         /*
96          * Now free up and destroy the culled sessions.
97          */
98         while (sessions_to_kill != NULL) {
99                 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
100                 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
101                 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
102                 sptr = sessions_to_kill->next;
103
104                 session_destroy_modules(&sessions_to_kill);
105                 sessions_to_kill = sptr;
106         }
107 }
108
109 /*
110  * Check the size of our thread pool.  If all threads are executing, spawn another.
111  */
112 void check_thread_pool_size(void)
113 {
114         if (time_to_die) return;                /* don't expand the thread pool during shutdown */
115
116         begin_critical_section(S_SPAWNER);      /* only one of these should run at a time */
117         if (
118                 (num_threads_executing >= num_threads_existing)
119                 && (num_threads_existing < MAX_WORKER_THREADS)
120         ) {
121                 lprintf(3, "%d of %d threads are executing.  Adding another worker thread.\n",
122                         num_threads_executing,
123                         num_threads_existing
124                 );
125                 spawn_another_worker_thread();
126         }
127         end_critical_section(S_SPAWNER);
128 }
129
130
131 /*
132  * Wake up occasionally and clean house
133  */
134 void housekeeping_loop(void)
135 {
136         while (1) {
137                 sleeeeeeeeeep(HOUSEKEEPING);
138                 do_housekeeping();
139         }
140 }
141
142
143 /*
144  * Create a Session id
145  * Generate a unique WebCit session ID (which is not the same thing as the
146  * Citadel session ID).
147  */
148 int GenerateSessionID(void)
149 {
150         static int seq = (-1);
151
152         if (seq < 0) {
153                 seq = (int) time(NULL);
154         }
155                 
156         return ++seq;
157 }
158
159 wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
160 {
161         wcsession *sptr = NULL;
162         wcsession *TheSession = NULL;   
163         
164         if (Hdr->HR.got_auth == AUTH_BASIC) {
165                 GetAuthBasic(Hdr);
166         }
167
168         CtdlLogResult(pthread_mutex_lock(ListMutex));
169         for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
170                 
171                 /* If HTTP-AUTH, look for a session with matching credentials */
172                 switch (Hdr->HR.got_auth)
173                 {
174                 case AUTH_BASIC:
175                         if ( (Hdr->HR.SessionKey != sptr->SessionKey))
176                                 continue;
177                         if ((!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username))) &&
178                             (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password))) ) {
179                                 TheSession = sptr;
180                         }
181                         if (TheSession == NULL)
182                                 lprintf(1, "found sessionkey [%ld], but credentials for [%s|%s] didn't match\n",
183                                         Hdr->HR.SessionKey,ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username));
184                         break;
185                 case AUTH_COOKIE:
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)) {
189                                 TheSession = sptr;
190                         }
191                         break;                       
192                 case NO_AUTH:
193                         break;
194                 }
195         }
196         CtdlLogResult(pthread_mutex_unlock(ListMutex));
197         if (TheSession == NULL)
198                 lprintf(1, "didn't find sessionkey [%ld] for user [%s]\n",
199                         Hdr->HR.SessionKey,ChrPtr(Hdr->c_username));
200         return TheSession;
201 }
202
203 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
204 {
205         wcsession *TheSession;
206         if (!Static)
207                 lprintf(3, "Creating a new session\n");
208         TheSession = (wcsession *) malloc(sizeof(wcsession));
209         memset(TheSession, 0, sizeof(wcsession));
210         TheSession->Hdr = Hdr;
211         TheSession->SessionKey = Hdr->HR.SessionKey;
212         TheSession->serv_sock = (-1);
213
214         pthread_setspecific(MyConKey, (void *)TheSession);
215         
216         /* If we're recreating a session that expired, it's best to give it the same
217          * session number that it had before.  The client browser ought to pick up
218          * the new session number and start using it, but in some rare situations it
219          * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
220          * to get created.
221          */     
222         if (Hdr->HR.desired_session == 0) {
223                 TheSession->wc_session = GenerateSessionID();
224         }
225         else {
226                 TheSession->wc_session = Hdr->HR.desired_session;
227         }
228         Hdr->HR.Static = Static;
229         session_new_modules(TheSession);
230
231         if (Lockable) {
232                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
233
234                 if (ListMutex != NULL)
235                         CtdlLogResult(pthread_mutex_lock(ListMutex));
236
237                 if (wclist != NULL) {
238                         TheSession->nonce = rand();
239                         TheSession->next = *wclist;
240                         *wclist = TheSession;
241                 }
242                 if (ListMutex != NULL)
243                         CtdlLogResult(pthread_mutex_unlock(ListMutex));
244         }
245         return TheSession;
246 }
247
248
249 /* If it's a "force 404" situation then display the error and bail. */
250 void do_404(void)
251 {
252         hprintf("HTTP/1.1 404 Not found\r\n");
253         hprintf("Content-Type: text/plain\r\n");
254         wc_printf("Not found\r\n");
255         end_burst();
256 }
257
258 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
259 {
260         const char *Args;
261         void *vLine, *vHandler;
262         const char *Pos = NULL;
263
264
265         Hdr->HR.ReqLine = Line;
266         /* The requesttype... GET, POST... */
267         StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
268         if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
269             (vLine != NULL))
270         {
271                 Hdr->HR.eReqType = *(long*)vLine;
272         }
273         else {
274                 Hdr->HR.eReqType = eGET;
275                 return 1;
276         }
277         StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
278
279         /* the HTTP Version... */
280         StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
281         StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
282         
283         if (StrLength(Buf) == 0) {
284                 Hdr->HR.eReqType = eGET;
285                 return 1;
286         }
287
288         StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
289
290         /* chop Filename / query arguments */
291         Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
292         if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
293                 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
294         if (Args != NULL) {
295                 Args ++; /* skip the ? */
296                 StrBufPlain(Hdr->PlainArgs, 
297                             Args, 
298                             StrLength(Hdr->HR.ReqLine) -
299                             (Args - ChrPtr(Hdr->HR.ReqLine)));
300                 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
301         } /* don't parse them yet, maybe we don't even care... */
302         
303         /* now lookup what we are going to do with this... */
304         /* skip first slash */
305         StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
306         do {
307                 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
308
309                 GetHash(HandlerHash, SKEY(Buf), &vHandler),
310                 Hdr->HR.Handler = (WebcitHandler*) vHandler;
311                 if (Hdr->HR.Handler == NULL)
312                         break;
313                 /*
314                  * If the request is prefixed by "/webcit" then chop that off.  This
315                  * allows a front end web server to forward all /webcit requests to us
316                  * while still using the same web server port for other things.
317                  */
318                 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
319                         continue;
320                 break;
321         } while (1);
322         /* remove the handlername from the URL */
323         if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
324                 StrBufCutLeft(Hdr->HR.ReqLine, 
325                               Pos - ChrPtr(Hdr->HR.ReqLine));
326         }
327
328         if (Hdr->HR.Handler != NULL) {
329                 if ((Hdr->HR.Handler->Flags & BOGUS) != 0)
330                         return 1;
331                 Hdr->HR.DontNeedAuth = (
332                         ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
333                         ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
334                         );
335         }
336         else {
337                 Hdr->HR.DontNeedAuth = 1; /* Flat request? show him the login screen... */
338         }
339
340         return 0;
341 }
342
343 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
344 {
345         OneHttpHeader *pHdr;
346         void *vHdr;
347         long HKLen;
348         const char *HashKey;
349         HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
350         
351         while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) && 
352                (vHdr != NULL)) {
353                 pHdr = (OneHttpHeader *)vHdr;
354                 if (pHdr->HaveEvaluator)
355                         pHdr->H(pHdr->Val, Hdr);
356
357         }
358         DeleteHashPos(&at);
359         return 0;
360 }
361
362 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
363
364 /*
365  * Read in the request
366  */
367 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
368 {
369         const char *pch, *pchs, *pche;
370         OneHttpHeader *pHdr;
371         StrBuf *Line, *LastLine, *HeaderName;
372         int nLine = 0;
373         void *vF;
374         int isbogus = 0;
375
376         HeaderName = NewStrBuf();
377         LastLine = NULL;
378         do {
379                 nLine ++;
380                 Line = NewStrBufPlain(NULL, SIZ / 4);
381
382                 if (ClientGetLine(Hdr, Line) < 0) return 1;
383
384                 if (StrLength(Line) == 0) {
385                         FreeStrBuf(&Line);
386                         continue;
387                 }
388                 if (nLine == 1) {
389                         Hdr->HTTPHeaders = NewHash(1, NULL);
390                         pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
391                         memset(pHdr, 0, sizeof(OneHttpHeader));
392                         pHdr->Val = Line;
393                         Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
394                         lprintf(9, "%s\n", ChrPtr(Line));
395                         isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
396                         if (isbogus) break;
397                         continue;
398                 }
399
400                 /* Do we need to Unfold? */
401                 if ((LastLine != NULL) && 
402                     (isspace(*ChrPtr(Line)))) {
403                         pch = pchs = ChrPtr(Line);
404                         pche = pchs + StrLength(Line);
405                         while (isspace(*pch) && (pch < pche))
406                                 pch ++;
407                         StrBufCutLeft(Line, pch - pchs);
408                         StrBufAppendBuf(LastLine, Line, 0);
409
410                         FreeStrBuf(&Line);
411                         continue;
412                 }
413
414                 StrBufSanitizeAscii(Line, '§');
415                 StrBufExtract_token(HeaderName, Line, 0, ':');
416
417                 pchs = ChrPtr(Line);
418                 pche = pchs + StrLength(Line);
419                 pch = pchs + StrLength(HeaderName) + 1;
420                 pche = pchs + StrLength(Line);
421                 while ((pch < pche) && isspace(*pch))
422                         pch ++;
423                 StrBufCutLeft(Line, pch - pchs);
424
425                 StrBufUpCase(HeaderName);
426
427                 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
428                 memset(pHdr, 0, sizeof(OneHttpHeader));
429                 pHdr->Val = Line;
430
431                 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
432                     (vF != NULL))
433                 {
434                         OneHttpHeader *FHdr = (OneHttpHeader*) vF;
435                         pHdr->H = FHdr->H;
436                         pHdr->HaveEvaluator = 1;
437                 }
438                 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
439                 LastLine = Line;
440         } while (Line != NULL);
441
442         FreeStrBuf(&HeaderName);
443
444         return isbogus;
445 }
446
447 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
448 {
449         StrBuf *Buf = NewStrBuf();
450
451         if (Hdr->HR.ReqLine != NULL) {
452                 FlushStrBuf(Hdr->HR.ReqLine);
453                 StrBufPlain(Hdr->HR.ReqLine, Line, len);
454         }
455         else {
456                 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
457         }
458         ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
459
460         FreeStrBuf(&Buf);
461 }
462
463 /*
464  * handle one request
465  *
466  * This loop gets called once for every HTTP connection made to WebCit.  At
467  * this entry point we have an HTTP socket with a browser allegedly on the
468  * other end, but we have not yet bound to a WebCit session.
469  *
470  * The job of this function is to locate the correct session and bind to it,
471  * or create a session if necessary and bind to it, then run the WebCit
472  * transaction loop.  Afterwards, we unbind from the session.  When this
473  * function returns, the worker thread is then free to handle another
474  * transaction.
475  */
476 void context_loop(ParsedHttpHdrs *Hdr)
477 {
478         int isbogus = 0;
479         wcsession *TheSession;
480         struct timeval tx_start;
481         struct timeval tx_finish;
482         
483         gettimeofday(&tx_start, NULL);          /* start a stopwatch for performance timing */
484
485         /*
486          * Find out what it is that the web browser is asking for
487          */
488         isbogus = ReadHTTPRequest(Hdr);
489
490         Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
491         if (!isbogus)
492                 isbogus = AnalyseHeaders(Hdr);
493
494         if ((isbogus) ||
495             ((Hdr->HR.Handler != NULL) &&
496              ((Hdr->HR.Handler->Flags & BOGUS) != 0)))
497         {
498                 wcsession *Bogus;
499
500                 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
501
502                 do_404();
503
504                 lprintf(9, "HTTP: 404 [%ld.%06ld] %s %s \n",
505                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
506                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
507                         ReqStrs[Hdr->HR.eReqType],
508                         ChrPtr(Hdr->this_page)
509                         );
510                 session_detach_modules(Bogus);
511                 session_destroy_modules(&Bogus);
512                 return;
513         }
514
515         if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
516         {
517                 wcsession *Static;
518                 Static = CreateSession(0, 1, NULL, Hdr, NULL);
519                 
520                 Hdr->HR.Handler->F();
521
522                 /* How long did this transaction take? */
523                 gettimeofday(&tx_finish, NULL);
524                 
525 #ifdef TECH_PREVIEW
526                 if ((Hdr->HR.Handler != NULL) ||
527                     ((Hdr->HR.Handler->Flags & LOGCHATTY) == 0))
528 #endif
529                         lprintf(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
530                                 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
531                                 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
532                                 ReqStrs[Hdr->HR.eReqType],
533                                 ChrPtr(Hdr->this_page)
534                                 );
535                 session_detach_modules(Static);
536                 session_destroy_modules(&Static);
537                 return;
538         }
539
540         if (Hdr->HR.got_auth == AUTH_BASIC) {
541                 CheckAuthBasic(Hdr);
542         }
543
544         /*
545          * See if there's an existing session open with the desired ID or user/pass
546          */
547         TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
548
549         /*
550          * Create a new session if we have to
551          */
552         if (TheSession == NULL) {
553                 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
554
555                 if (    (StrLength(Hdr->c_username) == 0)
556                         && (!Hdr->HR.DontNeedAuth)
557                         && (Hdr->HR.Handler != NULL)
558                         && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
559                 ) {
560                         OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
561                         Hdr->HR.prohibit_caching = 1;                           
562                 }
563                 
564                 if (StrLength(Hdr->c_language) > 0) {
565                         lprintf(9, "Session cookie requests language '%s'\n", ChrPtr(Hdr->c_language));
566                         set_selected_language(ChrPtr(Hdr->c_language));
567                         go_selected_language();
568                 }
569         }
570
571         /*
572          * A future improvement might be to check the session integrity
573          * at this point before continuing.
574          */
575
576         /*
577          * Bind to the session and perform the transaction
578          */
579         CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
580         pthread_setspecific(MyConKey, (void *)TheSession);
581         
582         TheSession->lastreq = time(NULL);                       /* log */
583         TheSession->Hdr = Hdr;
584
585         session_attach_modules(TheSession);
586         session_loop();                         /* do transaction */
587
588
589         /* How long did this transaction take? */
590         gettimeofday(&tx_finish, NULL);
591         
592
593 #ifdef TECH_PREVIEW
594         if ((Hdr->HR.Handler != NULL) &&
595             ((Hdr->HR.Handler->Flags & LOGCHATTY) == 0))
596 #endif
597                 lprintf(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
598                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
599                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
600                         ReqStrs[Hdr->HR.eReqType],
601                         ChrPtr(Hdr->this_page)
602                         );
603
604         session_detach_modules(TheSession);
605
606         TheSession->Hdr = NULL;
607         CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
608 }
609
610 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
611 {
612         wcsession *WCC = WC;
613         StrBufAppendPrintf(Target, "%ld",
614                            (WCC != NULL)? WCC->nonce:0);                   
615 }
616
617 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
618 {
619         StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
620 }
621
622 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
623 {
624         hdr->HR.ContentLength = StrToi(Line);
625 }
626
627 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
628 {
629         hdr->HR.ContentType = Line;
630 }
631
632
633 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
634 {
635         if (site_prefix == NULL) {
636                 site_prefix = NewStrBuf();
637                 StrBufAppendPrintf(site_prefix, "%s://", (is_https ? "https" : "http") );
638                 StrBufAppendBuf(site_prefix, Line, 0);
639         }
640 }
641
642 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
643 {
644         if ( (follow_xff) && (site_prefix == NULL)) {
645                 site_prefix = NewStrBuf();
646                 StrBufAppendPrintf(site_prefix, "http://");     /* this is naive; do something about it */
647                 StrBufAppendBuf(site_prefix, Line, 0);
648         }
649 }
650
651
652 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
653 {
654         hdr->HR.browser_host = Line;
655
656         while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
657                 StrBufRemove_token(hdr->HR.browser_host, 0, ',');
658         }
659         StrBufTrim(hdr->HR.browser_host);
660 }
661
662 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
663 {
664         hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
665 }
666
667 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
668 {
669         /*
670          * Can we compress?
671          */
672         if (strstr(&ChrPtr(Line)[16], "gzip")) {
673                 hdr->HR.gzip_ok = 1;
674         }
675 }
676 const char *ReqStrs[eNONE] = {
677         "GET",
678         "POST",
679         "OPTIONS",
680         "PROPFIND",
681         "PUT",
682         "DELETE",
683         "HEAD",
684         "MOVE",
685         "COPY"
686 };
687
688 void
689 ServerStartModule_CONTEXT
690 (void)
691 {
692         long *v;
693         HttpReqTypes = NewHash(1, NULL);
694         HttpHeaderHandler = NewHash(1, NULL);
695
696         v = malloc(sizeof(long));
697         *v = eGET;
698         Put(HttpReqTypes, HKEY("GET"), v, NULL);
699
700         v = malloc(sizeof(long));
701         *v = ePOST;
702         Put(HttpReqTypes, HKEY("POST"), v, NULL);
703
704         v = malloc(sizeof(long));
705         *v = eOPTIONS;
706         Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
707
708         v = malloc(sizeof(long));
709         *v = ePROPFIND;
710         Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
711
712         v = malloc(sizeof(long));
713         *v = ePUT;
714         Put(HttpReqTypes, HKEY("PUT"), v, NULL);
715
716         v = malloc(sizeof(long));
717         *v = eDELETE;
718         Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
719
720         v = malloc(sizeof(long));
721         *v = eHEAD;
722         Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
723
724         v = malloc(sizeof(long));
725         *v = eMOVE;
726         Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
727
728         v = malloc(sizeof(long));
729         *v = eCOPY;
730         Put(HttpReqTypes, HKEY("COPY"), v, NULL);
731 }
732
733 void 
734 ServerShutdownModule_CONTEXT
735 (void)
736 {
737         DeleteHash(&HttpReqTypes);
738         DeleteHash(&HttpHeaderHandler);
739 }
740
741 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
742 {
743         OneHttpHeader *pHdr;
744         pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
745         memset(pHdr, 0, sizeof(OneHttpHeader));
746         pHdr->H = F;
747         Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
748 }
749
750
751 void 
752 InitModule_CONTEXT
753 (void)
754 {
755         RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
756         RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
757         RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
758         RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost);        /* NGinX way... */
759         RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
760         RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
761         RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
762         RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
763
764         RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
765         RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
766
767         WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
768 /*
769  * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
770  * Short-circuit these requests so we don't have to send them through the full processing loop.
771  */
772         WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS);           /* /root.exe - Worms and trojans and viruses, oh my! */
773         WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS);         /* /winnt */
774         WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
775         WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS);              /* Broken Microsoft DAV implementation */
776         WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS);          /* Stoopid MSOffice thinks everyone is IIS */
777         WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS);    /* Exploit found in the wild January 2009 */
778 }
779         
780
781 void 
782 HttpNewModule_CONTEXT
783 (ParsedHttpHdrs *httpreq)
784 {
785         httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
786         httpreq->this_page = NewStrBufPlain(NULL, SIZ);
787 }
788
789 void 
790 HttpDetachModule_CONTEXT
791 (ParsedHttpHdrs *httpreq)
792 {
793         FlushStrBuf(httpreq->PlainArgs);
794         FlushStrBuf(httpreq->this_page);
795         FlushStrBuf(httpreq->PlainArgs);
796         DeleteHash(&httpreq->HTTPHeaders);
797         memset(&httpreq->HR, 0, sizeof(HdrRefs));
798 }
799
800 void 
801 HttpDestroyModule_CONTEXT
802 (ParsedHttpHdrs *httpreq)
803 {
804         FreeStrBuf(&httpreq->this_page);
805         FreeStrBuf(&httpreq->PlainArgs);
806         FreeStrBuf(&httpreq->this_page);
807         FreeStrBuf(&httpreq->PlainArgs);
808         DeleteHash(&httpreq->HTTPHeaders);
809
810 }