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