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

Commit f13ca27

Browse files
committed
Pull out chunked logic from read()
1 parent c8b9bad commit f13ca27

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

hyper/http11/response.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,8 @@ def read(self, amt=None, decode_content=True):
9494
return b''
9595

9696
if self._chunked:
97-
# If we're doing a full read, read it as chunked and then just join
98-
# the chunks together!
99-
if amt is None:
100-
return self._buffered_data + b''.join(self.read_chunked())
101-
102-
if self._chunker is None:
103-
self._chunker = self.read_chunked()
104-
105-
# Otherwise, we have a certain amount of data we want to read.
106-
current_amount = len(self._buffered_data)
107-
108-
extra_data = [self._buffered_data]
109-
while current_amount < amt:
110-
try:
111-
chunk = next(self._chunker)
112-
except StopIteration:
113-
self.close()
114-
break
115-
116-
current_amount += len(chunk)
117-
extra_data.append(chunk)
97+
return self._normal_read_chunked(amt, decode_content)
11898

119-
data = b''.join(extra_data)
120-
self._buffered_data = data[amt:]
121-
return data[:amt]
12299

123100
# If we're asked to do a read without a length, we need to read
124101
# everything. That means either the entire content length, or until the
@@ -273,6 +250,36 @@ def _read_expect_closed(self, decode_content):
273250

274251
return data
275252

253+
def _normal_read_chunked(self, amt, decode_content):
254+
"""
255+
Implements the logic for calling ``read()`` on a chunked response.
256+
"""
257+
# If we're doing a full read, read it as chunked and then just join
258+
# the chunks together!
259+
if amt is None:
260+
return self._buffered_data + b''.join(self.read_chunked())
261+
262+
if self._chunker is None:
263+
self._chunker = self.read_chunked()
264+
265+
# Otherwise, we have a certain amount of data we want to read.
266+
current_amount = len(self._buffered_data)
267+
268+
extra_data = [self._buffered_data]
269+
while current_amount < amt:
270+
try:
271+
chunk = next(self._chunker)
272+
except StopIteration:
273+
self.close()
274+
break
275+
276+
current_amount += len(chunk)
277+
extra_data.append(chunk)
278+
279+
data = b''.join(extra_data)
280+
self._buffered_data = data[amt:]
281+
return data[:amt]
282+
276283
# The following methods implement the context manager protocol.
277284
def __enter__(self):
278285
return self

0 commit comments

Comments
 (0)