5
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
10
from socket import timeout as SocketTimeout
12
12
from ._collections import HTTPHeaderDict
13
from .exceptions import DecodeError
13
from .exceptions import DecodeError, ReadTimeoutError
14
14
from .packages.six import string_types as basestring, binary_type
15
15
from .util import is_fp_closed
18
log = logging.getLogger(__name__)
21
18
class DeflateDecoder(object):
23
20
def __init__(self):
163
160
after having ``.read()`` the file object. (Overridden if ``amt`` is
166
# Note: content-encoding value should be case-insensitive, per RFC 2616
163
# Note: content-encoding value should be case-insensitive, per RFC 7230
168
165
content_encoding = self.headers.get('content-encoding', '').lower()
169
166
if self._decoder is None:
170
167
if content_encoding in self.CONTENT_DECODERS:
178
175
flush_decoder = False
182
# cStringIO doesn't like amt=None
183
data = self._fp.read()
186
cache_content = False
187
data = self._fp.read(amt)
188
if amt != 0 and not data: # Platform-specific: Buggy versions of Python.
189
# Close the connection when no data is returned
191
# This is redundant to what httplib/http.client _should_
192
# already do. However, versions of python released before
193
# December 15, 2012 (http://bugs.python.org/issue16298) do not
194
# properly close the connection in all cases. There is no harm
195
# in redundantly calling close.
180
# cStringIO doesn't like amt=None
181
data = self._fp.read()
197
182
flush_decoder = True
184
cache_content = False
185
data = self._fp.read(amt)
186
if amt != 0 and not data: # Platform-specific: Buggy versions of Python.
187
# Close the connection when no data is returned
189
# This is redundant to what httplib/http.client _should_
190
# already do. However, versions of python released before
191
# December 15, 2012 (http://bugs.python.org/issue16298) do
192
# not properly close the connection in all cases. There is
193
# no harm in redundantly calling close.
197
except SocketTimeout:
198
# FIXME: Ideally we'd like to include the url in the ReadTimeoutError but
199
# there is yet no clean way to get at it from this context.
200
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
199
202
self._fp_bytes_read += len(data)
204
207
except (IOError, zlib.error) as e:
205
208
raise DecodeError(
206
209
"Received response with content-encoding: %s, but "
207
"failed to decode it." % content_encoding,
210
"failed to decode it." % content_encoding, e)
210
212
if flush_decoder and decode_content and self._decoder:
211
213
buf = self._decoder.decompress(binary_type())
297
298
elif hasattr(self._fp, "fileno"):
298
299
return self._fp.fileno()
300
raise IOError("The file-like object this HTTPResponse is wrapped "
301
raise IOError("The file-like object this HTTPResponse is wrapped "
301
302
"around has no file descriptor")