* *** HUGE CHANGES *** *** WARNING: NOT FULLY FUNCTIONAL ***
[citadel.git] / webcit / context_loop.c
index 30a41e41b250652b83461b2c2b5d5401ae116cb0..e9ea3772ac210c8bc348c62a564ce6945b1bc2b1 100644 (file)
@@ -45,14 +45,31 @@ struct wcsession *SessionList = NULL;
 
 pthread_key_t MyConKey;                         /* TSD key for MySession() */
 
+
+void free_attachments(struct wcsession *sess) {
+       struct wc_attachment *att;
+
+       while (sess->first_attachment != NULL) {
+               att = sess->first_attachment;
+               sess->first_attachment = sess->first_attachment->next;
+               free(att->data);
+               free(att);
+       }
+}
+
+
 void do_housekeeping(void)
 {
        struct wcsession *sptr, *ss, *session_to_kill;
+       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)) >
@@ -63,6 +80,9 @@ void do_housekeeping(void)
                        /* Remove sessions flagged for kill */
                        if (sptr->killthis) {
 
+                               lprintf(3, "Destroying session %d\n",
+                                       sptr->wc_session);
+
                                /* remove session from linked list */
                                if (sptr == SessionList) {
                                        SessionList = SessionList->next;
@@ -82,14 +102,27 @@ 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);
                        }
+                       free_attachments(session_to_kill);
                        pthread_mutex_unlock(&session_to_kill->SessionMutex);
                        free(session_to_kill);
                }
 
        } while (session_to_kill != NULL);
+
+       /*
+        * See if we need more worker threads
+        */
+       while ( (num_sessions > num_threads)
+             && (num_threads <= MAX_WORKER_THREADS) ) {
+               spawn_another_worker_thread();
+               ++num_threads;
+               lprintf(3, "There are %d sessions and %d threads active.\n",
+                       num_sessions, num_threads);
+       }
 }
 
 
@@ -99,7 +132,7 @@ BREAKOUT:    pthread_mutex_unlock(&SessionListMutex);
 void housekeeping_loop(void)
 {
        while (1) {
-               sleep(HOUSEKEEPING);
+               sleeeeeeeeeep(HOUSEKEEPING);
                do_housekeeping();
        }
 }
@@ -109,7 +142,7 @@ void housekeeping_loop(void)
  * Generate a unique WebCit session ID (which is not the same thing as the
  * Citadel session ID).
  *
- * FIX ... ensure that session number is truly unique
+ * FIXME ... ensure that session number is truly unique
  *
  */
 int GenerateSessionID(void)
@@ -136,7 +169,7 @@ int req_gets(int sock, char *buf, char *hold)
                a = client_gets(sock, buf);
                if (a<1) return(-1);
        } else {
-               strcpy(buf, hold);
+               safestrncpy(buf, hold, SIZ);
        }
        strcpy(hold, "");
 
@@ -150,6 +183,7 @@ int req_gets(int sock, char *buf, char *hold)
                                return(0);
                        }
        }
+
        return(0);
 }
 
@@ -236,6 +270,7 @@ void context_loop(int sock)
        memset(hold, 0, sizeof(hold));
        do {
                if (req_gets(sock, buf, hold) < 0) return;
+
                if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
                        cookie_to_stuff(&buf[15], &desired_session,
                                NULL, NULL, NULL);
@@ -251,11 +286,11 @@ void context_loop(int sock)
                hptr->next = NULL;
                last = hptr;
 
-               strcpy(hptr->line, buf);
+               safestrncpy(hptr->line, buf, sizeof hptr->line);
 
        } while (strlen(buf) > 0);
 
-       strcpy(buf, req->line);
+       safestrncpy(buf, req->line, sizeof buf);
        lprintf(5, "HTTP: %s\n", buf);
 
        /* Check for bogus requests */
@@ -268,25 +303,35 @@ void context_loop(int sock)
         */
        if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
        else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
+       else if (!strncasecmp(buf, "POST ", 5)) strcpy(buf, &buf[5]);
        if (buf[1]==' ') buf[1]=0;
 
        /*
         * While we're at it, gracefully handle requests for the
-        * robots.txt file...
+        * robots.txt and favicon.ico files.
         */
        if (!strncasecmp(buf, "/robots.txt", 11)) {
                strcpy(req->line, "GET /static/robots.txt"
                                "?force_close_session=yes HTTP/1.0");
        }
+       else if (!strncasecmp(buf, "/favicon.ico", 12)) {
+               strcpy(req->line, "GET /static/favicon.ico");
+       }
 
-       /* Do the non-root-cookie check now. */
-       else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
+       /* These are the URL's which may be executed without a
+        * session cookie already set.  If it's not one of these,
+        * force the session to close because cookies are
+        * probably disabled on the client browser.
+        */
+       else if ( (strcmp(buf, "/"))
+               && (strncasecmp(buf, "/listsub", 8))
+               && (strncasecmp(buf, "/freebusy", 9))
+               && (strncasecmp(buf, "/do_logout", 10))
+               && (got_cookie == 0)) {
                strcpy(req->line, "GET /static/nocookies.html"
                                "?force_close_session=yes HTTP/1.0");
        }
 
-
-
        /*
         * See if there's an existing session open with the desired ID
         */
@@ -309,6 +354,8 @@ void context_loop(int sock)
                TheSession = (struct wcsession *)
                        malloc(sizeof(struct wcsession));
                memset(TheSession, 0, sizeof(struct wcsession));
+               TheSession->serv_sock = (-1);
+               TheSession->chat_sock = (-1);
                TheSession->wc_session = GenerateSessionID();
                pthread_mutex_init(&TheSession->SessionMutex, NULL);
 
@@ -318,14 +365,11 @@ void context_loop(int sock)
                pthread_mutex_unlock(&SessionListMutex);
        }
 
-
        /*
-        *
-        * FIX ... check session integrity here before continuing
-        *
+        * A future improvement might be to check the session integrity
+        * at this point before continuing.
         */
 
-
        /*
         * Bind to the session and perform the transaction
         */
@@ -333,7 +377,7 @@ void context_loop(int sock)
        pthread_setspecific(MyConKey, (void *)TheSession);
        TheSession->http_sock = sock;
        TheSession->lastreq = time(NULL);                       /* log */
-       session_loop(req);              /* perform the requested transaction */
+       session_loop(req);                              /* do transaction */
        pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
 
        /* Free the request buffer */
@@ -342,4 +386,9 @@ bail:       while (req != NULL) {
                free(req);
                req = hptr;
        }
+
+       /* Free up any session-local substitution variables which
+        * were set during this transaction
+        */
+       clear_local_substs();
 }