4 * This is the other half of the webserver. It handles the task of hooking
5 * up HTTP requests with the session 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 spawned.
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"
42 * We keep one of these around for each active session
45 struct wc_session *next; /* Next session in list */
46 int session_id; /* Session ID */
47 pid_t webcit_pid; /* PID of the webcit process */
48 int inpipe[2]; /* Data from webserver to session */
49 int outpipe[2]; /* Data from session to webserver */
50 pthread_mutex_t critter; /* Critical section uses pipes */
51 time_t lastreq; /* Timestamp of most recent http */
54 struct wc_session *SessionList = NULL;
55 extern const char *defaulthost;
56 extern const char *defaultport;
58 /* Only one thread may manipulate SessionList at a time... */
59 pthread_mutex_t MasterCritter;
63 * Grab a lock on the session, so other threads don't try to access
64 * the pipes at the same time.
66 static void lock_session(struct wc_session *session)
68 printf("Locking session %d...\n", session->session_id);
69 pthread_mutex_lock(&session->critter);
70 printf(" ...got lock\n");
76 static void unlock_session(struct wc_session *session)
78 printf("Unlocking.\n");
79 pthread_mutex_unlock(&session->critter);
83 * Remove a session context from the list.
84 * Set do_lock to 1, to lock the list while manipulating it. The ONLY
85 * situation in which this should _not_ be done is when it's already locked
88 void remove_session(struct wc_session *TheSession, int do_lock)
90 struct wc_session *sptr;
92 printf("Removing session.\n");
94 /* Lock the list while manipulating it */
96 pthread_mutex_lock(&MasterCritter);
98 if (SessionList == TheSession) {
99 SessionList = SessionList->next;
101 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
102 if (sptr->next == TheSession) {
103 sptr->next = TheSession->next;
109 pthread_mutex_unlock(&MasterCritter);
111 /* Now finish destroying the session */
112 close(TheSession->inpipe[1]);
113 close(TheSession->outpipe[0]);
115 unlock_session(TheSession);
123 void do_housekeeping(void)
125 struct wc_session *sptr;
127 pthread_mutex_lock(&MasterCritter);
129 /* Kill idle sessions */
130 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
131 if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
132 kill(sptr->webcit_pid, 15);
136 /* Remove dead sessions */
137 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
138 if (kill(sptr->webcit_pid, 0)) {
139 remove_session(sptr, 0);
143 pthread_mutex_unlock(&MasterCritter);
148 * Wake up occasionally and clean house
150 void housekeeping_loop(void)
162 int GenerateSessionID(void)
168 void gets0(int fd, char buf[])
173 buf[strlen(buf) + 1] = 0;
174 read(fd, &buf[strlen(buf)], 1);
175 } while (buf[strlen(buf) - 1] >= 32);
176 buf[strlen(buf) - 1] = 0;
180 * Collapse multiple cookies on one line
182 void req_gets(int sock, char *buf, char *hold)
186 if (strlen(hold) == 0) {
187 client_gets(sock, buf);
193 if (!strncasecmp(buf, "Cookie: ", 8)) {
194 for (a = 0; a < strlen(buf); ++a)
196 sprintf(hold, "Cookie: %s", &buf[a + 1]);
198 while (isspace(hold[8]))
199 strcpy(&hold[8], &hold[9]);
206 * lingering_close() a`la Apache. see
207 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
210 static int lingering_close(int fd)
215 struct timeval tv, start;
217 gettimeofday(&start, NULL);
221 gettimeofday(&tv, NULL);
222 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
223 tv.tv_usec = start.tv_usec - tv.tv_usec;
224 if (tv.tv_usec < 0) {
226 tv.tv_usec += 1000000;
230 i = select(fd + 1, &set, NULL, NULL, &tv);
231 } while (i == -1 && errno == EINTR);
236 i = read(fd, buf, sizeof buf);
237 } while (i != 0 && (i != -1 || errno == EINTR));
243 * This loop gets called once for every HTTP connection made to WebCit.
245 void *context_loop(int sock)
248 char buf[256], hold[256];
249 char browser_host[256];
254 int desired_session = 0;
256 char str_session[256];
257 struct wc_session *sptr;
258 struct wc_session *TheSession;
260 int CloseSession = 0;
262 if ((req = malloc((long) sizeof(char[256][256]))) == NULL) {
263 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
264 fprintf(stderr, "%s", buf);
265 write(sock, buf, strlen(buf));
269 bzero(req, sizeof(char[256][256])); /* clear it out */
270 strcpy(browser, "unknown");
272 printf("Reading request from socket %d\n", sock);
275 * Find out what it is that the web browser is asking for
279 req_gets(sock, buf, hold);
280 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
281 cookie_to_stuff(&buf[15], &desired_session,
285 else if (!strncasecmp(buf, "Content-length: ", 16)) {
286 ContentLength = atoi(&buf[16]);
288 else if (!strncasecmp(buf, "User-agent: ", 12)) {
289 strcpy(browser, &buf[12]);
291 strcpy(&req[num_lines++][0], buf);
292 } while (strlen(buf) > 0);
296 * If requesting a non-root page, there should already be a cookie
297 * set. If there isn't, the client browser has cookies turned off
298 * (or doesn't support them) and we have to barf & bail.
300 strcpy(buf, &req[0][0]);
301 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
302 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
303 if (buf[1]==' ') buf[1]=0;
306 * While we're at it, gracefully handle requests for the
309 if (!strncasecmp(buf, "/robots.txt", 11)) {
310 strcpy(&req[0][0], "GET /static/robots.txt HTTP/1.0");
313 /* Do the non-root-cookie check now. */
314 else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
315 strcpy(&req[0][0], "GET /static/nocookies.html HTTP/1.0");
321 * See if there's an existing session open with the desired ID
324 if (desired_session != 0) {
325 pthread_mutex_lock(&MasterCritter);
326 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
327 if (sptr->session_id == desired_session) {
331 pthread_mutex_unlock(&MasterCritter);
335 * Before we trumpet to the universe that the session we're looking
336 * for actually exists, check first to make sure it's still there.
338 if (TheSession != NULL) {
339 if (kill(TheSession->webcit_pid, 0)) {
340 printf(" Session is *DEAD* !!\n");
341 remove_session(TheSession, 1);
347 * Create a new session if we have to
349 if (TheSession == NULL) {
350 printf("Creating a new session\n");
351 locate_host(browser_host, sock);
352 TheSession = (struct wc_session *)
353 malloc(sizeof(struct wc_session));
354 TheSession->session_id = GenerateSessionID();
355 pipe(TheSession->inpipe);
356 pipe(TheSession->outpipe);
357 pthread_mutex_init(&TheSession->critter, NULL);
359 pthread_mutex_lock(&MasterCritter);
360 TheSession->next = SessionList;
361 SessionList = TheSession;
362 pthread_mutex_unlock(&MasterCritter);
364 sprintf(str_session, "%d", TheSession->session_id);
367 TheSession->webcit_pid = f;
373 /* Hook stdio to the ends of the pipe we're using */
374 dup2(TheSession->inpipe[0], 0);
375 dup2(TheSession->outpipe[1], 1);
377 /* Close the ends of the pipes that we're not using */
378 close(TheSession->inpipe[1]);
379 close(TheSession->outpipe[0]);
381 /* Close the HTTP socket in this pid; don't need it */
384 /* Run the actual WebCit session */
385 execlp("./webcit", "webcit", str_session, defaulthost,
386 defaultport, browser_host, browser, NULL);
388 /* Simple page to display if exec fails */
389 printf("HTTP/1.0 404 WebCit Failure\n\n");
390 printf("Server: %s\n", SERVER);
391 printf("X-WebCit-Session: close\n");
392 printf("Content-type: text/html\n");
393 printf("Content-length: 76\n");
395 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n");
396 printf("execlp() failed: %s</BODY></HTML>\n", strerror(errno));
399 /* Close the ends of the pipes that we're not using */
400 close(TheSession->inpipe[0]);
401 close(TheSession->outpipe[1]);
406 * Send the request to the appropriate session...
408 lock_session(TheSession);
409 TheSession->lastreq = time(NULL);
410 printf(" Writing %d lines of command\n", num_lines);
411 printf("%s\n", &req[0][0]);
412 for (a = 0; a < num_lines; ++a) {
413 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
414 write(TheSession->inpipe[1], "\n", 1);
416 printf(" Writing %d bytes of content\n", ContentLength);
417 while (ContentLength > 0) {
421 if (!client_read(sock, buf, a))
423 if (write(TheSession->inpipe[1], buf, a) != a)
429 * ...and get the response.
431 printf(" Reading response\n");
434 gets0(TheSession->outpipe[0], buf);
435 write(sock, buf, strlen(buf));
436 write(sock, "\n", 1);
437 if (!strncasecmp(buf, "Content-length: ", 16))
438 ContentLength = atoi(&buf[16]);
439 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
442 } while (strlen(buf) > 0);
444 printf(" Reading %d bytes of content\n", ContentLength);
446 while (ContentLength--) {
447 read(TheSession->outpipe[0], buf, 1);
452 * If the last response included a "close session" directive,
453 * remove the context now.
456 remove_session(TheSession, 1);
458 end: unlock_session(TheSession);
464 * Now our HTTP connection is done. It would be relatively easy
465 * to support HTTP/1.1 "persistent" connections by looping back to
466 * the top of this function. For now, we'll just close.
468 printf(" Closing socket %d ... ret=%d\n", sock,
469 lingering_close(sock));
472 * The thread handling this HTTP connection is now finished.
473 * Instead of calling pthread_exit(), just return. It does the same
474 * thing, and supresses a compiler warning.