[Inteproxy-commits] r351 - branches/compression/inteproxy

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Feb 23 16:26:28 CET 2012


Author: aheinecke
Date: 2012-02-23 16:26:28 +0100 (Thu, 23 Feb 2012)
New Revision: 351

Modified:
   branches/compression/inteproxy/httpmessage.py
Log:
Check for the need to decompress a response on initialization
of the response.
This allows to correctly change the headers (Content-Encoding/
Content-Length) before forwarding the response.


Modified: branches/compression/inteproxy/httpmessage.py
===================================================================
--- branches/compression/inteproxy/httpmessage.py	2012-02-23 15:22:26 UTC (rev 350)
+++ branches/compression/inteproxy/httpmessage.py	2012-02-23 15:26:28 UTC (rev 351)
@@ -38,7 +38,7 @@
         self.headers = headers
         self.infile = infile
         self._body = None
-        self._decompressobj = None
+        self.decompressobj = None
         self._body_stream = None
 
     def debug_log_message(self, log_function):
@@ -87,27 +87,23 @@
             A string representing the data from the amount of bytes in
             the message.
             This can be significantly larger then the "amount" parameter.
+        Decompression is done if the property decompressobj is set.
         """
         if self._body_stream is None and self.body_has_been_read():
             self._body_stream = StringIO(self.body)
 
         if self._body_stream is not None:
-            data = self._body_stream.read(amount)
-        else:
-            data = self.infile.read(amount)
+            # Body stream is already decompressed
+            return self._body_stream.read(amount)
 
-        if self.headers.get("Content-Encoding") == "deflate":
-            if self._decompressobj is None:
-                self._decompressobj = zlib.decompressobj(-zlib.MAX_WBITS)
-            return self._decompressobj.decompress(data)
+        data = self.infile.read(amount)
 
-        if self.headers.get("Content-Encoding") == "gzip":
-            if self._decompressobj is None:
-                self._decompressobj = zlib.decompressobj(16+zlib.MAX_WBITS)
-            return self._decompressobj.decompress(data)
+        if self.decompressobj is None:
+            # Nothing to decompress
+            return data
+        else:
+            return self.decompressobj.decompress(data)
 
-        return data
-
 class HTTPRequestMessage(HTTPMessage):
 
     """Represents a HTTP Message for a request.
@@ -160,6 +156,7 @@
 
     All of these parameters are available as attributes of the same
     name.
+    If the response is compressed it will be decompressed.
     """
 
     def __init__(self, version, status, reason, headers, infile):
@@ -168,6 +165,19 @@
         self.status = status
         self.reason = reason
 
+        # Decompress the response
+        if self.headers.get("Content-Encoding") == "deflate":
+            self.decompressobj = zlib.decompressobj(-zlib.MAX_WBITS)
+        elif self.headers.get("Content-Encoding") == "gzip":
+            self.decompressobj = zlib.decompressobj(16 + zlib.MAX_WBITS)
+
+        if self.decompressobj:
+            del self.headers["Content-Encoding"]
+            # Need to extract the message fist to calculate
+            # an uncompressed content length
+            if int(self.headers.get("Content-Length", "0")):
+                self.read_entire_message()
+
     def debug_log_message(self, log_function):
         log_function("HTTPResponseMessage: %s %s %s",
                      self.version, self.status, self.reason)
@@ -176,4 +186,11 @@
     def read_entire_message(self):
         if self.body_has_been_read():
             return
-        self.set_body(self.infile.read())
+        length = int(self.headers.get("Content-Length", "0"))
+        if length:
+            # Not using chunked so we read everything at once
+            self.set_body(self.read(length))
+        elif not self.headers.get("Content-Length"):
+            # Can't read the entire message because we are chunked
+            # FIXME how to handle this?
+            pass



More information about the Inteproxy-commits mailing list