2 # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
4 # This module is part of urllib3 and is released under
5 # the MIT License: http://www.opensource.org/licenses/mit-license.php
12 from .exceptions import DecodeError
13 from .packages.six import string_types as basestring, binary_type
14 from .util import is_fp_closed
17 log = logging.getLogger(__name__)
20 class DeflateDecoder(object):
23 self._first_try = True
24 self._data = binary_type()
25 self._obj = zlib.decompressobj()
27 def __getattr__(self, name):
28 return getattr(self._obj, name)
30 def decompress(self, data):
31 if not self._first_try:
32 return self._obj.decompress(data)
36 return self._obj.decompress(data)
38 self._first_try = False
39 self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
41 return self.decompress(self._data)
46 def _get_decoder(mode):
48 return zlib.decompressobj(16 + zlib.MAX_WBITS)
50 return DeflateDecoder()
53 class HTTPResponse(io.IOBase):
55 HTTP Response container.
57 Backwards-compatible to httplib's HTTPResponse but the response ``body`` is
58 loaded and decoded on-demand when the ``data`` property is accessed.
60 Extra parameters for behaviour not present in httplib.HTTPResponse:
62 :param preload_content:
63 If True, the response's body will be preloaded during construction.
65 :param decode_content:
66 If True, attempts to decode specific content-encoding's based on headers
67 (like 'gzip' and 'deflate') will be skipped and raw data will be used
70 :param original_response:
71 When this HTTPResponse wrapper is generated from an httplib.HTTPResponse
72 object, it's convenient to include the original for debug purposes. It's
76 CONTENT_DECODERS = ['gzip', 'deflate']
77 REDIRECT_STATUSES = [301, 302, 303, 307, 308]
79 def __init__(self, body='', headers=None, status=0, version=0, reason=None,
80 strict=0, preload_content=True, decode_content=True,
81 original_response=None, pool=None, connection=None):
82 self.headers = headers or {}
84 self.version = version
87 self.decode_content = decode_content
90 self._body = body if body and isinstance(body, basestring) else None
92 self._original_response = original_response
93 self._fp_bytes_read = 0
96 self._connection = connection
98 if hasattr(body, 'read'):
101 if preload_content and not self._body:
102 self._body = self.read(decode_content=decode_content)
104 def get_redirect_location(self):
106 Should we redirect and where to?
108 :returns: Truthy redirect location string if we got a redirect status
109 code and valid location. ``None`` if redirect status and no
110 location. ``False`` if not a redirect status code.
112 if self.status in self.REDIRECT_STATUSES:
113 return self.headers.get('location')
117 def release_conn(self):
118 if not self._pool or not self._connection:
121 self._pool._put_conn(self._connection)
122 self._connection = None
126 # For backwords-compat with earlier urllib3 0.4 and earlier.
131 return self.read(cache_content=True)
135 Obtain the number of bytes pulled over the wire so far. May differ from
136 the amount of content returned by :meth:``HTTPResponse.read`` if bytes
137 are encoded on the wire (e.g, compressed).
139 return self._fp_bytes_read
141 def read(self, amt=None, decode_content=None, cache_content=False):
143 Similar to :meth:`httplib.HTTPResponse.read`, but with two additional
144 parameters: ``decode_content`` and ``cache_content``.
147 How much of the content to read. If specified, caching is skipped
148 because it doesn't make sense to cache partial content as the full
151 :param decode_content:
152 If True, will attempt to decode the body based on the
153 'content-encoding' header.
155 :param cache_content:
156 If True, will save the returned data such that the same result is
157 returned despite of the state of the underlying file object. This
158 is useful if you want the ``.data`` property to continue working
159 after having ``.read()`` the file object. (Overridden if ``amt`` is
162 # Note: content-encoding value should be case-insensitive, per RFC 2616
164 content_encoding = self.headers.get('content-encoding', '').lower()
165 if self._decoder is None:
166 if content_encoding in self.CONTENT_DECODERS:
167 self._decoder = _get_decoder(content_encoding)
168 if decode_content is None:
169 decode_content = self.decode_content
174 flush_decoder = False
178 # cStringIO doesn't like amt=None
179 data = self._fp.read()
182 cache_content = False
183 data = self._fp.read(amt)
184 if amt != 0 and not data: # Platform-specific: Buggy versions of Python.
185 # Close the connection when no data is returned
187 # This is redundant to what httplib/http.client _should_
188 # already do. However, versions of python released before
189 # December 15, 2012 (http://bugs.python.org/issue16298) do not
190 # properly close the connection in all cases. There is no harm
191 # in redundantly calling close.
195 self._fp_bytes_read += len(data)
198 if decode_content and self._decoder:
199 data = self._decoder.decompress(data)
200 except (IOError, zlib.error) as e:
202 "Received response with content-encoding: %s, but "
203 "failed to decode it." % content_encoding,
206 if flush_decoder and decode_content and self._decoder:
207 buf = self._decoder.decompress(binary_type())
208 data += buf + self._decoder.flush()
216 if self._original_response and self._original_response.isclosed():
219 def stream(self, amt=2**16, decode_content=None):
221 A generator wrapper for the read() method. A call will block until
222 ``amt`` bytes have been read from the connection or until the
223 connection is closed.
226 How much of the content to read. The generator will return up to
227 much data per iteration, but may return less. This is particularly
228 likely when using compressed data. However, the empty string will
231 :param decode_content:
232 If True, will attempt to decode the body based on the
233 'content-encoding' header.
235 while not is_fp_closed(self._fp):
236 data = self.read(amt=amt, decode_content=decode_content)
243 def from_httplib(ResponseCls, r, **response_kw):
245 Given an :class:`httplib.HTTPResponse` instance ``r``, return a
246 corresponding :class:`urllib3.response.HTTPResponse` object.
248 Remaining parameters are passed to the HTTPResponse constructor, along
249 with ``original_response=r``.
252 # Normalize headers between different versions of Python
254 for k, v in r.getheaders():
255 # Python 3: Header keys are returned capitalised
258 has_value = headers.get(k)
259 if has_value: # Python 3: Repeating header keys are unmerged.
260 v = ', '.join([has_value, v])
264 # HTTPResponse objects in Python 3 don't have a .strict attribute
265 strict = getattr(r, 'strict', 0)
266 return ResponseCls(body=r,
275 # Backwards-compatibility methods for httplib.HTTPResponse
276 def getheaders(self):
279 def getheader(self, name, default=None):
280 return self.headers.get(name, default)
282 # Overrides from io.IOBase
291 elif hasattr(self._fp, 'closed'):
292 return self._fp.closed
293 elif hasattr(self._fp, 'isclosed'): # Python 2
294 return self._fp.isclosed()
300 raise IOError("HTTPResponse has no file to get a fileno from")
301 elif hasattr(self._fp, "fileno"):
302 return self._fp.fileno()
304 raise IOError("The file-like object this HTTPResponse is wrapped "
305 "around has no file descriptor")
308 if self._fp is not None and hasattr(self._fp, 'flush'):
309 return self._fp.flush()