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