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 if (session_to_kill->preferences != NULL) {
106 free(session_to_kill->preferences);
108 free_attachments(session_to_kill);
109 pthread_mutex_unlock(&session_to_kill->SessionMutex);
110 free(session_to_kill);
113 } while (session_to_kill != NULL);
116 * See if we need more worker threads
118 while ( (num_sessions > num_threads)
119 && (num_threads <= MAX_WORKER_THREADS) ) {
120 spawn_another_worker_thread();
122 lprintf(3, "There are %d sessions and %d threads active.\n",
123 num_sessions, num_threads);
129 * Wake up occasionally and clean house
131 void housekeeping_loop(void)
134 sleeeeeeeeeep(HOUSEKEEPING);
141 * Generate a unique WebCit session ID (which is not the same thing as the
142 * Citadel session ID).
144 * FIXME ... ensure that session number is truly unique
147 int GenerateSessionID(void)
149 static int seq = (-1);
152 seq = (int) time(NULL);
160 * Collapse multiple cookies on one line
162 int req_gets(int sock, char *buf, char *hold)
166 if (strlen(hold) == 0) {
168 a = client_gets(sock, buf);
175 if (!strncasecmp(buf, "Cookie: ", 8)) {
176 for (a = 0; a < strlen(buf); ++a)
178 sprintf(hold, "Cookie: %s", &buf[a + 1]);
180 while (isspace(hold[8]))
181 strcpy(&hold[8], &hold[9]);
190 * lingering_close() a`la Apache. see
191 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
194 int lingering_close(int fd)
199 struct timeval tv, start;
201 gettimeofday(&start, NULL);
205 gettimeofday(&tv, NULL);
206 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
207 tv.tv_usec = start.tv_usec - tv.tv_usec;
208 if (tv.tv_usec < 0) {
210 tv.tv_usec += 1000000;
214 i = select(fd + 1, &set, NULL, NULL, &tv);
215 } while (i == -1 && errno == EINTR);
220 i = read(fd, buf, sizeof buf);
221 } while (i != 0 && (i != -1 || errno == EINTR));
229 * Check for bogus requests coming from (for example) brain-dead
230 * Windoze boxes that are infected with the latest worm-of-the-week.
231 * If we detect one of these, bail out without bothering our Citadel
234 int is_bogus(char *http_cmd) {
236 if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
237 if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
238 if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
240 return(0); /* probably ok */
246 * This loop gets called once for every HTTP connection made to WebCit. At
247 * this entry point we have an HTTP socket with a browser allegedly on the
248 * other end, but we have not yet bound to a WebCit session.
250 * The job of this function is to locate the correct session and bind to it,
251 * or create a session if necessary and bind to it, then run the WebCit
252 * transaction loop. Afterwards, we unbind from the session. When this
253 * function returns, the worker thread is then free to handle another
256 void context_loop(int sock)
258 struct httprequest *req = NULL;
259 struct httprequest *last = NULL;
260 struct httprequest *hptr;
261 char buf[SIZ], hold[SIZ];
262 int desired_session = 0;
264 struct wcsession *TheSession, *sptr;
265 int outside_frameset_allowed = 0;
268 * Find out what it is that the web browser is asking for
270 memset(hold, 0, sizeof(hold));
272 if (req_gets(sock, buf, hold) < 0) return;
274 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
275 cookie_to_stuff(&buf[15], &desired_session,
280 hptr = (struct httprequest *)
281 malloc(sizeof(struct httprequest));
289 strcpy(hptr->line, buf);
291 } while (strlen(buf) > 0);
293 strcpy(buf, req->line);
294 lprintf(5, "HTTP: %s\n", buf);
296 /* Check for bogus requests */
297 if (is_bogus(buf)) goto bail;
300 * If requesting a non-root page, there should already be a cookie
301 * set. If there isn't, the client browser has cookies turned off
302 * (or doesn't support them) and we have to barf & bail.
304 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
305 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
306 else if (!strncasecmp(buf, "POST ", 5)) strcpy(buf, &buf[5]);
307 if (buf[1]==' ') buf[1]=0;
310 * While we're at it, gracefully handle requests for the
313 if (!strncasecmp(buf, "/robots.txt", 11)) {
314 strcpy(req->line, "GET /static/robots.txt"
315 "?force_close_session=yes HTTP/1.0");
318 /* These are the URL's which may be executed without a
319 * session cookie already set. If it's not one of these,
320 * force the session to close because cookies are
321 * probably disabled on the client browser.
323 else if ( (strcmp(buf, "/"))
324 && (strncasecmp(buf, "/listsub", 8))
325 && (strncasecmp(buf, "/freebusy", 9))
326 && (got_cookie == 0)) {
327 strcpy(req->line, "GET /static/nocookies.html"
328 "?force_close_session=yes HTTP/1.0");
331 /* These are the URL's which may be executed outside of the
332 * main frameset. If it's not one of these, the page will
333 * need JavaScript added to force the frameset to reload.
335 if ( (!strcasecmp(buf, "/"))
336 || (!strcasecmp(buf, "/static/mainframeset.html"))
337 || (!strcasecmp(buf, "/static/robots.txt"))
338 || (!strncasecmp(buf, "/listsub", 8))
339 || (!strncasecmp(buf, "/freebusy", 9))
340 || (!strncasecmp(buf, "/termquit", 9)) ) {
341 outside_frameset_allowed = 1;
344 outside_frameset_allowed = 0;
348 * See if there's an existing session open with the desired ID
351 if (desired_session != 0) {
352 pthread_mutex_lock(&SessionListMutex);
353 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
354 if (sptr->wc_session == desired_session) {
358 pthread_mutex_unlock(&SessionListMutex);
362 * Create a new session if we have to
364 if (TheSession == NULL) {
365 lprintf(3, "Creating a new session\n");
366 TheSession = (struct wcsession *)
367 malloc(sizeof(struct wcsession));
368 memset(TheSession, 0, sizeof(struct wcsession));
369 TheSession->wc_session = GenerateSessionID();
370 pthread_mutex_init(&TheSession->SessionMutex, NULL);
372 pthread_mutex_lock(&SessionListMutex);
373 TheSession->next = SessionList;
374 SessionList = TheSession;
375 pthread_mutex_unlock(&SessionListMutex);
379 * A future improvement might be to check the session integrity
380 * at this point before continuing.
384 * Bind to the session and perform the transaction
386 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
387 pthread_setspecific(MyConKey, (void *)TheSession);
388 TheSession->http_sock = sock;
389 TheSession->lastreq = time(NULL); /* log */
390 TheSession->outside_frameset_allowed = outside_frameset_allowed;
391 session_loop(req); /* do transaction */
392 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
394 /* Free the request buffer */
395 bail: while (req != NULL) {