sneaking up on that bug like a cat prowling in the jungle
[citadel.git] / webcit / context_loop.c
1 /*
2  * This is the other half of the webserver.  It handles the task of hooking
3  * up HTTP requests with the sessions they belong to, using HTTP cookies to
4  * keep track of things.  If the HTTP request doesn't belong to any currently
5  * active session, a new session is started.
6  *
7  * Copyright (c) 1996-2011 by the citadel.org team
8  *
9  * This program is open source software.  You can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include "webcit.h"
25 #include "webserver.h"
26 #include "modules_init.h"
27
28 /* Only one thread may manipulate SessionList at a time... */
29 pthread_mutex_t SessionListMutex;
30
31 wcsession *SessionList = NULL;  /* Linked list of all webcit sessions */
32
33 pthread_key_t MyConKey;         /* TSD key for MySession() */
34 HashList *HttpReqTypes = NULL;
35 HashList *HttpHeaderHandler = NULL;
36 extern HashList *HandlerHash;
37
38 /* the following two values start at 1 because the initial parent thread counts as one. */
39 int num_threads_existing = 1;           /* Number of worker threads which exist. */
40 int num_threads_executing = 1;          /* Number of worker threads currently executing. */
41
42 extern void session_loop(void);
43 void spawn_another_worker_thread(void);
44
45
46 void DestroyHttpHeaderHandler(void *V)
47 {
48         OneHttpHeader *pHdr;
49         pHdr = (OneHttpHeader*) V;
50         FreeStrBuf(&pHdr->Val);
51         free(pHdr);
52 }
53
54 void shutdown_sessions(void)
55 {
56         wcsession *sptr;
57         
58         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
59                         sptr->killthis = 1;
60         }
61 }
62
63 void do_housekeeping(void)
64 {
65         wcsession *sptr, *ss;
66         wcsession *sessions_to_kill = NULL;
67
68         /*
69          * Lock the session list, moving any candidates for euthanasia into
70          * a separate list.
71          */
72         CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
73         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
74
75                 /* Kill idle sessions */
76                 if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
77                         syslog(3, "Timeout session %d\n", sptr->wc_session);
78                         sptr->killthis = 1;
79                 }
80
81                 /* Remove sessions flagged for kill */
82                 if (sptr->killthis) {
83
84                         /* remove session from linked list */
85                         if (sptr == SessionList) {
86                                 SessionList = SessionList->next;
87                         }
88                         else for (ss=SessionList;ss!=NULL;ss=ss->next) {
89                                 if (ss->next == sptr) {
90                                         ss->next = ss->next->next;
91                                 }
92                         }
93
94                         sptr->next = sessions_to_kill;
95                         sessions_to_kill = sptr;
96                 }
97         }
98         CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
99
100         /*
101          * Now free up and destroy the culled sessions.
102          */
103         while (sessions_to_kill != NULL) {
104                 syslog(3, "Destroying session %d\n", sessions_to_kill->wc_session);
105                 sptr = sessions_to_kill->next;
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         if (
120                 (num_threads_executing >= num_threads_existing)
121                 && (num_threads_existing < MAX_WORKER_THREADS)
122         ) {
123                 syslog(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         CtdlLogResult(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                                 syslog(LOG_DEBUG, "-- matched a session with the same http-auth");
182                                 TheSession = sptr;
183                         }
184                         if (TheSession == NULL)
185                                 syslog(1, "found sessionkey [%d], but credentials for [%s|%s] didn't match",
186                                         Hdr->HR.SessionKey,
187                                         ChrPtr(Hdr->c_username),
188                                         ChrPtr(sptr->wc_username)
189                                 );
190                         break;
191                 case AUTH_COOKIE:
192                         /* If cookie-session, look for a session with matching session ID */
193                         if ( (Hdr->HR.desired_session != 0) && 
194                              (sptr->wc_session == Hdr->HR.desired_session))
195                         {
196                                 syslog(LOG_DEBUG, "-- matched a session with the same cookie");
197                                 TheSession = sptr;
198                         }
199                         break;                       
200                 case NO_AUTH:
201                         /* Any unbound session is a candidate */
202                         if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
203                                 syslog(LOG_DEBUG, "-- reusing an unbound session");
204                                 TheSession = sptr;
205                         }
206                         break;
207                 }
208         }
209         CtdlLogResult(pthread_mutex_unlock(ListMutex));
210         if (TheSession == NULL) {
211                 syslog(1, "didn't find sessionkey [%d] for user [%s]",
212                         Hdr->HR.SessionKey, ChrPtr(Hdr->c_username)
213                 );
214         }
215         return TheSession;
216 }
217
218 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
219 {
220         wcsession *TheSession;
221         TheSession = (wcsession *) malloc(sizeof(wcsession));
222         memset(TheSession, 0, sizeof(wcsession));
223         TheSession->Hdr = Hdr;
224         TheSession->SessionKey = Hdr->HR.SessionKey;
225         TheSession->serv_sock = (-1);
226
227         pthread_setspecific(MyConKey, (void *)TheSession);
228         
229         /* If we're recreating a session that expired, it's best to give it the same
230          * session number that it had before.  The client browser ought to pick up
231          * the new session number and start using it, but in some rare situations it
232          * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
233          * to get created.
234          */     
235         if (Hdr->HR.desired_session == 0) {
236                 TheSession->wc_session = GenerateSessionID();
237                 syslog(3, "Created new session %d", TheSession->wc_session);
238         }
239         else {
240                 TheSession->wc_session = Hdr->HR.desired_session;
241                 syslog(3, "Re-created session %d", TheSession->wc_session);
242         }
243         Hdr->HR.Static = Static;
244         session_new_modules(TheSession);
245
246         if (Lockable) {
247                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
248
249                 if (ListMutex != NULL)
250                         CtdlLogResult(pthread_mutex_lock(ListMutex));
251
252                 if (wclist != NULL) {
253                         TheSession->nonce = rand();
254                         TheSession->next = *wclist;
255                         *wclist = TheSession;
256                 }
257                 if (ListMutex != NULL)
258                         CtdlLogResult(pthread_mutex_unlock(ListMutex));
259         }
260         return TheSession;
261 }
262
263
264 /* If it's a "force 404" situation then display the error and bail. */
265 void do_404(void)
266 {
267         hprintf("HTTP/1.1 404 Not found\r\n");
268         hprintf("Content-Type: text/plain\r\n");
269         wc_printf("Not found\r\n");
270         end_burst();
271 }
272
273 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
274 {
275         const char *Args;
276         void *vLine, *vHandler;
277         const char *Pos = NULL;
278
279
280         Hdr->HR.ReqLine = Line;
281         /* The requesttype... GET, POST... */
282         StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
283         if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
284             (vLine != NULL))
285         {
286                 Hdr->HR.eReqType = *(long*)vLine;
287         }
288         else {
289                 Hdr->HR.eReqType = eGET;
290                 return 1;
291         }
292         StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
293
294         /* the HTTP Version... */
295         StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
296         StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
297         
298         if (StrLength(Buf) == 0) {
299                 Hdr->HR.eReqType = eGET;
300                 return 1;
301         }
302
303         StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
304
305         /* chop Filename / query arguments */
306         Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
307         if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
308                 Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
309         if (Args != NULL) {
310                 Args ++; /* skip the ? */
311                 StrBufPlain(Hdr->PlainArgs, 
312                             Args, 
313                             StrLength(Hdr->HR.ReqLine) -
314                             (Args - ChrPtr(Hdr->HR.ReqLine)));
315                 StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
316         } /* don't parse them yet, maybe we don't even care... */
317         
318         /* now lookup what we are going to do with this... */
319         /* skip first slash */
320         StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
321         do {
322                 StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
323
324                 GetHash(HandlerHash, SKEY(Buf), &vHandler),
325                 Hdr->HR.Handler = (WebcitHandler*) vHandler;
326                 if (Hdr->HR.Handler == NULL)
327                         break;
328                 /*
329                  * If the request is prefixed by "/webcit" then chop that off.  This
330                  * allows a front end web server to forward all /webcit requests to us
331                  * while still using the same web server port for other things.
332                  */
333                 if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
334                         continue;
335                 break;
336         } while (1);
337         /* remove the handlername from the URL */
338         if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
339                 StrBufCutLeft(Hdr->HR.ReqLine, 
340                               Pos - ChrPtr(Hdr->HR.ReqLine));
341         }
342
343         if (Hdr->HR.Handler != NULL) {
344                 if ((Hdr->HR.Handler->Flags & BOGUS) != 0)
345                         return 1;
346                 Hdr->HR.DontNeedAuth = (
347                         ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
348                         ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
349                         );
350         }
351         else {
352                 Hdr->HR.DontNeedAuth = 1; /* Flat request? show him the login screen... */
353         }
354
355         return 0;
356 }
357
358 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
359 {
360         OneHttpHeader *pHdr;
361         void *vHdr;
362         long HKLen;
363         const char *HashKey;
364         HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
365         
366         while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) && 
367                (vHdr != NULL)) {
368                 pHdr = (OneHttpHeader *)vHdr;
369                 if (pHdr->HaveEvaluator)
370                         pHdr->H(pHdr->Val, Hdr);
371
372         }
373         DeleteHashPos(&at);
374         return 0;
375 }
376
377 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
378
379 /*
380  * Read in the request
381  */
382 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
383 {
384         const char *pch, *pchs, *pche;
385         OneHttpHeader *pHdr;
386         StrBuf *Line, *LastLine, *HeaderName;
387         int nLine = 0;
388         void *vF;
389         int isbogus = 0;
390
391         HeaderName = NewStrBuf();
392         LastLine = NULL;
393         do {
394                 nLine ++;
395                 Line = NewStrBufPlain(NULL, SIZ / 4);
396
397                 if (ClientGetLine(Hdr, Line) < 0) return 1;
398
399                 if (StrLength(Line) == 0) {
400                         FreeStrBuf(&Line);
401                         continue;
402                 }
403                 if (nLine == 1) {
404                         Hdr->HTTPHeaders = NewHash(1, NULL);
405                         pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
406                         memset(pHdr, 0, sizeof(OneHttpHeader));
407                         pHdr->Val = Line;
408                         Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
409                         syslog(9, "%s\n", ChrPtr(Line));
410                         isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
411                         if (isbogus) break;
412                         continue;
413                 }
414
415                 /* Do we need to Unfold? */
416                 if ((LastLine != NULL) && 
417                     (isspace(*ChrPtr(Line)))) {
418                         pch = pchs = ChrPtr(Line);
419                         pche = pchs + StrLength(Line);
420                         while (isspace(*pch) && (pch < pche))
421                                 pch ++;
422                         StrBufCutLeft(Line, pch - pchs);
423                         StrBufAppendBuf(LastLine, Line, 0);
424
425                         FreeStrBuf(&Line);
426                         continue;
427                 }
428
429                 StrBufSanitizeAscii(Line, '§');
430                 StrBufExtract_token(HeaderName, Line, 0, ':');
431
432                 pchs = ChrPtr(Line);
433                 pche = pchs + StrLength(Line);
434                 pch = pchs + StrLength(HeaderName) + 1;
435                 pche = pchs + StrLength(Line);
436                 while ((pch < pche) && isspace(*pch))
437                         pch ++;
438                 StrBufCutLeft(Line, pch - pchs);
439
440                 StrBufUpCase(HeaderName);
441
442                 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
443                 memset(pHdr, 0, sizeof(OneHttpHeader));
444                 pHdr->Val = Line;
445
446                 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
447                     (vF != NULL))
448                 {
449                         OneHttpHeader *FHdr = (OneHttpHeader*) vF;
450                         pHdr->H = FHdr->H;
451                         pHdr->HaveEvaluator = 1;
452                 }
453                 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
454                 LastLine = Line;
455         } while (Line != NULL);
456
457         FreeStrBuf(&HeaderName);
458
459         return isbogus;
460 }
461
462 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
463 {
464         StrBuf *Buf = NewStrBuf();
465
466         if (Hdr->HR.ReqLine != NULL) {
467                 FlushStrBuf(Hdr->HR.ReqLine);
468                 StrBufPlain(Hdr->HR.ReqLine, Line, len);
469         }
470         else {
471                 Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
472         }
473         ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
474
475         FreeStrBuf(&Buf);
476 }
477
478 /*
479  * handle one request
480  *
481  * This loop gets called once for every HTTP connection made to WebCit.  At
482  * this entry point we have an HTTP socket with a browser allegedly on the
483  * other end, but we have not yet bound to a WebCit session.
484  *
485  * The job of this function is to locate the correct session and bind to it,
486  * or create a session if necessary and bind to it, then run the WebCit
487  * transaction loop.  Afterwards, we unbind from the session.  When this
488  * function returns, the worker thread is then free to handle another
489  * transaction.
490  */
491 void context_loop(ParsedHttpHdrs *Hdr)
492 {
493         int isbogus = 0;
494         wcsession *TheSession;
495         struct timeval tx_start;
496         struct timeval tx_finish;
497         int session_may_be_reused = 1;
498         
499         gettimeofday(&tx_start, NULL);          /* start a stopwatch for performance timing */
500
501         /*
502          * Find out what it is that the web browser is asking for
503          */
504         isbogus = ReadHTTPRequest(Hdr);
505
506         Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
507
508         if (!isbogus) {
509                 isbogus = AnalyseHeaders(Hdr);
510         }
511
512         if (    (isbogus)
513                 || ((Hdr->HR.Handler != NULL)
514                 && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
515         ) {
516                 wcsession *Bogus;
517                 Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
518                 do_404();
519                 syslog(9, "HTTP: 404 [%ld.%06ld] %s %s \n",
520                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
521                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
522                         ReqStrs[Hdr->HR.eReqType],
523                         ChrPtr(Hdr->this_page)
524                         );
525                 session_detach_modules(Bogus);
526                 session_destroy_modules(&Bogus);
527                 return;
528         }
529
530         if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
531         {
532                 wcsession *Static;
533                 Static = CreateSession(0, 1, NULL, Hdr, NULL);
534                 
535                 Hdr->HR.Handler->F();
536
537                 /* How long did this transaction take? */
538                 gettimeofday(&tx_finish, NULL);
539                 
540                 syslog(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
541                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
542                         ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
543                         ReqStrs[Hdr->HR.eReqType],
544                         ChrPtr(Hdr->this_page)
545                 );
546                 session_detach_modules(Static);
547                 session_destroy_modules(&Static);
548                 return;
549         }
550
551         if (Hdr->HR.got_auth == AUTH_BASIC) {
552                 CheckAuthBasic(Hdr);
553         }
554
555         if (Hdr->HR.got_auth) {
556                 session_may_be_reused = 0;
557         }
558
559         /*
560          * See if there's an existing session open with the desired ID or user/pass
561          */
562         TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
563
564         /*
565          * Create a new session if we have to
566          */
567         if (TheSession == NULL) {
568                 TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
569
570                 if (    (StrLength(Hdr->c_username) == 0)
571                         && (!Hdr->HR.DontNeedAuth)
572                         && (Hdr->HR.Handler != NULL)
573                         && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
574                 ) {
575                         OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
576                         Hdr->HR.prohibit_caching = 1;                           
577                 }
578                 
579                 if (StrLength(Hdr->c_language) > 0) {
580                         syslog(9, "Session cookie requests language '%s'\n", ChrPtr(Hdr->c_language));
581                         set_selected_language(ChrPtr(Hdr->c_language));
582                         go_selected_language();
583                 }
584         }
585
586         /*
587          * A future improvement might be to check the session integrity
588          * at this point before continuing.
589          */
590
591         /*
592          * Bind to the session and perform the transaction
593          */
594         CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
595         pthread_setspecific(MyConKey, (void *)TheSession);
596         
597         TheSession->inuse = 1;                                  /* mark the session as bound */
598         TheSession->lastreq = time(NULL);                       /* log */
599         TheSession->Hdr = Hdr;
600
601         session_attach_modules(TheSession);
602         session_loop();                         /* do transaction */
603
604         /* How long did this transaction take? */
605         gettimeofday(&tx_finish, NULL);
606
607         syslog(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
608                 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
609                 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
610                 ReqStrs[Hdr->HR.eReqType],
611                 ChrPtr(Hdr->this_page)
612         );
613
614         session_detach_modules(TheSession);
615
616         /* If *this* very transaction did not explicitly specify a session cookie,
617          * and it did not log in, we want to flag the session as a candidate for
618          * re-use by the next unbound client that comes along.  This keeps our session
619          * table from getting bombarded with new sessions when, for example, a web
620          * spider crawls the site without using cookies.
621          */
622         if ((session_may_be_reused) && (!WC->logged_in)) {
623                 WC->wc_session = 0;                     /* flag as available for re-use */
624                 TheSession->selected_language = 0;      /* clear any non-default language setting */
625         }
626
627         TheSession->Hdr = NULL;
628         TheSession->inuse = 0;                                  /* mark the session as unbound */
629         CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
630 }
631
632 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
633 {
634         wcsession *WCC = WC;
635         StrBufAppendPrintf(Target, "%ld",
636                            (WCC != NULL)? WCC->nonce:0);                   
637 }
638
639 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
640 {
641         StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
642 }
643
644 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
645 {
646         hdr->HR.ContentLength = StrToi(Line);
647 }
648
649 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
650 {
651         hdr->HR.ContentType = Line;
652 }
653
654
655 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
656 {
657         if (hdr->HostHeader != NULL) {
658                 FreeStrBuf(&hdr->HostHeader);
659         }
660         hdr->HostHeader = NewStrBuf();
661         StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
662         StrBufAppendBuf(hdr->HostHeader, Line, 0);
663 }
664
665 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
666 {
667         if (!follow_xff) return;
668
669         if (hdr->HostHeader != NULL) {
670                 FreeStrBuf(&hdr->HostHeader);
671         }
672
673         hdr->HostHeader = NewStrBuf();
674         StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
675         StrBufAppendBuf(hdr->HostHeader, Line, 0);
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("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
785         RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost);        /* NGinX way... */
786         RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
787         RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
788         RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
789         RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
790
791         RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
792         RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
793
794         WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
795 /*
796  * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
797  * Short-circuit these requests so we don't have to send them through the full processing loop.
798  */
799         WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS);           /* /root.exe - Worms and trojans and viruses, oh my! */
800         WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS);         /* /winnt */
801         WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
802         WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS);              /* Broken Microsoft DAV implementation */
803         WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS);          /* Stoopid MSOffice thinks everyone is IIS */
804         WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS);    /* Exploit found in the wild January 2009 */
805 }
806         
807
808 void 
809 HttpNewModule_CONTEXT
810 (ParsedHttpHdrs *httpreq)
811 {
812         httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
813         httpreq->this_page = NewStrBufPlain(NULL, SIZ);
814 }
815
816 void 
817 HttpDetachModule_CONTEXT
818 (ParsedHttpHdrs *httpreq)
819 {
820         FlushStrBuf(httpreq->PlainArgs);
821         FlushStrBuf(httpreq->HostHeader);
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         FreeStrBuf(&httpreq->HostHeader);
837         DeleteHash(&httpreq->HTTPHeaders);
838
839 }