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
Provenance
- Commit
59f916c- Tree
64834fb- Author
schlamar <marc.schlaich@gmail.com>- Committer
schlamar <marc.schlaich@gmail.com>- Parent
6d6252a
2 files changed+3 added−45 removed
modified:requests/models.py+3 added-3 removed
18 unmodified lines19 from .cookies import cookiejar_from_dict, get_cookie_header20 from .packages.urllib3.filepost import encode_multipart_formdata21 from .exceptions import HTTPError, RequestException, MissingSchema, InvalidURL22 from .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)26 from .compat import (27 cookielib, urlparse, urlunparse, urlsplit, urlencode, str, bytes, StringIO,499 unmodified lines527 return iter_slices(self._content, chunk_size)528 529 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 = True536 537- gen = stream_untransfer(generate(), self)537+ gen = generate()538 539 if decode_unicode:540 gen = stream_decode_response_unicode(gen, self)541 135 unmodified linesmodified:requests/utils.py-42 removed
344 unmodified lines345 except TypeError:346 return r.content347 348 349-def stream_decompress(iterator, mode='gzip'):350- """Stream decodes an iterator over compressed data351-352- :param iterator: An iterator over compressed data353- :param mode: 'gzip' or 'deflate'354- :return: An iterator over decompressed data355- """356-357- if mode not in ['gzip', 'deflate']:358- raise ValueError('stream_decompress mode must be gzip or deflate')359-360- 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 rv379-380-381-def 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')387-388- return gen389-390-349 # The unreserved URI characters (RFC 3986)350 UNRESERVED_SET = frozenset(351 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"352 + "0123456789-._~")197 unmodified lines