Automatically decompress gzipped responses if content-type is set. fixes #19
authorKenneth Reitz <me@kennethreitz.com>
Thu, 14 Apr 2011 23:44:29 +0000 (19:44 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Thu, 14 Apr 2011 23:44:29 +0000 (19:44 -0400)
requests/core.py

index 2aa0926..4ec6824 100644 (file)
@@ -14,6 +14,7 @@ from __future__ import absolute_import
 
 import urllib
 import urllib2
+import zlib
 
 from urllib2 import HTTPError
 from urlparse import urlparse
@@ -144,9 +145,15 @@ class Request(object):
 
         self.response.status_code = getattr(resp, 'code', None)
         self.response.headers = getattr(resp.info(), 'dict', None)
-        self.response.url = getattr(resp, 'url', None)
         self.response.content = resp.read()
 
+        if self.response.headers.get('content-encoding', None) == 'gzip':
+            try:
+                self.response.content = zlib.decompress(self.response.content, 16+zlib.MAX_WBITS)
+            except zlib.error:
+                pass
+
+        self.response.url = getattr(resp, 'url', None)
 
     @staticmethod
     def _build_url(url, data):