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;
247 char str_session[256];
248 struct wc_session *sptr;
249 struct wc_session *TheSession;
251 int CloseSession = 0;
253 if ((req = malloc((long) sizeof(char[256][256]))) == NULL) {
254 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
255 fprintf(stderr, "%s", buf);
256 write(sock, buf, strlen(buf));
260 bzero(req, sizeof(char[256][256])); /* clear it out */
261 strcpy(browser, "unknown");
263 printf("Reading request from socket %d\n", sock);
266 * Find out what it is that the web browser is asking for
270 req_gets(sock, buf, hold);
271 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
272 cookie_to_stuff(&buf[15], &desired_session, NULL, NULL, NULL, NULL);
274 else if (!strncasecmp(buf, "Content-length: ", 16)) {
275 ContentLength = atoi(&buf[16]);
277 else if (!strncasecmp(buf, "User-agent: ", 12)) {
278 strcpy(browser, &buf[12]);
280 strcpy(&req[num_lines++][0], buf);
281 } while (strlen(buf) > 0);
284 * See if there's an existing session open with the desired ID
287 if (desired_session != 0) {
288 pthread_mutex_lock(&MasterCritter);
289 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
290 if (sptr->session_id == desired_session) {
292 lock_session(TheSession);
295 pthread_mutex_unlock(&MasterCritter);
298 * Before we trumpet to the universe that the session we're looking
299 * for actually exists, check first to make sure it's still there.
301 if (TheSession != NULL) {
302 if (kill(TheSession->webcit_pid, 0)) {
303 printf(" Session is *DEAD* !!\n");
304 remove_session(TheSession, 1);
309 * Create a new session if we have to
311 if (TheSession == NULL) {
312 printf("Creating a new session\n");
313 locate_host(browser_host, sock);
314 pthread_mutex_lock(&MasterCritter);
315 TheSession = (struct wc_session *)
316 malloc(sizeof(struct wc_session));
317 TheSession->session_id = GenerateSessionID();
318 pipe(TheSession->inpipe);
319 pipe(TheSession->outpipe);
320 pthread_mutex_init(&TheSession->critter, NULL);
321 lock_session(TheSession);
322 TheSession->next = SessionList;
323 SessionList = TheSession;
324 pthread_mutex_unlock(&MasterCritter);
325 sprintf(str_session, "%d", TheSession->session_id);
328 TheSession->webcit_pid = f;
334 /* Hook stdio to the ends of the pipe we're using */
335 dup2(TheSession->inpipe[0], 0);
336 dup2(TheSession->outpipe[1], 1);
338 /* Close the ends of the pipes that we're not using */
339 close(TheSession->inpipe[1]);
340 close(TheSession->outpipe[0]);
342 /* Close the HTTP socket in this pid; don't need it */
345 /* Run the actual WebCit session */
346 execlp("./webcit", "webcit", str_session, defaulthost,
347 defaultport, browser_host, browser, NULL);
349 /* Simple page to display if exec fails */
350 printf("HTTP/1.0 404 WebCit Failure\n\n");
351 printf("Server: %s\n", SERVER);
352 printf("X-WebCit-Session: close\n");
353 printf("Content-type: text/html\n");
354 printf("Content-length: 76\n");
356 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n");
357 printf("execlp() failed: %s</BODY></HTML>\n", strerror(errno));
360 /* Close the ends of the pipes that we're not using */
361 close(TheSession->inpipe[0]);
362 close(TheSession->outpipe[1]);
366 * Send the request to the appropriate session...
368 TheSession->lastreq = time(NULL);
369 printf(" Writing %d lines of command\n", num_lines);
370 printf("%s\n", &req[0][0]);
371 for (a = 0; a < num_lines; ++a) {
372 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
373 write(TheSession->inpipe[1], "\n", 1);
375 printf(" Writing %d bytes of content\n", ContentLength);
376 while (ContentLength > 0) {
380 if (!client_read(sock, buf, a))
382 if (write(TheSession->inpipe[1], buf, a) != a)
388 * ...and get the response.
390 printf(" Reading response\n");
393 gets0(TheSession->outpipe[0], buf);
394 write(sock, buf, strlen(buf));
395 write(sock, "\n", 1);
396 if (!strncasecmp(buf, "Content-length: ", 16))
397 ContentLength = atoi(&buf[16]);
398 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
401 } while (strlen(buf) > 0);
403 printf(" Reading %d bytes of content\n", ContentLength);
405 while (ContentLength--) {
406 read(TheSession->outpipe[0], buf, 1);
411 * If the last response included a "close session" directive,
412 * remove the context now.
415 remove_session(TheSession, 1);
417 end: unlock_session(TheSession);
423 * Now our HTTP connection is done. It would be relatively easy
424 * to support HTTP/1.1 "persistent" connections by looping back to
425 * the top of this function. For now, we'll just close.
427 printf(" Closing socket %d ... ret=%d\n", sock,
428 lingering_close(sock));
431 * The thread handling this HTTP connection is now finished.
432 * Instead of calling pthread_exit(), just return. It does the same
433 * thing, and supresses a compiler warning.