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
79 client_gets(sock, buf);
80 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
81 desired_session = atoi(&buf[19]);
85 strcpy(&req[num_lines++][0], buf);
86 } while(strlen(buf)>0);
89 * See if there's an existing session open with the desired ID
92 if (desired_session != 0) {
93 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
94 if (sptr->session_id == desired_session) {
101 * Create a new session if we have to
103 if (TheSession == NULL) {
104 printf("Creating a new session\n");
105 TheSession = (struct wc_session *)
106 malloc(sizeof(struct wc_session));
107 TheSession->session_id = GenerateSessionID();
108 pthread_mutex_init(&TheSession->critter, NULL);
109 pipe(TheSession->inpipe);
110 pipe(TheSession->outpipe);
111 TheSession->next = SessionList;
112 SessionList = TheSession;
113 sprintf(str_session, "%d", TheSession->session_id);
115 fflush(stdout); fflush(stdin);
117 dup2(TheSession->inpipe[0], 0);
118 dup2(TheSession->outpipe[1], 1);
119 execlp("./webcit", "webcit", str_session, NULL);
120 printf("HTTP/1.0 404 WebCit Failure\n\n");
121 printf("Server: %s\n", SERVER);
122 printf("Content-type: text/html\n");
123 printf("Content-length: 76\n");
125 printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
126 printf("<BODY>execlp() failed</BODY></HTML>\n");
132 * Send the request to the appropriate session
134 pthread_mutex_lock(&TheSession->critter);
135 for (a=0; a<num_lines; ++a) {
136 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
137 write(TheSession->inpipe[1], "\n", 1);
139 /* write(TheSession->inpipe[1], "\n", 1); */
142 * ...and get the response (FIX for non-text)
146 gets0(TheSession->outpipe[0], buf);
147 write(sock, buf, strlen(buf));
148 write(sock, "\n", 1);
149 if (!strncasecmp(buf, "Content-length: ", 16))
150 ContentLength = atoi(&buf[16]);
151 } while (strlen(buf) > 0);
153 while(ContentLength--) {
154 read(TheSession->outpipe[0], buf, 1);
158 pthread_mutex_unlock(&TheSession->critter);
161 * Now our HTTP connection is done. It would be relatively easy
162 * to support HTTP/1.1 "persistent" connections by looping back to
163 * the top of this function. For now, we'll just exit.