5 * \defgroup WebServerII some of the webserver stuff.
6 * This is the other half of the webserver. It handles the task of hooking
7 * up HTTP requests with the sessions they belong to, using HTTP cookies to
8 * keep track of things. If the HTTP request doesn't belong to any currently
9 * active session, a new session is started.
10 * \ingroup WebcitHttpServer
15 #include "webserver.h"
17 /** Only one thread may manipulate SessionList at a time... */
18 pthread_mutex_t SessionListMutex;
20 struct wcsession *SessionList = NULL; /**< our sessions ????*/
22 pthread_key_t MyConKey; /**< TSD key for MySession() */
26 * \brief free the memory used for viewing atachments
27 * \param sess the session object to destroy
29 void free_attachments(struct wcsession *sess) {
30 struct wc_attachment *att;
32 while (sess->first_attachment != NULL) {
33 att = sess->first_attachment;
34 sess->first_attachment = sess->first_attachment->next;
43 void do_housekeeping(void)
45 struct wcsession *sptr, *ss;
46 struct wcsession *sessions_to_kill = NULL;
48 static int num_threads = MIN_WORKER_THREADS;
51 * Lock the session list, moving any candidates for euthanasia into
54 pthread_mutex_lock(&SessionListMutex);
56 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
59 /** Kill idle sessions */
60 if ((time(NULL) - (sptr->lastreq)) >
61 (time_t) WEBCIT_TIMEOUT) {
65 /** Remove sessions flagged for kill */
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 sptr->next = sessions_to_kill;
79 sessions_to_kill = sptr;
82 pthread_mutex_unlock(&SessionListMutex);
85 * Now free up and destroy the culled sessions.
87 while (sessions_to_kill != NULL) {
88 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
89 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
90 close(sessions_to_kill->serv_sock);
91 close(sessions_to_kill->chat_sock);
92 if (sessions_to_kill->preferences != NULL) {
93 free(sessions_to_kill->preferences);
95 if (sessions_to_kill->cache_fold != NULL) {
96 free(sessions_to_kill->cache_fold);
98 free_attachments(sessions_to_kill);
99 free_march_list(sessions_to_kill);
100 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
101 sptr = sessions_to_kill->next;
102 free(sessions_to_kill);
103 sessions_to_kill = sptr;
108 * If there are more sessions than threads, then we should spawn
109 * more threads ... up to a predefined maximum.
111 while ( (num_sessions > num_threads)
112 && (num_threads <= MAX_WORKER_THREADS) ) {
113 spawn_another_worker_thread();
115 lprintf(3, "There are %d sessions and %d threads active.\n",
116 num_sessions, num_threads);
122 * \brief Wake up occasionally and clean house
124 void housekeeping_loop(void)
127 sleeeeeeeeeep(HOUSEKEEPING);
134 * \brief Create a Session id
135 * Generate a unique WebCit session ID (which is not the same thing as the
136 * Citadel session ID).
138 * \todo FIXME ... ensure that session number is truly unique
141 int GenerateSessionID(void)
143 static int seq = (-1);
146 seq = (int) time(NULL);
154 * \brief Collapse multiple cookies on one line
155 * \param sock a socket?
156 * \param buf some bunch of chars?
157 * \param hold hold what?
159 int req_gets(int sock, char *buf, char *hold)
163 if (strlen(hold) == 0) {
165 a = client_getln(sock, buf, SIZ);
168 safestrncpy(buf, hold, SIZ);
172 if (!strncasecmp(buf, "Cookie: ", 8)) {
173 for (a = 0; a < strlen(buf); ++a)
175 sprintf(hold, "Cookie: %s", &buf[a + 1]);
177 while (isspace(hold[8]))
178 strcpy(&hold[8], &hold[9]);
187 * \brief close some fd for some reason???
188 * \param fd the fd to close??????
189 * lingering_close() a`la Apache. see
190 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
193 int lingering_close(int fd)
198 struct timeval tv, start;
200 gettimeofday(&start, NULL);
204 gettimeofday(&tv, NULL);
205 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
206 tv.tv_usec = start.tv_usec - tv.tv_usec;
207 if (tv.tv_usec < 0) {
209 tv.tv_usec += 1000000;
213 i = select(fd + 1, &set, NULL, NULL, &tv);
214 } while (i == -1 && errno == EINTR);
219 i = read(fd, buf, sizeof buf);
220 } while (i != 0 && (i != -1 || errno == EINTR));
228 * \brief sanity requests
229 * Check for bogus requests coming from brain-dead Windows boxes.
231 * \param http_cmd The HTTP request to check
233 int is_bogus(char *http_cmd) {
236 url = strstr(http_cmd, " ");
237 if (url == NULL) return(1);
240 /** Worms and trojans and viruses, oh my! */
241 if (!strncasecmp(url, "/scripts/root.exe", 17)) return(2);
242 if (!strncasecmp(url, "/c/winnt", 8)) return(2);
243 if (!strncasecmp(url, "/MSADC/", 7)) return(2);
245 /** Broken Microsoft DAV implementation */
246 if (!strncasecmp(url, "/_vti", 5)) return(3);
248 return(0); /* probably ok */
254 * \brief handle one request
255 * This loop gets called once for every HTTP connection made to WebCit. At
256 * this entry point we have an HTTP socket with a browser allegedly on the
257 * other end, but we have not yet bound to a WebCit session.
259 * The job of this function is to locate the correct session and bind to it,
260 * or create a session if necessary and bind to it, then run the WebCit
261 * transaction loop. Afterwards, we unbind from the session. When this
262 * function returns, the worker thread is then free to handle another
264 * \param sock the socket we will put our answer to
266 void context_loop(int sock)
268 struct httprequest *req = NULL;
269 struct httprequest *last = NULL;
270 struct httprequest *hptr;
271 char buf[SIZ], hold[SIZ];
272 int desired_session = 0;
275 struct wcsession *TheSession, *sptr;
276 char httpauth_string[1024];
277 char httpauth_user[1024];
278 char httpauth_pass[1024];
279 char accept_language[256];
281 int session_is_new = 0;
283 strcpy(httpauth_string, "");
284 strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
285 strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
288 * Find out what it is that the web browser is asking for
290 memset(hold, 0, sizeof(hold));
292 if (req_gets(sock, buf, hold) < 0) return;
297 if (!strncasecmp(buf, "Accept-encoding:", 16)) {
298 if (strstr(&buf[16], "gzip")) {
304 * Browser-based sessions use cookies for session authentication
306 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
307 cookie_to_stuff(&buf[15], &desired_session,
308 NULL, 0, NULL, 0, NULL, 0);
313 * GroupDAV-based sessions use HTTP authentication
315 if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
316 CtdlDecodeBase64(httpauth_string, &buf[21], strlen(&buf[21]));
317 extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
318 extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
321 if (!strncasecmp(buf, "If-Modified-Since: ", 19)) {
322 if_modified_since = httpdate_to_timestamp(&buf[19]);
325 if (!strncasecmp(buf, "Accept-Language: ", 17)) {
326 safestrncpy(accept_language, &buf[17], sizeof accept_language);
330 * Read in the request
332 hptr = (struct httprequest *)
333 malloc(sizeof(struct httprequest));
341 safestrncpy(hptr->line, buf, sizeof hptr->line);
343 } while (strlen(buf) > 0);
346 * If the request is prefixed by "/webcit" then chop that off. This
347 * allows a front end web server to forward all /webcit requests to us
348 * while still using the same web server port for other things.
351 ptr = strstr(req->line, " /webcit "); /*< Handle "/webcit" */
353 strcpy(ptr+2, ptr+8);
356 ptr = strstr(req->line, " /webcit"); /*< Handle "/webcit/" */
358 strcpy(ptr+1, ptr+8);
361 /** Begin parsing the request. */
363 safestrncpy(buf, req->line, sizeof buf);
364 lprintf(5, "HTTP: %s\n", buf);
366 /** Check for bogus requests */
368 strcpy(req->line, "GET /404 HTTP/1.1");
369 strcpy(buf, "GET /404 HTTP/1.1");
373 * Strip out the method, leaving the URL up front...
375 remove_token(buf, 0, ' ');
376 if (buf[1]==' ') buf[1]=0;
379 * While we're at it, gracefully handle requests for the
380 * robots.txt and favicon.ico files.
382 if (!strncasecmp(buf, "/robots.txt", 11)) {
383 strcpy(req->line, "GET /static/robots.txt"
384 "?force_close_session=yes HTTP/1.1");
386 else if (!strncasecmp(buf, "/favicon.ico", 12)) {
387 strcpy(req->line, "GET /static/favicon.ico");
391 * These are the URL's which may be executed without a
392 * session cookie already set. If it's not one of these,
393 * force the session to close because cookies are
394 * probably disabled on the client browser.
396 else if ( (strcmp(buf, "/"))
397 && (strncasecmp(buf, "/listsub", 8))
398 && (strncasecmp(buf, "/freebusy", 9))
399 && (strncasecmp(buf, "/do_logout", 10))
400 && (strncasecmp(buf, "/groupdav", 9))
401 && (strncasecmp(buf, "/static", 7))
402 && (strncasecmp(buf, "/rss", 4))
403 && (strncasecmp(buf, "/404", 4))
404 && (got_cookie == 0)) {
405 strcpy(req->line, "GET /static/nocookies.html"
406 "?force_close_session=yes HTTP/1.1");
410 * See if there's an existing session open with the desired ID or user/pass
414 if (TheSession == NULL) {
415 pthread_mutex_lock(&SessionListMutex);
416 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
418 /** If HTTP-AUTH, look for a session with matching credentials */
419 if ( (strlen(httpauth_user) > 0)
420 &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
421 &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
425 /** If cookie-session, look for a session with matching session ID */
426 if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
431 pthread_mutex_unlock(&SessionListMutex);
435 * Create a new session if we have to
437 if (TheSession == NULL) {
438 lprintf(3, "Creating a new session\n");
439 TheSession = (struct wcsession *)
440 malloc(sizeof(struct wcsession));
441 memset(TheSession, 0, sizeof(struct wcsession));
442 TheSession->serv_sock = (-1);
443 TheSession->chat_sock = (-1);
444 TheSession->wc_session = GenerateSessionID();
445 strcpy(TheSession->httpauth_user, httpauth_user);
446 strcpy(TheSession->httpauth_pass, httpauth_pass);
447 pthread_mutex_init(&TheSession->SessionMutex, NULL);
448 pthread_mutex_lock(&SessionListMutex);
449 TheSession->next = SessionList;
450 SessionList = TheSession;
451 pthread_mutex_unlock(&SessionListMutex);
456 * A future improvement might be to check the session integrity
457 * at this point before continuing.
461 * Bind to the session and perform the transaction
463 pthread_mutex_lock(&TheSession->SessionMutex); /*< bind */
464 pthread_setspecific(MyConKey, (void *)TheSession);
465 TheSession->http_sock = sock;
466 TheSession->lastreq = time(NULL); /*< log */
467 TheSession->gzip_ok = gzip_ok;
469 if (session_is_new) {
470 httplang_to_locale(accept_language);
472 go_selected_language(); /*< set locale */
474 session_loop(req); /*< do transaction */
476 stop_selected_language(); /*< unset locale */
478 pthread_mutex_unlock(&TheSession->SessionMutex); /*< unbind */
480 /** Free the request buffer */
481 while (req != NULL) {
488 * Free up any session-local substitution variables which
489 * were set during this transaction
491 clear_local_substs();