8347030f9187a8e9f0cd1d34d84f2d6aec565c8d
[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
140 /*
141  * lingering_close() a`la Apache. see
142  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
143  */
144 int lingering_close(int fd)
145 {
146         char buf[SIZ];
147         int i;
148         fd_set set;
149         struct timeval tv, start;
150
151         gettimeofday(&start, NULL);
152         shutdown(fd, 1);
153         do {
154                 do {
155                         gettimeofday(&tv, NULL);
156                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
157                         tv.tv_usec = start.tv_usec - tv.tv_usec;
158                         if (tv.tv_usec < 0) {
159                                 tv.tv_sec--;
160                                 tv.tv_usec += 1000000;
161                         }
162                         FD_ZERO(&set);
163                         FD_SET(fd, &set);
164                         i = select(fd + 1, &set, NULL, NULL, &tv);
165                 } while (i == -1 && errno == EINTR);
166
167                 if (i <= 0)
168                         break;
169
170                 i = read(fd, buf, sizeof buf);
171         } while (i != 0 && (i != -1 || errno == EINTR));
172
173         return close(fd);
174 }
175
176
177
178
179 /**
180  * \brief Detects a 'mobile' user agent 
181  */
182 int is_mobile_ua(char *user_agent) {
183       if (strstr(user_agent,"iPhone OS") != NULL) {
184         return 1;
185       } else if (strstr(user_agent,"Windows CE") != NULL) {
186         return 1;
187       } else if (strstr(user_agent,"SymbianOS") != NULL) {
188         return 1;
189       } else if (strstr(user_agent, "Opera Mobi") != NULL) {
190         return 1;
191       } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
192               /*  For some reason a new install of Opera 9.51beta decided to spoof. */
193           return 1;
194           }
195       return 0;
196 }
197
198 /* If it's a "force 404" situation then display the error and bail. */
199 void do_404(void)
200 {
201         hprintf("HTTP/1.1 404 Not found\r\n");
202         hprintf("Content-Type: text/plain\r\n");
203         wprintf("Not found\r\n");
204         end_burst();
205 }
206
207 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
208 {
209         const char *Args;
210         void *vLine, *vHandler;
211         const char *Pos = NULL;
212
213
214         Hdr->ReqLine = Line;
215         /* The requesttype... GET, POST... */
216         StrBufExtract_token(Buf, Hdr->ReqLine, 0, ' ');
217         if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
218             (vLine != NULL))
219         {
220                 Hdr->eReqType = *(long*)vLine;
221         }
222         else {
223                 Hdr->eReqType = eGET;
224                 return 1;
225         }
226         StrBufCutLeft(Hdr->ReqLine, StrLength(Buf) + 1);
227
228         /* the HTTP Version... */
229         StrBufExtract_token(Buf, Hdr->ReqLine, 1, ' ');
230         StrBufCutRight(Hdr->ReqLine, StrLength(Buf) + 1);
231         
232         if (StrLength(Buf) == 0) {
233                 Hdr->eReqType = eGET;
234                 return 1;
235         }
236
237         Hdr->this_page = NewStrBufDup(Hdr->ReqLine);
238         /* chop Filename / query arguments */
239         Args = strchr(ChrPtr(Hdr->ReqLine), '?');
240         if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
241                 Args = strchr(ChrPtr(Hdr->ReqLine), '&');
242         if (Args != NULL) {
243                 Args ++; /* skip the ? */
244                 Hdr->PlainArgs = NewStrBufPlain(
245                         Args, 
246                         StrLength(Hdr->ReqLine) -
247                         (Args - ChrPtr(Hdr->ReqLine)));
248                 StrBufCutAt(Hdr->ReqLine, 0, Args - 1);
249         } /* don't parse them yet, maybe we don't even care... */
250         
251         /* now lookup what we are going to do with this... */
252         /* skip first slash */
253         StrBufExtract_NextToken(Buf, Hdr->ReqLine, &Pos, '/');
254         do {
255                 StrBufExtract_NextToken(Buf, Hdr->ReqLine, &Pos, '/');
256
257                 GetHash(HandlerHash, SKEY(Buf), &vHandler),
258                 Hdr->Handler = (WebcitHandler*) vHandler;
259                 if (Hdr->Handler == NULL)
260                         break;
261                 /*
262                  * If the request is prefixed by "/webcit" then chop that off.  This
263                  * allows a front end web server to forward all /webcit requests to us
264                  * while still using the same web server port for other things.
265                  */
266                 if ((Hdr->Handler->Flags & URLNAMESPACE) != 0)
267                         continue;
268                 break;
269         } while (1);
270         /* remove the handlername from the URL */
271         if (Pos != NULL) {
272                 StrBufCutLeft(Hdr->ReqLine, 
273                               Pos - ChrPtr(Hdr->ReqLine));
274         }
275
276         if (Hdr->Handler != NULL) {
277                 if ((Hdr->Handler->Flags & BOGUS) != 0)
278                         return 1;
279                 Hdr->DontNeedAuth = (Hdr->Handler->Flags & ISSTATIC) != 0;
280         }
281
282         Hdr->HTTPHeaders = NewHash(1, NULL);
283         return 0;
284 }
285
286 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
287 {
288         OneHttpHeader *pHdr;
289         void *vHdr;
290         long HKLen;
291         const char *HashKey;
292         HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
293         
294         while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) && 
295                (vHdr != NULL)) {
296                 pHdr = (OneHttpHeader *)vHdr;
297                 if (pHdr->HaveEvaluator)
298                         pHdr->H(pHdr->Val, Hdr);
299
300         }
301         DeleteHashPos(&at);
302         return 0;
303 }
304
305 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
306
307 /*
308  * Read in the request
309  */
310 int ReadHTTPRequset (ParsedHttpHdrs *Hdr)
311 {
312         const char *pch, *pchs, *pche;
313         OneHttpHeader *pHdr;
314         StrBuf *Line, *LastLine, *HeaderName;
315         int nLine = 0;
316         void *vF;
317         int isbogus = 0;
318
319         HeaderName = NewStrBuf();
320         Hdr->ReadBuf = NewStrBuf();
321         LastLine = NULL;
322         do {
323                 nLine ++;
324                 Line = NewStrBuf();
325
326                 if (ClientGetLine(&Hdr->http_sock, Line, Hdr->ReadBuf, &Hdr->Pos) < 0) return 1;
327
328                 if (StrLength(Line) == 0) {
329                         FreeStrBuf(&Line);
330                         continue;
331                 }
332                 if (nLine == 1) {
333                         lprintf(9, "%s\n", ChrPtr(Line));
334                         isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
335                         if (isbogus) break;
336                         continue;
337                 }
338
339                 /* Do we need to Unfold? */
340                 if ((LastLine != NULL) && 
341                     (isspace(*ChrPtr(Line)))) {
342                         pch = pchs = ChrPtr(Line);
343                         pche = pchs + StrLength(Line);
344                         while (isspace(*pch) && (pch < pche))
345                                 pch ++;
346                         StrBufCutLeft(Line, pch - pchs);
347                         StrBufAppendBuf(LastLine, Line, 0);
348
349                         FreeStrBuf(&Line);
350                         continue;
351                 }
352
353                 StrBufSanitizeAscii(Line, '§');
354                 StrBufExtract_token(HeaderName, Line, 0, ':');
355
356                 pchs = ChrPtr(Line);
357                 pch = pchs + StrLength(HeaderName) + 1;
358                 pche = pchs + StrLength(Line);
359                 while (isspace(*pch) && (pch < pche))
360                         pch ++;
361                 StrBufCutLeft(Line, pch - pchs);
362
363                 StrBufUpCase(HeaderName);
364
365                 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
366                 memset(pHdr, 0, sizeof(OneHttpHeader));
367                 pHdr->Val = Line;
368
369                 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
370                     (vF != NULL))
371                 {
372                         OneHttpHeader *FHdr = (OneHttpHeader*) vF;
373                         pHdr->H = FHdr->H;
374                         pHdr->HaveEvaluator = 1;
375                 }
376                 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
377                 LastLine = Line;
378         } while (Line != NULL);
379
380         FreeStrBuf(&HeaderName);
381
382         return isbogus;
383 }
384
385
386
387 /*
388  * handle one request
389  *
390  * This loop gets called once for every HTTP connection made to WebCit.  At
391  * this entry point we have an HTTP socket with a browser allegedly on the
392  * other end, but we have not yet bound to a WebCit session.
393  *
394  * The job of this function is to locate the correct session and bind to it,
395  * or create a session if necessary and bind to it, then run the WebCit
396  * transaction loop.  Afterwards, we unbind from the session.  When this
397  * function returns, the worker thread is then free to handle another
398  * transaction.
399  */
400 void context_loop(int *sock)
401 {
402         ParsedHttpHdrs Hdr;
403         int isbogus = 0;
404         wcsession *TheSession, *sptr;
405         struct timeval tx_start;
406         struct timeval tx_finish;
407         
408         gettimeofday(&tx_start, NULL);          /* start a stopwatch for performance timing */
409
410         memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
411         Hdr.eReqType = eGET;
412         Hdr.http_sock = *sock;
413         /*
414          * Find out what it is that the web browser is asking for
415          */
416         isbogus = ReadHTTPRequset(&Hdr);
417
418         if (!isbogus)
419                 isbogus = AnalyseHeaders(&Hdr);
420
421         if ((isbogus) ||
422             ((Hdr.Handler != NULL) &&
423              ((Hdr.Handler->Flags & BOGUS) != 0)))
424         {
425                 wcsession *Bogus;
426                 Bogus = (wcsession *)
427                         malloc(sizeof(wcsession));
428                 memset(Bogus, 0, sizeof(wcsession));
429                 pthread_setspecific(MyConKey, (void *)Bogus);
430                 Bogus->Hdr = &Hdr;
431                 session_new_modules(Bogus);
432                 do_404();
433                 session_detach_modules(Bogus);
434                 http_destroy_modules(&Hdr);
435                 session_destroy_modules(&Bogus);
436                 return;
437         }
438
439         if ((Hdr.Handler != NULL) && 
440             ((Hdr.Handler->Flags & ISSTATIC) != 0))
441         {
442                 wcsession *Static;
443
444                 Static = (wcsession *)
445                         malloc(sizeof(wcsession));
446                 memset(Static, 0, sizeof(wcsession));
447                 pthread_setspecific(MyConKey, (void *)Static);
448                 Static->Hdr = &Hdr;
449                 Static->serv_sock = (-1);
450                 Static->chat_sock = (-1);
451                 Static->is_mobile = -1;
452                 session_new_modules(Static);
453                 
454                 Hdr.Handler->F();
455
456                 /* How long did this transaction take? */
457                 gettimeofday(&tx_finish, NULL);
458                 
459                 lprintf(9, "SL: Transaction [%s] completed in %ld.%06ld seconds.\n",
460                         ChrPtr(Hdr.this_page),
461                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
462                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
463                         );
464
465                 session_detach_modules(Static);
466                 http_destroy_modules(&Hdr);
467                 session_destroy_modules(&Static);
468                 return;
469         }
470
471         if (Hdr.got_auth == AUTH_BASIC) 
472                 CheckAuthBasic(&Hdr);
473
474 /*
475 TODO    HKEY("/static/nocookies.html?force_close_session=yes"));
476 */
477
478 /*      dbg_PrintHash(HTTPHeaders, nix, NULL);  */
479
480
481         /* Begin parsing the request. * /
482 #ifdef TECH_PREVIEW
483         if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
484             (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
485             (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
486             (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
487             (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
488 #endif
489                 lprintf(5, "HTTP: %s %s\n", ReqStrs[Hdr.eReqType], ChrPtr(ReqLine));
490 #ifdef TECH_PREVIEW
491         }
492 #endif
493
494 */
495
496         /**
497          * See if there's an existing session open with the desired ID or user/pass
498          */
499         TheSession = NULL;
500
501         if (TheSession == NULL) {
502                 pthread_mutex_lock(&SessionListMutex);
503                 for (sptr = SessionList; 
504                      ((sptr != NULL) && (TheSession == NULL)); 
505                       sptr = sptr->next) {
506
507                         /** If HTTP-AUTH, look for a session with matching credentials */
508                         switch (Hdr.got_auth)
509                         {
510                         case AUTH_BASIC:
511                                 if ( (Hdr.SessionKey != sptr->SessionKey))
512                                         continue;
513                                 GetAuthBasic(&Hdr);
514                                 if ((!strcasecmp(ChrPtr(Hdr.c_username), ChrPtr(sptr->wc_username))) &&
515                                     (!strcasecmp(ChrPtr(Hdr.c_password), ChrPtr(sptr->wc_password))) ) 
516                                         TheSession = sptr;
517                                 break;
518                         case AUTH_COOKIE:
519                         /** If cookie-session, look for a session with matching session ID */
520                                 if ( (Hdr.desired_session != 0) && 
521                                      (sptr->wc_session == Hdr.desired_session)) 
522                                         TheSession = sptr;
523                                 break;                       
524                         case NO_AUTH:
525                              break;
526                         }
527                 }
528                 pthread_mutex_unlock(&SessionListMutex);
529         }
530
531         /**
532          * Create a new session if we have to
533          */
534         if (TheSession == NULL) {
535                 lprintf(3, "Creating a new session\n");
536                 TheSession = (wcsession *)
537                         malloc(sizeof(wcsession));
538                 memset(TheSession, 0, sizeof(wcsession));
539                 TheSession->Hdr = &Hdr;
540                 TheSession->SessionKey = Hdr.SessionKey;
541                 TheSession->serv_sock = (-1);
542                 TheSession->chat_sock = (-1);
543         
544                 /* If we're recreating a session that expired, it's best to give it the same
545                  * session number that it had before.  The client browser ought to pick up
546                  * the new session number and start using it, but in some rare situations it
547                  * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
548                  * to get created.
549                  */     
550                 if (Hdr.desired_session == 0) {
551                         TheSession->wc_session = GenerateSessionID();
552                 }
553                 else {
554                         TheSession->wc_session = Hdr.desired_session;
555                 }
556
557                 pthread_setspecific(MyConKey, (void *)TheSession);
558                 session_new_modules(TheSession);
559
560                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
561                 pthread_mutex_lock(&SessionListMutex);
562                 TheSession->nonce = rand();
563                 TheSession->next = SessionList;
564                 TheSession->is_mobile = -1;
565                 SessionList = TheSession;
566                 pthread_mutex_unlock(&SessionListMutex);
567
568                 if (StrLength(Hdr.c_language) > 0) {
569                         lprintf(9, "Session cookie requests language '%s'\n", ChrPtr(Hdr.c_language));
570                         set_selected_language(ChrPtr(Hdr.c_language));
571                         go_selected_language();
572                 }
573         }
574
575         /*
576          * A future improvement might be to check the session integrity
577          * at this point before continuing.
578          */
579
580         /*
581          * Bind to the session and perform the transaction
582          */
583         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
584         pthread_setspecific(MyConKey, (void *)TheSession);
585         
586         TheSession->lastreq = time(NULL);                       /* log */
587         TheSession->Hdr = &Hdr;
588
589         session_attach_modules(TheSession);
590         session_loop();                         /* do transaction */
591
592
593         /* How long did this transaction take? */
594         gettimeofday(&tx_finish, NULL);
595         
596         lprintf(9, "Transaction [%s] completed in %ld.%06ld seconds.\n",
597                 ChrPtr(Hdr.this_page),
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         );
601
602         session_detach_modules(TheSession);
603
604         TheSession->Hdr = NULL;
605         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
606
607         http_destroy_modules(&Hdr);
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 tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
623 {
624         StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0); 
625 }
626
627 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
628 {
629         hdr->ContentLength = StrToi(Line);
630 }
631
632 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
633 {
634         hdr->ContentType = Line;
635 }
636
637 void Header_HandleUserAgent(StrBuf *Line, ParsedHttpHdrs *hdr)
638 {
639         hdr->user_agent = Line;
640 #ifdef TECH_PREVIEW
641 /* TODO: do this later on session creating
642         if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {                   
643                 WCC->is_mobile = 1;
644         }
645         else {
646                 WCC->is_mobile = 0;
647         }
648 */
649 #endif
650 }
651
652
653 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
654 {
655         if ((follow_xff) && (hdr->http_host != NULL))
656                 return;
657         else
658                 hdr->http_host = Line;
659 }
660
661 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
662 {
663         if (follow_xff)
664                 hdr->http_host = Line;
665 }
666
667
668 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
669 {
670         hdr->browser_host = Line;
671
672         while (StrBufNum_tokens(hdr->browser_host, ',') > 1) {
673                 StrBufRemove_token(hdr->browser_host, 0, ',');
674         }
675         StrBufTrim(hdr->browser_host);
676 }
677
678 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
679 {
680         hdr->if_modified_since = httpdate_to_timestamp(Line);
681 }
682
683 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
684 {
685         /*
686          * Can we compress?
687          */
688         if (strstr(&ChrPtr(Line)[16], "gzip")) {
689                 hdr->gzip_ok = 1;
690         }
691 }
692 const char *ReqStrs[eNONE] = {
693         "GET",
694         "POST",
695         "OPTIONS",
696         "PROPFIND",
697         "PUT",
698         "DELETE",
699         "HEAD",
700         "MOVE",
701         "COPY"
702 };
703
704 void
705 ServerStartModule_CONTEXT
706 (void)
707 {
708         long *v;
709         HttpReqTypes = NewHash(1, NULL);
710         HttpHeaderHandler = NewHash(1, NULL);
711
712         v = malloc(sizeof(long));
713         *v = eGET;
714         Put(HttpReqTypes, HKEY("GET"), v, NULL);
715
716         v = malloc(sizeof(long));
717         *v = ePOST;
718         Put(HttpReqTypes, HKEY("POST"), v, NULL);
719
720         v = malloc(sizeof(long));
721         *v = eOPTIONS;
722         Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
723
724         v = malloc(sizeof(long));
725         *v = ePROPFIND;
726         Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
727
728         v = malloc(sizeof(long));
729         *v = ePUT;
730         Put(HttpReqTypes, HKEY("PUT"), v, NULL);
731
732         v = malloc(sizeof(long));
733         *v = eDELETE;
734         Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
735
736         v = malloc(sizeof(long));
737         *v = eHEAD;
738         Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
739
740         v = malloc(sizeof(long));
741         *v = eMOVE;
742         Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
743
744         v = malloc(sizeof(long));
745         *v = eCOPY;
746         Put(HttpReqTypes, HKEY("COPY"), v, NULL);
747 }
748
749 void 
750 ServerShutdownModule_CONTEXT
751 (void)
752 {
753         DeleteHash(&HttpReqTypes);
754         DeleteHash(&HttpHeaderHandler);
755 }
756
757 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
758 {
759         OneHttpHeader *pHdr;
760         pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
761         memset(pHdr, 0, sizeof(OneHttpHeader));
762         pHdr->H = F;
763         Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
764 }
765
766
767 void 
768 InitModule_CONTEXT
769 (void)
770 {
771         RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
772         RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
773         RegisterHeaderHandler(HKEY("USER-AGENT"), Header_HandleUserAgent);
774         RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost);
775         RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
776         RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
777         RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
778         RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
779
780         RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
781         RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
782         RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
783
784         WebcitAddUrlHandler(HKEY("404"), do_404, ANONYMOUS|COOKIEUNNEEDED);
785 /*
786  * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
787  * Short-circuit these requests so we don't have to send them through the full processing loop.
788  */
789         WebcitAddUrlHandler(HKEY("scripts"), do_404, ANONYMOUS|BOGUS); /* /root.exe     /* Worms and trojans and viruses, oh my! */
790         WebcitAddUrlHandler(HKEY("c"), do_404, ANONYMOUS|BOGUS);        /* /winnt */
791         WebcitAddUrlHandler(HKEY("MSADC"), do_404, ANONYMOUS|BOGUS);
792         WebcitAddUrlHandler(HKEY("_vti"), do_404, ANONYMOUS|BOGUS);             /* Broken Microsoft DAV implementation */
793         WebcitAddUrlHandler(HKEY("MSOffice"), do_404, ANONYMOUS|BOGUS);         /* Stoopid MSOffice thinks everyone is IIS */
794         WebcitAddUrlHandler(HKEY("nonexistenshit"), do_404, ANONYMOUS|BOGUS);   /* Exploit found in the wild January 2009 */
795 }
796         
797
798
799 void 
800 HttpDestroyModule_CONTEXT
801 (ParsedHttpHdrs *httpreq)
802 {
803         FreeStrBuf(&httpreq->ReqLine);
804         FreeStrBuf(&httpreq->ReadBuf);
805         FreeStrBuf(&httpreq->PlainArgs);
806         FreeStrBuf(&httpreq->this_page);
807         DeleteHash(&httpreq->HTTPHeaders);
808
809 }