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