Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update brotli support to use the brotlicffi package #1605

Merged
merged 3 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The HTTPX project relies on these excellent libraries:
* `idna` - Internationalized domain name support.
* `sniffio` - Async library autodetection.
* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
* `brotlipy` - Decoding for "brotli" compressed responses. *(Optional)*
* `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional)*

A huge amount of credit is due to `requests` for the API layout that
much of this work follows, as well as to `urllib3` for plenty of design
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The HTTPX project relies on these excellent libraries:
* `idna` - Internationalized domain name support.
* `sniffio` - Async library autodetection.
* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
* `brotlipy` - Decoding for "brotli" compressed responses. *(Optional)*
* `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional)*

A huge amount of credit is due to `requests` for the API layout that
much of this work follows, as well as to `urllib3` for plenty of design
Expand Down
18 changes: 9 additions & 9 deletions httpx/_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from ._exceptions import DecodingError

try:
import brotli
import brotlicffi
except ImportError: # pragma: nocover
brotli = None
brotlicffi = None


class ContentDecoder:
Expand Down Expand Up @@ -99,14 +99,14 @@ class BrotliDecoder(ContentDecoder):
"""

def __init__(self) -> None:
if brotli is None: # pragma: nocover
if brotlicffi is None: # pragma: nocover
raise ImportError(
"Using 'BrotliDecoder', but the 'brotlipy' or 'brotli' library "
"Using 'BrotliDecoder', but the 'brotlicffi' library "
"is not installed."
"Make sure to install httpx using `pip install httpx[brotli]`."
) from None

self.decompressor = brotli.Decompressor()
self.decompressor = brotlicffi.Decompressor()
self.seen_data = False
if hasattr(self.decompressor, "decompress"):
self._decompress = self.decompressor.decompress
Expand All @@ -118,8 +118,8 @@ def decode(self, data: bytes) -> bytes:
return b""
self.seen_data = True
try:
return self._decompress(data)
except brotli.error as exc:
return self.decompressor.decompress(data)
except brotlicffi.Error as exc:
raise DecodingError(str(exc)) from exc

def flush(self) -> bytes:
Expand All @@ -129,7 +129,7 @@ def flush(self) -> bytes:
if hasattr(self.decompressor, "finish"):
self.decompressor.finish()
return b""
except brotli.error as exc: # pragma: nocover
except brotlicffi.Error as exc: # pragma: nocover
raise DecodingError(str(exc)) from exc


Expand Down Expand Up @@ -365,5 +365,5 @@ def flush(self) -> typing.List[str]:
}


if brotli is None:
if brotlicffi is None:
SUPPORTED_DECODERS.pop("br") # pragma: nocover
5 changes: 1 addition & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-e .[http2]

# Optional
brotlipy==0.7.*
-e .[http2,brotli]

# Documentation
mkdocs
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_packages(package):
],
extras_require={
"http2": "h2==3.*",
"brotli": "brotlipy==0.7.*",
"brotli": "brotlicffi==1.*",
},
classifiers=[
"Development Status :: 4 - Beta",
Expand Down
6 changes: 3 additions & 3 deletions tests/models/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pickle
from unittest import mock

import brotli
import brotlicffi
import pytest

import httpx
Expand Down Expand Up @@ -788,7 +788,7 @@ def test_link_headers(headers, expected):
def test_decode_error_with_request(header_value):
headers = [(b"Content-Encoding", header_value)]
body = b"test 123"
compressed_body = brotli.compress(body)[3:]
compressed_body = brotlicffi.compress(body)[3:]
with pytest.raises(httpx.DecodingError):
httpx.Response(
200,
Expand All @@ -809,7 +809,7 @@ def test_decode_error_with_request(header_value):
def test_value_error_without_request(header_value):
headers = [(b"Content-Encoding", header_value)]
body = b"test 123"
compressed_body = brotli.compress(body)[3:]
compressed_body = brotlicffi.compress(body)[3:]
with pytest.raises(httpx.DecodingError):
httpx.Response(200, headers=headers, content=compressed_body)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_decoders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import zlib

import brotli
import brotlicffi
import pytest

import httpx
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_gzip():

def test_brotli():
body = b"test 123"
compressed_body = brotli.compress(body)
compressed_body = brotlicffi.compress(body)

headers = [(b"Content-Encoding", b"br")]
response = httpx.Response(
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_multi():

def test_multi_with_identity():
body = b"test 123"
compressed_body = brotli.compress(body)
compressed_body = brotlicffi.compress(body)

headers = [(b"Content-Encoding", b"br, identity")]
response = httpx.Response(
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_decoders_empty_cases(decoder):
def test_decoding_errors(header_value):
headers = [(b"Content-Encoding", header_value)]
body = b"test 123"
compressed_body = brotli.compress(body)[3:]
compressed_body = brotlicffi.compress(body)[3:]
with pytest.raises(httpx.DecodingError):
request = httpx.Request("GET", "https://example.org")
httpx.Response(200, headers=headers, content=compressed_body, request=request)
Expand Down