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