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