Skip to content
Myr

new (elegant) request decoding model.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #282

authored by Kenneth Reitz · committed by Kenneth Reitz · parent bbf1021 · Browse files · 1 file+33 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+33 added−15 removed

  • other:+33 added−15 removed

1 file+33 added−15 removed

modified:requests/models.py+33 added-15 removed
24 unmodified lines
25from .packages.urllib3.filepost import encode_multipart_formdata26from .exceptions import (27 Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)28from .utils import (29 get_unicode_from_response, stream_decode_response_unicode,29 get_encoding_from_headers, stream_decode_response_unicode,30 decode_gzip, stream_decode_gzip, guess_filename, requote_path)313233REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)
123 unmodified lines
157158 # Make headers case-insensitive.159 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))160161 # Set encoding.162 response.encoding = get_encoding_from_headers(response.headers)163164 # Start off with our local cookies.165 cookies = self.cookies or dict()166167 # Add new cookies from the server.
334 unmodified lines
502503 #: Resulting :class:`HTTPError` of request, if one occurred.504 self.error = None505506 #: Encoding to decode with when accessing r.content.507 self.encoding = None508509 #: A list of :class:`Response <Response>` objects from510 #: the history of the Request. Any redirect responses will end511 #: up here.512 self.history = []
62 unmodified lines
575 """Content of the response, in bytes or unicode576 (if available).577 """578573 if self._content is not None:574 return self._content579 if self._content is None:580 # Read the contents.581 try:582 if self._content_consumed:583 raise RuntimeError(584 'The content for this response was already consumed')585576 if self._content_consumed:577 raise RuntimeError('The content for this response was '578 'already consumed')586 self._content = self.raw.read()587 except AttributeError:588 self._content = None589580 # Read the contents.581 try:582 self._content = self.raw.read()583 except AttributeError:584 return None590 content = self._content591586592 # Decode GZip'd content.593 if 'gzip' in self.headers.get('content-encoding', ''):594 try:590 self._content = decode_gzip(self._content)595 content = decode_gzip(self._content)596 except zlib.error:597 pass598599 # Decode unicode content.600 if self.config.get('decode_unicode'):596 self._content = get_unicode_from_response(self)601602 # Try charset from content-type603604 if self.encoding:605 try:606 content = unicode(content, self.encoding)607 except UnicodeError:608 pass609610 # Fall back:611 try:612 content = unicode(content, self.encoding, errors='replace')613 except TypeError:614 pass615616 self._content_consumed = True599 return self._content617 return content618619620 def raise_for_status(self):621 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred."""
14 unmodified lines