4 * This is the other half of the webserver. It handles the task of hooking
5 * up HTTP requests with the sessions they belong to, using HTTP cookies to
6 * keep track of things. If the HTTP request doesn't belong to any currently
7 * active session, a new session is started.
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #ifdef HAVE_SYS_TIME_H
31 #include <netinet/in.h>
39 #include "webserver.h"
41 /* Only one thread may manipulate SessionList at a time... */
42 pthread_mutex_t SessionListMutex;
44 struct wcsession *SessionList = NULL;
46 pthread_key_t MyConKey; /* TSD key for MySession() */
49 void free_attachments(struct wcsession *sess) {
50 struct wc_attachment *att;
52 while (sess->first_attachment != NULL) {
53 att = sess->first_attachment;
54 sess->first_attachment = sess->first_attachment->next;
61 void do_housekeeping(void)
63 struct wcsession *sptr, *ss, *session_to_kill;
65 static int num_threads = MIN_WORKER_THREADS;
68 session_to_kill = NULL;
69 pthread_mutex_lock(&SessionListMutex);
71 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
74 /* Kill idle sessions */
75 if ((time(NULL) - (sptr->lastreq)) >
76 (time_t) WEBCIT_TIMEOUT) {
80 /* Remove sessions flagged for kill */
83 lprintf(3, "Destroying session %d\n",
86 /* remove session from linked list */
87 if (sptr == SessionList) {
88 SessionList = SessionList->next;
90 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
91 if (ss->next == sptr) {
92 ss->next = ss->next->next;
96 session_to_kill = sptr;
100 BREAKOUT: pthread_mutex_unlock(&SessionListMutex);
102 if (session_to_kill != NULL) {
103 pthread_mutex_lock(&session_to_kill->SessionMutex);
104 close(session_to_kill->serv_sock);
105 close(session_to_kill->chat_sock);
106 if (session_to_kill->preferences != NULL) {
107 free(session_to_kill->preferences);
109 free_attachments(session_to_kill);
110 pthread_mutex_unlock(&session_to_kill->SessionMutex);
111 free(session_to_kill);
114 } while (session_to_kill != NULL);
117 * See if we need more worker threads
119 while ( (num_sessions > num_threads)
120 && (num_threads <= MAX_WORKER_THREADS) ) {
121 spawn_another_worker_thread();
123 lprintf(3, "There are %d sessions and %d threads active.\n",
124 num_sessions, num_threads);
130 * Wake up occasionally and clean house
132 void housekeeping_loop(void)
135 sleeeeeeeeeep(HOUSEKEEPING);
142 * Generate a unique WebCit session ID (which is not the same thing as the
143 * Citadel session ID).
145 * FIXME ... ensure that session number is truly unique
148 int GenerateSessionID(void)
150 static int seq = (-1);
153 seq = (int) time(NULL);
161 * Collapse multiple cookies on one line
163 int req_gets(int sock, char *buf, char *hold)
167 if (strlen(hold) == 0) {
169 a = client_gets(sock, buf);
172 safestrncpy(buf, hold, SIZ);
176 if (!strncasecmp(buf, "Cookie: ", 8)) {
177 for (a = 0; a < strlen(buf); ++a)
179 sprintf(hold, "Cookie: %s", &buf[a + 1]);
181 while (isspace(hold[8]))
182 strcpy(&hold[8], &hold[9]);
191 * lingering_close() a`la Apache. see
192 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
195 int lingering_close(int fd)
200 struct timeval tv, start;
202 gettimeofday(&start, NULL);
206 gettimeofday(&tv, NULL);
207 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
208 tv.tv_usec = start.tv_usec - tv.tv_usec;
209 if (tv.tv_usec < 0) {
211 tv.tv_usec += 1000000;
215 i = select(fd + 1, &set, NULL, NULL, &tv);
216 } while (i == -1 && errno == EINTR);
221 i = read(fd, buf, sizeof buf);
222 } while (i != 0 && (i != -1 || errno == EINTR));
230 * Check for bogus requests coming from (for example) brain-dead
231 * Windoze boxes that are infected with the latest worm-of-the-week.
232 * If we detect one of these, bail out without bothering our Citadel
235 int is_bogus(char *http_cmd) {
237 if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
238 if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
239 if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
241 return(0); /* probably ok */
247 * This loop gets called once for every HTTP connection made to WebCit. At
248 * this entry point we have an HTTP socket with a browser allegedly on the
249 * other end, but we have not yet bound to a WebCit session.
251 * The job of this function is to locate the correct session and bind to it,
252 * or create a session if necessary and bind to it, then run the WebCit
253 * transaction loop. Afterwards, we unbind from the session. When this
254 * function returns, the worker thread is then free to handle another
257 void context_loop(int sock)
259 struct httprequest *req = NULL;
260 struct httprequest *last = NULL;
261 struct httprequest *hptr;
262 char buf[SIZ], hold[SIZ];
263 int desired_session = 0;
265 struct wcsession *TheSession, *sptr;
266 int outside_frameset_allowed = 0;
269 * Find out what it is that the web browser is asking for
271 memset(hold, 0, sizeof(hold));
273 if (req_gets(sock, buf, hold) < 0) return;
275 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
276 cookie_to_stuff(&buf[15], &desired_session,
281 hptr = (struct httprequest *)
282 malloc(sizeof(struct httprequest));
290 safestrncpy(hptr->line, buf, sizeof hptr->line);
292 } while (strlen(buf) > 0);
294 safestrncpy(buf, req->line, sizeof buf);
295 lprintf(5, "HTTP: %s\n", buf);
297 /* Check for bogus requests */
298 if (is_bogus(buf)) goto bail;
301 * If requesting a non-root page, there should already be a cookie
302 * set. If there isn't, the client browser has cookies turned off
303 * (or doesn't support them) and we have to barf & bail.
305 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
306 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
307 else if (!strncasecmp(buf, "POST ", 5)) strcpy(buf, &buf[5]);
308 if (buf[1]==' ') buf[1]=0;
311 * While we're at it, gracefully handle requests for the
312 * robots.txt and favicon.ico files.
314 if (!strncasecmp(buf, "/robots.txt", 11)) {
315 strcpy(req->line, "GET /static/robots.txt"
316 "?force_close_session=yes HTTP/1.0");
318 else if (!strncasecmp(buf, "/favicon.ico", 12)) {
319 strcpy(req->line, "GET /static/favicon.ico");
322 /* These are the URL's which may be executed without a
323 * session cookie already set. If it's not one of these,
324 * force the session to close because cookies are
325 * probably disabled on the client browser.
327 else if ( (strcmp(buf, "/"))
328 && (strncasecmp(buf, "/listsub", 8))
329 && (strncasecmp(buf, "/freebusy", 9))
330 && (strncasecmp(buf, "/do_logout", 10))
331 && (got_cookie == 0)) {
332 strcpy(req->line, "GET /static/nocookies.html"
333 "?force_close_session=yes HTTP/1.0");
336 /* These are the URL's which may be executed outside of the
337 * main frameset. If it's not one of these, the page will
338 * need JavaScript added to force the frameset to reload.
340 if ( (!strcasecmp(buf, "/"))
341 || (!strncasecmp(buf, "/static/", 8))
342 || (!strncasecmp(buf, "/do_welcome", 11))
343 || (!strncasecmp(buf, "/do_logout", 10))
344 || (!strncasecmp(buf, "/login", 6))
345 || (!strncasecmp(buf, "/page_popup", 11))
346 || (!strncasecmp(buf, "/page_user", 10)) /* Sometimes this is wrong */
347 || (!strncasecmp(buf, "/display_page", 10)) /* Sometimes this is wrong */
348 || (!strncasecmp(buf, "/listsub", 8))
349 || (!strncasecmp(buf, "/freebusy", 9))
350 || (!strncasecmp(buf, "/termquit", 9)) ) {
351 outside_frameset_allowed = 1;
354 outside_frameset_allowed = 0;
358 * See if there's an existing session open with the desired ID
361 if (desired_session != 0) {
362 pthread_mutex_lock(&SessionListMutex);
363 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
364 if (sptr->wc_session == desired_session) {
368 pthread_mutex_unlock(&SessionListMutex);
372 * Create a new session if we have to
374 if (TheSession == NULL) {
375 lprintf(3, "Creating a new session\n");
376 TheSession = (struct wcsession *)
377 malloc(sizeof(struct wcsession));
378 memset(TheSession, 0, sizeof(struct wcsession));
379 TheSession->serv_sock = (-1);
380 TheSession->chat_sock = (-1);
381 TheSession->wc_session = GenerateSessionID();
382 pthread_mutex_init(&TheSession->SessionMutex, NULL);
384 pthread_mutex_lock(&SessionListMutex);
385 TheSession->next = SessionList;
386 SessionList = TheSession;
387 pthread_mutex_unlock(&SessionListMutex);
391 * A future improvement might be to check the session integrity
392 * at this point before continuing.
396 * Bind to the session and perform the transaction
398 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
399 pthread_setspecific(MyConKey, (void *)TheSession);
400 TheSession->http_sock = sock;
401 TheSession->lastreq = time(NULL); /* log */
402 TheSession->outside_frameset_allowed = outside_frameset_allowed;
403 session_loop(req); /* do transaction */
404 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
406 /* Free the request buffer */
407 bail: while (req != NULL) {
413 /* Free up any session-local substitution variables which
414 * were set during this transaction
416 clear_local_substs();