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