[Inteproxy-commits] r347 - in branches/compression: . inteproxy

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 22 18:06:21 CET 2012


Author: aheinecke
Date: 2012-02-22 18:06:21 +0100 (Wed, 22 Feb 2012)
New Revision: 347

Modified:
   branches/compression/ChangeLog
   branches/compression/inteproxy/httpmessage.py
Log:
Add support for gzip/deflate compression in HTTPMessage

The read method now uncompresses the data if necessary and always
returns the uncompressed value. Both compression methods use
a zlib decompression object with a different Window Size.
This also allows decompressing chunks of the stream without
loading the complete stream into memory.


Modified: branches/compression/ChangeLog
===================================================================
--- branches/compression/ChangeLog	2012-02-22 10:49:54 UTC (rev 346)
+++ branches/compression/ChangeLog	2012-02-22 17:06:21 UTC (rev 347)
@@ -1,3 +1,8 @@
+2012-02-22	Andre Heinecke	<aheinecke at intevation.de>
+
+	* M inteproxy/httpmessage.py:
+	  Added support for decompressing HTTPMessages on read
+
 2012-01-21	Bjoern Schilberg	<bjoern.schilberg at intevation.de>
 
 	* M setup.py:

Modified: branches/compression/inteproxy/httpmessage.py
===================================================================
--- branches/compression/inteproxy/httpmessage.py	2012-02-22 10:49:54 UTC (rev 346)
+++ branches/compression/inteproxy/httpmessage.py	2012-02-22 17:06:21 UTC (rev 347)
@@ -1,6 +1,7 @@
-# Copyright (C) 2008 by Intevation GmbH
+# Copyright (C) 2008, 2012 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
+# Andre Heinecke <aheinecke at intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with the software for details.
@@ -8,6 +9,7 @@
 """Abstractions for http request and response messages"""
 
 from StringIO import StringIO
+import zlib
 
 class HTTPMessage(object):
 
@@ -36,6 +38,7 @@
         self.headers = headers
         self.infile = infile
         self._body = None
+        self._decompressobj = None
         self._body_stream = None
 
     def debug_log_message(self, log_function):
@@ -77,14 +80,33 @@
         raise NotImplementedError
 
     def read(self, amount):
+        """ Read data from the messagebody
+            Parameters:
+                amount -- the amount of (possibly compressed) bytes to read
+            Returns:
+                A string representing the data from the amount of bytes in the message.
+                This can be significantly larger then the "amount" parameter.
+        """
         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:
-            return self._body_stream.read(amount)
+            data = self._body_stream.read(amount)
         else:
-            return self.infile.read(amount)
+            data = self.infile.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)
 
+        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)
+
+        return data
+
 class HTTPRequestMessage(HTTPMessage):
 
     """Represents a HTTP Message for a request.



More information about the Inteproxy-commits mailing list