* Moved to the new string tokenizer API
[citadel.git] / webcit / context_loop.c
index 995393b074249556716ba992d2aa85b46830ac77..9fd9ba8fe4f9b003457879b5ff7bb21ef72eb33a 100644 (file)
@@ -59,61 +59,67 @@ void free_attachments(struct wcsession *sess) {
 
 void do_housekeeping(void)
 {
-       struct wcsession *sptr, *ss, *session_to_kill;
+       struct wcsession *sptr, *ss;
+       struct wcsession *sessions_to_kill = NULL;
        int num_sessions = 0;
        static int num_threads = MIN_WORKER_THREADS;
 
-       do {
-               session_to_kill = NULL;
-               pthread_mutex_lock(&SessionListMutex);
-               num_sessions = 0;
-               for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
-                       ++num_sessions;
-
-                       /* Kill idle sessions */
-                       if ((time(NULL) - (sptr->lastreq)) >
-                          (time_t) WEBCIT_TIMEOUT) {
-                               sptr->killthis = 1;
-                       }
-
-                       /* Remove sessions flagged for kill */
-                       if (sptr->killthis) {
-
-                               lprintf(3, "Destroying session %d\n",
-                                       sptr->wc_session);
+       /*
+        * Lock the session list, moving any candidates for euthanasia into
+        * a separate list.
+        */
+       pthread_mutex_lock(&SessionListMutex);
+       num_sessions = 0;
+       for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+               ++num_sessions;
+
+               /* Kill idle sessions */
+               if ((time(NULL) - (sptr->lastreq)) >
+                  (time_t) WEBCIT_TIMEOUT) {
+                       sptr->killthis = 1;
+               }
 
-                               /* remove session from linked list */
-                               if (sptr == SessionList) {
-                                       SessionList = SessionList->next;
-                               }
-                               else for (ss=SessionList;ss!=NULL;ss=ss->next) {
-                                       if (ss->next == sptr) {
-                                               ss->next = ss->next->next;
-                                       }
-                               }
+               /* Remove sessions flagged for kill */
+               if (sptr->killthis) {
 
-                               session_to_kill = sptr;
-                               goto BREAKOUT;
+                       /* remove session from linked list */
+                       if (sptr == SessionList) {
+                               SessionList = SessionList->next;
                        }
-               }
-BREAKOUT:      pthread_mutex_unlock(&SessionListMutex);
-
-               if (session_to_kill != NULL) {
-                       pthread_mutex_lock(&session_to_kill->SessionMutex);
-                       close(session_to_kill->serv_sock);
-                       close(session_to_kill->chat_sock);
-                       if (session_to_kill->preferences != NULL) {
-                               free(session_to_kill->preferences);
+                       else for (ss=SessionList;ss!=NULL;ss=ss->next) {
+                               if (ss->next == sptr) {
+                                       ss->next = ss->next->next;
+                               }
                        }
-                       free_attachments(session_to_kill);
-                       pthread_mutex_unlock(&session_to_kill->SessionMutex);
-                       free(session_to_kill);
+
+                       sptr->next = sessions_to_kill;
+                       sessions_to_kill = sptr;
                }
+       }
+       pthread_mutex_unlock(&SessionListMutex);
 
-       } while (session_to_kill != NULL);
+       /*
+        * Now free up and destroy the culled sessions.
+        */
+       while (sessions_to_kill != NULL) {
+               lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
+               pthread_mutex_lock(&sessions_to_kill->SessionMutex);
+               close(sessions_to_kill->serv_sock);
+               close(sessions_to_kill->chat_sock);
+               if (sessions_to_kill->preferences != NULL) {
+                       free(sessions_to_kill->preferences);
+               }
+               free_attachments(sessions_to_kill);
+               pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
+               sptr = sessions_to_kill->next;
+               free(sessions_to_kill);
+               sessions_to_kill = sptr;
+               --num_sessions;
+       }
 
        /*
-        * See if we need more worker threads
+        * If there are more sessions than threads, then we should spawn
+        * more threads ... up to a predefined maximum.
         */
        while ( (num_sessions > num_threads)
              && (num_threads <= MAX_WORKER_THREADS) ) {
@@ -261,14 +267,15 @@ void context_loop(int sock)
        char buf[SIZ], hold[SIZ];
        int desired_session = 0;
        int got_cookie = 0;
+       int gzip_ok = 0;
        struct wcsession *TheSession, *sptr;
        char httpauth_string[SIZ];
        char httpauth_user[SIZ];
        char httpauth_pass[SIZ];
 
        strcpy(httpauth_string, "");
-       strcpy(httpauth_user, "");
-       strcpy(httpauth_pass, "");
+       strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
+       strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
 
        /*
         * Find out what it is that the web browser is asking for
@@ -277,12 +284,21 @@ void context_loop(int sock)
        do {
                if (req_gets(sock, buf, hold) < 0) return;
 
+               /*
+                * Can we compress?
+                */
+               if (!strncasecmp(buf, "Accept-encoding:", 16)) {
+                       if (strstr(&buf[16], "gzip")) {
+                               gzip_ok = 1;
+                       }
+               }
+
                /*
                 * Browser-based sessions use cookies for session authentication
                 */
                if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
                        cookie_to_stuff(&buf[15], &desired_session,
-                               NULL, NULL, NULL);
+                               NULL, 0, NULL, 0, NULL, 0);
                        got_cookie = 1;
                }
 
@@ -291,8 +307,8 @@ void context_loop(int sock)
                 */
                if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
                        CtdlDecodeBase64(httpauth_string, &buf[21], strlen(&buf[21]));
-                       extract_token(httpauth_user, httpauth_string, 0, ':');
-                       extract_token(httpauth_pass, httpauth_string, 1, ':');
+                       extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
+                       extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
                }
 
                /*
@@ -357,23 +373,22 @@ void context_loop(int sock)
         */
        TheSession = NULL;
 
-       if ( (TheSession == NULL) && (strlen(httpauth_user) > 0) ) {
+       if (TheSession == NULL) {
                pthread_mutex_lock(&SessionListMutex);
                for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
-                       if ( (!strcasecmp(sptr->httpauth_user, httpauth_user))
+
+                       /* If HTTP-AUTH, look for a session with matching credentials */
+                       if ( (strlen(httpauth_user) > 0)
+                          &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
                           &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
                                TheSession = sptr;
                        }
-               }
-               pthread_mutex_unlock(&SessionListMutex);
-       }
 
-       if ( (TheSession == NULL) && (desired_session != 0) ) {
-               pthread_mutex_lock(&SessionListMutex);
-               for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
-                       if (sptr->wc_session == desired_session) {
+                       /* If cookie-session, look for a session with matching session ID */
+                       if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
                                TheSession = sptr;
                        }
+
                }
                pthread_mutex_unlock(&SessionListMutex);
        }
@@ -411,6 +426,7 @@ void context_loop(int sock)
        pthread_setspecific(MyConKey, (void *)TheSession);
        TheSession->http_sock = sock;
        TheSession->lastreq = time(NULL);                       /* log */
+       TheSession->gzip_ok = gzip_ok;
        session_loop(req);                              /* do transaction */
        pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */