]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
Set up login banner graphic and background image graphic
[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         char http_method[256];
123         int num_lines = 0;
124         int a;
125         int f;
126         int desired_session = 0;
127         char str_session[256];
128         struct wc_session *sptr;
129         struct wc_session *TheSession;
130         int ContentLength;
131         int CloseSession = 0;
132
133         if ((req = malloc((long)sizeof(char[256][256]))) == NULL) {
134                 sprintf(buf, "Can't malloc buffers; dropping connection.\n");
135                 fprintf(stderr, "%s", buf);
136                 write(sock, buf, strlen(buf));
137                 close (sock);
138                 pthread_exit(NULL);
139                 }
140
141         printf("Reading request from socket %d\n", sock);
142
143         /*
144          * Find out what it is that the web browser is asking for
145          */
146         ContentLength = 0;
147         do {
148                 req_gets(sock, buf, hold);
149                 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
150                         desired_session = atoi(&buf[19]);
151                         }
152                 if (!strncasecmp(buf, "Content-length: ", 16)) {
153                         ContentLength = atoi(&buf[16]);
154                         }
155                 strcpy(&req[num_lines++][0], buf);
156                 } while(strlen(buf)>0);
157
158         strcpy(http_method, &req[0][0]);
159         for (a=0; a<strlen(http_method); ++a) {
160                 if (isspace(http_method[a])) http_method[a]=0;
161                 }
162
163         /*
164          * See if there's an existing session open with the desired ID
165          */
166         TheSession = NULL;
167         if (desired_session != 0) {
168                 pthread_mutex_lock(&MasterCritter);
169                 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
170                         if (sptr->session_id == desired_session) {
171                                 TheSession = sptr;
172                                 lock_session(TheSession);
173                                 }
174                         }
175                 pthread_mutex_unlock(&MasterCritter);
176                 }
177
178         /*
179          * Create a new session if we have to
180          */
181         if (TheSession == NULL) {
182                 printf("Creating a new session\n");
183                 pthread_mutex_lock(&MasterCritter);
184                 TheSession = (struct wc_session *)
185                         malloc(sizeof(struct wc_session));
186                 TheSession->session_id = GenerateSessionID();
187                 pipe(TheSession->inpipe);
188                 pipe(TheSession->outpipe);
189                 pthread_mutex_init(&TheSession->critter, NULL);
190                 lock_session(TheSession);
191                 TheSession->next = SessionList;
192                 SessionList = TheSession;
193                 pthread_mutex_unlock(&MasterCritter);
194                 sprintf(str_session, "%d", TheSession->session_id);
195                 f = fork();
196                 fflush(stdout); fflush(stdin);
197                 if (f==0) {
198                         dup2(TheSession->inpipe[0], 0);
199                         dup2(TheSession->outpipe[1], 1);
200                         /* Close the ends of the pipes that we're not using */
201                         close(TheSession->inpipe[1]);
202                         close(TheSession->outpipe[0]);
203                         close(sock);
204                         execlp("./webcit", "webcit", str_session, defaulthost,
205                                defaultport, NULL);
206                         printf("HTTP/1.0 404 WebCit Failure\n\n");
207                         printf("Server: %s\n", SERVER);
208                         printf("Content-type: text/html\n");
209                         printf("Content-length: 76\n");
210                         printf("\n");
211                         printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
212                         printf("<BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">execlp() failed</BODY></HTML>\n");
213                         exit(0);
214                         }
215                 /* Close the ends of the pipes that we're not using */
216                 close(TheSession->inpipe[0]);
217                 close(TheSession->outpipe[1]);
218                 }
219
220         /* 
221          * Send the request to the appropriate session...
222          */
223         printf("   Writing %d lines of command\n", num_lines);
224         printf("%s\n", &req[0][0]);
225         for (a=0; a<num_lines; ++a) {
226                 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
227                 write(TheSession->inpipe[1], "\n", 1);
228                 }
229         printf("   Writing %d bytes of content\n", ContentLength);
230         if (ContentLength > 0) {
231                 while (ContentLength > 0) {
232                         a = ContentLength;
233                         if (a > sizeof buf) a = sizeof buf;
234                         if (!client_read(sock, buf, a)) goto end;
235                         if (write(TheSession->inpipe[1], buf, a) != a) goto end;
236                         ContentLength -= a;
237                         }
238
239                 }       
240
241         /*
242          * ...and get the response.
243          */
244         printf("   Reading response\n");
245         ContentLength = 0;
246         do {
247                 gets0(TheSession->outpipe[0], buf);
248                 write(sock, buf, strlen(buf));
249                 write(sock, "\n", 1);
250                 if (!strncasecmp(buf, "Content-length: ", 16))
251                         ContentLength = atoi(&buf[16]);
252                 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
253                         CloseSession = 1;
254                         }
255                 } while (strlen(buf) > 0);
256
257         printf("   Reading %d bytes of content\n", ContentLength);
258         while(ContentLength--) {
259                 read(TheSession->outpipe[0], buf, 1);
260                 write(sock, buf, 1);
261                 }
262
263         /*
264          * If the last response included a "close session" directive,
265          * remove the context now.
266          */
267         if (CloseSession) {
268                 printf("Removing session.\n");
269                 pthread_mutex_lock(&MasterCritter);
270
271                 if (SessionList==TheSession) {
272                         SessionList = SessionList->next;
273                         }
274                 else {
275                         for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
276                                 if (sptr->next == TheSession) {
277                                         sptr->next = TheSession->next;
278                                         }
279                                 }
280                         }
281
282                 close(TheSession->inpipe[1]);
283                 close(TheSession->outpipe[0]);
284                 unlock_session(TheSession);
285                 free(TheSession);
286         
287                 pthread_mutex_unlock(&MasterCritter);
288                 }
289         else {
290         end:
291                 unlock_session(TheSession);
292                 }
293         free(req);
294
295
296         /* Discard the CRLF following the POST data. Yes, this is
297          * necessary. No, you don't want to know why. */
298         if (!strcasecmp(http_method, "POST")) {
299                 client_read(sock, buf, 2);
300                 }
301
302         /*
303          * Now our HTTP connection is done.  It would be relatively easy
304          * to support HTTP/1.1 "persistent" connections by looping back to
305          * the top of this function.  For now, we'll just close.
306          */
307         printf("   Closing socket %d ... ret=%d\n", sock, close(sock));
308
309         /*
310          * The thread handling this HTTP connection is now finished.
311          */
312         pthread_exit(NULL);
313         }