]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
48b288f376f8040c05eb47ad9cbdce3f416e145f
[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  * $Id$
10  */
11
12 #include <ctype.h>
13 #include <stdlib.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdio.h>
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <sys/socket.h>
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #ifdef HAVE_LIMITS_H
29 #include <limits.h>
30 #endif
31 #include <netinet/in.h>
32 #include <netdb.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <pthread.h>
37 #include "webcit.h"
38 #include "webserver.h"
39
40 /*
41  * We keep one of these around for each active session
42  */
43 struct wc_session {
44         struct wc_session *next;        /* Next session in list */
45         int session_id;                 /* Session ID */
46         int inpipe[2];                  /* Data from webserver to session */
47         int outpipe[2];                 /* Data from session to webserver */
48         pthread_mutex_t critter;        /* Critical section uses pipes */
49         };
50
51 struct wc_session *SessionList = NULL;
52
53 /* Only one thread may manipulate SessionList at a time... */
54 pthread_mutex_t MasterCritter;
55
56 int GenerateSessionID(void) {
57         return getpid();
58         }
59
60
61 void gets0(int fd, char buf[]) {
62
63         buf[0] = 0;
64         do {
65                 buf[strlen(buf)+1] = 0;
66                 read(fd, &buf[strlen(buf)], 1);
67                 } while (buf[strlen(buf)-1] >= 32);
68         buf[strlen(buf)-1] = 0;
69         }
70
71 /*
72  * Collapse multiple cookies on one line
73  */
74 void req_gets(int sock, char *buf, char *hold) {
75         int a;
76
77         if (strlen(hold)==0) {
78                 client_gets(sock, buf);
79                 }
80         else {
81                 strcpy(buf, hold);
82                 }
83         strcpy(hold, "");
84
85         if (!strncasecmp(buf, "Cookie: ", 8)) {
86                 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
87                         sprintf(hold, "Cookie: %s", &buf[a+1]);
88                         buf[a]=0;
89                         while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
90                         return;
91                         }
92                 }
93         }
94
95 /*
96  * Grab a lock on the session, so other threads don't try to access
97  * the pipes at the same time.
98  */
99 static void lock_session(struct wc_session *session) {
100         printf("Locking session %d...\n", session->session_id);
101         pthread_mutex_lock(&session->critter);
102         printf("   ...got lock\n");
103         }
104
105 /*
106  * Let go of the lock.
107  */
108 static void unlock_session(struct wc_session *session) {
109         printf("Unlocking.\n");
110         pthread_mutex_unlock(&session->critter);
111         }
112
113 extern const char *defaulthost;
114 extern const char *defaultport;
115
116 /*
117  * This loop gets called once for every HTTP connection made to WebCit.
118  */
119 void *context_loop(int sock) {
120         char (*req)[256];
121         char buf[256], hold[256];
122         int num_lines = 0;
123         int a;
124         int f;
125         int desired_session = 0;
126         char str_session[256];
127         struct wc_session *sptr;
128         struct wc_session *TheSession;
129         int ContentLength;
130         int CloseSession = 0;
131
132         if ((req = malloc((long)sizeof(char[256][256]))) == NULL) {
133                 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
134                 fprintf(stderr, "%s", buf);
135                 write(sock, buf, strlen(buf));
136                 close (sock);
137                 pthread_exit(NULL);
138                 }
139
140         printf("Reading request from socket %d\n", sock);
141
142         /*
143          * Find out what it is that the web browser is asking for
144          */
145         ContentLength = 0;
146         do {
147                 req_gets(sock, buf, hold);
148                 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
149                         desired_session = atoi(&buf[19]);
150                         }
151                 if (!strncasecmp(buf, "Content-length: ", 16)) {
152                         ContentLength = atoi(&buf[16]);
153                         }
154                 strcpy(&req[num_lines++][0], buf);
155                 } while(strlen(buf)>0);
156
157         /*
158          * See if there's an existing session open with the desired ID
159          */
160         TheSession = NULL;
161         if (desired_session != 0) {
162                 pthread_mutex_lock(&MasterCritter);
163                 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
164                         if (sptr->session_id == desired_session) {
165                                 TheSession = sptr;
166                                 lock_session(TheSession);
167                                 }
168                         }
169                 pthread_mutex_unlock(&MasterCritter);
170                 }
171
172         /*
173          * Create a new session if we have to
174          */
175         if (TheSession == NULL) {
176                 printf("Creating a new session\n");
177                 pthread_mutex_lock(&MasterCritter);
178                 TheSession = (struct wc_session *)
179                         malloc(sizeof(struct wc_session));
180                 TheSession->session_id = GenerateSessionID();
181                 pipe(TheSession->inpipe);
182                 pipe(TheSession->outpipe);
183                 pthread_mutex_init(&TheSession->critter, NULL);
184                 lock_session(TheSession);
185                 TheSession->next = SessionList;
186                 SessionList = TheSession;
187                 pthread_mutex_unlock(&MasterCritter);
188                 sprintf(str_session, "%d", TheSession->session_id);
189                 f = fork();
190                 fflush(stdout); fflush(stdin);
191                 if (f==0) {
192                         dup2(TheSession->inpipe[0], 0);
193                         dup2(TheSession->outpipe[1], 1);
194                         /* Close the ends of the pipes that we're not using */
195                         close(TheSession->inpipe[1]);
196                         close(TheSession->outpipe[0]);
197                         execlp("./webcit", "webcit", str_session, defaulthost,
198                                defaultport, NULL);
199                         printf("HTTP/1.0 404 WebCit Failure\n\n");
200                         printf("Server: %s\n", SERVER);
201                         printf("Content-type: text/html\n");
202                         printf("Content-length: 76\n");
203                         printf("\n");
204                         printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
205                         printf("<BODY>execlp() failed</BODY></HTML>\n");
206                         exit(0);
207                         }
208                 /* Close the ends of the pipes that we're not using */
209                 close(TheSession->inpipe[0]);
210                 close(TheSession->outpipe[1]);
211                 }
212
213         /* 
214          * Send the request to the appropriate session...
215          */
216         printf("   Writing %d lines of command\n", num_lines);
217         printf("%s\n", &req[0][0]);
218         for (a=0; a<num_lines; ++a) {
219                 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
220                 write(TheSession->inpipe[1], "\n", 1);
221                 }
222         printf("   Writing %d bytes of content\n", ContentLength);
223         while (ContentLength--) {
224                 read(sock, buf, 1);
225                 write(TheSession->inpipe[1], buf, 1);
226                 }
227
228         /*
229          * ...and get the response.
230          */
231         printf("   Reading response\n");
232         ContentLength = 0;
233         do {
234                 gets0(TheSession->outpipe[0], buf);
235                 write(sock, buf, strlen(buf));
236                 write(sock, "\n", 1);
237                 if (!strncasecmp(buf, "Content-length: ", 16))
238                         ContentLength = atoi(&buf[16]);
239                 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
240                         CloseSession = 1;
241                         }
242                 } while (strlen(buf) > 0);
243
244         printf("   Reading %d bytes of content\n", ContentLength);
245         while(ContentLength--) {
246                 read(TheSession->outpipe[0], buf, 1);
247                 write(sock, buf, 1);
248                 }
249
250         /*
251          * Now our HTTP connection is done.  It would be relatively easy
252          * to support HTTP/1.1 "persistent" connections by looping back to
253          * the top of this function.  For now, we'll just close.
254          */
255
256         /* FIX ... This is a weird problem, exhibited on Linux 2.1.130...
257          * the socket doesn't linger properly before closing, so we implement
258          * a ten-second delay before closing the socket.
259         printf("   Lingering...\n");
260         sleep(10);
261          */
262
263         printf("   Closing socket\n");
264         close(sock);
265
266         unlock_session(TheSession);
267
268         /*
269          * If the last response included a "close session" directive,
270          * remove the context now.
271          */
272         if (CloseSession) {
273                 printf("Removing session.\n");
274                 pthread_mutex_lock(&MasterCritter);
275
276                 lock_session(TheSession);
277
278                 if (SessionList==TheSession) {
279                         SessionList = SessionList->next;
280                         }
281                 else {
282                         for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
283                                 if (sptr->next == TheSession) {
284                                         sptr->next = TheSession->next;
285                                         }
286                                 }
287                         }
288
289                 close(TheSession->inpipe[1]);
290                 close(TheSession->outpipe[0]);
291                 unlock_session(TheSession);
292                 free(TheSession);
293         
294                 pthread_mutex_unlock(&MasterCritter);
295                 }
296
297         free(req);
298
299         /*
300          * The thread handling this HTTP connection is now finished.
301          */
302         pthread_exit(NULL);
303         }