Implement automatic deflating when using response.iter_content. refs #327
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Refactor response.iter_lines to use responset.iter_content.
authored by Chase Sterling · committed by Chase Sterling · parent 9c0a0fb · Browse files · 2 files·+32 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−38 removed
- other:+32 added−38 removed
Provenance
- Commit
f80984f- Tree
90d6a8e- Author
Chase Sterling <chase.sterling@gmail.com>- Committer
Chase Sterling <chase.sterling@gmail.com>- Parent
9c0a0fb
2 files changed+32 added−38 removed
modified:requests/models.py+17 added-38 removed
27 unmodified lines28 from .exceptions import (29 ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,30 URLRequired, SSLError)31 from .utils import (32- get_encoding_from_headers, stream_decode_response_unicode,33- decode_gzip, stream_decode_gzip, guess_filename, requote_path)32+ get_encoding_from_headers, stream_decode_response_unicode, decode_gzip,33+ stream_decode_gzip, stream_decode_deflate, guess_filename, requote_path)34 35 36 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)37 576 unmodified lines614 gen = generate()615 616 if 'gzip' in self.headers.get('content-encoding', ''):617 gen = stream_decode_gzip(gen)618+ elif 'deflate' in self.headers.get('content-encoding', ''):619+ gen = stream_decode_deflate(gen)620 621 if decode_unicode is None:622 decode_unicode = self.config.get('decode_unicode')623 13 unmodified lines637 638 if newlines is None:639 newlines = ('\r', '\n', '\r\n')640 639- if self._content_consumed:640- raise RuntimeError(641- 'The content for this response was already consumed'642- )641+ chunk = []642 644- def generate():645- if self.raw is not None:643+ for c in self.iter_content(1, decode_unicode=decode_unicode):644+ if not c:645+ break646+647+ if c in newlines:648+ yield ''.join(chunk)649 chunk = []650+ else:651+ chunk.append(c)652 648- while 1:649- c = self.raw.read(1)650- if not c:651- break653+ # Yield the remainder, in case the response654+ # did not terminate with a newline655+ if chunk:656+ yield ''.join(chunk)657 653- if c in newlines:654- yield ''.join(chunk)655- chunk = []656- else:657- chunk.append(c)658 659- # Yield the remainder, in case the response660- # did not terminate with a newline661- if chunk:662- yield ''.join(chunk)663-664- self._content_consumed = True665-666- gen = generate()667-668- if 'gzip' in self.headers.get('content-encoding', ''):669- gen = stream_decode_gzip(gen)670-671- if decode_unicode is None:672- decode_unicode = self.config.get('decode_unicode')673-674- if decode_unicode:675- gen = stream_decode_response_unicode(gen, self)676-677- return gen678-679-659 @property660 def content(self):661 """Content of the response, in bytes or unicode662 (if available).59 unmodified linesmodified:requests/utils.py+15 added
368 unmodified lines369 except zlib.error:370 pass371 372 373+def stream_decode_deflate(iterator):374+ """Stream decodes a deflate-encoded iterator"""375+ try:376+ dec = zlib.decompressobj(-zlib.MAX_WBITS)377+ for chunk in iterator:378+ rv = dec.decompress(chunk)379+ if rv:380+ yield rv381+ buf = dec.decompress('')382+ rv = buf + dec.flush()383+ if rv:384+ yield rv385+ except zlib.error:386+ pass387+388 def requote_path(path):389 """Re-quote the given URL path component.390 391 This function passes the given path through an unquote/quote cycle to5 unmodified lines