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