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

Commit e118dad

Browse files
KOLANICHLukasa
authored andcommitted
Added a dictionary of decompressors (#336)
* Added a dictionary of decompressors * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py * Update response.py
1 parent 8345852 commit e118dad

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

hyper/http20/response.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def strip_headers(headers):
2929
del headers[name]
3030

3131

32+
decompressors = {
33+
b'gzip': lambda: zlib.decompressobj(16 + zlib.MAX_WBITS),
34+
b'deflate': DeflateDecoder
35+
}
36+
37+
3238
class HTTP20Response(object):
3339
"""
3440
An ``HTTP20Response`` wraps the HTTP/2 response from the server. It
@@ -39,6 +45,7 @@ class HTTP20Response(object):
3945
"""
4046

4147
version = HTTPVersion.http20
48+
_decompressobj = None
4249

4350
def __init__(self, headers, stream):
4451
#: The reason phrase returned by the server. This is not used in
@@ -71,12 +78,9 @@ def __init__(self, headers, stream):
7178
# This 16 + MAX_WBITS nonsense is to force gzip. See this
7279
# Stack Overflow answer for more:
7380
# http://stackoverflow.com/a/2695466/1401686
74-
if b'gzip' in self.headers.get(b'content-encoding', []):
75-
self._decompressobj = zlib.decompressobj(16 + zlib.MAX_WBITS)
76-
elif b'deflate' in self.headers.get(b'content-encoding', []):
77-
self._decompressobj = DeflateDecoder()
78-
else:
79-
self._decompressobj = None
81+
for c in self.headers.get(b'content-encoding', []):
82+
self._decompressobj = decompressors.get(c)()
83+
break
8084

8185
@property
8286
def trailers(self):

0 commit comments

Comments
 (0)