From 5351469472eccee7ed1a6cae53341446c520d807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B8=D0=BB=D1=8F=D0=BD=20=D0=9F=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=D1=83=D0=B7=D0=BE=D0=B2?= Date: Wed, 7 Jul 2021 16:16:28 +0300 Subject: [PATCH] Add support for brotli decoding (#5783) * Add support for Brotli decoding When the brotli or brotlicffi packages are installed, urllib3.util.make_headers() inserts ',br' in the Accept-Encoding header and decodes br from the answers. * Create the default Accept-Encoding header once * Preserve the previous delimiter behavior * Update prose in quickstart.rst Co-authored-by: Seth Michael Larson --- HISTORY.md | 2 ++ docs/community/faq.rst | 3 +++ docs/user/quickstart.rst | 3 +++ requests/utils.py | 8 +++++++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 9b08a7f2d6..6a6040c974 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,8 @@ dev --- - \[Short description of non-trivial change.\] +- Requests Brotli compression, if either the `brotli` or `brotlicffi` package + is installed. **Dependencies** diff --git a/docs/community/faq.rst b/docs/community/faq.rst index c0177719bd..19732aedac 100644 --- a/docs/community/faq.rst +++ b/docs/community/faq.rst @@ -11,6 +11,9 @@ Encoded Data? Requests automatically decompresses gzip-encoded responses, and does its best to decode response content to unicode when possible. +When either the `brotli `_ or `brotlicffi `_ +package is installed, requests also decodes Brotli-encoded responses. + You can get direct access to the raw response (and even the socket), if needed as well. diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 0e2bcadfdc..b4649f00d5 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -128,6 +128,9 @@ You can also access the response body as bytes, for non-text requests:: The ``gzip`` and ``deflate`` transfer-encodings are automatically decoded for you. +The ``br`` transfer-encoding is automatically decoded for you if a Brotli library +like `brotli `_ or `brotlicffi `_ is installed. + For example, to create an image from binary data returned by a request, you can use the following code:: diff --git a/requests/utils.py b/requests/utils.py index 6705c9a300..dbb02a0d79 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -20,6 +20,7 @@ import warnings import zipfile from collections import OrderedDict +from urllib3.util import make_headers from .__version__ import __version__ from . import certs @@ -41,6 +42,11 @@ DEFAULT_PORTS = {'http': 80, 'https': 443} +# Ensure that ', ' is used to preserve previous delimiter behavior. +DEFAULT_ACCEPT_ENCODING = ", ".join( + re.split(r",\s*", make_headers(accept_encoding=True)["accept-encoding"]) +) + if sys.platform == 'win32': # provide a proxy_bypass version on Windows without DNS lookups @@ -835,7 +841,7 @@ def default_headers(): """ return CaseInsensitiveDict({ 'User-Agent': default_user_agent(), - 'Accept-Encoding': ', '.join(('gzip', 'deflate')), + 'Accept-Encoding': DEFAULT_ACCEPT_ENCODING, 'Accept': '*/*', 'Connection': 'keep-alive', })