From 49066861863495f8e401ea22a25d01b07ccbab0e Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sat, 20 Aug 2011 19:46:50 -0400 Subject: [PATCH] utils.decode_gzip --- requests/models.py | 4 ++-- requests/utils.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 8692bbb..5152460 100644 --- a/requests/models.py +++ b/requests/models.py @@ -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 diff --git a/requests/utils.py b/requests/utils.py index 7270573..beed3c1 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -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 -- 2.34.1