]> code.citadel.org Git - citadel.git/blobdiff - webcit/context_loop.c
* Finished gzip compression of dynamic pages (when browser supports it)
[citadel.git] / webcit / context_loop.c
index fce94fb86c928463a84e5408474e5b98f2943f03..3a9a29178d9dc1fdc604139a72d738c889fdeb5e 100644 (file)
@@ -2,9 +2,9 @@
  * context_loop.c
  *
  * This is the other half of the webserver.  It handles the task of hooking
- * up HTTP requests with the session they belong to, using HTTP cookies to
+ * up HTTP requests with the sessions they belong to, using HTTP cookies to
  * keep track of things.  If the HTTP request doesn't belong to any currently
- * active session, a new session is spawned.
+ * active session, a new session is started.
  *
  * $Id$
  */
@@ -47,7 +47,7 @@ pthread_key_t MyConKey;                         /* TSD key for MySession() */
 
 void do_housekeeping(void)
 {
-       struct wcsession *sptr, *session_to_kill;
+       struct wcsession *sptr, *ss, *session_to_kill;
 
        do {
                session_to_kill = NULL;
@@ -62,7 +62,19 @@ void do_housekeeping(void)
 
                        /* Remove sessions flagged for kill */
                        if (sptr->killthis) {
-                               remove session from linked list
+
+                               lprintf(3, "Destroying session\n");
+
+                               /* remove session from linked list */
+                               if (sptr == SessionList) {
+                                       SessionList = SessionList->next;
+                               }
+                               else for (ss=SessionList;ss!=NULL;ss=ss->next) {
+                                       if (ss->next == sptr) {
+                                               ss->next = ss->next->next;
+                                       }
+                               }
+
                                session_to_kill = sptr;
                                goto BREAKOUT;
                        }
@@ -72,6 +84,9 @@ BREAKOUT:     pthread_mutex_unlock(&SessionListMutex);
                if (session_to_kill != NULL) {
                        pthread_mutex_lock(&session_to_kill->SessionMutex);
                        close(session_to_kill->serv_sock);
+                       if (session_to_kill->preferences != NULL) {
+                               free(session_to_kill->preferences);
+                       }
                        pthread_mutex_unlock(&session_to_kill->SessionMutex);
                        free(session_to_kill);
                }
@@ -111,26 +126,17 @@ int GenerateSessionID(void)
 }
 
 
-void gets0(int fd, char buf[])
-{
-
-       buf[0] = 0;
-       do {
-               buf[strlen(buf) + 1] = 0;
-               read(fd, &buf[strlen(buf)], 1);
-       } while (buf[strlen(buf) - 1] >= 32);
-       buf[strlen(buf) - 1] = 0;
-}
-
 /*
  * Collapse multiple cookies on one line
  */
-void req_gets(int sock, char *buf, char *hold)
+int req_gets(int sock, char *buf, char *hold)
 {
        int a;
 
        if (strlen(hold) == 0) {
-               client_gets(sock, buf);
+               strcpy(buf, "");
+               a = client_gets(sock, buf);
+               if (a<1) return(-1);
        } else {
                strcpy(buf, hold);
        }
@@ -143,9 +149,11 @@ void req_gets(int sock, char *buf, char *hold)
                                buf[a] = 0;
                                while (isspace(hold[8]))
                                        strcpy(&hold[8], &hold[9]);
-                               return;
+                               return(0);
                        }
        }
+
+       return(0);
 }
 
 /*
@@ -153,9 +161,9 @@ void req_gets(int sock, char *buf, char *hold)
  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
  */
 
-static int lingering_close(int fd)
+int lingering_close(int fd)
 {
-       char buf[256];
+       char buf[SIZ];
        int i;
        fd_set set;
        struct timeval tv, start;
@@ -187,6 +195,22 @@ static int lingering_close(int fd)
 
 
 
+/*
+ * Check for bogus requests coming from (for example) brain-dead
+ * Windoze boxes that are infected with the latest worm-of-the-week.
+ * If we detect one of these, bail out without bothering our Citadel
+ * server.
+ */
+int is_bogus(char *http_cmd) {
+
+       if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
+       if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
+       if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
+
+       return(0);      /* probably ok */
+}
+
+
 
 /*
  * This loop gets called once for every HTTP connection made to WebCit.  At
@@ -204,24 +228,40 @@ void context_loop(int sock)
        struct httprequest *req = NULL;
        struct httprequest *last = NULL;
        struct httprequest *hptr;
-       char buf[256], hold[256];
+       char buf[SIZ], hold[SIZ];
        int desired_session = 0;
        int got_cookie = 0;
        struct wcsession *TheSession, *sptr;
-
-       fprintf(stderr, "Reading request from socket %d\n", sock);
+       char enc[SIZ];
+       char encodings[SIZ];
+       int gzip = 0;
+       int i;
 
        /*
         * Find out what it is that the web browser is asking for
         */
+       memset(hold, 0, sizeof(hold));
        do {
-               req_gets(sock, buf, hold);
+               if (req_gets(sock, buf, hold) < 0) return;
+
                if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
                        cookie_to_stuff(&buf[15], &desired_session,
                                NULL, NULL, NULL);
                        got_cookie = 1;
                }
 
+#ifdef WITH_ZLIB
+               if (!strncasecmp(buf, "Accept-encoding: ", 17)) {
+                       extract_token(encodings, &buf[17], 0, ';');
+                       for (i=0; i<num_tokens(encodings, ','); ++i) {
+                               extract_token(enc, encodings, i, ',');
+                               if (!strcasecmp(enc, "gzip")) {
+                                       gzip = 1;
+                               }
+                       }
+               }
+#endif
+
                hptr = (struct httprequest *)
                        malloc(sizeof(struct httprequest));
                if (req == NULL)
@@ -235,15 +275,20 @@ void context_loop(int sock)
 
        } while (strlen(buf) > 0);
 
+       strcpy(buf, req->line);
+       lprintf(5, "HTTP: %s\n", buf);
+
+       /* Check for bogus requests */
+       if (is_bogus(buf)) goto bail;
 
        /*
         * If requesting a non-root page, there should already be a cookie
         * set.  If there isn't, the client browser has cookies turned off
         * (or doesn't support them) and we have to barf & bail.
         */
-       strcpy(buf, req->line);
        if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
        else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
+       else if (!strncasecmp(buf, "POST ", 5)) strcpy(buf, &buf[5]);
        if (buf[1]==' ') buf[1]=0;
 
        /*
@@ -251,16 +296,19 @@ void context_loop(int sock)
         * robots.txt file...
         */
        if (!strncasecmp(buf, "/robots.txt", 11)) {
-               strcpy(req->line, "GET /static/robots.txt HTTP/1.0");
+               strcpy(req->line, "GET /static/robots.txt"
+                               "?force_close_session=yes HTTP/1.0");
        }
 
        /* Do the non-root-cookie check now. */
-       else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
-               strcpy(req->line, "GET /static/nocookies.html HTTP/1.0");
+       else if ( (strcmp(buf, "/"))
+               && (strncasecmp(buf, "/listsub", 8))
+               && (got_cookie == 0)) {
+               strcpy(req->line, "GET /static/nocookies.html"
+                               "?force_close_session=yes HTTP/1.0");
        }
 
 
-
        /*
         * See if there's an existing session open with the desired ID
         */
@@ -279,7 +327,7 @@ void context_loop(int sock)
         * Create a new session if we have to
         */
        if (TheSession == NULL) {
-               printf("Creating a new session\n");
+               lprintf(3, "Creating a new session\n");
                TheSession = (struct wcsession *)
                        malloc(sizeof(struct wcsession));
                memset(TheSession, 0, sizeof(struct wcsession));
@@ -292,15 +340,11 @@ void context_loop(int sock)
                pthread_mutex_unlock(&SessionListMutex);
        }
 
-
        /*
-        *
-        * FIX ... check session integrity here before continuing
-        *
+        * A future improvement might be to check the session integrity
+        * at this point before continuing.
         */
 
-
-
        /*
         * Bind to the session and perform the transaction
         */
@@ -308,22 +352,13 @@ void context_loop(int sock)
        pthread_setspecific(MyConKey, (void *)TheSession);
        TheSession->http_sock = sock;
        TheSession->lastreq = time(NULL);                       /* log */
-       fprintf(stderr, "Transaction: %s\n", req->line);
-       session_loop(req);              /* perform the requested transaction */
+       session_loop(req, gzip);        /* perform the requested transaction */
        pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
 
        /* Free the request buffer */
-       while (req != NULL) {
+bail:  while (req != NULL) {
                hptr = req->next;
                free(req);
                req = hptr;
        }
-
-       /*
-        * Now our HTTP connection is done.  Close the socket and exit this
-        * function, so the worker thread can handle a new HTTP connection.
-        */
-       printf("   Closing socket %d ... ret=%d\n", sock,
-              lingering_close(sock));
-       return;
 }