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) {
67 printf("Locking session %d...\n", session->session_id);
68 pthread_mutex_lock(&session->critter);
69 printf(" ...got lock\n");
75 static void unlock_session(struct wc_session *session) {
76 printf("Unlocking.\n");
77 pthread_mutex_unlock(&session->critter);
81 * Remove a session context from the list
83 void remove_session(struct wc_session *TheSession, int do_lock) {
84 struct wc_session *sptr;
86 printf("Removing session.\n");
87 if (do_lock) pthread_mutex_lock(&MasterCritter);
89 if (SessionList==TheSession) {
90 SessionList = SessionList->next;
93 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
94 if (sptr->next == TheSession) {
95 sptr->next = TheSession->next;
100 close(TheSession->inpipe[1]);
101 close(TheSession->outpipe[0]);
102 if (do_lock) unlock_session(TheSession);
105 pthread_mutex_unlock(&MasterCritter);
111 void do_housekeeping(void) {
112 struct wc_session *sptr;
114 pthread_mutex_lock(&MasterCritter);
116 /* Kill idle sessions */
117 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
118 if ((time(NULL) - (sptr->lastreq)) > (time_t)WEBCIT_TIMEOUT) {
119 kill(sptr->webcit_pid, 15);
123 /* Remove dead sessions */
124 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
125 if (kill(sptr->webcit_pid, 0)) {
126 remove_session(sptr, 0);
130 pthread_mutex_unlock(&MasterCritter);
135 * Wake up occasionally and clean house
137 void housekeeping_loop(void) {
148 int GenerateSessionID(void) {
153 void gets0(int fd, char buf[]) {
157 buf[strlen(buf)+1] = 0;
158 read(fd, &buf[strlen(buf)], 1);
159 } while (buf[strlen(buf)-1] >= 32);
160 buf[strlen(buf)-1] = 0;
164 * Collapse multiple cookies on one line
166 void req_gets(int sock, char *buf, char *hold) {
169 if (strlen(hold)==0) {
170 client_gets(sock, buf);
177 if (!strncasecmp(buf, "Cookie: ", 8)) {
178 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
179 sprintf(hold, "Cookie: %s", &buf[a+1]);
181 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
188 * lingering_close() a`la Apache. see
189 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
192 static int lingering_close(int fd) {
196 struct timeval tv, start;
198 gettimeofday(&start, NULL);
202 gettimeofday(&tv, NULL);
203 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
204 tv.tv_usec = start.tv_usec - tv.tv_usec;
205 if (tv.tv_usec < 0) {
207 tv.tv_usec += 1000000;
212 i = select(fd + 1, &set, NULL, NULL, &tv);
213 } while (i == -1 && errno == EINTR);
218 i = read(fd, buf, sizeof buf);
219 } while (i != 0 && (i != -1 || errno == EINTR));
225 * This loop gets called once for every HTTP connection made to WebCit.
227 void *context_loop(int sock) {
229 char buf[256], hold[256];
233 int desired_session = 0;
234 char str_session[256];
235 struct wc_session *sptr;
236 struct wc_session *TheSession;
238 int CloseSession = 0;
240 if ((req = malloc((long)sizeof(char[256][256]))) == NULL) {
241 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
242 fprintf(stderr, "%s", buf);
243 write(sock, buf, strlen(buf));
248 printf("Reading request from socket %d\n", sock);
251 * Find out what it is that the web browser is asking for
255 req_gets(sock, buf, hold);
256 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
257 desired_session = atoi(&buf[19]);
259 if (!strncasecmp(buf, "Content-length: ", 16)) {
260 ContentLength = atoi(&buf[16]);
262 strcpy(&req[num_lines++][0], buf);
263 } while(strlen(buf)>0);
266 * See if there's an existing session open with the desired ID
269 if (desired_session != 0) {
270 pthread_mutex_lock(&MasterCritter);
271 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
272 if (sptr->session_id == desired_session) {
274 lock_session(TheSession);
277 pthread_mutex_unlock(&MasterCritter);
281 * Before we trumpet to the universe that the session we're looking
282 * for actually exists, check first to make sure it's still there.
284 if (TheSession != NULL) {
285 if (kill(TheSession->webcit_pid, 0)) {
286 printf(" Session is *DEAD* !!\n");
287 remove_session(TheSession, 1);
293 * Create a new session if we have to
295 if (TheSession == NULL) {
296 printf("Creating a new session\n");
297 pthread_mutex_lock(&MasterCritter);
298 TheSession = (struct wc_session *)
299 malloc(sizeof(struct wc_session));
300 TheSession->session_id = GenerateSessionID();
301 pipe(TheSession->inpipe);
302 pipe(TheSession->outpipe);
303 pthread_mutex_init(&TheSession->critter, NULL);
304 lock_session(TheSession);
305 TheSession->next = SessionList;
306 SessionList = TheSession;
307 pthread_mutex_unlock(&MasterCritter);
308 sprintf(str_session, "%d", TheSession->session_id);
310 if (f > 0) TheSession->webcit_pid = f;
312 fflush(stdout); fflush(stdin);
315 /* Hook stdio to the ends of the pipe we're using */
316 dup2(TheSession->inpipe[0], 0);
317 dup2(TheSession->outpipe[1], 1);
319 /* Close the ends of the pipes that we're not using */
320 close(TheSession->inpipe[1]);
321 close(TheSession->outpipe[0]);
323 /* Close the HTTP socket in this pid; don't need it */
326 /* Run the actual WebCit session */
327 execlp("./webcit", "webcit", str_session, defaulthost,
330 /* Simple page to display if exec fails */
331 printf("HTTP/1.0 404 WebCit Failure\n\n");
332 printf("Server: %s\n", SERVER);
333 printf("X-WebCit-Session: close\n");
334 printf("Content-type: text/html\n");
335 printf("Content-length: 76\n");
337 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
338 printf("<BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">execlp() failed</BODY></HTML>\n");
342 /* Close the ends of the pipes that we're not using */
343 close(TheSession->inpipe[0]);
344 close(TheSession->outpipe[1]);
349 * Send the request to the appropriate session...
351 TheSession->lastreq = time(NULL);
352 printf(" Writing %d lines of command\n", num_lines);
353 printf("%s\n", &req[0][0]);
354 for (a=0; a<num_lines; ++a) {
355 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
356 write(TheSession->inpipe[1], "\n", 1);
358 printf(" Writing %d bytes of content\n", ContentLength);
359 while (ContentLength > 0) {
361 if (a > sizeof buf) a = sizeof buf;
362 if (!client_read(sock, buf, a)) goto end;
363 if (write(TheSession->inpipe[1], buf, a) != a) goto end;
368 * ...and get the response.
370 printf(" Reading response\n");
373 gets0(TheSession->outpipe[0], buf);
374 write(sock, buf, strlen(buf));
375 write(sock, "\n", 1);
376 if (!strncasecmp(buf, "Content-length: ", 16))
377 ContentLength = atoi(&buf[16]);
378 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
381 } while (strlen(buf) > 0);
383 printf(" Reading %d bytes of content\n", ContentLength);
384 while(ContentLength--) {
385 read(TheSession->outpipe[0], buf, 1);
390 * If the last response included a "close session" directive,
391 * remove the context now.
394 remove_session(TheSession, 1);
397 end: unlock_session(TheSession);
403 * Now our HTTP connection is done. It would be relatively easy
404 * to support HTTP/1.1 "persistent" connections by looping back to
405 * the top of this function. For now, we'll just close.
407 printf(" Closing socket %d ... ret=%d\n", sock,
408 lingering_close(sock));
411 * The thread handling this HTTP connection is now finished.