decoders.py
This file
Path
httpcore/decoders.py
146 lines · 3.6 KiB · Python
Last changed by
response.next()Tom Christiebefore adoptiond652479
History of this fileTake it
1"""2Handlers for Content-Encoding.34See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding5"""6import typing7import zlib89import httpcore.exceptions1011try:12 import brotli13except ImportError: # pragma: nocover14 brotli = None151617class Decoder:18 def decode(self, data: bytes) -> bytes:19 raise NotImplementedError() # pragma: nocover2021 def flush(self) -> bytes:22 raise NotImplementedError() # pragma: nocover232425class IdentityDecoder(Decoder):26 def decode(self, data: bytes) -> bytes:27 return data2829 def flush(self) -> bytes:30 return b""313233class DeflateDecoder(Decoder):34 """35 Handle 'deflate' decoding.3637 See: https://stackoverflow.com/questions/183869938 """3940 def __init__(self) -> None:41 self.decompressor = zlib.decompressobj(-zlib.MAX_WBITS)4243 def decode(self, data: bytes) -> bytes:44 try:45 return self.decompressor.decompress(data)46 except zlib.error as exc:47 raise httpcore.exceptions.DecodingError from exc4849 def flush(self) -> bytes:50 try:51 return self.decompressor.flush()52 except zlib.error as exc:53 raise httpcore.exceptions.DecodingError from exc545556class GZipDecoder(Decoder):57 """58 Handle 'gzip' decoding.5960 See: https://stackoverflow.com/questions/183869961 """6263 def __init__(self) -> None:64 self.decompressor = zlib.decompressobj(zlib.MAX_WBITS | 16)6566 def decode(self, data: bytes) -> bytes:67 try:68 return self.decompressor.decompress(data)69 except zlib.error as exc:70 raise httpcore.exceptions.DecodingError from exc7172 def flush(self) -> bytes:73 try:74 return self.decompressor.flush()75 except zlib.error as exc:76 raise httpcore.exceptions.DecodingError from exc777879class BrotliDecoder(Decoder):80 """81 Handle 'brotli' decoding.8283 Requires `pip install brotlipy`.84 See: https://brotlipy.readthedocs.io/85 """8687 def __init__(self) -> None:88 assert (89 brotli is not None90 ), "The 'brotlipy' library must be installed to use 'BrotliDecoder'"91 self.decompressor = brotli.Decompressor()9293 def decode(self, data: bytes) -> bytes:94 try:95 return self.decompressor.decompress(data)96 except brotli.Error as exc:97 raise httpcore.exceptions.DecodingError from exc9899 def flush(self) -> bytes:100 try:101 self.decompressor.finish()102 return b""103 except brotli.Error as exc:104 raise httpcore.exceptions.DecodingError from exc105106107class MultiDecoder(Decoder):108 """109 Handle the case where multiple encodings have been applied.110 """111112 def __init__(self, children: typing.Sequence[Decoder]) -> None:113 """114 children should be a sequence of decoders in the order in which115 each was applied.116 """117 # Note that we reverse the order for decoding.118 self.children = list(reversed(children))119120 def decode(self, data: bytes) -> bytes:121 for child in self.children:122 data = child.decode(data)123 return data124125 def flush(self) -> bytes:126 data = b""127 for child in self.children:128 data = child.decode(data) + child.flush()129 return data130131132SUPPORTED_DECODERS = {133 "identity": IdentityDecoder,134 "deflate": DeflateDecoder,135 "gzip": GZipDecoder,136 "br": BrotliDecoder,137}138139140if brotli is None:141 SUPPORTED_DECODERS.pop("br") # pragma: nocover142143144ACCEPT_ENCODING = ", ".join(145 [key for key in SUPPORTED_DECODERS.keys() if key != "identity"]146)