utils.decode_gzip
authorKenneth Reitz <me@kennethreitz.com>
Sat, 20 Aug 2011 23:46:50 +0000 (19:46 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Sat, 20 Aug 2011 23:46:50 +0000 (19:46 -0400)
requests/models.py
requests/utils.py

index 8692bbb..5152460 100644 (file)
@@ -21,7 +21,7 @@ from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicA
 from .structures import CaseInsensitiveDict
 from .packages.poster.encode import multipart_encode
 from .packages.poster.streaminghttp import register_openers, get_handlers
-from .utils import dict_from_cookiejar, get_unicode_from_response
+from .utils import dict_from_cookiejar, get_unicode_from_response, decode_gzip
 from .status_codes import codes
 from .exceptions import RequestException, AuthenticationError, Timeout, URLRequired, InvalidMethod, TooManyRedirects
 
@@ -448,7 +448,7 @@ class Response(object):
             # Decode GZip'd content.
             if 'gzip' in self.headers.get('content-encoding', ''):
                 try:
-                    self._content = zlib.decompress(self._content, 16+zlib.MAX_WBITS)
+                    self._content = decode_gzip(self._content)
                 except zlib.error:
                     pass
 
index 7270573..beed3c1 100644 (file)
@@ -12,6 +12,7 @@ that are also useful for external consumption.
 import cgi
 import cookielib
 import re
+import zlib
 
 
 def dict_from_cookiejar(cookiejar):
@@ -131,3 +132,9 @@ def get_unicode_from_response(r):
         return unicode(r.content, encoding, errors='replace')
     except TypeError:
         return r.content
+
+
+def decode_gzip(content):
+    """Return gzip-decoded string."""
+
+    return zlib.decompress(content, 16+zlib.MAX_WBITS)
\ No newline at end of file