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
85 void remove_session(struct wc_session *TheSession, int do_lock)
87 struct wc_session *sptr;
89 printf("Removing session.\n");
91 pthread_mutex_lock(&MasterCritter);
93 if (SessionList == TheSession) {
94 SessionList = SessionList->next;
96 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
97 if (sptr->next == TheSession) {
98 sptr->next = TheSession->next;
103 close(TheSession->inpipe[1]);
104 close(TheSession->outpipe[0]);
106 unlock_session(TheSession);
109 pthread_mutex_unlock(&MasterCritter);
115 void do_housekeeping(void)
117 struct wc_session *sptr;
119 pthread_mutex_lock(&MasterCritter);
121 /* Kill idle sessions */
122 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
123 if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
124 kill(sptr->webcit_pid, 15);
128 /* Remove dead sessions */
129 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
130 if (kill(sptr->webcit_pid, 0)) {
131 remove_session(sptr, 0);
135 pthread_mutex_unlock(&MasterCritter);
140 * Wake up occasionally and clean house
142 void housekeeping_loop(void)
154 int GenerateSessionID(void)
160 void gets0(int fd, char buf[])
165 buf[strlen(buf) + 1] = 0;
166 read(fd, &buf[strlen(buf)], 1);
167 } while (buf[strlen(buf) - 1] >= 32);
168 buf[strlen(buf) - 1] = 0;
172 * Collapse multiple cookies on one line
174 void req_gets(int sock, char *buf, char *hold)
178 if (strlen(hold) == 0) {
179 client_gets(sock, buf);
185 if (!strncasecmp(buf, "Cookie: ", 8)) {
186 for (a = 0; a < strlen(buf); ++a)
188 sprintf(hold, "Cookie: %s", &buf[a + 1]);
190 while (isspace(hold[8]))
191 strcpy(&hold[8], &hold[9]);
198 * lingering_close() a`la Apache. see
199 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
202 static int lingering_close(int fd)
207 struct timeval tv, start;
209 gettimeofday(&start, NULL);
213 gettimeofday(&tv, NULL);
214 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
215 tv.tv_usec = start.tv_usec - tv.tv_usec;
216 if (tv.tv_usec < 0) {
218 tv.tv_usec += 1000000;
222 i = select(fd + 1, &set, NULL, NULL, &tv);
223 } while (i == -1 && errno == EINTR);
228 i = read(fd, buf, sizeof buf);
229 } while (i != 0 && (i != -1 || errno == EINTR));
235 * This loop gets called once for every HTTP connection made to WebCit.
237 void *context_loop(int sock)
240 char buf[256], hold[256];
241 char browser_host[256];
246 int desired_session = 0;
248 char str_session[256];
249 struct wc_session *sptr;
250 struct wc_session *TheSession;
252 int CloseSession = 0;
254 if ((req = malloc((long) sizeof(char[256][256]))) == NULL) {
255 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
256 fprintf(stderr, "%s", buf);
257 write(sock, buf, strlen(buf));
261 bzero(req, sizeof(char[256][256])); /* clear it out */
262 strcpy(browser, "unknown");
264 printf("Reading request from socket %d\n", sock);
267 * Find out what it is that the web browser is asking for
271 req_gets(sock, buf, hold);
272 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
273 cookie_to_stuff(&buf[15], &desired_session,
277 else if (!strncasecmp(buf, "Content-length: ", 16)) {
278 ContentLength = atoi(&buf[16]);
280 else if (!strncasecmp(buf, "User-agent: ", 12)) {
281 strcpy(browser, &buf[12]);
283 strcpy(&req[num_lines++][0], buf);
284 } while (strlen(buf) > 0);
288 * If requesting a non-root page, there should already be a cookie
289 * set. If there isn't, the client browser has cookies turned off
290 * (or doesn't support them) and we have to barf & bail.
292 strcpy(buf, &req[0][0]);
293 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
294 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
295 if (buf[1]==' ') buf[1]=0;
296 if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
297 strcpy(&req[0][0], "GET /static/nocookies.html HTTP/1.0");
302 * See if there's an existing session open with the desired ID
305 if (desired_session != 0) {
306 pthread_mutex_lock(&MasterCritter);
307 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
308 if (sptr->session_id == desired_session) {
310 lock_session(TheSession);
313 pthread_mutex_unlock(&MasterCritter);
316 * Before we trumpet to the universe that the session we're looking
317 * for actually exists, check first to make sure it's still there.
319 if (TheSession != NULL) {
320 if (kill(TheSession->webcit_pid, 0)) {
321 printf(" Session is *DEAD* !!\n");
322 remove_session(TheSession, 1);
327 * Create a new session if we have to
329 if (TheSession == NULL) {
330 printf("Creating a new session\n");
331 locate_host(browser_host, sock);
332 pthread_mutex_lock(&MasterCritter);
333 TheSession = (struct wc_session *)
334 malloc(sizeof(struct wc_session));
335 TheSession->session_id = GenerateSessionID();
336 pipe(TheSession->inpipe);
337 pipe(TheSession->outpipe);
338 pthread_mutex_init(&TheSession->critter, NULL);
339 lock_session(TheSession);
340 TheSession->next = SessionList;
341 SessionList = TheSession;
342 pthread_mutex_unlock(&MasterCritter);
343 sprintf(str_session, "%d", TheSession->session_id);
346 TheSession->webcit_pid = f;
352 /* Hook stdio to the ends of the pipe we're using */
353 dup2(TheSession->inpipe[0], 0);
354 dup2(TheSession->outpipe[1], 1);
356 /* Close the ends of the pipes that we're not using */
357 close(TheSession->inpipe[1]);
358 close(TheSession->outpipe[0]);
360 /* Close the HTTP socket in this pid; don't need it */
363 /* Run the actual WebCit session */
364 execlp("./webcit", "webcit", str_session, defaulthost,
365 defaultport, browser_host, browser, NULL);
367 /* Simple page to display if exec fails */
368 printf("HTTP/1.0 404 WebCit Failure\n\n");
369 printf("Server: %s\n", SERVER);
370 printf("X-WebCit-Session: close\n");
371 printf("Content-type: text/html\n");
372 printf("Content-length: 76\n");
374 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n");
375 printf("execlp() failed: %s</BODY></HTML>\n", strerror(errno));
378 /* Close the ends of the pipes that we're not using */
379 close(TheSession->inpipe[0]);
380 close(TheSession->outpipe[1]);
384 * Send the request to the appropriate session...
386 TheSession->lastreq = time(NULL);
387 printf(" Writing %d lines of command\n", num_lines);
388 printf("%s\n", &req[0][0]);
389 for (a = 0; a < num_lines; ++a) {
390 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
391 write(TheSession->inpipe[1], "\n", 1);
393 printf(" Writing %d bytes of content\n", ContentLength);
394 while (ContentLength > 0) {
398 if (!client_read(sock, buf, a))
400 if (write(TheSession->inpipe[1], buf, a) != a)
406 * ...and get the response.
408 printf(" Reading response\n");
411 gets0(TheSession->outpipe[0], buf);
412 write(sock, buf, strlen(buf));
413 write(sock, "\n", 1);
414 if (!strncasecmp(buf, "Content-length: ", 16))
415 ContentLength = atoi(&buf[16]);
416 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
419 } while (strlen(buf) > 0);
421 printf(" Reading %d bytes of content\n", ContentLength);
423 while (ContentLength--) {
424 read(TheSession->outpipe[0], buf, 1);
429 * If the last response included a "close session" directive,
430 * remove the context now.
433 remove_session(TheSession, 1);
435 end: unlock_session(TheSession);
441 * Now our HTTP connection is done. It would be relatively easy
442 * to support HTTP/1.1 "persistent" connections by looping back to
443 * the top of this function. For now, we'll just close.
445 printf(" Closing socket %d ... ret=%d\n", sock,
446 lingering_close(sock));
449 * The thread handling this HTTP connection is now finished.
450 * Instead of calling pthread_exit(), just return. It does the same
451 * thing, and supresses a compiler warning.