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.
18 #include <sys/types.h>
20 #include <sys/socket.h>
23 #include <netinet/in.h>
30 #include "webserver.h"
33 * We keep one of these around for each active session
36 struct wc_session *next; /* Next session in list */
37 int session_id; /* Session ID */
38 int inpipe[2]; /* Data from webserver to session */
39 int outpipe[2]; /* Data from session to webserver */
40 pthread_mutex_t critter; /* Critical section uses pipes */
43 struct wc_session *SessionList = NULL;
45 /* Only one thread may manipulate SessionList at a time... */
46 pthread_mutex_t MasterCritter;
48 int GenerateSessionID() {
53 void gets0(int fd, char buf[]) {
57 buf[strlen(buf)+1] = 0;
58 read(fd, &buf[strlen(buf)], 1);
59 } while (buf[strlen(buf)-1] >= 32);
60 buf[strlen(buf)-1] = 0;
64 * Collapse multiple cookies on one line
66 void req_gets(int sock, char *buf, char *hold) {
69 if (strlen(hold)==0) {
70 client_gets(sock, buf);
77 if (!strncasecmp(buf, "Cookie: ", 8)) {
78 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
79 sprintf(hold, "Cookie: %s", &buf[a+1]);
81 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
87 extern const char *defaulthost;
88 extern const char *defaultport;
91 * This loop gets called once for every HTTP connection made to WebCit.
93 void *context_loop(int sock) {
95 char buf[256], hold[256];
99 int desired_session = 0;
100 char str_session[256];
101 struct wc_session *sptr;
102 struct wc_session *TheSession;
104 int CloseSession = 0;
106 if ((req = malloc(sizeof(char[256][256]))) == NULL) {
107 printf("Can't malloc buffers; dropping connection.\n");
112 printf("Reading request from socket %d\n", sock);
115 * Find out what it is that the web browser is asking for
119 req_gets(sock, buf, hold);
120 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
121 desired_session = atoi(&buf[19]);
123 if (!strncasecmp(buf, "Content-length: ", 16)) {
124 ContentLength = atoi(&buf[16]);
126 strcpy(&req[num_lines++][0], buf);
127 } while(strlen(buf)>0);
130 * See if there's an existing session open with the desired ID
133 if (desired_session != 0) {
134 pthread_mutex_lock(&MasterCritter);
135 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
136 if (sptr->session_id == desired_session) {
140 pthread_mutex_unlock(&MasterCritter);
144 * Create a new session if we have to
146 if (TheSession == NULL) {
147 printf("Creating a new session\n");
148 pthread_mutex_lock(&MasterCritter);
149 TheSession = (struct wc_session *)
150 malloc(sizeof(struct wc_session));
151 TheSession->session_id = GenerateSessionID();
152 pipe(TheSession->inpipe);
153 pipe(TheSession->outpipe);
154 pthread_mutex_init(&TheSession->critter, NULL);
155 TheSession->next = SessionList;
156 SessionList = TheSession;
157 sprintf(str_session, "%d", TheSession->session_id);
159 fflush(stdout); fflush(stdin);
161 dup2(TheSession->inpipe[0], 0);
162 dup2(TheSession->outpipe[1], 1);
163 execlp("./webcit", "webcit", str_session, defaulthost,
165 printf("HTTP/1.0 404 WebCit Failure\n\n");
166 printf("Server: %s\n", SERVER);
167 printf("Content-type: text/html\n");
168 printf("Content-length: 76\n");
170 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
171 printf("<BODY>execlp() failed</BODY></HTML>\n");
174 pthread_mutex_unlock(&MasterCritter);
178 * Grab a lock on the session, so other threads don't try to access
179 * the pipes at the same time.
181 printf("Locking session %d...\n", TheSession->session_id);
182 pthread_mutex_lock(&TheSession->critter);
183 printf(" ...got lock\n");
186 * Send the request to the appropriate session...
188 printf(" Writing %d lines of command\n", num_lines);
189 printf("%s\n", &req[0][0]);
190 for (a=0; a<num_lines; ++a) {
191 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
192 write(TheSession->inpipe[1], "\n", 1);
194 printf(" Writing %d bytes of content\n", ContentLength);
195 while (ContentLength--) {
197 write(TheSession->inpipe[1], buf, 1);
201 * ...and get the response.
203 printf(" Reading response\n");
206 gets0(TheSession->outpipe[0], buf);
207 write(sock, buf, strlen(buf));
208 write(sock, "\n", 1);
209 if (!strncasecmp(buf, "Content-length: ", 16))
210 ContentLength = atoi(&buf[16]);
211 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
214 } while (strlen(buf) > 0);
216 printf(" Reading %d bytes of content\n", ContentLength);
217 while(ContentLength--) {
218 read(TheSession->outpipe[0], buf, 1);
223 * Now our HTTP connection is done. It would be relatively easy
224 * to support HTTP/1.1 "persistent" connections by looping back to
225 * the top of this function. For now, we'll just close.
227 printf(" Closing socket\n");
233 printf("Unlocking.\n");
234 pthread_mutex_unlock(&TheSession->critter);
239 * If the last response included a "close session" directive,
240 * remove the context now.
243 printf("Removing session.\n");
244 pthread_mutex_lock(&MasterCritter);
246 if (SessionList==TheSession) {
247 SessionList = SessionList->next;
250 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
251 if (sptr->next == TheSession) {
252 sptr->next = TheSession->next;
259 pthread_mutex_unlock(&MasterCritter);
265 * The thread handling this HTTP connection is now finished.