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.
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #ifdef HAVE_SYS_TIME_H
31 #include <netinet/in.h>
39 #include "webserver.h"
41 /* Only one thread may manipulate SessionList at a time... */
42 pthread_mutex_t SessionListMutex;
44 struct wcsession *SessionList = NULL;
46 pthread_key_t MyConKey; /* TSD key for MySession() */
48 void do_housekeeping(void)
50 struct wcsession *sptr;
52 pthread_mutex_lock(&SessionListMutex);
54 /* Kill idle sessions */
55 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
56 if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
57 /* FIX do something here */
61 pthread_mutex_unlock(&SessionListMutex);
66 * Wake up occasionally and clean house
68 void housekeeping_loop(void)
78 * Generate a unique WebCit session ID (which is not the same thing as the
79 * Citadel session ID).
81 * FIX ... we really should check to make sure we're generating a truly
82 * unique session ID by traversing the SessionList.
85 int GenerateSessionID(void)
87 static int seq = (-1);
90 seq = (int) time(NULL);
97 void gets0(int fd, char buf[])
102 buf[strlen(buf) + 1] = 0;
103 read(fd, &buf[strlen(buf)], 1);
104 } while (buf[strlen(buf) - 1] >= 32);
105 buf[strlen(buf) - 1] = 0;
109 * Collapse multiple cookies on one line
111 void req_gets(int sock, char *buf, char *hold)
115 if (strlen(hold) == 0) {
116 client_gets(sock, buf);
122 if (!strncasecmp(buf, "Cookie: ", 8)) {
123 for (a = 0; a < strlen(buf); ++a)
125 sprintf(hold, "Cookie: %s", &buf[a + 1]);
127 while (isspace(hold[8]))
128 strcpy(&hold[8], &hold[9]);
135 * lingering_close() a`la Apache. see
136 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
139 static int lingering_close(int fd)
144 struct timeval tv, start;
146 gettimeofday(&start, NULL);
150 gettimeofday(&tv, NULL);
151 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
152 tv.tv_usec = start.tv_usec - tv.tv_usec;
153 if (tv.tv_usec < 0) {
155 tv.tv_usec += 1000000;
159 i = select(fd + 1, &set, NULL, NULL, &tv);
160 } while (i == -1 && errno == EINTR);
165 i = read(fd, buf, sizeof buf);
166 } while (i != 0 && (i != -1 || errno == EINTR));
175 * This loop gets called once for every HTTP connection made to WebCit. At
176 * this entry point we have an HTTP socket with a browser allegedly on the
177 * other end, but we have not yet bound to a WebCit session.
179 * The job of this function is to locate the correct session and bind to it,
180 * or create a session if necessary and bind to it, then run the WebCit
181 * transaction loop. Afterwards, we unbind from the session. When this
182 * function returns, the worker thread is then free to handle another
185 void context_loop(int sock)
187 struct httprequest *req = NULL;
188 struct httprequest *last = NULL;
189 struct httprequest *hptr;
190 char buf[256], hold[256];
191 int desired_session = 0;
193 struct wcsession *TheSession, *sptr;
194 int CloseSession = 0;
197 fprintf(stderr, "Reading request from socket %d\n", sock);
200 * Find out what it is that the web browser is asking for
203 req_gets(sock, buf, hold);
204 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
205 cookie_to_stuff(&buf[15], &desired_session,
210 hptr = (struct httprequest *)
211 malloc(sizeof(struct httprequest));
219 strcpy(hptr->line, buf);
221 } while (strlen(buf) > 0);
225 * If requesting a non-root page, there should already be a cookie
226 * set. If there isn't, the client browser has cookies turned off
227 * (or doesn't support them) and we have to barf & bail.
229 strcpy(buf, req->line);
230 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
231 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
232 if (buf[1]==' ') buf[1]=0;
235 * While we're at it, gracefully handle requests for the
238 if (!strncasecmp(buf, "/robots.txt", 11)) {
239 strcpy(req->line, "GET /static/robots.txt HTTP/1.0");
242 /* Do the non-root-cookie check now. */
243 else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
244 strcpy(req->line, "GET /static/nocookies.html HTTP/1.0");
250 * See if there's an existing session open with the desired ID
253 if (desired_session != 0) {
254 pthread_mutex_lock(&SessionListMutex);
255 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
256 if (sptr->wc_session == desired_session) {
260 pthread_mutex_unlock(&SessionListMutex);
264 * Create a new session if we have to
266 if (TheSession == NULL) {
267 printf("Creating a new session\n");
268 TheSession = (struct wcsession *)
269 malloc(sizeof(struct wcsession));
270 memset(TheSession, 0, sizeof(struct wcsession));
271 TheSession->wc_session = GenerateSessionID();
272 pthread_mutex_init(&TheSession->SessionMutex, NULL);
274 pthread_mutex_lock(&SessionListMutex);
275 TheSession->next = SessionList;
276 SessionList = TheSession;
277 pthread_mutex_unlock(&SessionListMutex);
283 * FIX ... check session integrity here before continuing
290 * Bind to the session
292 pthread_mutex_lock(&TheSession->SessionMutex);
293 pthread_setspecific(MyConKey, (void *)TheSession);
294 TheSession->lastreq = time(NULL);
295 TheSession->http_sock = sock;
298 * Perform the WebCit transaction
300 fprintf(stderr, "Transaction: %s\n", req->line);
302 fprintf(stderr, "Returned from transaction loop\n");
305 * If the last response included a "close session" directive,
306 * remove the context now.
309 /* FIX remove_session(TheSession, 1); */
312 pthread_mutex_unlock(&TheSession->SessionMutex);
315 /* Free the request buffer */
316 while (req != NULL) {
323 * Now our HTTP connection is done. Close the socket and exit this
324 * function, so the worker thread can handle a new HTTP connection.
326 printf(" Closing socket %d ... ret=%d\n", sock,
327 lingering_close(sock));