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