4 * This is the other half of the webserver. It handles the task of hooking
5 * up HTTP requests with the sessions 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 started.
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 lprintf(3, "Destroying session\n");
68 /* remove session from linked list */
69 if (sptr == SessionList) {
70 SessionList = SessionList->next;
72 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
73 if (ss->next == sptr) {
74 ss->next = ss->next->next;
78 session_to_kill = sptr;
82 BREAKOUT: pthread_mutex_unlock(&SessionListMutex);
84 if (session_to_kill != NULL) {
85 pthread_mutex_lock(&session_to_kill->SessionMutex);
86 close(session_to_kill->serv_sock);
87 if (session_to_kill->preferences != NULL) {
88 free(session_to_kill->preferences);
90 pthread_mutex_unlock(&session_to_kill->SessionMutex);
91 free(session_to_kill);
94 } while (session_to_kill != NULL);
99 * Wake up occasionally and clean house
101 void housekeeping_loop(void)
111 * Generate a unique WebCit session ID (which is not the same thing as the
112 * Citadel session ID).
114 * FIX ... ensure that session number is truly unique
117 int GenerateSessionID(void)
119 static int seq = (-1);
122 seq = (int) time(NULL);
130 * Collapse multiple cookies on one line
132 int req_gets(int sock, char *buf, char *hold)
136 if (strlen(hold) == 0) {
138 a = client_gets(sock, buf);
145 if (!strncasecmp(buf, "Cookie: ", 8)) {
146 for (a = 0; a < strlen(buf); ++a)
148 sprintf(hold, "Cookie: %s", &buf[a + 1]);
150 while (isspace(hold[8]))
151 strcpy(&hold[8], &hold[9]);
159 * lingering_close() a`la Apache. see
160 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
163 int lingering_close(int fd)
168 struct timeval tv, start;
170 gettimeofday(&start, NULL);
174 gettimeofday(&tv, NULL);
175 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
176 tv.tv_usec = start.tv_usec - tv.tv_usec;
177 if (tv.tv_usec < 0) {
179 tv.tv_usec += 1000000;
183 i = select(fd + 1, &set, NULL, NULL, &tv);
184 } while (i == -1 && errno == EINTR);
189 i = read(fd, buf, sizeof buf);
190 } while (i != 0 && (i != -1 || errno == EINTR));
198 * Check for bogus requests coming from (for example) brain-dead
199 * Windoze boxes that are infected with the latest worm-of-the-week.
200 * If we detect one of these, bail out without bothering our Citadel
203 int is_bogus(char *http_cmd) {
205 if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
206 if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
207 if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
209 return(0); /* probably ok */
215 * This loop gets called once for every HTTP connection made to WebCit. At
216 * this entry point we have an HTTP socket with a browser allegedly on the
217 * other end, but we have not yet bound to a WebCit session.
219 * The job of this function is to locate the correct session and bind to it,
220 * or create a session if necessary and bind to it, then run the WebCit
221 * transaction loop. Afterwards, we unbind from the session. When this
222 * function returns, the worker thread is then free to handle another
225 void context_loop(int sock)
227 struct httprequest *req = NULL;
228 struct httprequest *last = NULL;
229 struct httprequest *hptr;
230 char buf[SIZ], hold[SIZ];
231 int desired_session = 0;
233 struct wcsession *TheSession, *sptr;
236 * Find out what it is that the web browser is asking for
238 memset(hold, 0, sizeof(hold));
240 if (req_gets(sock, buf, hold) < 0) return;
241 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
242 cookie_to_stuff(&buf[15], &desired_session,
247 hptr = (struct httprequest *)
248 malloc(sizeof(struct httprequest));
256 strcpy(hptr->line, buf);
258 } while (strlen(buf) > 0);
260 strcpy(buf, req->line);
261 lprintf(5, "HTTP: %s\n", buf);
263 /* Check for bogus requests */
264 if (is_bogus(buf)) goto bail;
267 * If requesting a non-root page, there should already be a cookie
268 * set. If there isn't, the client browser has cookies turned off
269 * (or doesn't support them) and we have to barf & bail.
271 if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
272 else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
273 if (buf[1]==' ') buf[1]=0;
276 * While we're at it, gracefully handle requests for the
279 if (!strncasecmp(buf, "/robots.txt", 11)) {
280 strcpy(req->line, "GET /static/robots.txt"
281 "?force_close_session=yes HTTP/1.0");
284 /* Do the non-root-cookie check now. */
285 else if ( (strcmp(buf, "/"))
286 && (strncasecmp(buf, "/listsub", 8))
287 && (got_cookie == 0)) {
288 strcpy(req->line, "GET /static/nocookies.html"
289 "?force_close_session=yes HTTP/1.0");
294 * See if there's an existing session open with the desired ID
297 if (desired_session != 0) {
298 pthread_mutex_lock(&SessionListMutex);
299 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
300 if (sptr->wc_session == desired_session) {
304 pthread_mutex_unlock(&SessionListMutex);
308 * Create a new session if we have to
310 if (TheSession == NULL) {
311 lprintf(3, "Creating a new session\n");
312 TheSession = (struct wcsession *)
313 malloc(sizeof(struct wcsession));
314 memset(TheSession, 0, sizeof(struct wcsession));
315 TheSession->wc_session = GenerateSessionID();
316 pthread_mutex_init(&TheSession->SessionMutex, NULL);
318 pthread_mutex_lock(&SessionListMutex);
319 TheSession->next = SessionList;
320 SessionList = TheSession;
321 pthread_mutex_unlock(&SessionListMutex);
325 * A future improvement might be to check the session integrity
326 * at this point before continuing.
330 * Bind to the session and perform the transaction
332 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
333 pthread_setspecific(MyConKey, (void *)TheSession);
334 TheSession->http_sock = sock;
335 TheSession->lastreq = time(NULL); /* log */
336 session_loop(req); /* perform the requested transaction */
337 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
339 /* Free the request buffer */
340 bail: while (req != NULL) {