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 int GenerateSessionID() {
46 void gets0(int fd, char buf[]) {
50 buf[strlen(buf)+1] = 0;
51 read(fd, &buf[strlen(buf)], 1);
52 } while (buf[strlen(buf)-1] >= 32);
53 buf[strlen(buf)-1] = 0;
57 * Collapse multiple cookies on one line
59 void req_gets(int sock, char *buf, char *hold) {
62 if (strlen(hold)==0) {
63 client_gets(sock, buf);
70 if (!strncasecmp(buf, "Cookie: ", 8)) {
71 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
72 sprintf(hold, "Cookie: %s", &buf[a+1]);
74 while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
82 * This loop gets called once for every HTTP connection made to WebCit.
84 void *context_loop(int *socknumptr) {
86 char buf[256], hold[256];
91 int desired_session = 0;
92 char str_session[256];
93 struct wc_session *sptr;
94 struct wc_session *TheSession;
99 printf("Reading request from socket %d\n", sock);
102 * Find out what it is that the web browser is asking for
106 req_gets(sock, buf, hold);
107 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
108 desired_session = atoi(&buf[19]);
110 if (!strncasecmp(buf, "Content-length: ", 16)) {
111 ContentLength = atoi(&buf[16]);
113 strcpy(&req[num_lines++][0], buf);
114 } while(strlen(buf)>0);
117 * See if there's an existing session open with the desired ID
120 if (desired_session != 0) {
121 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
122 if (sptr->session_id == desired_session) {
129 * Create a new session if we have to
131 if (TheSession == NULL) {
132 printf("Creating a new session\n");
133 TheSession = (struct wc_session *)
134 malloc(sizeof(struct wc_session));
135 TheSession->session_id = GenerateSessionID();
136 pthread_mutex_init(&TheSession->critter, NULL);
137 pipe(TheSession->inpipe);
138 pipe(TheSession->outpipe);
139 TheSession->next = SessionList;
140 SessionList = TheSession;
141 sprintf(str_session, "%d", TheSession->session_id);
143 fflush(stdout); fflush(stdin);
145 dup2(TheSession->inpipe[0], 0);
146 dup2(TheSession->outpipe[1], 1);
147 execlp("./webcit", "webcit", str_session, NULL);
148 printf("HTTP/1.0 404 WebCit Failure\n\n");
149 printf("Server: %s\n", SERVER);
150 printf("Content-type: text/html\n");
151 printf("Content-length: 76\n");
153 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
154 printf("<BODY>execlp() failed</BODY></HTML>\n");
160 * Grab a lock on the session, so other threads don't try to access
161 * the pipes at the same time.
163 printf("Locking...\n");
164 pthread_mutex_lock(&TheSession->critter);
167 * Send the request to the appropriate session...
169 printf(" Writing %d lines of command\n", num_lines);
170 printf("%s\n", &req[0][0]);
171 for (a=0; a<num_lines; ++a) {
172 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
173 write(TheSession->inpipe[1], "\n", 1);
175 printf(" Writing %d bytes of content\n", ContentLength);
176 while (ContentLength--) {
178 write(TheSession->inpipe[1], buf, 1);
182 * ...and get the response.
184 printf(" Reading response\n");
187 gets0(TheSession->outpipe[0], buf);
188 write(sock, buf, strlen(buf));
189 write(sock, "\n", 1);
190 if (!strncasecmp(buf, "Content-length: ", 16))
191 ContentLength = atoi(&buf[16]);
192 } while (strlen(buf) > 0);
194 printf(" Reading %d bytes of content\n");
195 while(ContentLength--) {
196 read(TheSession->outpipe[0], buf, 1);
201 * Now our HTTP connection is done. It would be relatively easy
202 * to support HTTP/1.1 "persistent" connections by looping back to
203 * the top of this function. For now, we'll just close.
205 printf(" Closing socket\n");
211 printf("Unlocking.\n");
212 pthread_mutex_unlock(&TheSession->critter);
215 * The thread handling this HTTP connection is now finished.