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 */
38 struct wc_session *SessionList = NULL;
40 int GenerateSessionID() {
45 void gets0(int fd, char buf[]) {
49 buf[strlen(buf)+1] = 0;
50 read(fd, &buf[strlen(buf)], 1);
51 } while (buf[strlen(buf)-1] >= 32);
52 buf[strlen(buf)-1] = 0;
57 * This loop gets called once for every HTTP connection made to WebCit.
59 void *context_loop(int *socknumptr) {
66 int desired_session = 0;
67 char str_session[256];
68 struct wc_session *sptr;
69 struct wc_session *TheSession;
75 * Find out what it is that the web browser is asking for
78 client_gets(sock, buf);
79 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
80 desired_session = atoi(&buf[19]);
84 strcpy(&req[num_lines++][0], buf);
85 } while(strlen(buf)>0);
88 * See if there's an existing session open with the desired ID
91 if (desired_session != 0) {
92 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
93 if (sptr->session_id == desired_session) {
100 * Create a new session if we have to
102 if (TheSession == NULL) {
103 printf("Creating a new session\n");
104 TheSession = (struct wc_session *)
105 malloc(sizeof(struct wc_session));
106 TheSession->session_id = GenerateSessionID();
107 pipe(TheSession->inpipe);
108 pipe(TheSession->outpipe);
109 TheSession->next = SessionList;
110 SessionList = TheSession;
111 sprintf(str_session, "%d", TheSession->session_id);
113 fflush(stdout); fflush(stdin);
115 dup2(TheSession->inpipe[0], 0);
116 dup2(TheSession->outpipe[1], 1);
117 execlp("./webcit", "webcit", str_session, NULL);
118 printf("HTTP/1.0 404 WebCit Failure\n\n");
119 printf("Content-type: text/html\n");
120 printf("Content-length: 76\n");
122 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
123 printf("<BODY>execlp() failed</BODY></HTML>\n");
129 * Send the request to the appropriate session
131 for (a=0; a<num_lines; ++a) {
132 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
133 write(TheSession->inpipe[1], "\n", 1);
135 write(TheSession->inpipe[1], "\n", 1);
138 * ...and get the response (FIX for non-text)
142 gets0(TheSession->outpipe[0], buf);
143 write(sock, buf, strlen(buf));
144 write(sock, "\n", 1);
145 if (!strncasecmp(buf, "Content-length: ", 16))
146 ContentLength = atoi(&buf[16]);
147 } while (strlen(buf) > 0);
149 while(ContentLength--) {
150 read(TheSession->outpipe[0], buf, 1);
155 * Now our HTTP connection is done. It would be relatively easy
156 * to support HTTP/1.1 "persistent" connections by looping back to
157 * the top of this function. For now, we'll just exit.