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