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

Commit f041329

Browse files
committed
Move decoder to common code.
1 parent bb2f7fb commit f041329

File tree

3 files changed

+52
-80
lines changed

3 files changed

+52
-80
lines changed

hyper/common/decoder.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
hyper/common/decoder
4+
~~~~~~~~~~~~~~~~~~~~
5+
6+
Contains hyper's code for handling compressed bodies.
7+
"""
8+
import zlib
9+
10+
11+
class DeflateDecoder(object):
12+
"""
13+
This is a decoding object that wraps ``zlib`` and is used for decoding
14+
deflated content.
15+
16+
This rationale for the existence of this object is pretty unpleasant.
17+
The HTTP RFC specifies that 'deflate' is a valid content encoding. However,
18+
the spec _meant_ the zlib encoding form. Unfortunately, people who didn't
19+
read the RFC very carefully actually implemented a different form of
20+
'deflate'. Insanely, ``zlib`` handles them using two wbits values. This is
21+
such a mess it's hard to adequately articulate.
22+
23+
This class was lovingly borrowed from the excellent urllib3 library under
24+
license: see NOTICES. If you ever see @shazow, you should probably buy him
25+
a drink or something.
26+
"""
27+
def __init__(self):
28+
self._first_try = True
29+
self._data = b''
30+
self._obj = zlib.decompressobj(zlib.MAX_WBITS)
31+
32+
def __getattr__(self, name):
33+
return getattr(self._obj, name)
34+
35+
def decompress(self, data):
36+
if not self._first_try:
37+
return self._obj.decompress(data)
38+
39+
self._data += data
40+
try:
41+
return self._obj.decompress(data)
42+
except zlib.error:
43+
self._first_try = False
44+
self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
45+
try:
46+
return self.decompress(self._data)
47+
finally:
48+
self._data = None

hyper/http11/response.py

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,10 @@
99
import logging
1010
import zlib
1111

12-
log = logging.getLogger(__name__)
13-
14-
15-
class DeflateDecoder(object):
16-
"""
17-
This is a decoding object that wraps ``zlib`` and is used for decoding
18-
deflated content.
19-
20-
This rationale for the existence of this object is pretty unpleasant.
21-
The HTTP RFC specifies that 'deflate' is a valid content encoding. However,
22-
the spec _meant_ the zlib encoding form. Unfortunately, people who didn't
23-
read the RFC very carefully actually implemented a different form of
24-
'deflate'. Insanely, ``zlib`` handles them using two wbits values. This is
25-
such a mess it's hard to adequately articulate.
26-
27-
This class was lovingly borrowed from the excellent urllib3 library under
28-
license: see NOTICES. If you ever see @shazow, you should probably buy him
29-
a drink or something.
30-
"""
31-
def __init__(self):
32-
self._first_try = True
33-
self._data = b''
34-
self._obj = zlib.decompressobj(zlib.MAX_WBITS)
12+
from ..common.decoder import DeflateDecoder
13+
from ..http20.exceptions import ConnectionResetError
3514

36-
def __getattr__(self, name):
37-
return getattr(self._obj, name)
38-
39-
def decompress(self, data):
40-
if not self._first_try:
41-
return self._obj.decompress(data)
42-
43-
self._data += data
44-
try:
45-
return self._obj.decompress(data)
46-
except zlib.error:
47-
self._first_try = False
48-
self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
49-
try:
50-
return self.decompress(self._data)
51-
finally:
52-
self._data = None
15+
log = logging.getLogger(__name__)
5316

5417

5518
class HTTP11Response(object):

hyper/http20/response.py

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,12 @@
99
import logging
1010
import zlib
1111

12+
from ..common.decoder import DeflateDecoder
1213
from .util import pop_from_key_value_set
1314

1415
log = logging.getLogger(__name__)
1516

1617

17-
class DeflateDecoder(object):
18-
"""
19-
This is a decoding object that wraps ``zlib`` and is used for decoding
20-
deflated content.
21-
22-
This rationale for the existence of this object is pretty unpleasant.
23-
The HTTP RFC specifies that 'deflate' is a valid content encoding. However,
24-
the spec _meant_ the zlib encoding form. Unfortunately, people who didn't
25-
read the RFC very carefully actually implemented a different form of
26-
'deflate'. Insanely, ``zlib`` handles them using two wbits values. This is
27-
such a mess it's hard to adequately articulate.
28-
29-
This class was lovingly borrowed from the excellent urllib3 library under
30-
license: see NOTICES. If you ever see @shazow, you should probably buy him
31-
a drink or something.
32-
"""
33-
def __init__(self):
34-
self._first_try = True
35-
self._data = b''
36-
self._obj = zlib.decompressobj(zlib.MAX_WBITS)
37-
38-
def __getattr__(self, name):
39-
return getattr(self._obj, name)
40-
41-
def decompress(self, data):
42-
if not self._first_try:
43-
return self._obj.decompress(data)
44-
45-
self._data += data
46-
try:
47-
return self._obj.decompress(data)
48-
except zlib.error:
49-
self._first_try = False
50-
self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
51-
try:
52-
return self.decompress(self._data)
53-
finally:
54-
self._data = None
55-
56-
5718
class Headers(object):
5819
def __init__(self, pairs):
5920
# This conversion to dictionary is unwise, as there may be repeated

0 commit comments

Comments
 (0)