/*
- * context_loop.c
+ * $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.
*
- * $Id$
*/
#include <ctype.h>
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)) >
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);
}
}
} 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);
+ }
}
a = client_gets(sock, buf);
if (a<1) return(-1);
} else {
- strcpy(buf, hold);
+ safestrncpy(buf, hold, SIZ);
}
strcpy(hold, "");
int desired_session = 0;
int got_cookie = 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, "");
/*
* Find out what it is that the web browser is asking for
do {
if (req_gets(sock, buf, hold) < 0) return;
+ /*
+ * Browser-based sessions use cookies for session authentication
+ */
if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
cookie_to_stuff(&buf[15], &desired_session,
NULL, NULL, NULL);
got_cookie = 1;
}
+ /*
+ * GroupDAV-based sessions use HTTP authentication
+ */
+ 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, ':');
+ }
+
+ /*
+ * Read in the request
+ */
hptr = (struct httprequest *)
malloc(sizeof(struct httprequest));
if (req == 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 */
* set. If there isn't, the client browser has cookies turned off
* (or doesn't support them) and we have to barf & bail.
*/
- 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]);
+ remove_token(buf, 0, ' ');
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. */
+ /* 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))
+ && (strncasecmp(buf, "/groupdav", 9))
&& (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
+ * See if there's an existing session open with the desired ID or user/pass
*/
TheSession = NULL;
- if (desired_session != 0) {
+
+ if ( (TheSession == NULL) && (strlen(httpauth_user) > 0) ) {
+ pthread_mutex_lock(&SessionListMutex);
+ for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+ if ( (!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) {
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();
+ strcpy(TheSession->httpauth_user, httpauth_user);
+ strcpy(TheSession->httpauth_pass, httpauth_pass);
pthread_mutex_init(&TheSession->SessionMutex, NULL);
pthread_mutex_lock(&SessionListMutex);
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();
}