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>
38 #include "webserver.h"
41 * We keep one of these around for each active session
44 struct wc_session *next; /* Next session in list */
45 int session_id; /* Session ID */
46 int inpipe[2]; /* Data from webserver to session */
47 int outpipe[2]; /* Data from session to webserver */
48 pthread_mutex_t critter; /* Critical section uses pipes */
51 struct wc_session *SessionList = NULL;
53 /* Only one thread may manipulate SessionList at a time... */
54 pthread_mutex_t MasterCritter;
56 int GenerateSessionID() {
61 void gets0(int fd, char buf[]) {
65 buf[strlen(buf)+1] = 0;
66 read(fd, &buf[strlen(buf)], 1);
67 } while (buf[strlen(buf)-1] >= 32);
68 buf[strlen(buf)-1] = 0;
72 * Collapse multiple cookies on one line
74 void req_gets(int sock, char *buf, char *hold) {
77 if (strlen(hold)==0) {
78 client_gets(sock, buf);
85 if (!strncasecmp(buf, "Cookie: ", 8)) {
86 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
87 sprintf(hold, "Cookie: %s", &buf[a+1]);
89 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
96 * Grab a lock on the session, so other threads don't try to access
97 * the pipes at the same time.
99 static void lock_session(struct wc_session *session) {
100 printf("Locking session %d...\n", session->session_id);
101 pthread_mutex_lock(&session->critter);
102 printf(" ...got lock\n");
106 * Let go of the lock.
108 static void unlock_session(struct wc_session *session) {
109 printf("Unlocking.\n");
110 pthread_mutex_unlock(&session->critter);
113 extern const char *defaulthost;
114 extern const char *defaultport;
117 * This loop gets called once for every HTTP connection made to WebCit.
119 void *context_loop(int sock) {
121 char buf[256], hold[256];
125 int desired_session = 0;
126 char str_session[256];
127 struct wc_session *sptr;
128 struct wc_session *TheSession;
130 int CloseSession = 0;
132 if ((req = malloc(sizeof(char[256][256]))) == NULL) {
133 printf("Can't malloc buffers; dropping connection.\n");
138 printf("Reading request from socket %d\n", sock);
141 * Find out what it is that the web browser is asking for
145 req_gets(sock, buf, hold);
146 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
147 desired_session = atoi(&buf[19]);
149 if (!strncasecmp(buf, "Content-length: ", 16)) {
150 ContentLength = atoi(&buf[16]);
152 strcpy(&req[num_lines++][0], buf);
153 } while(strlen(buf)>0);
156 * See if there's an existing session open with the desired ID
159 if (desired_session != 0) {
160 pthread_mutex_lock(&MasterCritter);
161 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
162 if (sptr->session_id == desired_session) {
164 lock_session(TheSession);
167 pthread_mutex_unlock(&MasterCritter);
171 * Create a new session if we have to
173 if (TheSession == NULL) {
174 printf("Creating a new session\n");
175 pthread_mutex_lock(&MasterCritter);
176 TheSession = (struct wc_session *)
177 malloc(sizeof(struct wc_session));
178 TheSession->session_id = GenerateSessionID();
179 pipe(TheSession->inpipe);
180 pipe(TheSession->outpipe);
181 pthread_mutex_init(&TheSession->critter, NULL);
182 lock_session(TheSession);
183 TheSession->next = SessionList;
184 SessionList = TheSession;
185 pthread_mutex_unlock(&MasterCritter);
186 sprintf(str_session, "%d", TheSession->session_id);
188 fflush(stdout); fflush(stdin);
190 dup2(TheSession->inpipe[0], 0);
191 dup2(TheSession->outpipe[1], 1);
192 /* Close the ends of the pipes that we're not using */
193 close(TheSession->inpipe[1]);
194 close(TheSession->outpipe[0]);
195 execlp("./webcit", "webcit", str_session, defaulthost,
197 printf("HTTP/1.0 404 WebCit Failure\n\n");
198 printf("Server: %s\n", SERVER);
199 printf("Content-type: text/html\n");
200 printf("Content-length: 76\n");
202 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
203 printf("<BODY>execlp() failed</BODY></HTML>\n");
206 /* Close the ends of the pipes that we're not using */
207 close(TheSession->inpipe[0]);
208 close(TheSession->outpipe[1]);
212 * Send the request to the appropriate session...
214 printf(" Writing %d lines of command\n", num_lines);
215 printf("%s\n", &req[0][0]);
216 for (a=0; a<num_lines; ++a) {
217 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
218 write(TheSession->inpipe[1], "\n", 1);
220 printf(" Writing %d bytes of content\n", ContentLength);
221 while (ContentLength--) {
223 write(TheSession->inpipe[1], buf, 1);
227 * ...and get the response.
229 printf(" Reading response\n");
232 gets0(TheSession->outpipe[0], buf);
233 write(sock, buf, strlen(buf));
234 write(sock, "\n", 1);
235 if (!strncasecmp(buf, "Content-length: ", 16))
236 ContentLength = atoi(&buf[16]);
237 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
240 } while (strlen(buf) > 0);
242 printf(" Reading %d bytes of content\n", ContentLength);
243 while(ContentLength--) {
244 read(TheSession->outpipe[0], buf, 1);
249 * Now our HTTP connection is done. It would be relatively easy
250 * to support HTTP/1.1 "persistent" connections by looping back to
251 * the top of this function. For now, we'll just close.
253 printf(" Closing socket\n");
256 unlock_session(TheSession);
259 * If the last response included a "close session" directive,
260 * remove the context now.
263 printf("Removing session.\n");
264 pthread_mutex_lock(&MasterCritter);
266 lock_session(TheSession);
268 if (SessionList==TheSession) {
269 SessionList = SessionList->next;
272 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
273 if (sptr->next == TheSession) {
274 sptr->next = TheSession->next;
279 close(TheSession->inpipe[1]);
280 close(TheSession->outpipe[0]);
281 unlock_session(TheSession);
284 pthread_mutex_unlock(&MasterCritter);
290 * The thread handling this HTTP connection is now finished.