Back to index

webcit  8.12-dfsg
context_loop.c
Go to the documentation of this file.
00001 /*
00002  * This is the other half of the webserver.  It handles the task of hooking
00003  * up HTTP requests with the sessions they belong to, using HTTP cookies to
00004  * keep track of things.  If the HTTP request doesn't belong to any currently
00005  * active session, a new session is started.
00006  *
00007  * Copyright (c) 1996-2012 by the citadel.org team
00008  *
00009  * This program is open source software.  You can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License version 3.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  */
00017 
00018 #include "webcit.h"
00019 #include "webserver.h"
00020 #include "modules_init.h"
00021 
00022 /* Only one thread may manipulate SessionList at a time... */
00023 pthread_mutex_t SessionListMutex;
00024 
00025 wcsession *SessionList = NULL;     /* Linked list of all webcit sessions */
00026 
00027 pthread_key_t MyConKey;         /* TSD key for MySession() */
00028 HashList *HttpReqTypes = NULL;
00029 HashList *HttpHeaderHandler = NULL;
00030 extern HashList *HandlerHash;
00031 
00032 /* the following two values start at 1 because the initial parent thread counts as one. */
00033 int num_threads_existing = 1;             /* Number of worker threads which exist. */
00034 int num_threads_executing = 1;            /* Number of worker threads currently executing. */
00035 
00036 extern void session_loop(void);
00037 void spawn_another_worker_thread(void);
00038 
00039 
00040 void DestroyHttpHeaderHandler(void *V)
00041 {
00042        OneHttpHeader *pHdr;
00043        pHdr = (OneHttpHeader*) V;
00044        FreeStrBuf(&pHdr->Val);
00045        free(pHdr);
00046 }
00047 
00048 void shutdown_sessions(void)
00049 {
00050        wcsession *sptr;
00051        
00052        for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
00053               sptr->killthis = 1;
00054        }
00055 }
00056 
00057 void do_housekeeping(void)
00058 {
00059        wcsession *sptr, *ss;
00060        wcsession *sessions_to_kill = NULL;
00061 
00062        /*
00063         * Lock the session list, moving any candidates for euthanasia into
00064         * a separate list.
00065         */
00066        CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
00067        for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
00068 
00069               /* Kill idle sessions */
00070               if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
00071                      syslog(3, "Timeout session %d", sptr->wc_session);
00072                      sptr->killthis = 1;
00073               }
00074 
00075               /* Remove sessions flagged for kill */
00076               if (sptr->killthis) {
00077 
00078                      /* remove session from linked list */
00079                      if (sptr == SessionList) {
00080                             SessionList = SessionList->next;
00081                      }
00082                      else for (ss=SessionList;ss!=NULL;ss=ss->next) {
00083                             if (ss->next == sptr) {
00084                                    ss->next = ss->next->next;
00085                             }
00086                      }
00087 
00088                      sptr->next = sessions_to_kill;
00089                      sessions_to_kill = sptr;
00090               }
00091        }
00092        CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
00093 
00094        /*
00095         * Now free up and destroy the culled sessions.
00096         */
00097        while (sessions_to_kill != NULL) {
00098               syslog(3, "Destroying session %d", sessions_to_kill->wc_session);
00099               sptr = sessions_to_kill->next;
00100               session_destroy_modules(&sessions_to_kill);
00101               sessions_to_kill = sptr;
00102        }
00103 }
00104 
00105 /*
00106  * Check the size of our thread pool.  If all threads are executing, spawn another.
00107  */
00108 void check_thread_pool_size(void)
00109 {
00110        if (time_to_die) return;           /* don't expand the thread pool during shutdown */
00111 
00112        begin_critical_section(S_SPAWNER); /* only one of these should run at a time */
00113        if (
00114               (num_threads_executing >= num_threads_existing)
00115               && (num_threads_existing < MAX_WORKER_THREADS)
00116        ) {
00117               syslog(3, "%d of %d threads are executing.  Adding another worker thread.",
00118                      num_threads_executing,
00119                      num_threads_existing
00120               );
00121               spawn_another_worker_thread();
00122        }
00123        end_critical_section(S_SPAWNER);
00124 }
00125 
00126 
00127 /*
00128  * Wake up occasionally and clean house
00129  */
00130 void housekeeping_loop(void)
00131 {
00132        while (1) {
00133               sleeeeeeeeeep(HOUSEKEEPING);
00134               do_housekeeping();
00135        }
00136 }
00137 
00138 
00139 /*
00140  * Create a Session id
00141  * Generate a unique WebCit session ID (which is not the same thing as the
00142  * Citadel session ID).
00143  */
00144 int GenerateSessionID(void)
00145 {
00146        static int seq = (-1);
00147 
00148        if (seq < 0) {
00149               seq = (int) time(NULL);
00150        }
00151               
00152        return ++seq;
00153 }
00154 
00155 wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
00156 {
00157        wcsession *sptr = NULL;
00158        wcsession *TheSession = NULL;      
00159        
00160        if (Hdr->HR.got_auth == AUTH_BASIC) {
00161               GetAuthBasic(Hdr);
00162        }
00163 
00164        CtdlLogResult(pthread_mutex_lock(ListMutex));
00165        for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
00166               
00167               /* If HTTP-AUTH, look for a session with matching credentials */
00168               switch (Hdr->HR.got_auth)
00169               {
00170               case AUTH_BASIC:
00171                      if (   (!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username)))
00172                             && (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password)))
00173                             && (sptr->killthis == 0)
00174                      ) {
00175                             syslog(LOG_DEBUG, "Matched a session with the same http-auth");
00176                             TheSession = sptr;
00177                      }
00178                      break;
00179               case AUTH_COOKIE:
00180                      /* If cookie-session, look for a session with matching session ID */
00181                      if (   (Hdr->HR.desired_session != 0)
00182                             && (sptr->wc_session == Hdr->HR.desired_session)
00183                      ) {
00184                             syslog(LOG_DEBUG, "Matched a session with the same cookie");
00185                             TheSession = sptr;
00186                      }
00187                      break;                    
00188               case NO_AUTH:
00189                      /* Any unbound session is a candidate */
00190                      if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
00191                             syslog(LOG_DEBUG, "Reusing an unbound session");
00192                             TheSession = sptr;
00193                      }
00194                      break;
00195               }
00196        }
00197        CtdlLogResult(pthread_mutex_unlock(ListMutex));
00198        if (TheSession == NULL) {
00199               syslog(LOG_DEBUG, "No existing session was matched");
00200        }
00201        return TheSession;
00202 }
00203 
00204 wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
00205 {
00206        wcsession *TheSession;
00207        TheSession = (wcsession *) malloc(sizeof(wcsession));
00208        memset(TheSession, 0, sizeof(wcsession));
00209        TheSession->Hdr = Hdr;
00210        TheSession->serv_sock = (-1);
00211 
00212        pthread_setspecific(MyConKey, (void *)TheSession);
00213        
00214        /* If we're recreating a session that expired, it's best to give it the same
00215         * session number that it had before.  The client browser ought to pick up
00216         * the new session number and start using it, but in some rare situations it
00217         * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
00218         * to get created.
00219         */    
00220        if (Hdr->HR.desired_session == 0) {
00221               TheSession->wc_session = GenerateSessionID();
00222               syslog(3, "Created new session %d", TheSession->wc_session);
00223        }
00224        else {
00225               TheSession->wc_session = Hdr->HR.desired_session;
00226               syslog(3, "Re-created session %d", TheSession->wc_session);
00227        }
00228        Hdr->HR.Static = Static;
00229        session_new_modules(TheSession);
00230 
00231        if (Lockable) {
00232               pthread_mutex_init(&TheSession->SessionMutex, NULL);
00233 
00234               if (ListMutex != NULL)
00235                      CtdlLogResult(pthread_mutex_lock(ListMutex));
00236 
00237               if (wclist != NULL) {
00238                      TheSession->nonce = rand();
00239                      TheSession->next = *wclist;
00240                      *wclist = TheSession;
00241               }
00242               if (ListMutex != NULL)
00243                      CtdlLogResult(pthread_mutex_unlock(ListMutex));
00244        }
00245        return TheSession;
00246 }
00247 
00248 
00249 /* If it's a "force 404" situation then display the error and bail. */
00250 void do_404(void)
00251 {
00252        hprintf("HTTP/1.1 404 Not found\r\n");
00253        hprintf("Content-Type: text/plain\r\n");
00254        wc_printf("Not found\r\n");
00255        end_burst();
00256 }
00257 
00258 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
00259 {
00260        const char *Args;
00261        void *vLine, *vHandler;
00262        const char *Pos = NULL;
00263 
00264        Hdr->HR.ReqLine = Line;
00265        /* The requesttype... GET, POST... */
00266        StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
00267        if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
00268            (vLine != NULL))
00269        {
00270               Hdr->HR.eReqType = *(long*)vLine;
00271        }
00272        else {
00273               Hdr->HR.eReqType = eGET;
00274               return 1;
00275        }
00276        StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
00277 
00278        /* the HTTP Version... */
00279        StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
00280        StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
00281        
00282        if (StrLength(Buf) == 0) {
00283               Hdr->HR.eReqType = eGET;
00284               return 1;
00285        }
00286 
00287        StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
00288 
00289        /* chop Filename / query arguments */
00290        Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
00291        if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
00292               Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
00293        if (Args != NULL) {
00294               Args ++; /* skip the ? */
00295               StrBufPlain(Hdr->PlainArgs, 
00296                          Args, 
00297                          StrLength(Hdr->HR.ReqLine) -
00298                          (Args - ChrPtr(Hdr->HR.ReqLine)));
00299               StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
00300        } /* don't parse them yet, maybe we don't even care... */
00301        
00302        /* now lookup what we are going to do with this... */
00303        /* skip first slash */
00304        StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
00305        do {
00306               StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
00307 
00308               GetHash(HandlerHash, SKEY(Buf), &vHandler),
00309               Hdr->HR.Handler = (WebcitHandler*) vHandler;
00310               if (Hdr->HR.Handler == NULL)
00311                      break;
00312               /*
00313                * If the request is prefixed by "/webcit" then chop that off.  This
00314                * allows a front end web server to forward all /webcit requests to us
00315                * while still using the same web server port for other things.
00316                */
00317               if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
00318                      continue;
00319               break;
00320        } while (1);
00321        /* remove the handlername from the URL */
00322        if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
00323               StrBufCutLeft(Hdr->HR.ReqLine, 
00324                            Pos - ChrPtr(Hdr->HR.ReqLine));
00325        }
00326 
00327        if (Hdr->HR.Handler != NULL) {
00328               if ((Hdr->HR.Handler->Flags & BOGUS) != 0) {
00329                      return 1;
00330               }
00331               Hdr->HR.DontNeedAuth = (
00332                      ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
00333                      ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
00334               );
00335        }
00336        else {
00337               /* If this is a "flat" request for the root, display the configured landing page. */
00338               int return_value;
00339               StrBuf *NewLine = NewStrBuf();
00340               Hdr->HR.DontNeedAuth = 1;
00341               StrBufAppendPrintf(NewLine, "GET /landing?go=%s HTTP/1.0", ChrPtr(Buf));
00342               syslog(LOG_DEBUG, "Replacing with: %s", ChrPtr(NewLine));
00343               return_value = ReadHttpSubject(Hdr, NewLine, Buf);
00344               FreeStrBuf(&NewLine);
00345               return return_value;
00346        }
00347 
00348        return 0;
00349 }
00350 
00351 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
00352 {
00353        OneHttpHeader *pHdr;
00354        void *vHdr;
00355        long HKLen;
00356        const char *HashKey;
00357        HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
00358        
00359        while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) && 
00360               (vHdr != NULL)) {
00361               pHdr = (OneHttpHeader *)vHdr;
00362               if (pHdr->HaveEvaluator)
00363                      pHdr->H(pHdr->Val, Hdr);
00364 
00365        }
00366        DeleteHashPos(&at);
00367        return 0;
00368 }
00369 
00370 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
00371 
00372 /*
00373  * Read in the request
00374  */
00375 int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
00376 {
00377        const char *pch, *pchs, *pche;
00378        OneHttpHeader *pHdr;
00379        StrBuf *Line, *LastLine, *HeaderName;
00380        int nLine = 0;
00381        void *vF;
00382        int isbogus = 0;
00383 
00384        HeaderName = NewStrBuf();
00385        LastLine = NULL;
00386        do {
00387               nLine ++;
00388               Line = NewStrBufPlain(NULL, SIZ / 4);
00389 
00390               if (ClientGetLine(Hdr, Line) < 0) return 1;
00391 
00392               if (StrLength(Line) == 0) {
00393                      FreeStrBuf(&Line);
00394                      continue;
00395               }
00396               if (nLine == 1) {
00397                      Hdr->HTTPHeaders = NewHash(1, NULL);
00398                      pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
00399                      memset(pHdr, 0, sizeof(OneHttpHeader));
00400                      pHdr->Val = Line;
00401                      Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
00402                      syslog(9, "%s", ChrPtr(Line));
00403                      isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
00404                      if (isbogus) break;
00405                      continue;
00406               }
00407 
00408               /* Do we need to Unfold? */
00409               if ((LastLine != NULL) && 
00410                   (isspace(*ChrPtr(Line)))) {
00411                      pch = pchs = ChrPtr(Line);
00412                      pche = pchs + StrLength(Line);
00413                      while (isspace(*pch) && (pch < pche))
00414                             pch ++;
00415                      StrBufCutLeft(Line, pch - pchs);
00416                      StrBufAppendBuf(LastLine, Line, 0);
00417 
00418                      FreeStrBuf(&Line);
00419                      continue;
00420               }
00421 
00422               StrBufSanitizeAscii(Line, '');
00423               StrBufExtract_token(HeaderName, Line, 0, ':');
00424 
00425               pchs = ChrPtr(Line);
00426               pche = pchs + StrLength(Line);
00427               pch = pchs + StrLength(HeaderName) + 1;
00428               pche = pchs + StrLength(Line);
00429               while ((pch < pche) && isspace(*pch))
00430                      pch ++;
00431               StrBufCutLeft(Line, pch - pchs);
00432 
00433               StrBufUpCase(HeaderName);
00434 
00435               pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
00436               memset(pHdr, 0, sizeof(OneHttpHeader));
00437               pHdr->Val = Line;
00438 
00439               if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
00440                   (vF != NULL))
00441               {
00442                      OneHttpHeader *FHdr = (OneHttpHeader*) vF;
00443                      pHdr->H = FHdr->H;
00444                      pHdr->HaveEvaluator = 1;
00445               }
00446               Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
00447               LastLine = Line;
00448        } while (Line != NULL);
00449 
00450        FreeStrBuf(&HeaderName);
00451 
00452        return isbogus;
00453 }
00454 
00455 void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
00456 {
00457        StrBuf *Buf = NewStrBuf();
00458 
00459        if (Hdr->HR.ReqLine != NULL) {
00460               FlushStrBuf(Hdr->HR.ReqLine);
00461               StrBufPlain(Hdr->HR.ReqLine, Line, len);
00462        }
00463        else {
00464               Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
00465        }
00466        ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
00467 
00468        FreeStrBuf(&Buf);
00469 }
00470 
00471 /*
00472  * handle one request
00473  *
00474  * This loop gets called once for every HTTP connection made to WebCit.  At
00475  * this entry point we have an HTTP socket with a browser allegedly on the
00476  * other end, but we have not yet bound to a WebCit session.
00477  *
00478  * The job of this function is to locate the correct session and bind to it,
00479  * or create a session if necessary and bind to it, then run the WebCit
00480  * transaction loop.  Afterwards, we unbind from the session.  When this
00481  * function returns, the worker thread is then free to handle another
00482  * transaction.
00483  */
00484 void context_loop(ParsedHttpHdrs *Hdr)
00485 {
00486        int isbogus = 0;
00487        wcsession *TheSession;
00488        struct timeval tx_start;
00489        struct timeval tx_finish;
00490        int session_may_be_reused = 1;
00491        
00492        gettimeofday(&tx_start, NULL);            /* start a stopwatch for performance timing */
00493 
00494        /*
00495         * Find out what it is that the web browser is asking for
00496         */
00497        isbogus = ReadHTTPRequest(Hdr);
00498 
00499        Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
00500 
00501        if (!isbogus) {
00502               isbogus = AnalyseHeaders(Hdr);
00503        }
00504 
00505        if (   (isbogus)
00506               || ((Hdr->HR.Handler != NULL)
00507               && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
00508        ) {
00509               wcsession *Bogus;
00510               Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
00511               do_404();
00512               syslog(9, "HTTP: 404 [%ld.%06ld] %s %s",
00513                      ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
00514                      ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
00515                      ReqStrs[Hdr->HR.eReqType],
00516                      ChrPtr(Hdr->this_page)
00517                      );
00518               session_detach_modules(Bogus);
00519               session_destroy_modules(&Bogus);
00520               return;
00521        }
00522 
00523        if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
00524        {
00525               wcsession *Static;
00526               Static = CreateSession(0, 1, NULL, Hdr, NULL);
00527               
00528               Hdr->HR.Handler->F();
00529 
00530               /* How long did this transaction take? */
00531               gettimeofday(&tx_finish, NULL);
00532               
00533               syslog(9, "HTTP: 200 [%ld.%06ld] %s %s",
00534                      ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
00535                      ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
00536                      ReqStrs[Hdr->HR.eReqType],
00537                      ChrPtr(Hdr->this_page)
00538               );
00539               session_detach_modules(Static);
00540               session_destroy_modules(&Static);
00541               return;
00542        }
00543 
00544        if (Hdr->HR.got_auth == AUTH_BASIC) {
00545               CheckAuthBasic(Hdr);
00546        }
00547 
00548        if (Hdr->HR.got_auth) {
00549               session_may_be_reused = 0;
00550        }
00551 
00552        /*
00553         * See if there's an existing session open with any of:
00554         * - The desired Session ID
00555         * - A matching http-auth username and password
00556         * - An unbound session flagged as reusable
00557         */
00558        TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
00559 
00560        /*
00561         * If there were no qualifying sessions, then create a new one.
00562         */
00563        if (TheSession == NULL) {
00564               TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
00565        }
00566 
00567        /*
00568         * Reject transactions which require http-auth, if http-auth was not provided
00569         */
00570        if (   (StrLength(Hdr->c_username) == 0)
00571               && (!Hdr->HR.DontNeedAuth)
00572               && (Hdr->HR.Handler != NULL)
00573               && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
00574        ) {
00575               syslog(LOG_DEBUG, "http-auth required but not provided");
00576               OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
00577               Hdr->HR.prohibit_caching = 1;                           
00578        }
00579 
00580        /*
00581         * A future improvement might be to check the session integrity
00582         * at this point before continuing.
00583         */
00584 
00585        /*
00586         * Bind to the session and perform the transaction
00587         */
00588        CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
00589        pthread_setspecific(MyConKey, (void *)TheSession);
00590        
00591        TheSession->inuse = 1;                                  /* mark the session as bound */
00592        TheSession->lastreq = time(NULL);                /* log */
00593        TheSession->Hdr = Hdr;
00594 
00595        /*
00596         * If a language was requested via a cookie, select that language now.
00597         */
00598        if (StrLength(Hdr->c_language) > 0) {
00599               syslog(LOG_DEBUG, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
00600               set_selected_language(ChrPtr(Hdr->c_language));
00601               go_selected_language();
00602        }
00603 
00604        /*
00605         * do the transaction
00606         */
00607        session_attach_modules(TheSession);
00608        session_loop();
00609 
00610        /* How long did this transaction take? */
00611        gettimeofday(&tx_finish, NULL);
00612 
00613        syslog(9, "HTTP: 200 [%ld.%06ld] %s %s",
00614               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
00615               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
00616               ReqStrs[Hdr->HR.eReqType],
00617               ChrPtr(Hdr->this_page)
00618        );
00619 
00620        session_detach_modules(TheSession);
00621 
00622        /* If *this* very transaction did not explicitly specify a session cookie,
00623         * and it did not log in, we want to flag the session as a candidate for
00624         * re-use by the next unbound client that comes along.  This keeps our session
00625         * table from getting bombarded with new sessions when, for example, a web
00626         * spider crawls the site without using cookies.
00627         */
00628        if ((session_may_be_reused) && (!WC->logged_in)) {
00629               WC->wc_session = 0;                /* flag as available for re-use */
00630               TheSession->selected_language = 0; /* clear any non-default language setting */
00631        }
00632 
00633        TheSession->Hdr = NULL;
00634        TheSession->inuse = 0;                                  /* mark the session as unbound */
00635        CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
00636 }
00637 
00638 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
00639 {
00640        wcsession *WCC = WC;
00641        StrBufAppendPrintf(Target, "%ld",
00642                         (WCC != NULL)? WCC->nonce:0);             
00643 }
00644 
00645 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
00646 {
00647        StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
00648 }
00649 
00650 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
00651 {
00652        hdr->HR.ContentLength = StrToi(Line);
00653 }
00654 
00655 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
00656 {
00657        hdr->HR.ContentType = Line;
00658 }
00659 
00660 
00661 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
00662 {
00663        if (hdr->HostHeader != NULL) {
00664               FreeStrBuf(&hdr->HostHeader);
00665        }
00666        hdr->HostHeader = NewStrBuf();
00667        StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
00668        StrBufAppendBuf(hdr->HostHeader, Line, 0);
00669 }
00670 
00671 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
00672 {
00673        if (!follow_xff) return;
00674 
00675        if (hdr->HostHeader != NULL) {
00676               FreeStrBuf(&hdr->HostHeader);
00677        }
00678 
00679        hdr->HostHeader = NewStrBuf();
00680        StrBufAppendPrintf(hdr->HostHeader, "http://");  /* this is naive; do something about it */
00681        StrBufAppendBuf(hdr->HostHeader, Line, 0);
00682 }
00683 
00684 
00685 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
00686 {
00687        hdr->HR.browser_host = Line;
00688 
00689        while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
00690               StrBufRemove_token(hdr->HR.browser_host, 0, ',');
00691        }
00692        StrBufTrim(hdr->HR.browser_host);
00693 }
00694 
00695 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
00696 {
00697        hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
00698 }
00699 
00700 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
00701 {
00702        /*
00703         * Can we compress?
00704         */
00705        if (strstr(&ChrPtr(Line)[16], "gzip")) {
00706               hdr->HR.gzip_ok = 1;
00707        }
00708 }
00709 const char *ReqStrs[eNONE] = {
00710        "GET",
00711        "POST",
00712        "OPTIONS",
00713        "PROPFIND",
00714        "PUT",
00715        "DELETE",
00716        "HEAD",
00717        "MOVE",
00718        "COPY",
00719        "REPORT"
00720 };
00721 
00722 void
00723 ServerStartModule_CONTEXT
00724 (void)
00725 {
00726        long *v;
00727        HttpReqTypes = NewHash(1, NULL);
00728        HttpHeaderHandler = NewHash(1, NULL);
00729 
00730        v = malloc(sizeof(long));
00731        *v = eGET;
00732        Put(HttpReqTypes, HKEY("GET"), v, NULL);
00733 
00734        v = malloc(sizeof(long));
00735        *v = ePOST;
00736        Put(HttpReqTypes, HKEY("POST"), v, NULL);
00737 
00738        v = malloc(sizeof(long));
00739        *v = eOPTIONS;
00740        Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
00741 
00742        v = malloc(sizeof(long));
00743        *v = ePROPFIND;
00744        Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
00745 
00746        v = malloc(sizeof(long));
00747        *v = ePUT;
00748        Put(HttpReqTypes, HKEY("PUT"), v, NULL);
00749 
00750        v = malloc(sizeof(long));
00751        *v = eDELETE;
00752        Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
00753 
00754        v = malloc(sizeof(long));
00755        *v = eHEAD;
00756        Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
00757 
00758        v = malloc(sizeof(long));
00759        *v = eMOVE;
00760        Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
00761 
00762        v = malloc(sizeof(long));
00763        *v = eCOPY;
00764        Put(HttpReqTypes, HKEY("COPY"), v, NULL);
00765 
00766        v = malloc(sizeof(long));
00767        *v = eREPORT;
00768        Put(HttpReqTypes, HKEY("REPORT"), v, NULL);
00769 }
00770 
00771 void 
00772 ServerShutdownModule_CONTEXT
00773 (void)
00774 {
00775        DeleteHash(&HttpReqTypes);
00776        DeleteHash(&HttpHeaderHandler);
00777 }
00778 
00779 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
00780 {
00781        OneHttpHeader *pHdr;
00782        pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
00783        memset(pHdr, 0, sizeof(OneHttpHeader));
00784        pHdr->H = F;
00785        Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
00786 }
00787 
00788 
00789 void 
00790 InitModule_CONTEXT
00791 (void)
00792 {
00793        RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
00794        RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
00795        RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
00796        RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost);        /* NGinX way... */
00797        RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
00798        RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
00799        RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
00800        RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
00801 
00802        RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
00803        RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
00804 
00805        WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
00806 /*
00807  * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
00808  * Short-circuit these requests so we don't have to send them through the full processing loop.
00809  */
00810        WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS);        /* /root.exe - Worms and trojans and viruses, oh my! */
00811        WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS);              /* /winnt */
00812        WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
00813        WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS);           /* Broken Microsoft DAV implementation */
00814        WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS);              /* Stoopid MSOffice thinks everyone is IIS */
00815        WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
00816 }
00817        
00818 
00819 void 
00820 HttpNewModule_CONTEXT
00821 (ParsedHttpHdrs *httpreq)
00822 {
00823        httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
00824        httpreq->this_page = NewStrBufPlain(NULL, SIZ);
00825 }
00826 
00827 void 
00828 HttpDetachModule_CONTEXT
00829 (ParsedHttpHdrs *httpreq)
00830 {
00831        FlushStrBuf(httpreq->PlainArgs);
00832        FlushStrBuf(httpreq->HostHeader);
00833        FlushStrBuf(httpreq->this_page);
00834        FlushStrBuf(httpreq->PlainArgs);
00835        DeleteHash(&httpreq->HTTPHeaders);
00836        memset(&httpreq->HR, 0, sizeof(HdrRefs));
00837 }
00838 
00839 void 
00840 HttpDestroyModule_CONTEXT
00841 (ParsedHttpHdrs *httpreq)
00842 {
00843        FreeStrBuf(&httpreq->this_page);
00844        FreeStrBuf(&httpreq->PlainArgs);
00845        FreeStrBuf(&httpreq->this_page);
00846        FreeStrBuf(&httpreq->PlainArgs);
00847        FreeStrBuf(&httpreq->HostHeader);
00848        DeleteHash(&httpreq->HTTPHeaders);
00849 
00850 }