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 */
53 struct wc_session *SessionList = NULL;
54 extern const char *defaulthost;
55 extern const char *defaultport;
57 /* Only one thread may manipulate SessionList at a time... */
58 pthread_mutex_t MasterCritter;
60 int GenerateSessionID(void) {
65 void gets0(int fd, char buf[]) {
69 buf[strlen(buf)+1] = 0;
70 read(fd, &buf[strlen(buf)], 1);
71 } while (buf[strlen(buf)-1] >= 32);
72 buf[strlen(buf)-1] = 0;
76 * Collapse multiple cookies on one line
78 void req_gets(int sock, char *buf, char *hold) {
81 if (strlen(hold)==0) {
82 client_gets(sock, buf);
89 if (!strncasecmp(buf, "Cookie: ", 8)) {
90 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
91 sprintf(hold, "Cookie: %s", &buf[a+1]);
93 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
100 * Grab a lock on the session, so other threads don't try to access
101 * the pipes at the same time.
103 static void lock_session(struct wc_session *session) {
104 printf("Locking session %d...\n", session->session_id);
105 pthread_mutex_lock(&session->critter);
106 printf(" ...got lock\n");
110 * Let go of the lock.
112 static void unlock_session(struct wc_session *session) {
113 printf("Unlocking.\n");
114 pthread_mutex_unlock(&session->critter);
118 * lingering_close() a`la Apache. see
119 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
122 static int lingering_close(int fd) {
126 struct timeval tv, start;
128 gettimeofday(&start, NULL);
132 gettimeofday(&tv, NULL);
133 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
134 tv.tv_usec = start.tv_usec - tv.tv_usec;
135 if (tv.tv_usec < 0) {
137 tv.tv_usec += 1000000;
142 i = select(fd + 1, &set, NULL, NULL, &tv);
143 } while (i == -1 && errno == EINTR);
148 i = read(fd, buf, sizeof buf);
149 } while (i != 0 && (i != -1 || errno == EINTR));
155 * Remove a session context from the list
157 void remove_session(struct wc_session *TheSession) {
158 struct wc_session *sptr;
160 printf("Removing session.\n");
161 pthread_mutex_lock(&MasterCritter);
163 if (SessionList==TheSession) {
164 SessionList = SessionList->next;
167 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
168 if (sptr->next == TheSession) {
169 sptr->next = TheSession->next;
174 close(TheSession->inpipe[1]);
175 close(TheSession->outpipe[0]);
176 unlock_session(TheSession);
179 pthread_mutex_unlock(&MasterCritter);
183 * This loop gets called once for every HTTP connection made to WebCit.
185 void *context_loop(int sock) {
187 char buf[256], hold[256];
191 int desired_session = 0;
192 char str_session[256];
193 struct wc_session *sptr;
194 struct wc_session *TheSession;
196 int CloseSession = 0;
198 if ((req = malloc((long)sizeof(char[256][256]))) == NULL) {
199 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
200 fprintf(stderr, "%s", buf);
201 write(sock, buf, strlen(buf));
206 printf("Reading request from socket %d\n", sock);
209 * Find out what it is that the web browser is asking for
213 req_gets(sock, buf, hold);
214 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
215 desired_session = atoi(&buf[19]);
217 if (!strncasecmp(buf, "Content-length: ", 16)) {
218 ContentLength = atoi(&buf[16]);
220 strcpy(&req[num_lines++][0], buf);
221 } while(strlen(buf)>0);
224 * See if there's an existing session open with the desired ID
227 if (desired_session != 0) {
228 pthread_mutex_lock(&MasterCritter);
229 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
230 if (sptr->session_id == desired_session) {
232 lock_session(TheSession);
235 pthread_mutex_unlock(&MasterCritter);
239 * Before we trumpet to the universe that the session we're looking
240 * for actually exists, check first to make sure it's still there.
242 if (TheSession != NULL) {
243 if (kill(TheSession->webcit_pid, 0)) {
244 printf(" Session is *DEAD* !!\n");
245 remove_session(TheSession);
251 * Create a new session if we have to
253 if (TheSession == NULL) {
254 printf("Creating a new session\n");
255 pthread_mutex_lock(&MasterCritter);
256 TheSession = (struct wc_session *)
257 malloc(sizeof(struct wc_session));
258 TheSession->session_id = GenerateSessionID();
259 pipe(TheSession->inpipe);
260 pipe(TheSession->outpipe);
261 pthread_mutex_init(&TheSession->critter, NULL);
262 lock_session(TheSession);
263 TheSession->next = SessionList;
264 SessionList = TheSession;
265 pthread_mutex_unlock(&MasterCritter);
266 sprintf(str_session, "%d", TheSession->session_id);
268 if (f > 0) TheSession->webcit_pid = f;
270 fflush(stdout); fflush(stdin);
273 /* Hook stdio to the ends of the pipe we're using */
274 dup2(TheSession->inpipe[0], 0);
275 dup2(TheSession->outpipe[1], 1);
277 /* Close the ends of the pipes that we're not using */
278 close(TheSession->inpipe[1]);
279 close(TheSession->outpipe[0]);
281 /* Close the HTTP socket in this pid; don't need it */
284 /* Run the actual WebCit session */
285 execlp("./webcit", "webcit", str_session, defaulthost,
288 /* Simple page to display if exec fails */
289 printf("HTTP/1.0 404 WebCit Failure\n\n");
290 printf("Server: %s\n", SERVER);
291 printf("X-WebCit-Session: close\n");
292 printf("Content-type: text/html\n");
293 printf("Content-length: 76\n");
295 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
296 printf("<BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">execlp() failed</BODY></HTML>\n");
300 /* Close the ends of the pipes that we're not using */
301 close(TheSession->inpipe[0]);
302 close(TheSession->outpipe[1]);
307 * Send the request to the appropriate session...
309 printf(" Writing %d lines of command\n", num_lines);
310 printf("%s\n", &req[0][0]);
311 for (a=0; a<num_lines; ++a) {
312 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
313 write(TheSession->inpipe[1], "\n", 1);
315 printf(" Writing %d bytes of content\n", ContentLength);
316 while (ContentLength > 0) {
318 if (a > sizeof buf) a = sizeof buf;
319 if (!client_read(sock, buf, a)) goto end;
320 if (write(TheSession->inpipe[1], buf, a) != a) goto end;
325 * ...and get the response.
327 printf(" Reading response\n");
330 gets0(TheSession->outpipe[0], buf);
331 write(sock, buf, strlen(buf));
332 write(sock, "\n", 1);
333 if (!strncasecmp(buf, "Content-length: ", 16))
334 ContentLength = atoi(&buf[16]);
335 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
338 } while (strlen(buf) > 0);
340 printf(" Reading %d bytes of content\n", ContentLength);
341 while(ContentLength--) {
342 read(TheSession->outpipe[0], buf, 1);
347 * If the last response included a "close session" directive,
348 * remove the context now.
351 remove_session(TheSession);
354 end: unlock_session(TheSession);
360 * Now our HTTP connection is done. It would be relatively easy
361 * to support HTTP/1.1 "persistent" connections by looping back to
362 * the top of this function. For now, we'll just close.
364 printf(" Remember, the pid is %d\n", TheSession->webcit_pid);
365 printf(" Closing socket %d ... ret=%d\n", sock,
366 lingering_close(sock));
369 * The thread handling this HTTP connection is now finished.