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.
15 #include <sys/types.h>
17 #include <sys/socket.h>
20 #include <netinet/in.h>
29 * We keep one of these around for each active session
32 struct wc_session *next; /* Next session in list */
33 int session_id; /* Session ID */
34 int inpipe[2]; /* Data from webserver to session */
35 int outpipe[2]; /* Data from session to webserver */
36 pthread_mutex_t critter; /* Critical section uses pipes */
39 struct wc_session *SessionList = NULL;
41 /* Only one thread may manipulate SessionList at a time... */
42 pthread_mutex_t MasterCritter;
44 int GenerateSessionID() {
49 void gets0(int fd, char buf[]) {
53 buf[strlen(buf)+1] = 0;
54 read(fd, &buf[strlen(buf)], 1);
55 } while (buf[strlen(buf)-1] >= 32);
56 buf[strlen(buf)-1] = 0;
60 * Collapse multiple cookies on one line
62 void req_gets(int sock, char *buf, char *hold) {
65 if (strlen(hold)==0) {
66 client_gets(sock, buf);
73 if (!strncasecmp(buf, "Cookie: ", 8)) {
74 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
75 sprintf(hold, "Cookie: %s", &buf[a+1]);
77 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
85 * This loop gets called once for every HTTP connection made to WebCit.
87 void *context_loop(int sock) {
89 char buf[256], hold[256];
93 int desired_session = 0;
94 char str_session[256];
95 struct wc_session *sptr;
96 struct wc_session *TheSession;
100 printf("Reading request from socket %d\n", sock);
103 * Find out what it is that the web browser is asking for
107 req_gets(sock, buf, hold);
108 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
109 desired_session = atoi(&buf[19]);
111 if (!strncasecmp(buf, "Content-length: ", 16)) {
112 ContentLength = atoi(&buf[16]);
114 strcpy(&req[num_lines++][0], buf);
115 } while(strlen(buf)>0);
118 * See if there's an existing session open with the desired ID
121 if (desired_session != 0) {
122 pthread_mutex_lock(&MasterCritter);
123 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
124 if (sptr->session_id == desired_session) {
128 pthread_mutex_unlock(&MasterCritter);
132 * Create a new session if we have to
134 if (TheSession == NULL) {
135 printf("Creating a new session\n");
136 pthread_mutex_lock(&MasterCritter);
137 TheSession = (struct wc_session *)
138 malloc(sizeof(struct wc_session));
139 TheSession->session_id = GenerateSessionID();
140 pipe(TheSession->inpipe);
141 pipe(TheSession->outpipe);
142 pthread_mutex_init(&TheSession->critter, NULL);
143 TheSession->next = SessionList;
144 SessionList = TheSession;
145 sprintf(str_session, "%d", TheSession->session_id);
147 fflush(stdout); fflush(stdin);
149 dup2(TheSession->inpipe[0], 0);
150 dup2(TheSession->outpipe[1], 1);
151 execlp("./webcit", "webcit", str_session, NULL);
152 printf("HTTP/1.0 404 WebCit Failure\n\n");
153 printf("Server: %s\n", SERVER);
154 printf("Content-type: text/html\n");
155 printf("Content-length: 76\n");
157 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
158 printf("<BODY>execlp() failed</BODY></HTML>\n");
161 pthread_mutex_unlock(&MasterCritter);
165 * Grab a lock on the session, so other threads don't try to access
166 * the pipes at the same time.
168 printf("Locking session %d...\n", TheSession->session_id);
169 pthread_mutex_lock(&TheSession->critter);
170 printf(" ...got lock\n");
173 * Send the request to the appropriate session...
175 printf(" Writing %d lines of command\n", num_lines);
176 printf("%s\n", &req[0][0]);
177 for (a=0; a<num_lines; ++a) {
178 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
179 write(TheSession->inpipe[1], "\n", 1);
181 printf(" Writing %d bytes of content\n", ContentLength);
182 while (ContentLength--) {
184 write(TheSession->inpipe[1], buf, 1);
188 * ...and get the response.
190 printf(" Reading response\n");
193 gets0(TheSession->outpipe[0], buf);
194 write(sock, buf, strlen(buf));
195 write(sock, "\n", 1);
196 if (!strncasecmp(buf, "Content-length: ", 16))
197 ContentLength = atoi(&buf[16]);
198 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
201 } while (strlen(buf) > 0);
203 printf(" Reading %d bytes of content\n");
204 while(ContentLength--) {
205 read(TheSession->outpipe[0], buf, 1);
210 * Now our HTTP connection is done. It would be relatively easy
211 * to support HTTP/1.1 "persistent" connections by looping back to
212 * the top of this function. For now, we'll just close.
214 printf(" Closing socket\n");
220 printf("Unlocking.\n");
221 pthread_mutex_unlock(&TheSession->critter);
226 * If the last response included a "close session" directive,
227 * remove the context now.
230 printf("Removing session.\n");
231 pthread_mutex_lock(&MasterCritter);
233 if (SessionList==TheSession) {
234 SessionList = SessionList->next;
237 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
238 if (sptr->next == TheSession) {
239 sptr->next = TheSession->next;
246 pthread_mutex_unlock(&MasterCritter);
252 * The thread handling this HTTP connection is now finished.