]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
* tcp_sockets.c: removed some dead code
[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 /*
88  * Grab a lock on the session, so other threads don't try to access
89  * the pipes at the same time.
90  */
91 static void lock_session(struct wc_session *session) {
92         printf("Locking session %d...\n", session->session_id);
93         pthread_mutex_lock(&session->critter);
94         printf("   ...got lock\n");
95         }
96
97 /*
98  * Let go of the lock.
99  */
100 static void unlock_session(struct wc_session *session) {
101         printf("Unlocking.\n");
102         pthread_mutex_unlock(&session->critter);
103         }
104
105 extern const char *defaulthost;
106 extern const char *defaultport;
107
108 /*
109  * This loop gets called once for every HTTP connection made to WebCit.
110  */
111 void *context_loop(int sock) {
112         char (*req)[256];
113         char buf[256], hold[256];
114         int num_lines = 0;
115         int a;
116         int f;
117         int desired_session = 0;
118         char str_session[256];
119         struct wc_session *sptr;
120         struct wc_session *TheSession;
121         int ContentLength;
122         int CloseSession = 0;
123
124         if ((req = malloc(sizeof(char[256][256]))) == NULL) {
125                 printf("Can't malloc buffers; dropping connection.\n");
126                 close (sock);
127                 return NULL;
128                 }
129
130         printf("Reading request from socket %d\n", sock);
131
132         /*
133          * Find out what it is that the web browser is asking for
134          */
135         ContentLength = 0;
136         do {
137                 req_gets(sock, buf, hold);
138                 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
139                         desired_session = atoi(&buf[19]);
140                         }
141                 if (!strncasecmp(buf, "Content-length: ", 16)) {
142                         ContentLength = atoi(&buf[16]);
143                         }
144                 strcpy(&req[num_lines++][0], buf);
145                 } while(strlen(buf)>0);
146
147         /*
148          * See if there's an existing session open with the desired ID
149          */
150         TheSession = NULL;
151         if (desired_session != 0) {
152                 pthread_mutex_lock(&MasterCritter);
153                 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
154                         if (sptr->session_id == desired_session) {
155                                 TheSession = sptr;
156                                 lock_session(TheSession);
157                                 }
158                         }
159                 pthread_mutex_unlock(&MasterCritter);
160                 }
161
162         /*
163          * Create a new session if we have to
164          */
165         if (TheSession == NULL) {
166                 printf("Creating a new session\n");
167                 pthread_mutex_lock(&MasterCritter);
168                 TheSession = (struct wc_session *)
169                         malloc(sizeof(struct wc_session));
170                 TheSession->session_id = GenerateSessionID();
171                 pipe(TheSession->inpipe);
172                 pipe(TheSession->outpipe);
173                 pthread_mutex_init(&TheSession->critter, NULL);
174                 lock_session(TheSession);
175                 TheSession->next = SessionList;
176                 SessionList = TheSession;
177                 pthread_mutex_unlock(&MasterCritter);
178                 sprintf(str_session, "%d", TheSession->session_id);
179                 f = fork();
180                 fflush(stdout); fflush(stdin);
181                 if (f==0) {
182                         dup2(TheSession->inpipe[0], 0);
183                         dup2(TheSession->outpipe[1], 1);
184                         /* Close the ends of the pipes that we're not using */
185                         close(TheSession->inpipe[1]);
186                         close(TheSession->outpipe[0]);
187                         execlp("./webcit", "webcit", str_session, defaulthost,
188                                defaultport, NULL);
189                         printf("HTTP/1.0 404 WebCit Failure\n\n");
190                         printf("Server: %s\n", SERVER);
191                         printf("Content-type: text/html\n");
192                         printf("Content-length: 76\n");
193                         printf("\n");
194                         printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
195                         printf("<BODY>execlp() failed</BODY></HTML>\n");
196                         exit(0);
197                         }
198                 /* Close the ends of the pipes that we're not using */
199                 close(TheSession->inpipe[0]);
200                 close(TheSession->outpipe[1]);
201                 }
202
203         /* 
204          * Send the request to the appropriate session...
205          */
206         printf("   Writing %d lines of command\n", num_lines);
207         printf("%s\n", &req[0][0]);
208         for (a=0; a<num_lines; ++a) {
209                 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
210                 write(TheSession->inpipe[1], "\n", 1);
211                 }
212         printf("   Writing %d bytes of content\n", ContentLength);
213         while (ContentLength--) {
214                 read(sock, buf, 1);
215                 write(TheSession->inpipe[1], buf, 1);
216                 }
217
218         /*
219          * ...and get the response.
220          */
221         printf("   Reading response\n");
222         ContentLength = 0;
223         do {
224                 gets0(TheSession->outpipe[0], buf);
225                 write(sock, buf, strlen(buf));
226                 write(sock, "\n", 1);
227                 if (!strncasecmp(buf, "Content-length: ", 16))
228                         ContentLength = atoi(&buf[16]);
229                 if (!strcasecmp(buf, "X-WebCit-Session: close")) {
230                         CloseSession = 1;
231                         }
232                 } while (strlen(buf) > 0);
233
234         printf("   Reading %d bytes of content\n", ContentLength);
235         while(ContentLength--) {
236                 read(TheSession->outpipe[0], buf, 1);
237                 write(sock, buf, 1);
238                 }
239
240         /*
241          * Now our HTTP connection is done.  It would be relatively easy
242          * to support HTTP/1.1 "persistent" connections by looping back to
243          * the top of this function.  For now, we'll just close.
244          */
245         printf("   Closing socket\n");
246         close(sock);
247
248         unlock_session(TheSession);
249
250         /*
251          * If the last response included a "close session" directive,
252          * remove the context now.
253          */
254         if (CloseSession) {
255                 printf("Removing session.\n");
256                 pthread_mutex_lock(&MasterCritter);
257
258                 lock_session(TheSession);
259
260                 if (SessionList==TheSession) {
261                         SessionList = SessionList->next;
262                         }
263                 else {
264                         for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
265                                 if (sptr->next == TheSession) {
266                                         sptr->next = TheSession->next;
267                                         }
268                                 }
269                         }
270
271                 close(TheSession->inpipe[1]);
272                 close(TheSession->outpipe[0]);
273                 unlock_session(TheSession);
274                 free(TheSession);
275         
276                 pthread_mutex_unlock(&MasterCritter);
277                 }
278
279         free(req);
280
281         /*
282          * The thread handling this HTTP connection is now finished.
283          */
284         return NULL;
285         }