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 printf("Reading request from socket %d\n", sock);
109 * Find out what it is that the web browser is asking for
113 req_gets(sock, buf, hold);
114 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
115 desired_session = atoi(&buf[19]);
117 if (!strncasecmp(buf, "Content-length: ", 16)) {
118 ContentLength = atoi(&buf[16]);
120 strcpy(&req[num_lines++][0], buf);
121 } while(strlen(buf)>0);
124 * See if there's an existing session open with the desired ID
127 if (desired_session != 0) {
128 pthread_mutex_lock(&MasterCritter);
129 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
130 if (sptr->session_id == desired_session) {
134 pthread_mutex_unlock(&MasterCritter);
138 * Create a new session if we have to
140 if (TheSession == NULL) {
141 printf("Creating a new session\n");
142 pthread_mutex_lock(&MasterCritter);
143 TheSession = (struct wc_session *)
144 malloc(sizeof(struct wc_session));
145 TheSession->session_id = GenerateSessionID();
146 pipe(TheSession->inpipe);
147 pipe(TheSession->outpipe);
148 pthread_mutex_init(&TheSession->critter, NULL);
149 TheSession->next = SessionList;
150 SessionList = TheSession;
151 sprintf(str_session, "%d", TheSession->session_id);
153 fflush(stdout); fflush(stdin);
155 dup2(TheSession->inpipe[0], 0);
156 dup2(TheSession->outpipe[1], 1);
157 execlp("./webcit", "webcit", str_session, defaulthost,
159 printf("HTTP/1.0 404 WebCit Failure\n\n");
160 printf("Server: %s\n", SERVER);
161 printf("Content-type: text/html\n");
162 printf("Content-length: 76\n");
164 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
165 printf("<BODY>execlp() failed</BODY></HTML>\n");
168 pthread_mutex_unlock(&MasterCritter);
172 * Grab a lock on the session, so other threads don't try to access
173 * the pipes at the same time.
175 printf("Locking session %d...\n", TheSession->session_id);
176 pthread_mutex_lock(&TheSession->critter);
177 printf(" ...got lock\n");
180 * Send the request to the appropriate session...
182 printf(" Writing %d lines of command\n", num_lines);
183 printf("%s\n", &req[0][0]);
184 for (a=0; a<num_lines; ++a) {
185 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
186 write(TheSession->inpipe[1], "\n", 1);
188 printf(" Writing %d bytes of content\n", ContentLength);
189 while (ContentLength--) {
191 write(TheSession->inpipe[1], buf, 1);
195 * ...and get the response.
197 printf(" Reading response\n");
200 gets0(TheSession->outpipe[0], buf);
201 write(sock, buf, strlen(buf));
202 write(sock, "\n", 1);
203 if (!strncasecmp(buf, "Content-length: ", 16))
204 ContentLength = atoi(&buf[16]);
205 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
208 } while (strlen(buf) > 0);
210 printf(" Reading %d bytes of content\n", ContentLength);
211 while(ContentLength--) {
212 read(TheSession->outpipe[0], buf, 1);
217 * Now our HTTP connection is done. It would be relatively easy
218 * to support HTTP/1.1 "persistent" connections by looping back to
219 * the top of this function. For now, we'll just close.
221 printf(" Closing socket\n");
227 printf("Unlocking.\n");
228 pthread_mutex_unlock(&TheSession->critter);
233 * If the last response included a "close session" directive,
234 * remove the context now.
237 printf("Removing session.\n");
238 pthread_mutex_lock(&MasterCritter);
240 if (SessionList==TheSession) {
241 SessionList = SessionList->next;
244 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
245 if (sptr->next == TheSession) {
246 sptr->next = TheSession->next;
253 pthread_mutex_unlock(&MasterCritter);
259 * The thread handling this HTTP connection is now finished.