From 00b13673e32e45d2eb6517c0cf81915467331d8f Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 15 Feb 2012 02:28:53 -0500 Subject: [PATCH] simplify --- requests/models.py | 12 ++++-------- requests/utils.py | 9 +++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/requests/models.py b/requests/models.py index dd24947..e977b3e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -25,8 +25,8 @@ from .exceptions import ( ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects, URLRequired, SSLError) from .utils import ( - get_encoding_from_headers, stream_decode_response_unicode, - stream_decompress, guess_filename, requote_uri, dict_from_string) + get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, + dict_from_string) from .compat import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, str, bytes, SimpleCookie, is_py3, is_py2 @@ -684,10 +684,7 @@ class Response(object): else: gen = generate() - if 'gzip' in self.headers.get('content-encoding', ''): - gen = stream_decompress(gen, mode='gzip') - elif 'deflate' in self.headers.get('content-encoding', ''): - gen = stream_decompress(gen, mode='deflate') + gen = stream_untransfer(gen, self) if decode_unicode: gen = stream_decode_response_unicode(gen, self) @@ -729,7 +726,6 @@ class Response(object): if pending is not None: yield pending.rstrip() - @property def content(self): """Content of the response, in bytes.""" @@ -741,7 +737,7 @@ class Response(object): raise RuntimeError( 'The content for this response was already consumed') - self._content = self.raw.read() + self._content = bytes('').join(self.iter_content()) or None except AttributeError: self._content = None diff --git a/requests/utils.py b/requests/utils.py index 11b3bf0..d7b9322 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -400,6 +400,15 @@ def stream_decompress(iterator, mode='gzip'): if rv: yield rv +def stream_untransfer(gen, resp): + if 'gzip' in resp.headers.get('content-encoding', ''): + gen = stream_decompress(gen, mode='gzip') + elif 'deflate' in resp.headers.get('content-encoding', ''): + gen = stream_decompress(gen, mode='deflate') + + return gen + + # The unreserved URI characters (RFC 3986) UNRESERVED_SET = frozenset( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" -- 2.7.4