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