mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
even more evil, fixes my misstakes with chunked reading
This commit is contained in:
+12
-5
@@ -615,12 +615,12 @@ class Response(object):
|
||||
def generate_chunked():
|
||||
resp = self.raw._original_response
|
||||
fp = resp.fp
|
||||
yield fp.read(resp.chunk_left)
|
||||
if resp.chunk_left:
|
||||
yield fp.read(resp.chunk_left)
|
||||
fp.read(2) #throw away crlf
|
||||
while 1:
|
||||
#XXX correct line size
|
||||
pending_bytes = fp.readline(80).strip()
|
||||
if not pending_bytes:
|
||||
break
|
||||
pending_bytes = fp.readline(40).strip()
|
||||
pending_bytes = int(pending_bytes, 16)
|
||||
if pending_bytes == 0:
|
||||
break
|
||||
@@ -628,11 +628,18 @@ class Response(object):
|
||||
chunk = fp.read(min(chunk_size, pending_bytes))
|
||||
pending_bytes-=len(chunk)
|
||||
yield chunk
|
||||
fp.read(2) # throw away crlf
|
||||
self._content_consumed = True
|
||||
|
||||
|
||||
if getattr(self.raw._original_response, 'chunked', False):
|
||||
if getattr(getattr(self.raw, '_original_response', None), 'chunked', False):
|
||||
gen = generate_chunked()
|
||||
|
||||
def hack_gen(gen=gen):
|
||||
for item in gen:
|
||||
print repr(item)
|
||||
yield item
|
||||
gen = hack_gen()
|
||||
else:
|
||||
gen = generate()
|
||||
|
||||
|
||||
+2
-2
@@ -608,9 +608,9 @@ class RequestsTestSuite(unittest.TestCase):
|
||||
lines = (0, 2, 10, 100)
|
||||
|
||||
for i in lines:
|
||||
|
||||
r = requests.get(httpbin('stream', str(i)), prefetch=False)
|
||||
len_lines = len([l for l in r.iter_lines()])
|
||||
lines = list(r.iter_lines())
|
||||
len_lines = len(lines)
|
||||
|
||||
self.assertEqual(i, len_lines)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user