Skip to content
Myr

Update brotli support to use the brotlicffi package (#1605)

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

* Update brotli support to use the brotlicffi package

authored by Tom Christie · committed by GitHub · parent 0c2cb24 · Browse files · 7 files+20 added−23 removed
This commit

State

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

Diff stats

7 files+20 added−23 removed

  • tests:+7 added−7 removed
  • other:+13 added−16 removed

7 files+20 added−23 removed

modified:docs/index.md+1 added-1 removed
modified:httpx/_decoders.py+9 added-9 removed
9 unmodified lines
1011from ._exceptions import DecodingError1213try:14 import brotli14 import brotlicffi15except ImportError: # pragma: nocover16 brotli = None16 brotlicffi = None171819class ContentDecoder:20 def decode(self, data: bytes) -> bytes:
77 unmodified lines
98 name. The top branches are for 'brotlipy' and bottom branches for 'Brotli'99 """100101 def __init__(self) -> None:102 if brotli is None: # pragma: nocover102 if brotlicffi is None: # pragma: nocover103 raise ImportError(104 "Using 'BrotliDecoder', but the 'brotlipy' or 'brotli' library "104 "Using 'BrotliDecoder', but the 'brotlicffi' library "105 "is not installed."106 "Make sure to install httpx using `pip install httpx[brotli]`."107 ) from None108109 self.decompressor = brotli.Decompressor()109 self.decompressor = brotlicffi.Decompressor()110 self.seen_data = False111 if hasattr(self.decompressor, "decompress"):112 self._decompress = self.decompressor.decompress113 else:
3 unmodified lines
117 if not data:118 return b""119 self.seen_data = True120 try:121 return self._decompress(data)122 except brotli.error as exc:121 return self.decompressor.decompress(data)122 except brotlicffi.Error as exc:123 raise DecodingError(str(exc)) from exc124125 def flush(self) -> bytes:126 if not self.seen_data:
1 unmodified line
128 try:129 if hasattr(self.decompressor, "finish"):130 self.decompressor.finish()131 return b""132 except brotli.error as exc: # pragma: nocover132 except brotlicffi.Error as exc: # pragma: nocover133 raise DecodingError(str(exc)) from exc134135136class MultiDecoder(ContentDecoder):
227 unmodified lines
364 "br": BrotliDecoder,365}366367368if brotli is None:368if brotlicffi is None:369 SUPPORTED_DECODERS.pop("br") # pragma: nocover
modified:tests/models/test_responses.py+3 added-3 removed
1import json2import pickle3from unittest import mock45import brotli5import brotlicffi6import pytest78import httpx9
777 unmodified lines
787@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))788def test_decode_error_with_request(header_value):789 headers = [(b"Content-Encoding", header_value)]790 body = b"test 123"791 compressed_body = brotli.compress(body)[3:]791 compressed_body = brotlicffi.compress(body)[3:]792 with pytest.raises(httpx.DecodingError):793 httpx.Response(794 200,795 headers=headers,
12 unmodified lines
808@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))809def test_value_error_without_request(header_value):810 headers = [(b"Content-Encoding", header_value)]811 body = b"test 123"812 compressed_body = brotli.compress(body)[3:]812 compressed_body = brotlicffi.compress(body)[3:]813 with pytest.raises(httpx.DecodingError):814 httpx.Response(200, headers=headers, content=compressed_body)815816
78 unmodified lines
modified:tests/test_decoders.py+4 added-4 removed
1import zlib23import brotli3import brotlicffi4import pytest56import httpx7from httpx._decoders import (
60 unmodified lines
686970def test_brotli():71 body = b"test 123"72 compressed_body = brotli.compress(body)72 compressed_body = brotlicffi.compress(body)7374 headers = [(b"Content-Encoding", b"br")]75 response = httpx.Response(76 200,
24 unmodified lines
101102103def test_multi_with_identity():104 body = b"test 123"105 compressed_body = brotli.compress(body)105 compressed_body = brotlicffi.compress(body)106107 headers = [(b"Content-Encoding", b"br, identity")]108 response = httpx.Response(109 200,
54 unmodified lines
164@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))165def test_decoding_errors(header_value):166 headers = [(b"Content-Encoding", header_value)]167 body = b"test 123"168 compressed_body = brotli.compress(body)[3:]168 compressed_body = brotlicffi.compress(body)[3:]169 with pytest.raises(httpx.DecodingError):170 request = httpx.Request("GET", "https://example.org")171 httpx.Response(200, headers=headers, content=compressed_body, request=request)172
186 unmodified lines
modified:README.md+1 added-1 removed
121 unmodified lines
122* `rfc3986` - URL parsing & normalization.123 * `idna` - Internationalized domain name support.124* `sniffio` - Async library autodetection.125* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*126* `brotlipy` - Decoding for "brotli" compressed responses. *(Optional)*126* `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional)*127128A huge amount of credit is due to `requests` for the API layout that129much of this work follows, as well as to `urllib3` for plenty of design130inspiration around the lower-level networking details.
3 unmodified lines
modified:requirements.txt+1 added-4 removed
1-e .[http2]1-e .[http2,brotli]23# Optional4brotlipy==0.7.*53# Documentation4mkdocs5mkautodoc6mkdocs-material
24 unmodified lines
modified:setup.py+1 added-1 removed
62 unmodified lines
63 "async_generator; python_version < '3.7'"64 ],65 extras_require={66 "http2": "h2==3.*",67 "brotli": "brotlipy==0.7.*",67 "brotli": "brotlicffi==1.*",68 },69 classifiers=[70 "Development Status :: 4 - Beta",71 "Environment :: Web Environment",
14 unmodified lines