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