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)) >
/* 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;
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);
+ }
}
void housekeeping_loop(void)
{
while (1) {
- sleep(HOUSEKEEPING);
+ sleeeeeeeeeep(HOUSEKEEPING);
do_housekeeping();
}
}
* 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)
a = client_gets(sock, buf);
if (a<1) return(-1);
} else {
- strcpy(buf, hold);
+ safestrncpy(buf, hold, SIZ);
}
strcpy(hold, "");
return(0);
}
}
+
return(0);
}
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);
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 */
*/
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
*/
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);
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
*/
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 */
free(req);
req = hptr;
}
+
+ /* Free up any session-local substitution variables which
+ * were set during this transaction
+ */
+ clear_local_substs();
}