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.
12 #include "webserver.h"
14 /* Only one thread may manipulate SessionList at a time... */
15 pthread_mutex_t SessionListMutex;
17 struct wcsession *SessionList = NULL; /**< our sessions ????*/
19 pthread_key_t MyConKey; /**< TSD key for MySession() */
23 * free the memory used for viewing atachments
25 void free_attachments(struct wcsession *sess) {
26 struct wc_attachment *att;
28 while (sess->first_attachment != NULL) {
29 att = sess->first_attachment;
30 sess->first_attachment = sess->first_attachment->next;
37 void shutdown_sessions(void)
39 struct wcsession *sptr;
41 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
46 void do_housekeeping(void)
48 struct wcsession *sptr, *ss;
49 struct wcsession *sessions_to_kill = NULL;
51 static int num_threads = MIN_WORKER_THREADS;
54 * Lock the session list, moving any candidates for euthanasia into
57 pthread_mutex_lock(&SessionListMutex);
59 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
62 /** Kill idle sessions */
63 if ((time(NULL) - (sptr->lastreq)) >
64 (time_t) WEBCIT_TIMEOUT) {
68 /** Remove sessions flagged for kill */
71 /** remove session from linked list */
72 if (sptr == SessionList) {
73 SessionList = SessionList->next;
75 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
76 if (ss->next == sptr) {
77 ss->next = ss->next->next;
81 sptr->next = sessions_to_kill;
82 sessions_to_kill = sptr;
85 pthread_mutex_unlock(&SessionListMutex);
88 * Now free up and destroy the culled sessions.
90 while (sessions_to_kill != NULL) {
91 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
92 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
93 close(sessions_to_kill->serv_sock);
94 close(sessions_to_kill->chat_sock);
95 // if (sessions_to_kill->preferences != NULL) {
96 // free(sessions_to_kill->preferences);
98 if (sessions_to_kill->cache_fold != NULL) {
99 free(sessions_to_kill->cache_fold);
101 free_attachments(sessions_to_kill);
102 free_march_list(sessions_to_kill);
103 DeleteHash(&(sessions_to_kill->hash_prefs));
104 DeleteHash(&(sessions_to_kill->IconBarSetttings));
105 FreeStrBuf(&(sessions_to_kill->UrlFragment1));
106 FreeStrBuf(&(sessions_to_kill->UrlFragment2));
107 FreeStrBuf(&(sessions_to_kill->WBuf));
109 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
110 sptr = sessions_to_kill->next;
111 free(sessions_to_kill);
112 sessions_to_kill = sptr;
117 * If there are more sessions than threads, then we should spawn
118 * more threads ... up to a predefined maximum.
120 while ( (num_sessions > num_threads)
121 && (num_threads <= MAX_WORKER_THREADS) ) {
122 spawn_another_worker_thread();
124 lprintf(3, "There are %d sessions and %d threads active.\n",
125 num_sessions, num_threads);
131 * \brief Wake up occasionally and clean house
133 void housekeeping_loop(void)
136 sleeeeeeeeeep(HOUSEKEEPING);
143 * \brief Create a Session id
144 * Generate a unique WebCit session ID (which is not the same thing as the
145 * Citadel session ID).
147 * \todo FIXME ... ensure that session number is truly unique
150 int GenerateSessionID(void)
152 static int seq = (-1);
155 seq = (int) time(NULL);
163 * Collapse multiple cookies on one line
165 int req_gets(int sock, char *buf, char *hold, size_t hlen)
169 if (IsEmptyStr(hold)) {
171 a = client_getln(sock, buf, SIZ);
174 safestrncpy(buf, hold, SIZ);
178 if (!strncasecmp(buf, "Cookie: ", 8)) {
181 for (a = 0; a < len; ++a)
183 // we don't refresh len, because of we
184 // only exit from here.
185 snprintf(hold, hlen, "Cookie: %s", &buf[a + 1]);
188 while (isspace(hold[b]))
191 memmove(&hold[8], &hold[b], len - b + 1);
200 * lingering_close() a`la Apache. see
201 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
203 int lingering_close(int fd)
208 struct timeval tv, start;
210 gettimeofday(&start, NULL);
214 gettimeofday(&tv, NULL);
215 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
216 tv.tv_usec = start.tv_usec - tv.tv_usec;
217 if (tv.tv_usec < 0) {
219 tv.tv_usec += 1000000;
223 i = select(fd + 1, &set, NULL, NULL, &tv);
224 } while (i == -1 && errno == EINTR);
229 i = read(fd, buf, sizeof buf);
230 } while (i != 0 && (i != -1 || errno == EINTR));
238 * \brief sanity requests
239 * Check for bogus requests coming from brain-dead Windows boxes.
241 * \param http_cmd The HTTP request to check
243 int is_bogus(char *http_cmd) {
247 url = strstr(http_cmd, " ");
248 if (url == NULL) return(1);
251 char *bogus_prefixes[] = {
252 "/scripts/root.exe", /**< Worms and trojans and viruses, oh my! */
255 "/_vti", /**< Broken Microsoft DAV implementation */
256 "/MSOffice" /**< Stoopid MSOffice thinks everyone is IIS */
259 max = sizeof(bogus_prefixes) / sizeof(char *);
261 for (i=0; i<max; ++i) {
262 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
267 return(0); /* probably ok */
273 * \brief handle one request
274 * This loop gets called once for every HTTP connection made to WebCit. At
275 * this entry point we have an HTTP socket with a browser allegedly on the
276 * other end, but we have not yet bound to a WebCit session.
278 * The job of this function is to locate the correct session and bind to it,
279 * or create a session if necessary and bind to it, then run the WebCit
280 * transaction loop. Afterwards, we unbind from the session. When this
281 * function returns, the worker thread is then free to handle another
283 * \param sock the socket we will put our answer to
285 void context_loop(int sock)
287 struct httprequest *req = NULL;
288 struct httprequest *last = NULL;
289 struct httprequest *hptr;
290 char buf[SIZ], hold[SIZ];
291 int desired_session = 0;
294 struct wcsession *TheSession, *sptr;
295 char httpauth_string[1024];
296 char httpauth_user[1024];
297 char httpauth_pass[1024];
298 char accept_language[256];
300 int session_is_new = 0;
302 strcpy(httpauth_string, "");
303 strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
304 strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
307 * Find out what it is that the web browser is asking for
309 memset(hold, 0, sizeof(hold));
311 if (req_gets(sock, buf, hold, SIZ) < 0) return;
316 if (!strncasecmp(buf, "Accept-encoding:", 16)) {
317 if (strstr(&buf[16], "gzip")) {
323 * Browser-based sessions use cookies for session authentication
325 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
326 cookie_to_stuff(&buf[15], &desired_session,
327 NULL, 0, NULL, 0, NULL, 0);
332 * GroupDAV-based sessions use HTTP authentication
334 if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
335 CtdlDecodeBase64(httpauth_string, &buf[21], strlen(&buf[21]));
336 extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
337 extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
340 if (!strncasecmp(buf, "If-Modified-Since: ", 19)) {
341 if_modified_since = httpdate_to_timestamp(&buf[19]);
344 if (!strncasecmp(buf, "Accept-Language: ", 17)) {
345 safestrncpy(accept_language, &buf[17], sizeof accept_language);
349 * Read in the request
351 hptr = (struct httprequest *)
352 malloc(sizeof(struct httprequest));
360 safestrncpy(hptr->line, buf, sizeof hptr->line);
362 } while (!IsEmptyStr(buf));
365 * If the request is prefixed by "/webcit" then chop that off. This
366 * allows a front end web server to forward all /webcit requests to us
367 * while still using the same web server port for other things.
370 ptr = strstr(req->line, " /webcit "); /*< Handle "/webcit" */
372 strcpy(ptr+2, ptr+8);
375 ptr = strstr(req->line, " /webcit"); /*< Handle "/webcit/" */
377 strcpy(ptr+1, ptr+8);
380 /** Begin parsing the request. */
382 safestrncpy(buf, req->line, sizeof buf);
383 lprintf(5, "HTTP: %s\n", buf);
385 /** Check for bogus requests */
387 strcpy(req->line, "GET /404 HTTP/1.1");
388 strcpy(buf, "GET /404 HTTP/1.1");
392 * Strip out the method, leaving the URL up front...
394 remove_token(buf, 0, ' ');
395 if (buf[1]==' ') buf[1]=0;
398 * While we're at it, gracefully handle requests for the
399 * robots.txt and favicon.ico files.
401 if (!strncasecmp(buf, "/robots.txt", 11)) {
402 strcpy(req->line, "GET /static/robots.txt"
403 "?force_close_session=yes HTTP/1.1");
405 else if (!strncasecmp(buf, "/favicon.ico", 12)) {
406 strcpy(req->line, "GET /static/favicon.ico");
410 * These are the URL's which may be executed without a
411 * session cookie already set. If it's not one of these,
412 * force the session to close because cookies are
413 * probably disabled on the client browser.
415 else if ( (strcmp(buf, "/"))
416 && (strncasecmp(buf, "/listsub", 8))
417 && (strncasecmp(buf, "/freebusy", 9))
418 && (strncasecmp(buf, "/do_logout", 10))
419 && (strncasecmp(buf, "/groupdav", 9))
420 && (strncasecmp(buf, "/static", 7))
421 && (strncasecmp(buf, "/rss", 4))
422 && (strncasecmp(buf, "/404", 4))
423 && (got_cookie == 0)) {
424 strcpy(req->line, "GET /static/nocookies.html"
425 "?force_close_session=yes HTTP/1.1");
429 * See if there's an existing session open with the desired ID or user/pass
433 if (TheSession == NULL) {
434 pthread_mutex_lock(&SessionListMutex);
435 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
437 /** If HTTP-AUTH, look for a session with matching credentials */
438 if ( (!IsEmptyStr(httpauth_user))
439 &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
440 &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
444 /** If cookie-session, look for a session with matching session ID */
445 if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
450 pthread_mutex_unlock(&SessionListMutex);
454 * Create a new session if we have to
456 if (TheSession == NULL) {
457 lprintf(3, "Creating a new session\n");
458 TheSession = (struct wcsession *)
459 malloc(sizeof(struct wcsession));
460 memset(TheSession, 0, sizeof(struct wcsession));
461 TheSession->serv_sock = (-1);
462 TheSession->chat_sock = (-1);
464 /* If we're recreating a session that expired, it's best to give it the same
465 * session number that it had before. The client browser ought to pick up
466 * the new session number and start using it, but in some rare situations it
467 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
470 if (desired_session == 0) {
471 TheSession->wc_session = GenerateSessionID();
474 TheSession->wc_session = desired_session;
477 strcpy(TheSession->httpauth_user, httpauth_user);
478 strcpy(TheSession->httpauth_pass, httpauth_pass);
479 TheSession->hash_prefs = NewHash(1,NULL); /* Get a hash table for the user preferences */
480 pthread_mutex_init(&TheSession->SessionMutex, NULL);
481 pthread_mutex_lock(&SessionListMutex);
482 TheSession->urlstrings = NULL;
483 TheSession->vars = NULL;
484 TheSession->nonce = rand();
485 TheSession->next = SessionList;
486 SessionList = TheSession;
487 pthread_mutex_unlock(&SessionListMutex);
492 * A future improvement might be to check the session integrity
493 * at this point before continuing.
497 * Bind to the session and perform the transaction
499 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
500 pthread_setspecific(MyConKey, (void *)TheSession);
502 TheSession->urlstrings = NewHash(1,NULL);
503 TheSession->vars = NewHash(1,NULL);
504 TheSession->http_sock = sock;
505 TheSession->lastreq = time(NULL); /* log */
506 TheSession->gzip_ok = gzip_ok;
508 if (session_is_new) {
509 httplang_to_locale(accept_language);
511 go_selected_language(); /* set locale */
513 session_loop(req); /* do transaction */
515 stop_selected_language(); /* unset locale */
517 DeleteHash(&TheSession->urlstrings);
518 DeleteHash(&TheSession->vars);
520 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
522 /* Free the request buffer */
523 while (req != NULL) {
530 * Free up any session-local substitution variables which
531 * were set during this transaction