~ubuntu-branches/ubuntu/utopic/requests/utopic

« back to all changes in this revision

Viewing changes to requests/packages/urllib3/response.py

  • Committer: Package Import Robot
  • Author(s): Daniele Tricoli
  • Date: 2013-10-18 19:20:21 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20131018192021-hzbd8k13wgx2z8am
Tags: 2.0.0-1
* New upstream release (Closes: #725784)
* Switched to pybuild
* debian/clean
  - Switched to debian/clean for cleaning instead of using debian/rules
* debian/control
  - Bumped python(3)-urllib3 to (>=1.7.1)
* debian/copyright
  - Updated copyright year
* debian/patches/02_use-system-chardet-and-urllib3.patches
  - Refreshed
* debian/watch
  - Switched download URL to https

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# urllib3/response.py
2
 
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
 
2
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
3
3
#
4
4
# This module is part of urllib3 and is released under
5
5
# the MIT License: http://www.opensource.org/licenses/mit-license.php
7
7
 
8
8
import logging
9
9
import zlib
 
10
import io
10
11
 
11
12
from .exceptions import DecodeError
12
13
from .packages.six import string_types as basestring, binary_type
 
14
from .util import is_fp_closed
13
15
 
14
16
 
15
17
log = logging.getLogger(__name__)
48
50
    return DeflateDecoder()
49
51
 
50
52
 
51
 
class HTTPResponse(object):
 
53
class HTTPResponse(io.IOBase):
52
54
    """
53
55
    HTTP Response container.
54
56
 
72
74
    """
73
75
 
74
76
    CONTENT_DECODERS = ['gzip', 'deflate']
 
77
    REDIRECT_STATUSES = [301, 302, 303, 307, 308]
75
78
 
76
79
    def __init__(self, body='', headers=None, status=0, version=0, reason=None,
77
80
                 strict=0, preload_content=True, decode_content=True,
105
108
            code and valid location. ``None`` if redirect status and no
106
109
            location. ``False`` if not a redirect status code.
107
110
        """
108
 
        if self.status in [301, 302, 303, 307]:
 
111
        if self.status in self.REDIRECT_STATUSES:
109
112
            return self.headers.get('location')
110
113
 
111
114
        return False
183
186
            try:
184
187
                if decode_content and self._decoder:
185
188
                    data = self._decoder.decompress(data)
186
 
            except (IOError, zlib.error):
187
 
                raise DecodeError("Received response with content-encoding: %s, but "
188
 
                                  "failed to decode it." % content_encoding)
 
189
            except (IOError, zlib.error) as e:
 
190
                raise DecodeError(
 
191
                    "Received response with content-encoding: %s, but "
 
192
                    "failed to decode it." % content_encoding,
 
193
                    e)
189
194
 
190
 
            if flush_decoder and self._decoder:
 
195
            if flush_decoder and decode_content and self._decoder:
191
196
                buf = self._decoder.decompress(binary_type())
192
197
                data += buf + self._decoder.flush()
193
198
 
200
205
            if self._original_response and self._original_response.isclosed():
201
206
                self.release_conn()
202
207
 
 
208
    def stream(self, amt=2**16, decode_content=None):
 
209
        """
 
210
        A generator wrapper for the read() method. A call will block until
 
211
        ``amt`` bytes have been read from the connection or until the
 
212
        connection is closed.
 
213
 
 
214
        :param amt:
 
215
            How much of the content to read. The generator will return up to
 
216
            much data per iteration, but may return less. This is particularly
 
217
            likely when using compressed data. However, the empty string will
 
218
            never be returned.
 
219
 
 
220
        :param decode_content:
 
221
            If True, will attempt to decode the body based on the
 
222
            'content-encoding' header.
 
223
        """
 
224
        while not is_fp_closed(self._fp):
 
225
            data = self.read(amt=amt, decode_content=decode_content)
 
226
 
 
227
            if data:
 
228
                yield data
 
229
 
 
230
 
203
231
    @classmethod
204
232
    def from_httplib(ResponseCls, r, **response_kw):
205
233
        """
239
267
 
240
268
    def getheader(self, name, default=None):
241
269
        return self.headers.get(name, default)
 
270
 
 
271
    # Overrides from io.IOBase
 
272
    def close(self):
 
273
        if not self.closed:
 
274
            self._fp.close()
 
275
 
 
276
    @property
 
277
    def closed(self):
 
278
        if self._fp is None:
 
279
            return True
 
280
        elif hasattr(self._fp, 'closed'):
 
281
            return self._fp.closed
 
282
        elif hasattr(self._fp, 'isclosed'):  # Python 2
 
283
            return self._fp.isclosed()
 
284
        else:
 
285
            return True
 
286
 
 
287
    def fileno(self):
 
288
        if self._fp is None:
 
289
            raise IOError("HTTPResponse has no file to get a fileno from")
 
290
        elif hasattr(self._fp, "fileno"):
 
291
            return self._fp.fileno()
 
292
        else:
 
293
            raise IOError("The file-like object  this HTTPResponse is wrapped "
 
294
                          "around has no file descriptor")
 
295
 
 
296
    def flush(self):
 
297
        if self._fp is not None and hasattr(self._fp, 'flush'):
 
298
            return self._fp.flush()
 
299
 
 
300
    def readable(self):
 
301
        return True