Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 3a18105

Browse files
committed
Add support for brotli compression over HTTP/1.1
1 parent 36bf309 commit 3a18105

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

hyper/http11/response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import weakref
1111
import zlib
12+
import brotli
1213

1314
from ..common.decoder import DeflateDecoder
1415
from ..common.exceptions import ChunkedDecodeError, InvalidResponseError
@@ -88,6 +89,8 @@ def __init__(self, code, reason, headers, sock, connection=None,
8889
# http://stackoverflow.com/a/2695466/1401686
8990
if b'gzip' in self.headers.get(b'content-encoding', []):
9091
self._decompressobj = zlib.decompressobj(16 + zlib.MAX_WBITS)
92+
elif b'br' in self.headers.get(b'content-encoding', []):
93+
self._decompressobj = brotli.Decompressor()
9194
elif b'deflate' in self.headers.get(b'content-encoding', []):
9295
self._decompressobj = DeflateDecoder()
9396
else:

test/test_http11.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import os
99
import zlib
10+
import brotli
1011

1112
from collections import namedtuple
1213
from io import BytesIO, StringIO
@@ -644,6 +645,16 @@ def test_response_transparently_decrypts_gzip(self):
644645

645646
assert r.read() == b'this is test data'
646647

648+
def test_response_transparently_decrypts_brotli(self):
649+
d = DummySocket()
650+
headers = {b'content-encoding': [b'br'], b'connection': [b'close']}
651+
r = HTTP11Response(200, 'OK', headers, d, None)
652+
653+
body = brotli.compress(b'this is test data')
654+
d._buffer = BytesIO(body)
655+
656+
assert r.read() == b'this is test data'
657+
647658
def test_response_transparently_decrypts_real_deflate(self):
648659
d = DummySocket()
649660
headers = {

0 commit comments

Comments
 (0)