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