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, *ss, *session_to_kill;
53 session_to_kill = NULL;
54 pthread_mutex_lock(&SessionListMutex);
55 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
57 /* Kill idle sessions */
58 if ((time(NULL) - (sptr->lastreq)) >
59 (time_t) WEBCIT_TIMEOUT) {
63 /* Remove sessions flagged for kill */
66 /* remove session from linked list */
67 if (sptr == SessionList) {
68 SessionList = SessionList->next;
70 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
71 if (ss->next == sptr) {
72 ss->next = ss->next->next;
76 session_to_kill = sptr;
80 BREAKOUT: pthread_mutex_unlock(&SessionListMutex);
82 if (session_to_kill != NULL) {
83 pthread_mutex_lock(&session_to_kill->SessionMutex);
84 close(session_to_kill->serv_sock);
85 pthread_mutex_unlock(&session_to_kill->SessionMutex);
86 free(session_to_kill);
89 } while (session_to_kill != NULL);
94 * Wake up occasionally and clean house
96 void housekeeping_loop(void)
106 * Generate a unique WebCit session ID (which is not the same thing as the
107 * Citadel session ID).
109 * FIX ... ensure that session number is truly unique
112 int GenerateSessionID(void)
114 static int seq = (-1);
117 seq = (int) time(NULL);
125 * Collapse multiple cookies on one line
127 int req_gets(int sock, char *buf, char *hold)
131 if (strlen(hold) == 0) {
133 a = client_gets(sock, buf);
140 if (!strncasecmp(buf, "Cookie: ", 8)) {
141 for (a = 0; a < strlen(buf); ++a)
143 sprintf(hold, "Cookie: %s", &buf[a + 1]);
145 while (isspace(hold[8]))
146 strcpy(&hold[8], &hold[9]);
154 * lingering_close() a`la Apache. see
155 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
158 static int lingering_close(int fd)
163 struct timeval tv, start;
165 gettimeofday(&start, NULL);
169 gettimeofday(&tv, NULL);
170 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
171 tv.tv_usec = start.tv_usec - tv.tv_usec;
172 if (tv.tv_usec < 0) {
174 tv.tv_usec += 1000000;
178 i = select(fd + 1, &set, NULL, NULL, &tv);
179 } while (i == -1 && errno == EINTR);
184 i = read(fd, buf, sizeof buf);
185 } while (i != 0 && (i != -1 || errno == EINTR));
194 * This loop gets called once for every HTTP connection made to WebCit. At
195 * this entry point we have an HTTP socket with a browser allegedly on the
196 * other end, but we have not yet bound to a WebCit session.
198 * The job of this function is to locate the correct session and bind to it,
199 * or create a session if necessary and bind to it, then run the WebCit
200 * transaction loop. Afterwards, we unbind from the session. When this
201 * function returns, the worker thread is then free to handle another
204 void context_loop(int sock)
206 struct httprequest *req = NULL;
207 struct httprequest *last = NULL;
208 struct httprequest *hptr;
209 char buf[256], hold[256];
210 int desired_session = 0;
212 struct wcsession *TheSession, *sptr;
215 * Find out what it is that the web browser is asking for
217 memset(hold, 0, sizeof(hold));
219 if (req_gets(sock, buf, hold) < 0) return;
220 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
221 cookie_to_stuff(&buf[15], &desired_session,
226 hptr = (struct httprequest *)
227 malloc(sizeof(struct httprequest));
235 strcpy(hptr->line, buf);
237 } while (strlen(buf) > 0);
241 * If requesting a non-root page, there should already be a cookie
242 * set. If there isn't, the client browser has cookies turned off
243 * (or doesn't support them) and we have to barf & bail.
245 strcpy(buf, req->line);
246 fprintf(stderr, "%s\n", buf);
247 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
248 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
249 if (buf[1]==' ') buf[1]=0;
252 * While we're at it, gracefully handle requests for the
255 if (!strncasecmp(buf, "/robots.txt", 11)) {
256 strcpy(req->line, "GET /static/robots.txt HTTP/1.0");
259 /* Do the non-root-cookie check now. */
260 else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
261 strcpy(req->line, "GET /static/nocookies.html HTTP/1.0");
267 * See if there's an existing session open with the desired ID
270 if (desired_session != 0) {
271 pthread_mutex_lock(&SessionListMutex);
272 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
273 if (sptr->wc_session == desired_session) {
277 pthread_mutex_unlock(&SessionListMutex);
281 * Create a new session if we have to
283 if (TheSession == NULL) {
284 fprintf(stderr, "Creating a new session\n");
285 TheSession = (struct wcsession *)
286 malloc(sizeof(struct wcsession));
287 memset(TheSession, 0, sizeof(struct wcsession));
288 TheSession->wc_session = GenerateSessionID();
289 pthread_mutex_init(&TheSession->SessionMutex, NULL);
291 pthread_mutex_lock(&SessionListMutex);
292 TheSession->next = SessionList;
293 SessionList = TheSession;
294 pthread_mutex_unlock(&SessionListMutex);
300 * FIX ... check session integrity here before continuing
307 * Bind to the session and perform the transaction
309 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
310 pthread_setspecific(MyConKey, (void *)TheSession);
311 TheSession->http_sock = sock;
312 TheSession->lastreq = time(NULL); /* log */
313 session_loop(req); /* perform the requested transaction */
314 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
316 /* Free the request buffer */
317 while (req != NULL) {
324 * Now our HTTP connection is done. Close the socket and exit this
325 * function, so the worker thread can handle a new HTTP connection.
327 lingering_close(sock);