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.
17 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
31 * We keep one of these around for each active session
34 struct wc_session *next; /* Next session in list */
35 int session_id; /* Session ID */
36 int inpipe[2]; /* Data from webserver to session */
37 int outpipe[2]; /* Data from session to webserver */
38 pthread_mutex_t critter; /* Critical section uses pipes */
41 struct wc_session *SessionList = NULL;
43 /* Only one thread may manipulate SessionList at a time... */
44 pthread_mutex_t MasterCritter;
46 int GenerateSessionID() {
51 void gets0(int fd, char buf[]) {
55 buf[strlen(buf)+1] = 0;
56 read(fd, &buf[strlen(buf)], 1);
57 } while (buf[strlen(buf)-1] >= 32);
58 buf[strlen(buf)-1] = 0;
62 * Collapse multiple cookies on one line
64 void req_gets(int sock, char *buf, char *hold) {
67 if (strlen(hold)==0) {
68 client_gets(sock, buf);
75 if (!strncasecmp(buf, "Cookie: ", 8)) {
76 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
77 sprintf(hold, "Cookie: %s", &buf[a+1]);
79 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
85 extern const char *defaulthost;
86 extern const char *defaultport;
89 * This loop gets called once for every HTTP connection made to WebCit.
91 void *context_loop(int sock) {
93 char buf[256], hold[256];
97 int desired_session = 0;
98 char str_session[256];
99 struct wc_session *sptr;
100 struct wc_session *TheSession;
102 int CloseSession = 0;
104 printf("Reading request from socket %d\n", sock);
107 * Find out what it is that the web browser is asking for
111 req_gets(sock, buf, hold);
112 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
113 desired_session = atoi(&buf[19]);
115 if (!strncasecmp(buf, "Content-length: ", 16)) {
116 ContentLength = atoi(&buf[16]);
118 strcpy(&req[num_lines++][0], buf);
119 } while(strlen(buf)>0);
122 * See if there's an existing session open with the desired ID
125 if (desired_session != 0) {
126 pthread_mutex_lock(&MasterCritter);
127 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
128 if (sptr->session_id == desired_session) {
132 pthread_mutex_unlock(&MasterCritter);
136 * Create a new session if we have to
138 if (TheSession == NULL) {
139 printf("Creating a new session\n");
140 pthread_mutex_lock(&MasterCritter);
141 TheSession = (struct wc_session *)
142 malloc(sizeof(struct wc_session));
143 TheSession->session_id = GenerateSessionID();
144 pipe(TheSession->inpipe);
145 pipe(TheSession->outpipe);
146 pthread_mutex_init(&TheSession->critter, NULL);
147 TheSession->next = SessionList;
148 SessionList = TheSession;
149 sprintf(str_session, "%d", TheSession->session_id);
151 fflush(stdout); fflush(stdin);
153 dup2(TheSession->inpipe[0], 0);
154 dup2(TheSession->outpipe[1], 1);
155 execlp("./webcit", "webcit", str_session, defaulthost,
157 printf("HTTP/1.0 404 WebCit Failure\n\n");
158 printf("Server: %s\n", SERVER);
159 printf("Content-type: text/html\n");
160 printf("Content-length: 76\n");
162 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
163 printf("<BODY>execlp() failed</BODY></HTML>\n");
166 pthread_mutex_unlock(&MasterCritter);
170 * Grab a lock on the session, so other threads don't try to access
171 * the pipes at the same time.
173 printf("Locking session %d...\n", TheSession->session_id);
174 pthread_mutex_lock(&TheSession->critter);
175 printf(" ...got lock\n");
178 * Send the request to the appropriate session...
180 printf(" Writing %d lines of command\n", num_lines);
181 printf("%s\n", &req[0][0]);
182 for (a=0; a<num_lines; ++a) {
183 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
184 write(TheSession->inpipe[1], "\n", 1);
186 printf(" Writing %d bytes of content\n", ContentLength);
187 while (ContentLength--) {
189 write(TheSession->inpipe[1], buf, 1);
193 * ...and get the response.
195 printf(" Reading response\n");
198 gets0(TheSession->outpipe[0], buf);
199 write(sock, buf, strlen(buf));
200 write(sock, "\n", 1);
201 if (!strncasecmp(buf, "Content-length: ", 16))
202 ContentLength = atoi(&buf[16]);
203 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
206 } while (strlen(buf) > 0);
208 printf(" Reading %d bytes of content\n");
209 while(ContentLength--) {
210 read(TheSession->outpipe[0], buf, 1);
215 * Now our HTTP connection is done. It would be relatively easy
216 * to support HTTP/1.1 "persistent" connections by looping back to
217 * the top of this function. For now, we'll just close.
219 printf(" Closing socket\n");
225 printf("Unlocking.\n");
226 pthread_mutex_unlock(&TheSession->critter);
231 * If the last response included a "close session" directive,
232 * remove the context now.
235 printf("Removing session.\n");
236 pthread_mutex_lock(&MasterCritter);
238 if (SessionList==TheSession) {
239 SessionList = SessionList->next;
242 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
243 if (sptr->next == TheSession) {
244 sptr->next = TheSession->next;
251 pthread_mutex_unlock(&MasterCritter);
257 * The thread handling this HTTP connection is now finished.