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]);
88 * Grab a lock on the session, so other threads don't try to access
89 * the pipes at the same time.
91 static void lock_session(struct wc_session *session) {
92 printf("Locking session %d...\n", session->session_id);
93 pthread_mutex_lock(&session->critter);
94 printf(" ...got lock\n");
100 static void unlock_session(struct wc_session *session) {
101 printf("Unlocking.\n");
102 pthread_mutex_unlock(&session->critter);
105 extern const char *defaulthost;
106 extern const char *defaultport;
109 * This loop gets called once for every HTTP connection made to WebCit.
111 void *context_loop(int sock) {
113 char buf[256], hold[256];
117 int desired_session = 0;
118 char str_session[256];
119 struct wc_session *sptr;
120 struct wc_session *TheSession;
122 int CloseSession = 0;
124 if ((req = malloc(sizeof(char[256][256]))) == NULL) {
125 printf("Can't malloc buffers; dropping connection.\n");
130 printf("Reading request from socket %d\n", sock);
133 * Find out what it is that the web browser is asking for
137 req_gets(sock, buf, hold);
138 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
139 desired_session = atoi(&buf[19]);
141 if (!strncasecmp(buf, "Content-length: ", 16)) {
142 ContentLength = atoi(&buf[16]);
144 strcpy(&req[num_lines++][0], buf);
145 } while(strlen(buf)>0);
148 * See if there's an existing session open with the desired ID
151 if (desired_session != 0) {
152 pthread_mutex_lock(&MasterCritter);
153 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
154 if (sptr->session_id == desired_session) {
156 lock_session(TheSession);
159 pthread_mutex_unlock(&MasterCritter);
163 * Create a new session if we have to
165 if (TheSession == NULL) {
166 printf("Creating a new session\n");
167 pthread_mutex_lock(&MasterCritter);
168 TheSession = (struct wc_session *)
169 malloc(sizeof(struct wc_session));
170 TheSession->session_id = GenerateSessionID();
171 pipe(TheSession->inpipe);
172 pipe(TheSession->outpipe);
173 pthread_mutex_init(&TheSession->critter, NULL);
174 lock_session(TheSession);
175 TheSession->next = SessionList;
176 SessionList = TheSession;
177 pthread_mutex_unlock(&MasterCritter);
178 sprintf(str_session, "%d", TheSession->session_id);
180 fflush(stdout); fflush(stdin);
182 dup2(TheSession->inpipe[0], 0);
183 dup2(TheSession->outpipe[1], 1);
184 /* Close the ends of the pipes that we're not using */
185 close(TheSession->inpipe[1]);
186 close(TheSession->outpipe[0]);
187 execlp("./webcit", "webcit", str_session, defaulthost,
189 printf("HTTP/1.0 404 WebCit Failure\n\n");
190 printf("Server: %s\n", SERVER);
191 printf("Content-type: text/html\n");
192 printf("Content-length: 76\n");
194 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
195 printf("<BODY>execlp() failed</BODY></HTML>\n");
198 /* Close the ends of the pipes that we're not using */
199 close(TheSession->inpipe[0]);
200 close(TheSession->outpipe[1]);
204 * Send the request to the appropriate session...
206 printf(" Writing %d lines of command\n", num_lines);
207 printf("%s\n", &req[0][0]);
208 for (a=0; a<num_lines; ++a) {
209 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
210 write(TheSession->inpipe[1], "\n", 1);
212 printf(" Writing %d bytes of content\n", ContentLength);
213 while (ContentLength--) {
215 write(TheSession->inpipe[1], buf, 1);
219 * ...and get the response.
221 printf(" Reading response\n");
224 gets0(TheSession->outpipe[0], buf);
225 write(sock, buf, strlen(buf));
226 write(sock, "\n", 1);
227 if (!strncasecmp(buf, "Content-length: ", 16))
228 ContentLength = atoi(&buf[16]);
229 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
232 } while (strlen(buf) > 0);
234 printf(" Reading %d bytes of content\n", ContentLength);
235 while(ContentLength--) {
236 read(TheSession->outpipe[0], buf, 1);
241 * Now our HTTP connection is done. It would be relatively easy
242 * to support HTTP/1.1 "persistent" connections by looping back to
243 * the top of this function. For now, we'll just close.
245 printf(" Closing socket\n");
248 unlock_session(TheSession);
251 * If the last response included a "close session" directive,
252 * remove the context now.
255 printf("Removing session.\n");
256 pthread_mutex_lock(&MasterCritter);
258 lock_session(TheSession);
260 if (SessionList==TheSession) {
261 SessionList = SessionList->next;
264 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
265 if (sptr->next == TheSession) {
266 sptr->next = TheSession->next;
271 close(TheSession->inpipe[1]);
272 close(TheSession->outpipe[0]);
273 unlock_session(TheSession);
276 pthread_mutex_unlock(&MasterCritter);
282 * The thread handling this HTTP connection is now finished.