-
Notifications
You must be signed in to change notification settings - Fork 389
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
Enable brotli decompression if it is available #620
base: master
Are you sure you want to change the base?
Enable brotli decompression if it is available #620
Conversation
9d94e14
to
c7b3b93
Compare
7fbcf70
to
3e31acf
Compare
3e31acf
to
36d72ea
Compare
@jairhenrique would you be able to look at this at your convenience? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compression is an ongoing field of research. This suggestion will make it easier to adopt new algorithms.
AVAILABLE_DECOMPRESSORS = {"gzip", "deflate"} | ||
if brotli is not None: | ||
AVAILABLE_DECOMPRESSORS.add("br") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is easier to expand with future or custom decompressors if AVAILABLE_DECOMPRESSORS
is a Dict[[str], Callable[[bytes], str]]
AVAILABLE_DECOMPRESSORS = {
"brotli": brotli.decompress,
"deflate": zlib.decompress,
"gzip": lambda body: zlib.decompress(body, zlib.MAX_WBITS | 16),
}
Then decompress_body
can just be
def decompress_body(body, encoding):
return AVAILABLE_DECOMPRESSORS[encoding](body)
Adding a new scheme will be as easy as adding a function to the dict.
df3997c
to
34d5384
Compare
This PR enables brotli decompression of responses if it is available (via
brotli
,brotlipy
orbrotlicffi
)I have added all three brotli variations to the test suite, but I'm at a bit of a loss, because
httpbin
brings inbrotlipy
unconditionally, so there is no real way to test:brotli.decompress(data)
API)Happy to hear any feedback, but also feel free to make any adjustments that feel right (I have enabled edits from maintainers).
Fixes #599