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