Skip to content
Myr

Use streaming decompression feature of urllib3.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by schlamar · committed by schlamar · parent 6d6252a · Browse files · 2 files+3 added−45 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+3 added−45 removed

  • other:+3 added−45 removed

2 files+3 added−45 removed

modified:requests/models.py+3 added-3 removed
18 unmodified lines
19from .cookies import cookiejar_from_dict, get_cookie_header20from .packages.urllib3.filepost import encode_multipart_formdata21from .exceptions import HTTPError, RequestException, MissingSchema, InvalidURL22from .utils import (23 stream_untransfer, guess_filename, get_auth_from_url, requote_uri,23 guess_filename, get_auth_from_url, requote_uri,24 stream_decode_response_unicode, to_key_val_list, parse_header_links,25 iter_slices, guess_json_utf, super_len)26from .compat import (27 cookielib, urlparse, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
499 unmodified lines
527 return iter_slices(self._content, chunk_size)528529 def generate():530 while 1:531 chunk = self.raw.read(chunk_size)531 chunk = self.raw.read(chunk_size, decode_content=True)532 if not chunk:533 break534 yield chunk535 self._content_consumed = True536537 gen = stream_untransfer(generate(), self)537 gen = generate()538539 if decode_unicode:540 gen = stream_decode_response_unicode(gen, self)541
135 unmodified lines
modified:requests/utils.py-42 removed
344 unmodified lines
345 except TypeError:346 return r.content347348349def stream_decompress(iterator, mode='gzip'):350 """Stream decodes an iterator over compressed data351352 :param iterator: An iterator over compressed data353 :param mode: 'gzip' or 'deflate'354 :return: An iterator over decompressed data355 """356357 if mode not in ['gzip', 'deflate']:358 raise ValueError('stream_decompress mode must be gzip or deflate')359360 zlib_mode = 16 + zlib.MAX_WBITS if mode == 'gzip' else -zlib.MAX_WBITS361 dec = zlib.decompressobj(zlib_mode)362 try:363 for chunk in iterator:364 rv = dec.decompress(chunk)365 if rv:366 yield rv367 except zlib.error:368 # If there was an error decompressing, just return the raw chunk369 yield chunk370 # Continue to return the rest of the raw data371 for chunk in iterator:372 yield chunk373 else:374 # Make sure everything has been returned from the decompression object375 buf = dec.decompress(bytes())376 rv = buf + dec.flush()377 if rv:378 yield rv379380381def stream_untransfer(gen, resp):382 ce = resp.headers.get('content-encoding', '').lower()383 if 'gzip' in ce:384 gen = stream_decompress(gen, mode='gzip')385 elif 'deflate' in ce:386 gen = stream_decompress(gen, mode='deflate')387388 return gen389390349# The unreserved URI characters (RFC 3986)350UNRESERVED_SET = frozenset(351 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"352 + "0123456789-._~")
197 unmodified lines