]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
Fixed the locks, though I'm not sure I did it correctly.
[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 int GenerateSessionID() {
42         return getpid();
43         }
44
45
46 void gets0(int fd, char buf[]) {
47
48         buf[0] = 0;
49         do {
50                 buf[strlen(buf)+1] = 0;
51                 read(fd, &buf[strlen(buf)], 1);
52                 } while (buf[strlen(buf)-1] >= 32);
53         buf[strlen(buf)-1] = 0;
54         }
55
56 /*
57  * Collapse multiple cookies on one line
58  */
59 void req_gets(int sock, char *buf, char *hold) {
60         int a;
61
62         if (strlen(hold)==0) {
63                 client_gets(sock, buf);
64                 }
65         else {
66                 strcpy(buf, hold);
67                 }
68         strcpy(hold, "");
69
70         if (!strncasecmp(buf, "Cookie: ", 8)) {
71                 for (a=0; a<strlen(buf); ++a) if (buf[a]==';') {
72                         sprintf(hold, "Cookie: %s", &buf[a+1]);
73                         buf[a]=0;
74                         while (isspace(hold[8])) strcpy(&hold[8], &hold[9]);
75                         return;
76                         }
77                 }
78         }
79
80
81 /*
82  * This loop gets called once for every HTTP connection made to WebCit.
83  */
84 void *context_loop(int sock) {
85         char req[256][256];
86         char buf[256], hold[256];
87         int num_lines = 0;
88         int a;
89         int f;
90         int desired_session = 0;
91         char str_session[256];
92         struct wc_session *sptr;
93         struct wc_session *TheSession;
94         int ContentLength;
95
96         printf("Reading request from socket %d\n", sock);
97
98         /*
99          * Find out what it is that the web browser is asking for
100          */
101         ContentLength = 0;
102         do {
103                 req_gets(sock, buf, hold);
104                 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
105                         desired_session = atoi(&buf[19]);
106                         }
107                 if (!strncasecmp(buf, "Content-length: ", 16)) {
108                         ContentLength = atoi(&buf[16]);
109                         }
110                 strcpy(&req[num_lines++][0], buf);
111                 } while(strlen(buf)>0);
112
113         /*
114          * See if there's an existing session open with the desired ID
115          */
116         TheSession = NULL;
117         if (desired_session != 0) {
118                 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
119                         if (sptr->session_id == desired_session) {
120                                 TheSession = sptr;
121                                 }
122                         }
123                 }
124
125         /*
126          * Create a new session if we have to
127          */
128         if (TheSession == NULL) {
129                 printf("Creating a new session\n");
130                 TheSession = (struct wc_session *)
131                         malloc(sizeof(struct wc_session));
132                 TheSession->session_id = GenerateSessionID();
133                 pipe(TheSession->inpipe);
134                 pipe(TheSession->outpipe);
135                 pthread_mutex_init(&TheSession->critter, NULL);
136                 TheSession->next = SessionList;
137                 SessionList = TheSession;
138                 sprintf(str_session, "%d", TheSession->session_id);
139                 f = fork();
140                 fflush(stdout); fflush(stdin);
141                 if (f==0) {
142                         dup2(TheSession->inpipe[0], 0);
143                         dup2(TheSession->outpipe[1], 1);
144                         execlp("./webcit", "webcit", str_session, NULL);
145                         printf("HTTP/1.0 404 WebCit Failure\n\n");
146                         printf("Server: %s\n", SERVER);
147                         printf("Content-type: text/html\n");
148                         printf("Content-length: 76\n");
149                         printf("\n");
150                         printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
151                         printf("<BODY>execlp() failed</BODY></HTML>\n");
152                         exit(0);
153                         }
154                 }
155
156         /*
157          * Grab a lock on the session, so other threads don't try to access
158          * the pipes at the same time.
159          */
160         printf("Locking session %d...\n", TheSession->session_id);
161         pthread_mutex_lock(&TheSession->critter);
162         printf("   ...got lock\n");
163
164         /* 
165          * Send the request to the appropriate session...
166          */
167         printf("   Writing %d lines of command\n", num_lines);
168         printf("%s\n", &req[0][0]);
169         for (a=0; a<num_lines; ++a) {
170                 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
171                 write(TheSession->inpipe[1], "\n", 1);
172                 }
173         printf("   Writing %d bytes of content\n", ContentLength);
174         while (ContentLength--) {
175                 read(sock, buf, 1);
176                 write(TheSession->inpipe[1], buf, 1);
177                 }
178
179         /*
180          * ...and get the response.
181          */
182         printf("   Reading response\n");
183         ContentLength = 0;
184         do {
185                 gets0(TheSession->outpipe[0], buf);
186                 write(sock, buf, strlen(buf));
187                 write(sock, "\n", 1);
188                 if (!strncasecmp(buf, "Content-length: ", 16))
189                         ContentLength = atoi(&buf[16]);
190                 } while (strlen(buf) > 0);
191
192         printf("   Reading %d bytes of content\n");
193         while(ContentLength--) {
194                 read(TheSession->outpipe[0], buf, 1);
195                 write(sock, buf, 1);
196                 }
197
198         /*
199          * Now our HTTP connection is done.  It would be relatively easy
200          * to support HTTP/1.1 "persistent" connections by looping back to
201          * the top of this function.  For now, we'll just close.
202          */
203         printf("   Closing socket\n");
204         close(sock);
205
206         /*
207          * Let go of the lock
208          */
209         printf("Unlocking.\n");
210         pthread_mutex_unlock(&TheSession->critter);
211
212         /*
213          * The thread handling this HTTP connection is now finished.
214          */
215         pthread_exit(NULL);
216         }