* fix multiple compiler warnings...
[citadel.git] / webcit / context_loop.c
index 7521207b21b67fa90469ad2f95ed01f0fe8c6d78..f8a9966d0c48140a8df58684b7d92f4027379018 100644 (file)
@@ -1,17 +1,15 @@
 /*
- * $Id$
- *
  * This is the other half of the webserver.  It handles the task of hooking
  * up HTTP requests with the sessions they belong to, using HTTP cookies to
  * keep track of things.  If the HTTP request doesn't belong to any currently
  * active session, a new session is started.
  *
- * Copyright (c) 1996-2010 by the citadel.org team
+ * Copyright (c) 1996-2011 by the citadel.org team
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * This program is open source software.  You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
 #include "webcit.h"
@@ -67,7 +65,7 @@ void do_housekeeping(void)
         * Lock the session list, moving any candidates for euthanasia into
         * a separate list.
         */
-       pthread_mutex_lock(&SessionListMutex);
+       CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
        for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
 
                /* Kill idle sessions */
@@ -92,13 +90,13 @@ void do_housekeeping(void)
                        sessions_to_kill = sptr;
                }
        }
-       pthread_mutex_unlock(&SessionListMutex);
+       CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
 
        /*
         * Now free up and destroy the culled sessions.
         */
        while (sessions_to_kill != NULL) {
-               lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
+               syslog(3, "Destroying session %d\n", sessions_to_kill->wc_session);
                pthread_mutex_lock(&sessions_to_kill->SessionMutex);
                pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
                sptr = sessions_to_kill->next;
@@ -106,15 +104,24 @@ void do_housekeeping(void)
                session_destroy_modules(&sessions_to_kill);
                sessions_to_kill = sptr;
        }
+}
 
-       /*
-        * Check the size of our thread pool.  If all threads are executing, spawn another.
-        */
-       begin_critical_section(S_SPAWNER);
-       while (
+/*
+ * Check the size of our thread pool.  If all threads are executing, spawn another.
+ */
+void check_thread_pool_size(void)
+{
+       if (time_to_die) return;                /* don't expand the thread pool during shutdown */
+
+       begin_critical_section(S_SPAWNER);      /* only one of these should run at a time */
+       if (
                (num_threads_executing >= num_threads_existing)
                && (num_threads_existing < MAX_WORKER_THREADS)
        ) {
+               syslog(3, "%d of %d threads are executing.  Adding another worker thread.\n",
+                       num_threads_executing,
+                       num_threads_existing
+               );
                spawn_another_worker_thread();
        }
        end_critical_section(S_SPAWNER);
@@ -158,7 +165,7 @@ wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t
                GetAuthBasic(Hdr);
        }
 
-       pthread_mutex_lock(ListMutex);
+       CtdlLogResult(pthread_mutex_lock(ListMutex));
        for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
                
                /* If HTTP-AUTH, look for a session with matching credentials */
@@ -172,7 +179,7 @@ wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t
                                TheSession = sptr;
                        }
                        if (TheSession == NULL)
-                               lprintf(1, "found sessionkey [%ld], but credentials for [%s|%s] didn't match\n",
+                               syslog(1, "found sessionkey [%d], but credentials for [%s|%s] didn't match\n",
                                        Hdr->HR.SessionKey,ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username));
                        break;
                case AUTH_COOKIE:
@@ -186,10 +193,10 @@ wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t
                        break;
                }
        }
-       pthread_mutex_unlock(ListMutex);
+       CtdlLogResult(pthread_mutex_unlock(ListMutex));
        if (TheSession == NULL)
-               lprintf(1, "didn't find sessionkey [%ld] for user [%s]\n",
-                       Hdr->HR.SessionKey,ChrPtr(Hdr->c_username));
+               syslog(1, "didn't find sessionkey [%d] for user [%s]\n",
+                       Hdr->HR.SessionKey, ChrPtr(Hdr->c_username));
        return TheSession;
 }
 
@@ -197,13 +204,12 @@ wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHtt
 {
        wcsession *TheSession;
        if (!Static)
-               lprintf(3, "Creating a new session\n");
+               syslog(3, "Creating a new session\n");
        TheSession = (wcsession *) malloc(sizeof(wcsession));
        memset(TheSession, 0, sizeof(wcsession));
        TheSession->Hdr = Hdr;
        TheSession->SessionKey = Hdr->HR.SessionKey;
        TheSession->serv_sock = (-1);
-       TheSession->is_mobile = -1;
 
        pthread_setspecific(MyConKey, (void *)TheSession);
        
@@ -226,7 +232,7 @@ wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHtt
                pthread_mutex_init(&TheSession->SessionMutex, NULL);
 
                if (ListMutex != NULL)
-                       pthread_mutex_lock(ListMutex);
+                       CtdlLogResult(pthread_mutex_lock(ListMutex));
 
                if (wclist != NULL) {
                        TheSession->nonce = rand();
@@ -234,31 +240,12 @@ wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHtt
                        *wclist = TheSession;
                }
                if (ListMutex != NULL)
-                       pthread_mutex_unlock(ListMutex);
+                       CtdlLogResult(pthread_mutex_unlock(ListMutex));
        }
        return TheSession;
 }
 
 
-/*
- * Detects a 'mobile' user agent 
- */
-int is_mobile_ua(char *user_agent) {
-      if (strstr(user_agent,"iPhone OS") != NULL) {
-       return 1;
-      } else if (strstr(user_agent,"Windows CE") != NULL) {
-       return 1;
-      } else if (strstr(user_agent,"SymbianOS") != NULL) {
-       return 1;
-      } else if (strstr(user_agent, "Opera Mobi") != NULL) {
-       return 1;
-      } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
-             /*  For some reason a new install of Opera 9.51beta decided to spoof. */
-         return 1;
-         }
-      return 0;
-}
-
 /* If it's a "force 404" situation then display the error and bail. */
 void do_404(void)
 {
@@ -299,6 +286,7 @@ int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
        }
 
        StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
+
        /* chop Filename / query arguments */
        Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
        if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
@@ -403,7 +391,7 @@ int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
                        memset(pHdr, 0, sizeof(OneHttpHeader));
                        pHdr->Val = Line;
                        Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
-                       lprintf(9, "%s\n", ChrPtr(Line));
+                       syslog(9, "%s\n", ChrPtr(Line));
                        isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
                        if (isbogus) break;
                        continue;
@@ -513,7 +501,7 @@ void context_loop(ParsedHttpHdrs *Hdr)
 
                do_404();
 
-               lprintf(9, "HTTP: 404 [%ld.%06ld] %s %s \n",
+               syslog(9, "HTTP: 404 [%ld.%06ld] %s %s \n",
                        ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
                        ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
                        ReqStrs[Hdr->HR.eReqType],
@@ -534,16 +522,12 @@ void context_loop(ParsedHttpHdrs *Hdr)
                /* How long did this transaction take? */
                gettimeofday(&tx_finish, NULL);
                
-#ifdef TECH_PREVIEW
-               if ((Hdr->HR.Handler != NULL) ||
-                   ((Hdr->HR.Handler->Flags & LOGCHATTY) == 0))
-#endif
-                       lprintf(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
-                               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
-                               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
-                               ReqStrs[Hdr->HR.eReqType],
-                               ChrPtr(Hdr->this_page)
-                               );
+               syslog(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
+                       ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+                       ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
+                       ReqStrs[Hdr->HR.eReqType],
+                       ChrPtr(Hdr->this_page)
+               );
                session_detach_modules(Static);
                session_destroy_modules(&Static);
                return;
@@ -564,21 +548,17 @@ void context_loop(ParsedHttpHdrs *Hdr)
        if (TheSession == NULL) {
                TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
 
-               if ((StrLength(Hdr->c_username) == 0) && (!Hdr->HR.DontNeedAuth)) {
-
-                       if ((Hdr->HR.Handler != NULL) && 
-                           (XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS) {
-                               OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
-                               Hdr->HR.prohibit_caching = 1;                           
-                       }
-                       else {
-                               OverrideRequest(Hdr, HKEY("GET /static/nocookies.html?force_close_session=yes HTTP/1.0"));
-                               Hdr->HR.prohibit_caching = 1;
-                       }
+               if (    (StrLength(Hdr->c_username) == 0)
+                       && (!Hdr->HR.DontNeedAuth)
+                       && (Hdr->HR.Handler != NULL)
+                       && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
+               ) {
+                       OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
+                       Hdr->HR.prohibit_caching = 1;                           
                }
                
                if (StrLength(Hdr->c_language) > 0) {
-                       lprintf(9, "Session cookie requests language '%s'\n", ChrPtr(Hdr->c_language));
+                       syslog(9, "Session cookie requests language '%s'\n", ChrPtr(Hdr->c_language));
                        set_selected_language(ChrPtr(Hdr->c_language));
                        go_selected_language();
                }
@@ -592,7 +572,7 @@ void context_loop(ParsedHttpHdrs *Hdr)
        /*
         * Bind to the session and perform the transaction
         */
-       pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
+       CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
        pthread_setspecific(MyConKey, (void *)TheSession);
        
        TheSession->lastreq = time(NULL);                       /* log */
@@ -606,21 +586,17 @@ void context_loop(ParsedHttpHdrs *Hdr)
        gettimeofday(&tx_finish, NULL);
        
 
-#ifdef TECH_PREVIEW
-       if ((Hdr->HR.Handler != NULL) &&
-           ((Hdr->HR.Handler->Flags & LOGCHATTY) == 0))
-#endif
-               lprintf(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
-                       ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
-                       ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
-                       ReqStrs[Hdr->HR.eReqType],
-                       ChrPtr(Hdr->this_page)
-                       );
+       syslog(9, "HTTP: 200 [%ld.%06ld] %s %s \n",
+               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
+               ReqStrs[Hdr->HR.eReqType],
+               ChrPtr(Hdr->this_page)
+       );
 
        session_detach_modules(TheSession);
 
        TheSession->Hdr = NULL;
-       pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
+       CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
 }
 
 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
@@ -645,34 +621,28 @@ void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
        hdr->HR.ContentType = Line;
 }
 
-void Header_HandleUserAgent(StrBuf *Line, ParsedHttpHdrs *hdr)
-{
-       hdr->HR.user_agent = Line;
-#ifdef TECH_PREVIEW
-/* TODO: do this later on session creating
-       if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {                   
-               WCC->is_mobile = 1;
-       }
-       else {
-               WCC->is_mobile = 0;
-       }
-*/
-#endif
-}
-
 
 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
 {
-       if ((follow_xff) && (hdr->HR.http_host != NULL))
-               return;
-       else
-               hdr->HR.http_host = Line;
+       if (hdr->HostHeader != NULL) {
+               FreeStrBuf(&hdr->HostHeader);
+       }
+       hdr->HostHeader = NewStrBuf();
+       StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
+       StrBufAppendBuf(hdr->HostHeader, Line, 0);
 }
 
 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
 {
-       if (follow_xff)
-               hdr->HR.http_host = Line;
+       if (!follow_xff) return;
+
+       if (hdr->HostHeader != NULL) {
+               FreeStrBuf(&hdr->HostHeader);
+       }
+
+       hdr->HostHeader = NewStrBuf();
+       StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
+       StrBufAppendBuf(hdr->HostHeader, Line, 0);
 }
 
 
@@ -781,7 +751,6 @@ InitModule_CONTEXT
 {
        RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
        RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
-       RegisterHeaderHandler(HKEY("USER-AGENT"), Header_HandleUserAgent);
        RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
        RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost);        /* NGinX way... */
        RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
@@ -819,6 +788,7 @@ HttpDetachModule_CONTEXT
 (ParsedHttpHdrs *httpreq)
 {
        FlushStrBuf(httpreq->PlainArgs);
+       FlushStrBuf(httpreq->HostHeader);
        FlushStrBuf(httpreq->this_page);
        FlushStrBuf(httpreq->PlainArgs);
        DeleteHash(&httpreq->HTTPHeaders);
@@ -833,6 +803,7 @@ HttpDestroyModule_CONTEXT
        FreeStrBuf(&httpreq->PlainArgs);
        FreeStrBuf(&httpreq->this_page);
        FreeStrBuf(&httpreq->PlainArgs);
+       FreeStrBuf(&httpreq->HostHeader);
        DeleteHash(&httpreq->HTTPHeaders);
 
 }