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
Provenance
- Commit
296d8cc- Tree
405bde9- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
bbf1021
1 file changed+33 added−15 removed
modified:requests/models.py+33 added-15 removed
24 unmodified lines25 from .packages.urllib3.filepost import encode_multipart_formdata26 from .exceptions import (27 Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)28 from .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)31 32 33 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)123 unmodified lines157 158 # Make headers case-insensitive.159 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))160 161+ # Set encoding.162+ response.encoding = get_encoding_from_headers(response.headers)163+164 # Start off with our local cookies.165 cookies = self.cookies or dict()166 167 # Add new cookies from the server.334 unmodified lines502 503 #: Resulting :class:`HTTPError` of request, if one occurred.504 self.error = None505 506+ #: Encoding to decode with when accessing r.content.507+ self.encoding = None508+509 #: 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 lines575 """Content of the response, in bytes or unicode576 (if available).577 """578 573- 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')585 576- 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 = None589 580- # Read the contents.581- try:582- self._content = self.raw.read()583- except AttributeError:584- return None590+ content = self._content591 586-592 # 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 pass598 599 # Decode unicode content.600 if self.config.get('decode_unicode'):596- self._content = get_unicode_from_response(self)601 602+ # Try charset from content-type603+604+ if self.encoding:605+ try:606+ content = unicode(content, self.encoding)607+ except UnicodeError:608+ pass609+610+ # Fall back:611+ try:612+ content = unicode(content, self.encoding, errors='replace')613+ except TypeError:614+ pass615+616 self._content_consumed = True599- return self._content617+ return content618 619 620 def raise_for_status(self):621 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred."""14 unmodified lines