[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] backport compression-detection code to maintenance branch s...



Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv30729/src/or

Modified Files:
      Tag: tor-0_0_9-patches
	directory.c 
Log Message:
backport compression-detection code to maintenance branch so 0093 can handle directories from sources that are confused about content-encoding.

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/directory.c,v
retrieving revision 1.181.2.7
retrieving revision 1.181.2.8
diff -u -d -r1.181.2.7 -r1.181.2.8
--- directory.c	10 Jan 2005 17:46:11 -0000	1.181.2.7
+++ directory.c	21 Jan 2005 02:06:27 -0000	1.181.2.8
@@ -48,6 +48,7 @@
                        int purpose, const char *resource,
                        const char *payload, size_t payload_len);
 static int directory_handle_command(connection_t *conn);
+static int body_is_plausible(const char *body, size_t body_len, int purpose);
 
 /********* START VARIABLES **********/
 
@@ -546,6 +547,31 @@
   return 0;
 }
 
+/** Return true iff <b>body</b> doesn't start with a plausible router or
+ * running-list or directory opening.  This is a sign of possible compression.
+ **/
+static int
+body_is_plausible(const char *body, size_t len, int purpose)
+{
+  int i;
+  if (len < 32)
+    return 0;
+  if (purpose != DIR_PURPOSE_FETCH_RENDDESC) {
+    if (!strcmpstart(body,"router") ||
+        !strcmpstart(body,"signed-directory") ||
+        !strcmpstart(body,"network-status") ||
+        !strcmpstart(body,"running-routers"))
+    return 1;
+    for (i=0;i<32;++i) {
+      if (!isprint(body[i]) && !isspace(body[i]))
+        return 0;
+    }
+    return 1;
+  } else {
+    return 1;
+  }
+}
+
 /** We are a client, and we've finished reading the server's
  * response. Parse and it and act appropriately.
  *
@@ -562,6 +588,7 @@
   time_t now, date_header=0;
   int delta;
   int compression;
+  int plausible;
 
   switch (fetch_from_buf_http(conn->inbuf,
                               &headers, MAX_HEADERS_SIZE,
@@ -593,17 +620,55 @@
     }
   }
 
-  if (compression != 0) {
-    char *new_body;
-    size_t new_len;
-    if (tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression)) {
-      log_fn(LOG_WARN, "Unable to decompress HTTP body.");
+  plausible = body_is_plausible(body, body_len, conn->purpose);
+  if (compression || !plausible) {
+    char *new_body = NULL;
+    size_t new_len = 0;
+    int guessed = detect_compression_method(body, body_len);
+    if (compression <= 0 || guessed != compression) {
+      /* Tell the user if we don't believe what we're told about compression.*/
+      const char *description1, *description2;
+      if (compression == ZLIB_METHOD)
+        description1 = "as deflated";
+      else if (compression == GZIP_METHOD)
+        description1 = "as gzipped";
+      else if (compression == 0)
+        description1 = "as uncompressed";
+      else
+        description1 = "with an unknown Content-Encoding";
+      if (guessed == ZLIB_METHOD)
+        description2 = "deflated";
+      else if (guessed == GZIP_METHOD)
+        description2 = "gzipped";
+      else if (!plausible)
+        description2 = "confusing binary junk";
+      else
+        description2 = "uncompressed";
+
+      log_fn(LOG_INFO, "HTTP body from server '%s' was labeled %s,"
+             "but it seems to be %s.%s",
+             conn->address, description1, description2,
+             (compression>0 && guessed>0)?"  Trying both.":"");
+    }
+    /* Try declared compression first if we can. */
+    if (compression > 0)
+      tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression);
+    /* Okay, if that didn't work, and we think that it was compressed
+     * differently, try that. */
+    if (!new_body && guessed > 0 && compression != guessed)
+      tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed);
+    /* If we're pretty sure that we have a compressed directory, and
+     * we didn't manage to uncompress it, then warn and bail. */
+    if (!plausible && !new_body) {
+      log_fn(LOG_WARN, "Unable to decompress HTTP body (server '%s').", conn->address);
       tor_free(body); tor_free(headers);
       return -1;
     }
-    tor_free(body);
-    body = new_body;
-    body_len = new_len;
+    if (new_body) {
+      tor_free(body);
+      body = new_body;
+      body_len = new_len;
+    }
   }
 
   if (conn->purpose == DIR_PURPOSE_FETCH_DIR) {