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