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