Refactor stream_decode_gzip and _deflate into one function, stream_decompress.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
stream_decompress will now iterate over the raw data if there is a problem with decompression Remove gzip decoding from Response.content, as urllib3 was doing it anyway.
authored by Chase Sterling · committed by Chase Sterling · parent f80984f · Browse files · 2 files·+26 added−31 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+26 added−31 removed
- other:+26 added−31 removed
Provenance
- Commit
9c6ffc5- Tree
46f76dc- Author
Chase Sterling <chase.sterling@gmail.com>- Committer
Chase Sterling <chase.sterling@gmail.com>- Parent
f80984f
2 files changed+26 added−31 removed
modified:requests/models.py+5 added-12 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, decode_gzip,33- stream_decode_gzip, stream_decode_deflate, guess_filename, requote_path)32+ get_encoding_from_headers, stream_decode_response_unicode,33+ stream_decompress, guess_filename, requote_path)34 35 36 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)37 449 unmodified lines487 headers=self.headers,488 redirect=False,489 assert_same_host=False,490 preload_content=False,491- decode_content=False,491+ decode_content=True,492 retries=self.config.get('max_retries', 0),493 timeout=self.timeout,494 )495 self.sent = True117 unmodified lines613 614 gen = generate()615 616 if 'gzip' in self.headers.get('content-encoding', ''):617- gen = stream_decode_gzip(gen)617+ gen = stream_decompress(gen, mode='gzip')618 elif 'deflate' in self.headers.get('content-encoding', ''):619- gen = stream_decode_deflate(gen)619+ gen = stream_decompress(gen, mode='deflate')620 621 if decode_unicode is None:622 decode_unicode = self.config.get('decode_unicode')623 50 unmodified lines674 self._content = None675 676 content = self._content677 678- # Decode GZip'd content.679- if 'gzip' in self.headers.get('content-encoding', ''):680- try:681- content = decode_gzip(self._content)682- except zlib.error:683- pass684-678 # Decode unicode content.679 if self.config.get('decode_unicode'):680 681 # Try charset from content-type33 unmodified linesmodified:requests/utils.py+21 added-19 removed
352 unmodified lines353 354 return zlib.decompress(content, 16 + zlib.MAX_WBITS)355 356 357-def stream_decode_gzip(iterator):358- """Stream decodes a gzip-encoded iterator"""359- try:360- dec = zlib.decompressobj(16 + zlib.MAX_WBITS)361- for chunk in iterator:362- rv = dec.decompress(chunk)363- if rv:364- yield rv365- buf = dec.decompress('')366- rv = buf + dec.flush()367- if rv:368- yield rv369- except zlib.error:370- pass357+def stream_decompress(iterator, mode='gzip'):358+ """359+ Stream decodes an iterator over compressed data360 361+ :param iterator: An iterator over compressed data362+ :param mode: 'gzip' or 'deflate'363+ :return: An iterator over decompressed data364+ """365 373-def stream_decode_deflate(iterator):374- """Stream decodes a deflate-encoded iterator"""366+ if mode not in ['gzip', 'deflate']:367+ raise ValueError('stream_decompress mode must be gzip or deflate')368+369+ zlib_mode = 16 + zlib.MAX_WBITS if mode == 'gzip' else -zlib.MAX_WBITS370+ dec = zlib.decompressobj(zlib_mode)371 try:376- dec = zlib.decompressobj(-zlib.MAX_WBITS)372 for chunk in iterator:373 rv = dec.decompress(chunk)374 if rv:375 yield rv376+ except zlib.error:377+ # If there was an error decompressing, just return the raw chunk378+ yield chunk379+ # Continue to return the rest of the raw data380+ for chunk in iterator:381+ yield chunk382+ else:383+ # Make sure everything has been returned from the decompression object384 buf = dec.decompress('')385 rv = buf + dec.flush()386 if rv:387 yield rv385- except zlib.error:386- pass388 389+390 def requote_path(path):391 """Re-quote the given URL path component.392 393 This function passes the given path through an unquote/quote cycle to5 unmodified lines