]> code.citadel.org Git - citadel.git/blob - webcit/context_loop.c
Initial revision
[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
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.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         };
37
38 struct wc_session *SessionList = NULL;
39
40 int GenerateSessionID() {
41         return getpid();
42         }
43
44
45 void gets0(int fd, char buf[]) {
46
47         buf[0] = 0;
48         do {
49                 buf[strlen(buf)+1] = 0;
50                 read(fd, &buf[strlen(buf)], 1);
51                 } while (buf[strlen(buf)-1] >= 32);
52         buf[strlen(buf)-1] = 0;
53         }
54
55
56 /*
57  * This loop gets called once for every HTTP connection made to WebCit.
58  */
59 void *context_loop(int *socknumptr) {
60         char req[256][256];
61         char buf[256];
62         int num_lines = 0;
63         int a;
64         int f;
65         int sock;
66         int desired_session = 0;
67         char str_session[256];
68         struct wc_session *sptr;
69         struct wc_session *TheSession;
70         int ContentLength;
71
72         sock = *socknumptr;
73
74         /*
75          * Find out what it is that the web browser is asking for
76          */
77         do {
78                 client_gets(sock, buf);
79                 if (!strncasecmp(buf, "Cookie: wc_session=", 19)) {
80                         desired_session = atoi(&buf[19]);
81                         }
82
83
84                 strcpy(&req[num_lines++][0], buf);
85                 } while(strlen(buf)>0);
86
87         /*
88          * See if there's an existing session open with the desired ID
89          */
90         TheSession = NULL;
91         if (desired_session != 0) {
92                 for (sptr=SessionList; sptr!=NULL; sptr=sptr->next) {
93                         if (sptr->session_id == desired_session) {
94                                 TheSession = sptr;
95                                 }
96                         }
97                 }
98
99         /*
100          * Create a new session if we have to
101          */
102         if (TheSession == NULL) {
103                 printf("Creating a new session\n");
104                 TheSession = (struct wc_session *)
105                         malloc(sizeof(struct wc_session));
106                 TheSession->session_id = GenerateSessionID();
107                 pipe(TheSession->inpipe);
108                 pipe(TheSession->outpipe);
109                 TheSession->next = SessionList;
110                 SessionList = TheSession;
111                 sprintf(str_session, "%d", TheSession->session_id);
112                 f = fork();
113                 fflush(stdout); fflush(stdin);
114                 if (f==0) {
115                         dup2(TheSession->inpipe[0], 0);
116                         dup2(TheSession->outpipe[1], 1);
117                         execlp("./webcit", "webcit", str_session, NULL);
118                         printf("HTTP/1.0 404 WebCit Failure\n\n");
119                         printf("Content-type: text/html\n");
120                         printf("Content-length: 76\n");
121                         printf("\n");
122                         printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n");
123                         printf("<BODY>execlp() failed</BODY></HTML>\n");
124                         exit(0);
125                         }
126                 }
127
128         /* 
129          * Send the request to the appropriate session
130          */
131         for (a=0; a<num_lines; ++a) {
132                 write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
133                 write(TheSession->inpipe[1], "\n", 1);
134                 }
135         write(TheSession->inpipe[1], "\n", 1);
136
137         /*
138          * ...and get the response (FIX for non-text)
139          */
140         ContentLength = 0;
141         do {
142                 gets0(TheSession->outpipe[0], buf);
143                 write(sock, buf, strlen(buf));
144                 write(sock, "\n", 1);
145                 if (!strncasecmp(buf, "Content-length: ", 16))
146                         ContentLength = atoi(&buf[16]);
147                 } while (strlen(buf) > 0);
148
149         while(ContentLength--) {
150                 read(TheSession->outpipe[0], buf, 1);
151                 write(sock, buf, 1);
152                 }
153
154         /*
155          * Now our HTTP connection is done.  It would be relatively easy
156          * to support HTTP/1.1 "persistent" connections by looping back to
157          * the top of this function.  For now, we'll just exit.
158          */
159         close(sock);
160         pthread_exit(NULL);
161         }