* shuffle auth arround...
[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
199
200 /*
201  * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
202  * Short-circuit these requests so we don't have to send them through the full processing loop.
203  */
204 int is_bogus(StrBuf *http_cmd) {////TODO!
205         const char *url;
206         int i, max;
207         const char *bogus_prefixes[] = {
208                 "/scripts/root.exe",    /* Worms and trojans and viruses, oh my! */
209                 "/c/winnt",
210                 "/MSADC/",
211                 "/_vti",                /* Broken Microsoft DAV implementation */
212                 "/MSOffice",            /* Stoopid MSOffice thinks everyone is IIS */
213                 "/nonexistenshit"       /* Exploit found in the wild January 2009 */
214         };
215
216         url = ChrPtr(http_cmd);
217         if (IsEmptyStr(url)) return(1);
218         ++url;
219
220         max = sizeof(bogus_prefixes) / sizeof(char *);
221
222         for (i=0; i<max; ++i) {
223                 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
224                         return(2);
225                 }
226         }
227
228         return(0);      /* probably ok */
229 }
230
231
232 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
233 {
234         const char *Args;
235         void *vLine, *vHandler;
236         const char *Pos = NULL;
237
238
239         Hdr->ReqLine = Line;
240         /* The requesttype... GET, POST... */
241         StrBufExtract_token(Buf, Hdr->ReqLine, 0, ' ');
242         if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
243             (vLine != NULL))
244         {
245                 Hdr->eReqType = *(long*)vLine;
246         }
247         else {
248                 Hdr->eReqType = eGET;
249                 return 1;
250         }
251         StrBufCutLeft(Hdr->ReqLine, StrLength(Buf) + 1);
252
253         /* the HTTP Version... */
254         StrBufExtract_token(Buf, Hdr->ReqLine, 1, ' ');
255         StrBufCutRight(Hdr->ReqLine, StrLength(Buf) + 1);
256         if ((StrLength(Buf) == 0) ||
257             is_bogus(Hdr->ReqLine)) {
258                 Hdr->eReqType = eGET;
259                 return 1;
260         }
261
262         Hdr->this_page = NewStrBufDup(Hdr->ReqLine);
263         /* chop Filename / query arguments */
264         Args = strchr(ChrPtr(Hdr->ReqLine), '?');
265         if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
266                 Args = strchr(ChrPtr(Hdr->ReqLine), '&');
267         if (Args != NULL) {
268                 Args ++; /* skip the ? */
269                 Hdr->PlainArgs = NewStrBufPlain(
270                         Args, 
271                         StrLength(Hdr->ReqLine) -
272                         (Args - ChrPtr(Hdr->ReqLine)));
273                 StrBufCutAt(Hdr->ReqLine, 0, Args - 1);
274         } /* don't parse them yet, maybe we don't even care... */
275         
276         /* now lookup what we are going to do with this... */
277         /* skip first slash */
278         StrBufExtract_NextToken(Buf, Hdr->ReqLine, &Pos, '/');
279         do {
280                 StrBufExtract_NextToken(Buf, Hdr->ReqLine, &Pos, '/');
281
282                 GetHash(HandlerHash, SKEY(Buf), &vHandler),
283                 Hdr->Handler = (WebcitHandler*) vHandler;
284                 if (Hdr->Handler == NULL)
285                         break;
286                 /*
287                  * If the request is prefixed by "/webcit" then chop that off.  This
288                  * allows a front end web server to forward all /webcit requests to us
289                  * while still using the same web server port for other things.
290                  */
291                 if ((Hdr->Handler->Flags & URLNAMESPACE) == 0)
292                         break;
293         } while (1);
294         /* remove the handlername from the URL */
295         if (Pos != NULL) {
296                 StrBufCutLeft(Hdr->ReqLine, 
297                               Pos - ChrPtr(Hdr->ReqLine));
298         }
299 /*
300         if (Hdr->Handler == NULL)
301                 return 1;
302 */
303         Hdr->HTTPHeaders = NewHash(1, NULL);
304
305         return 0;
306 }
307
308 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
309 {
310         OneHttpHeader *pHdr;
311         void *vHdr;
312         long HKLen;
313         const char *HashKey;
314         HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
315         
316         while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) && 
317                (vHdr != NULL)) {
318                 pHdr = (OneHttpHeader *)vHdr;
319                 if (pHdr->HaveEvaluator)
320                         pHdr->H(pHdr->Val, Hdr);
321
322         }
323         DeleteHashPos(&at);
324         return 0;
325 }
326
327 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
328
329 /*
330  * Read in the request
331  */
332 int ReadHTTPRequset (ParsedHttpHdrs *Hdr)
333 {
334         const char *pch, *pchs, *pche;
335         OneHttpHeader *pHdr;
336         StrBuf *Line, *LastLine, *HeaderName;
337         int nLine = 0;
338         void *vF;
339         int isbogus = 0;
340
341         HeaderName = NewStrBuf();
342         Hdr->ReadBuf = NewStrBuf();
343         LastLine = NULL;
344         do {
345                 nLine ++;
346                 Line = NewStrBuf();
347
348                 if (ClientGetLine(&Hdr->http_sock, Line, Hdr->ReadBuf, &Hdr->Pos) < 0) return 1;
349
350                 if (StrLength(Line) == 0) {
351                         FreeStrBuf(&Line);
352                         continue;
353                 }
354                 if (nLine == 1) {
355                         isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
356                         if (isbogus) break;
357                         continue;
358                 }
359
360                 /* Do we need to Unfold? */
361                 if ((LastLine != NULL) && 
362                     (isspace(*ChrPtr(Line)))) {
363                         pch = pchs = ChrPtr(Line);
364                         pche = pchs + StrLength(Line);
365                         while (isspace(*pch) && (pch < pche))
366                                 pch ++;
367                         StrBufCutLeft(Line, pch - pchs);
368                         StrBufAppendBuf(LastLine, Line, 0);
369
370                         FreeStrBuf(&Line);
371                         continue;
372                 }
373
374                 StrBufSanitizeAscii(Line, '§');
375                 StrBufExtract_token(HeaderName, Line, 0, ':');
376
377                 pchs = ChrPtr(Line);
378                 pch = pchs + StrLength(HeaderName) + 1;
379                 pche = pchs + StrLength(Line);
380                 while (isspace(*pch) && (pch < pche))
381                         pch ++;
382                 StrBufCutLeft(Line, pch - pchs);
383
384                 StrBufUpCase(HeaderName);
385
386                 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
387                 memset(pHdr, 0, sizeof(OneHttpHeader));
388                 pHdr->Val = Line;
389
390                 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
391                     (vF != NULL))
392                 {
393                         OneHttpHeader *FHdr = (OneHttpHeader*) vF;
394                         pHdr->H = FHdr->H;
395                         pHdr->HaveEvaluator = 1;
396                 }
397                 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
398                 LastLine = Line;
399         } while (Line != NULL);
400
401         FreeStrBuf(&HeaderName);
402
403         return isbogus;
404 }
405
406
407
408 /*
409  * handle one request
410  *
411  * This loop gets called once for every HTTP connection made to WebCit.  At
412  * this entry point we have an HTTP socket with a browser allegedly on the
413  * other end, but we have not yet bound to a WebCit session.
414  *
415  * The job of this function is to locate the correct session and bind to it,
416  * or create a session if necessary and bind to it, then run the WebCit
417  * transaction loop.  Afterwards, we unbind from the session.  When this
418  * function returns, the worker thread is then free to handle another
419  * transaction.
420  */
421 void context_loop(int *sock)
422 {
423         ParsedHttpHdrs Hdr;
424         int isbogus = 0;
425         wcsession *TheSession, *sptr;
426         struct timeval tx_start;
427         struct timeval tx_finish;
428         
429         gettimeofday(&tx_start, NULL);          /* start a stopwatch for performance timing */
430
431         memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
432         Hdr.eReqType = eGET;
433         Hdr.http_sock = *sock;
434         /*
435          * Find out what it is that the web browser is asking for
436          */
437         isbogus = ReadHTTPRequset(&Hdr);
438
439         if (!isbogus)
440                 isbogus = AnalyseHeaders(&Hdr);
441 /*
442         if (isbogus)
443                 StrBufPlain(ReqLine, HKEY("/404"));
444 TODO    HKEY("/static/nocookies.html?force_close_session=yes"));
445 */
446
447 /*      dbg_PrintHash(HTTPHeaders, nix, NULL);  */
448
449
450         /* Begin parsing the request. * /
451 #ifdef TECH_PREVIEW
452         if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
453             (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
454             (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
455             (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
456             (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
457 #endif
458                 lprintf(5, "HTTP: %s %s\n", ReqStrs[Hdr.eReqType], ChrPtr(ReqLine));
459 #ifdef TECH_PREVIEW
460         }
461 #endif
462
463 */
464
465         /**
466          * See if there's an existing session open with the desired ID or user/pass
467          */
468         TheSession = NULL;
469
470         if (TheSession == NULL) {
471                 pthread_mutex_lock(&SessionListMutex);
472                 for (sptr = SessionList; 
473                      ((sptr != NULL) && (TheSession == NULL)); 
474                       sptr = sptr->next) {
475
476                         /** If HTTP-AUTH, look for a session with matching credentials * /
477                         if ( (////TODO check auth type here...
478                              &&(!strcasecmp(ChrPtr(sptr->httpauth_user), httpauth_user))
479                              &&(!strcasecmp(ChrPtr(sptr->httpauth_pass), httpauth_pass)) ) {
480                                 TheSession = sptr;
481                         }
482
483                         /** If cookie-session, look for a session with matching session ID */
484                         if ( (Hdr.desired_session != 0) && (sptr->wc_session == Hdr.desired_session)) {
485                                 TheSession = sptr;
486                         }
487
488                 }
489                 pthread_mutex_unlock(&SessionListMutex);
490         }
491
492         /**
493          * Create a new session if we have to
494          */
495         if (TheSession == NULL) {
496                 lprintf(3, "Creating a new session\n");
497                 TheSession = (wcsession *)
498                         malloc(sizeof(wcsession));
499                 memset(TheSession, 0, sizeof(wcsession));
500                 TheSession->Hdr = &Hdr;
501                 TheSession->serv_sock = (-1);
502                 TheSession->chat_sock = (-1);
503         
504                 /* If we're recreating a session that expired, it's best to give it the same
505                  * session number that it had before.  The client browser ought to pick up
506                  * the new session number and start using it, but in some rare situations it
507                  * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
508                  * to get created.
509                  */     
510                 if (Hdr.desired_session == 0) {
511                         TheSession->wc_session = GenerateSessionID();
512                 }
513                 else {
514                         TheSession->wc_session = Hdr.desired_session;
515                 }
516 /*
517                 TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
518                         TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
519 */
520                 pthread_setspecific(MyConKey, (void *)TheSession);
521                 session_new_modules(TheSession);
522
523                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
524                 pthread_mutex_lock(&SessionListMutex);
525                 TheSession->nonce = rand();
526                 TheSession->next = SessionList;
527                 TheSession->is_mobile = -1;
528                 SessionList = TheSession;
529                 pthread_mutex_unlock(&SessionListMutex);
530         }
531
532         /*
533          * A future improvement might be to check the session integrity
534          * at this point before continuing.
535          */
536
537         /*
538          * Bind to the session and perform the transaction
539          */
540         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
541         pthread_setspecific(MyConKey, (void *)TheSession);
542         
543         TheSession->lastreq = time(NULL);                       /* log */
544         TheSession->Hdr = &Hdr;
545
546         session_attach_modules(TheSession);
547         session_loop();                         /* do transaction */
548
549
550         /* How long did this transaction take? */
551         gettimeofday(&tx_finish, NULL);
552         
553         lprintf(9, "Transaction [%s] completed in %ld.%06ld seconds.\n",
554                 ChrPtr(Hdr.this_page),
555                 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
556                 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
557         );
558
559         session_detach_modules(TheSession);
560
561         TheSession->Hdr = NULL;
562         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
563
564         http_destroy_modules(&Hdr);
565 }
566
567 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
568 {
569         wcsession *WCC = WC;
570         StrBufAppendPrintf(Target, "%ld",
571                            (WCC != NULL)? WCC->nonce:0);                   
572 }
573
574 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
575 {
576         StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
577 }
578
579 void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
580 {
581         StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0); 
582 }
583
584 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
585 {
586         hdr->ContentLength = StrToi(Line);
587 }
588
589 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
590 {
591         hdr->ContentType = Line;
592 }
593
594 void Header_HandleUserAgent(StrBuf *Line, ParsedHttpHdrs *hdr)
595 {
596         hdr->user_agent = Line;
597 #ifdef TECH_PREVIEW
598 /* TODO: do this later on session creating
599         if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {                   
600                 WCC->is_mobile = 1;
601         }
602         else {
603                 WCC->is_mobile = 0;
604         }
605 */
606 #endif
607 }
608
609
610 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
611 {
612         if ((follow_xff) && (hdr->http_host != NULL))
613                 return;
614         else
615                 hdr->http_host = Line;
616 }
617
618 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
619 {
620         if (follow_xff)
621                 hdr->http_host = Line;
622 }
623
624
625 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
626 {
627         hdr->browser_host = Line;
628
629         while (StrBufNum_tokens(hdr->browser_host, ',') > 1) {
630                 StrBufRemove_token(hdr->browser_host, 0, ',');
631         }
632         StrBufTrim(hdr->browser_host);
633 }
634
635 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
636 {
637         hdr->if_modified_since = httpdate_to_timestamp(Line);
638 }
639
640 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
641 {
642         /*
643          * Can we compress?
644          */
645         if (strstr(&ChrPtr(Line)[16], "gzip")) {
646                 hdr->gzip_ok = 1;
647         }
648 }
649 const char *ReqStrs[eNONE] = {
650         "GET",
651         "POST",
652         "OPTIONS",
653         "PROPFIND",
654         "PUT",
655         "DELETE",
656         "HEAD",
657         "MOVE",
658         "COPY"
659 };
660
661 void
662 ServerStartModule_CONTEXT
663 (void)
664 {
665         long *v;
666         HttpReqTypes = NewHash(1, NULL);
667         HttpHeaderHandler = NewHash(1, NULL);
668
669         v = malloc(sizeof(long));
670         *v = eGET;
671         Put(HttpReqTypes, HKEY("GET"), v, NULL);
672
673         v = malloc(sizeof(long));
674         *v = ePOST;
675         Put(HttpReqTypes, HKEY("POST"), v, NULL);
676
677         v = malloc(sizeof(long));
678         *v = eOPTIONS;
679         Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
680
681         v = malloc(sizeof(long));
682         *v = ePROPFIND;
683         Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
684
685         v = malloc(sizeof(long));
686         *v = ePUT;
687         Put(HttpReqTypes, HKEY("PUT"), v, NULL);
688
689         v = malloc(sizeof(long));
690         *v = eDELETE;
691         Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
692
693         v = malloc(sizeof(long));
694         *v = eHEAD;
695         Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
696
697         v = malloc(sizeof(long));
698         *v = eMOVE;
699         Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
700
701         v = malloc(sizeof(long));
702         *v = eCOPY;
703         Put(HttpReqTypes, HKEY("COPY"), v, NULL);
704 }
705
706 void 
707 ServerShutdownModule_CONTEXT
708 (void)
709 {
710         DeleteHash(&HttpReqTypes);
711         DeleteHash(&HttpHeaderHandler);
712 }
713
714 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
715 {
716         OneHttpHeader *pHdr;
717         pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
718         memset(pHdr, 0, sizeof(OneHttpHeader));
719         pHdr->H = F;
720         Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
721 }
722 extern void blank_page(void); ///TODO: remove me
723 void 
724 InitModule_CONTEXT
725 (void)
726 {
727         RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
728         RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
729         RegisterHeaderHandler(HKEY("USER-AGENT"), Header_HandleUserAgent);
730         RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost);
731         RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
732         RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
733         RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
734         RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
735
736         RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
737         RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
738         RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
739
740
741
742         WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS|BOGUS);
743
744         WebcitAddUrlHandler(HKEY("webcit"), blank_page, URLNAMESPACE);
745 }
746         
747
748
749 void 
750 HttpDestroyModule_CONTEXT
751 (ParsedHttpHdrs *httpreq)
752 {
753         FreeStrBuf(&httpreq->ReqLine);
754         FreeStrBuf(&httpreq->ReadBuf);
755         FreeStrBuf(&httpreq->PlainArgs);
756         FreeStrBuf(&httpreq->this_page);
757         DeleteHash(&httpreq->HTTPHeaders);
758
759 }