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;
58 * This loop gets called once for every HTTP connection made to WebCit.
60 void *context_loop(int *socknumptr) {
67 int desired_session = 0;
68 char str_session[256];
69 struct wc_session *sptr;
70 struct wc_session *TheSession;
76 * Find out what it is that the web browser is asking for
80 client_gets(sock, buf);
81 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
82 desired_session = atoi(&buf[19]);
84 if (!strncasecmp(buf, "Content-length: ", 16)) {
85 ContentLength = atoi(&buf[16]);
87 strcpy(&req[num_lines++][0], buf);
88 } while(strlen(buf)>0);
91 * See if there's an existing session open with the desired ID
94 if (desired_session != 0) {
95 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
96 if (sptr->session_id == desired_session) {
103 * Create a new session if we have to
105 if (TheSession == NULL) {
106 printf("Creating a new session\n");
107 TheSession = (struct wc_session *)
108 malloc(sizeof(struct wc_session));
109 TheSession->session_id = GenerateSessionID();
110 pthread_mutex_init(&TheSession->critter, NULL);
111 pipe(TheSession->inpipe);
112 pipe(TheSession->outpipe);
113 TheSession->next = SessionList;
114 SessionList = TheSession;
115 sprintf(str_session, "%d", TheSession->session_id);
117 fflush(stdout); fflush(stdin);
119 dup2(TheSession->inpipe[0], 0);
120 dup2(TheSession->outpipe[1], 1);
121 execlp("./webcit", "webcit", str_session, NULL);
122 printf("HTTP/1.0 404 WebCit Failure\n\n");
123 printf("Server: %s\n", SERVER);
124 printf("Content-type: text/html\n");
125 printf("Content-length: 76\n");
127 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
128 printf("<BODY>execlp() failed</BODY></HTML>\n");
134 * Send the request to the appropriate session
136 pthread_mutex_lock(&TheSession->critter);
137 for (a=0; a<num_lines; ++a) {
138 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
139 write(TheSession->inpipe[1], "\n", 1);
141 while (ContentLength--) {
143 write(TheSession->inpipe[1], buf, 1);
147 * ...and get the response (FIX for non-text)
151 gets0(TheSession->outpipe[0], buf);
152 write(sock, buf, strlen(buf));
153 write(sock, "\n", 1);
154 if (!strncasecmp(buf, "Content-length: ", 16))
155 ContentLength = atoi(&buf[16]);
156 } while (strlen(buf) > 0);
158 while(ContentLength--) {
159 read(TheSession->outpipe[0], buf, 1);
163 pthread_mutex_unlock(&TheSession->critter);
166 * Now our HTTP connection is done. It would be relatively easy
167 * to support HTTP/1.1 "persistent" connections by looping back to
168 * the top of this function. For now, we'll just exit.