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