even more evil, fixes my misstakes with chunked reading
authorRonny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
Tue, 17 Jan 2012 16:38:52 +0000 (17:38 +0100)
committerRonny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
Tue, 17 Jan 2012 16:38:52 +0000 (17:38 +0100)
requests/models.py
test_requests.py

index 728dbc9..4196376 100644 (file)
@@ -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()
 
index f6547de..dab3dd4 100755 (executable)
@@ -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)