Skip to content

Commit 5351469

Browse files
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 <[email protected]>
1 parent 2463074 commit 5351469

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ dev
55
---
66

77
- \[Short description of non-trivial change.\]
8+
- Requests Brotli compression, if either the `brotli` or `brotlicffi` package
9+
is installed.
810

911
**Dependencies**
1012

docs/community/faq.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Encoded Data?
1111
Requests automatically decompresses gzip-encoded responses, and does
1212
its best to decode response content to unicode when possible.
1313

14+
When either the `brotli <https://pypi.org/project/Brotli/>`_ or `brotlicffi <https://pypi.org/project/brotlicffi/>`_
15+
package is installed, requests also decodes Brotli-encoded responses.
16+
1417
You can get direct access to the raw response (and even the socket),
1518
if needed as well.
1619

docs/user/quickstart.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ You can also access the response body as bytes, for non-text requests::
128128

129129
The ``gzip`` and ``deflate`` transfer-encodings are automatically decoded for you.
130130

131+
The ``br`` transfer-encoding is automatically decoded for you if a Brotli library
132+
like `brotli <https://pypi.org/project/brotli>`_ or `brotlicffi <https://pypi.org/project/brotli>`_ is installed.
133+
131134
For example, to create an image from binary data returned by a request, you can
132135
use the following code::
133136

requests/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import warnings
2121
import zipfile
2222
from collections import OrderedDict
23+
from urllib3.util import make_headers
2324

2425
from .__version__ import __version__
2526
from . import certs
@@ -41,6 +42,11 @@
4142

4243
DEFAULT_PORTS = {'http': 80, 'https': 443}
4344

45+
# Ensure that ', ' is used to preserve previous delimiter behavior.
46+
DEFAULT_ACCEPT_ENCODING = ", ".join(
47+
re.split(r",\s*", make_headers(accept_encoding=True)["accept-encoding"])
48+
)
49+
4450

4551
if sys.platform == 'win32':
4652
# provide a proxy_bypass version on Windows without DNS lookups
@@ -835,7 +841,7 @@ def default_headers():
835841
"""
836842
return CaseInsensitiveDict({
837843
'User-Agent': default_user_agent(),
838-
'Accept-Encoding': ', '.join(('gzip', 'deflate')),
844+
'Accept-Encoding': DEFAULT_ACCEPT_ENCODING,
839845
'Accept': '*/*',
840846
'Connection': 'keep-alive',
841847
})

0 commit comments

Comments
 (0)