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