Skip to content
Myr

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

2 files+32 added−38 removed

modified:requests/models.py+17 added-38 removed
27 unmodified lines
28from .exceptions import (29 ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,30 URLRequired, SSLError)31from .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)343536REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)37
576 unmodified lines
614 gen = generate()615616 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)620621 if decode_unicode is None:622 decode_unicode = self.config.get('decode_unicode')623
13 unmodified lines
637638 if newlines is None:639 newlines = ('\r', '\n', '\r\n')640639 if self._content_consumed:640 raise RuntimeError(641 'The content for this response was already consumed'642 )641 chunk = []642644 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 break646647 if c in newlines:648 yield ''.join(chunk)649 chunk = []650 else:651 chunk.append(c)652648 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)657653 if c in newlines:654 yield ''.join(chunk)655 chunk = []656 else:657 chunk.append(c)658659 # Yield the remainder, in case the response660 # did not terminate with a newline661 if chunk:662 yield ''.join(chunk)663664 self._content_consumed = True665666 gen = generate()667668 if 'gzip' in self.headers.get('content-encoding', ''):669 gen = stream_decode_gzip(gen)670671 if decode_unicode is None:672 decode_unicode = self.config.get('decode_unicode')673674 if decode_unicode:675 gen = stream_decode_response_unicode(gen, self)676677 return gen678679659 @property660 def content(self):661 """Content of the response, in bytes or unicode662 (if available).
59 unmodified lines
modified:requests/utils.py+15 added
368 unmodified lines
369 except zlib.error:370 pass371372373def 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 pass387388def requote_path(path):389 """Re-quote the given URL path component.390391 This function passes the given path through an unquote/quote cycle to
5 unmodified lines