]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
* Rewrote the HTTP engine and application coupling to run in a worker thread
[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 <signal.h>
38 #include "webcit.h"
39 #include "webserver.h"
40
41 /* Only one thread may manipulate SessionList at a time... */
42 pthread_mutex_t SessionListMutex;
43
44 struct wcsession *SessionList = NULL;
45
46 pthread_key_t MyConKey;                         /* TSD key for MySession() */
47
48 void do_housekeeping(void)
49 {
50         struct wcsession *sptr;
51
52         pthread_mutex_lock(&SessionListMutex);
53
54         /* Kill idle sessions */
55         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
56                 if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
57                         /* FIX do something here */
58                 }
59         }
60
61         pthread_mutex_unlock(&SessionListMutex);
62 }
63
64
65 /* 
66  * Wake up occasionally and clean house
67  */
68 void housekeeping_loop(void)
69 {
70         while (1) {
71                 sleep(HOUSEKEEPING);
72                 do_housekeeping();
73         }
74 }
75
76
77 /*
78  * Generate a unique WebCit session ID (which is not the same thing as the
79  * Citadel session ID).
80  *
81  * FIX ... we really should check to make sure we're generating a truly
82  * unique session ID by traversing the SessionList.
83  *
84  */
85 int GenerateSessionID(void)
86 {
87         static int seq = (-1);
88
89         if (seq < 0) {
90                 seq = (int) time(NULL);
91         }
92                 
93         return ++seq;
94 }
95
96
97 void gets0(int fd, char buf[])
98 {
99
100         buf[0] = 0;
101         do {
102                 buf[strlen(buf) + 1] = 0;
103                 read(fd, &buf[strlen(buf)], 1);
104         } while (buf[strlen(buf) - 1] >= 32);
105         buf[strlen(buf) - 1] = 0;
106 }
107
108 /*
109  * Collapse multiple cookies on one line
110  */
111 void req_gets(int sock, char *buf, char *hold)
112 {
113         int a;
114
115         if (strlen(hold) == 0) {
116                 client_gets(sock, buf);
117         } else {
118                 strcpy(buf, hold);
119         }
120         strcpy(hold, "");
121
122         if (!strncasecmp(buf, "Cookie: ", 8)) {
123                 for (a = 0; a < strlen(buf); ++a)
124                         if (buf[a] == ';') {
125                                 sprintf(hold, "Cookie: %s", &buf[a + 1]);
126                                 buf[a] = 0;
127                                 while (isspace(hold[8]))
128                                         strcpy(&hold[8], &hold[9]);
129                                 return;
130                         }
131         }
132 }
133
134 /*
135  * lingering_close() a`la Apache. see
136  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
137  */
138
139 static int lingering_close(int fd)
140 {
141         char buf[256];
142         int i;
143         fd_set set;
144         struct timeval tv, start;
145
146         gettimeofday(&start, NULL);
147         shutdown(fd, 1);
148         do {
149                 do {
150                         gettimeofday(&tv, NULL);
151                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
152                         tv.tv_usec = start.tv_usec - tv.tv_usec;
153                         if (tv.tv_usec < 0) {
154                                 tv.tv_sec--;
155                                 tv.tv_usec += 1000000;
156                         }
157                         FD_ZERO(&set);
158                         FD_SET(fd, &set);
159                         i = select(fd + 1, &set, NULL, NULL, &tv);
160                 } while (i == -1 && errno == EINTR);
161
162                 if (i <= 0)
163                         break;
164
165                 i = read(fd, buf, sizeof buf);
166         } while (i != 0 && (i != -1 || errno == EINTR));
167
168         return close(fd);
169 }
170
171
172
173
174 /*
175  * This loop gets called once for every HTTP connection made to WebCit.  At
176  * this entry point we have an HTTP socket with a browser allegedly on the
177  * other end, but we have not yet bound to a WebCit session.
178  *
179  * The job of this function is to locate the correct session and bind to it,
180  * or create a session if necessary and bind to it, then run the WebCit
181  * transaction loop.  Afterwards, we unbind from the session.  When this
182  * function returns, the worker thread is then free to handle another
183  * transaction.
184  */
185 void context_loop(int sock)
186 {
187         struct httprequest *req = NULL;
188         struct httprequest *last = NULL;
189         struct httprequest *hptr;
190         char buf[256], hold[256];
191         int desired_session = 0;
192         int got_cookie = 0;
193         struct wcsession *TheSession, *sptr;
194         int CloseSession = 0;
195
196
197         fprintf(stderr, "Reading request from socket %d\n", sock);
198
199         /*
200          * Find out what it is that the web browser is asking for
201          */
202         do {
203                 req_gets(sock, buf, hold);
204                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
205                         cookie_to_stuff(&buf[15], &desired_session,
206                                 NULL, NULL, NULL);
207                         got_cookie = 1;
208                 }
209
210                 hptr = (struct httprequest *)
211                         malloc(sizeof(struct httprequest));
212                 if (req == NULL)
213                         req = hptr;
214                 else
215                         last->next = hptr;
216                 hptr->next = NULL;
217                 last = hptr;
218
219                 strcpy(hptr->line, buf);
220
221         } while (strlen(buf) > 0);
222
223
224         /*
225          * If requesting a non-root page, there should already be a cookie
226          * set.  If there isn't, the client browser has cookies turned off
227          * (or doesn't support them) and we have to barf & bail.
228          */
229         strcpy(buf, req->line);
230         if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
231         else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
232         if (buf[1]==' ') buf[1]=0;
233
234         /*
235          * While we're at it, gracefully handle requests for the
236          * robots.txt file...
237          */
238         if (!strncasecmp(buf, "/robots.txt", 11)) {
239                 strcpy(req->line, "GET /static/robots.txt HTTP/1.0");
240         }
241
242         /* Do the non-root-cookie check now. */
243         else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
244                 strcpy(req->line, "GET /static/nocookies.html HTTP/1.0");
245         }
246
247
248
249         /*
250          * See if there's an existing session open with the desired ID
251          */
252         TheSession = NULL;
253         if (desired_session != 0) {
254                 pthread_mutex_lock(&SessionListMutex);
255                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
256                         if (sptr->wc_session == desired_session) {
257                                 TheSession = sptr;
258                         }
259                 }
260                 pthread_mutex_unlock(&SessionListMutex);
261         }
262
263         /*
264          * Create a new session if we have to
265          */
266         if (TheSession == NULL) {
267                 printf("Creating a new session\n");
268                 TheSession = (struct wcsession *)
269                         malloc(sizeof(struct wcsession));
270                 memset(TheSession, 0, sizeof(struct wcsession));
271                 TheSession->wc_session = GenerateSessionID();
272                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
273
274                 pthread_mutex_lock(&SessionListMutex);
275                 TheSession->next = SessionList;
276                 SessionList = TheSession;
277                 pthread_mutex_unlock(&SessionListMutex);
278         }
279
280
281         /*
282          *
283          * FIX ... check session integrity here before continuing
284          *
285          */
286
287
288
289         /*
290          * Bind to the session
291          */
292         pthread_mutex_lock(&TheSession->SessionMutex);
293         pthread_setspecific(MyConKey, (void *)TheSession);
294         TheSession->lastreq = time(NULL);
295         TheSession->http_sock = sock;
296
297         /* 
298          * Perform the WebCit transaction
299          */
300         fprintf(stderr, "Transaction: %s\n", req->line);
301         session_loop(req);
302         fprintf(stderr, "Returned from transaction loop\n");
303
304         /*
305          * If the last response included a "close session" directive,
306          * remove the context now.
307          */
308         if (CloseSession) {
309                 /*  FIX   remove_session(TheSession, 1);   */
310         } else {
311
312                 pthread_mutex_unlock(&TheSession->SessionMutex);
313         }
314
315         /* Free the request buffer */
316         while (req != NULL) {
317                 hptr = req->next;
318                 free(req);
319                 req = hptr;
320         }
321
322         /*
323          * Now our HTTP connection is done.  Close the socket and exit this
324          * function, so the worker thread can handle a new HTTP connection.
325          */
326         printf("   Closing socket %d ... ret=%d\n", sock,
327                lingering_close(sock));
328         return;
329 }