]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
handled content
[citadel.git] / webcit / context_loop.c
1 /*
2  * context_loop.c
3  *
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.
8  */
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <pthread.h>
26 #include "webcit.h"
27
28 /*
29  * We keep one of these around for each active session
30  */
31 struct wc_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 */
37         };
38
39 struct wc_session *SessionList = NULL;
40
41 int GenerateSessionID() {
42         return getpid();
43         }
44
45
46 void gets0(int fd, char buf[]) {
47
48         buf[0] = 0;
49         do {
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;
54         }
55
56
57 /*
58  * This loop gets called once for every HTTP connection made to WebCit.
59  */
60 void *context_loop(int *socknumptr) {
61         char req[256][256];
62         char buf[256];
63         int num_lines = 0;
64         int a;
65         int f;
66         int sock;
67         int desired_session = 0;
68         char str_session[256];
69         struct wc_session *sptr;
70         struct wc_session *TheSession;
71         int ContentLength;
72
73         sock = *socknumptr;
74
75         /*
76          * Find out what it is that the web browser is asking for
77          */
78         ContentLength = 0;
79         do {
80                 client_gets(sock, buf);
81                 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
82                         desired_session = atoi(&buf[19]);
83                         }
84                 if (!strncasecmp(buf, "Content-length: ", 16)) {
85                         ContentLength = atoi(&buf[16]);
86                         }
87                 strcpy(&req[num_lines++][0], buf);
88                 } while(strlen(buf)>0);
89
90         /*
91          * See if there's an existing session open with the desired ID
92          */
93         TheSession = NULL;
94         if (desired_session != 0) {
95                 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
96                         if (sptr->session_id == desired_session) {
97                                 TheSession = sptr;
98                                 }
99                         }
100                 }
101
102         /*
103          * Create a new session if we have to
104          */
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);
116                 f = fork();
117                 fflush(stdout); fflush(stdin);
118                 if (f==0) {
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");
126                         printf("\n");
127                         printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
128                         printf("<BODY>execlp() failed</BODY></HTML>\n");
129                         exit(0);
130                         }
131                 }
132
133         /* 
134          * Send the request to the appropriate session
135          */
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);
140                 }
141         while (ContentLength--) {
142                 read(sock, buf, 1);
143                 write(TheSession->inpipe[1], buf, 1);
144                 }
145
146         /*
147          * ...and get the response (FIX for non-text)
148          */
149         ContentLength = 0;
150         do {
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);
157
158         while(ContentLength--) {
159                 read(TheSession->outpipe[0], buf, 1);
160                 write(sock, buf, 1);
161                 }
162
163         pthread_mutex_unlock(&TheSession->critter);
164
165         /*
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.
169          */
170         close(sock);
171         pthread_exit(NULL);
172         }