close the chunked fd at the end and honor content chunksizes for a potential first...
authorRonny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
Tue, 17 Jan 2012 17:14:47 +0000 (18:14 +0100)
committerRonny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
Tue, 17 Jan 2012 17:14:47 +0000 (18:14 +0100)
requests/models.py

index 5d45d7a477039edbec69dec33f532129ae863297..2e97694d13d9dc6c4bb6a37ed7dc52dba9fdc840 100644 (file)
@@ -615,11 +615,15 @@ class Response(object):
         def generate_chunked():
             resp = self.raw._original_response
             fp = resp.fp
-            if resp.chunk_left:
-                yield fp.read(resp.chunk_left)
-                fp.read(2) #throw away crlf
+            if resp.chunk_left is not None:
+                pending_bytes = resp.chunk_left
+                while pending_bytes:
+                    chunk = fp.read(min(chunk_size, pending_bytes))
+                    pending_bytes-=len(chunk)
+                    yield chunk
+                fp.read(2) # throw away crlf
             while 1:
-                #XXX correct line size
+                #XXX correct line size? (httplib has 64kb, seems insane)
                 pending_bytes = fp.readline(40).strip()
                 pending_bytes = int(pending_bytes, 16)
                 if pending_bytes == 0:
@@ -630,6 +634,7 @@ class Response(object):
                     yield chunk
                 fp.read(2) # throw away crlf
             self._content_consumed = True
+            fp.close()
 
 
         if getattr(getattr(self.raw, '_original_response', None), 'chunked', False):