Merge branch 'streaming' of https://github.com/RonnyPfannschmidt/requests into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 557480c, 7f9cef0 · Browse files · 2 files·+41 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+41 added−5 removed
- other:+41 added−5 removed
Provenance
- Commit
f3ba022- Tree
8b3ba2c- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
557480c- Parent 2
7f9cef0
2 files changed+41 added−5 removed
modified:requests/models.py+39 added-3 removed
609 unmodified lines610 if not chunk:611 break612 yield chunk613 self._content_consumed = True614+ 615+ def generate_chunked():616+ resp = self.raw._original_response617+ fp = resp.fp618+ if resp.chunk_left is not None:619+ pending_bytes = resp.chunk_left620+ while pending_bytes:621+ chunk = fp.read(min(chunk_size, pending_bytes))622+ pending_bytes-=len(chunk)623+ yield chunk624+ fp.read(2) # throw away crlf625+ while 1:626+ #XXX correct line size? (httplib has 64kb, seems insane)627+ pending_bytes = fp.readline(40).strip()628+ pending_bytes = int(pending_bytes, 16)629+ if pending_bytes == 0:630+ break631+ while pending_bytes:632+ chunk = fp.read(min(chunk_size, pending_bytes))633+ pending_bytes-=len(chunk)634+ yield chunk635+ fp.read(2) # throw away crlf636+ self._content_consumed = True637+ fp.close()638 615- gen = generate()639 640+ if getattr(getattr(self.raw, '_original_response', None), 'chunked', False):641+ gen = generate_chunked()642+ else:643+ gen = generate()644+645 if 'gzip' in self.headers.get('content-encoding', ''):646 gen = stream_decompress(gen, mode='gzip')647 elif 'deflate' in self.headers.get('content-encoding', ''):648 gen = stream_decompress(gen, mode='deflate')11 unmodified lines660 """Iterates over the response data, one line at a time. This661 avoids reading the content at once into memory for large662 responses.663 """636-664+ #XXX: why rstrip by default665 pending = None666 for chunk in self.iter_content(chunk_size, decode_unicode=decode_unicode):667 if pending is not None:668 chunk = pending + chunk669 lines = chunk.splitlines(True)670 for line in lines[:-1]:671 yield line.rstrip()672 # Save the last part of the chunk for next iteration, to keep full line together673+ # lines may be empty for the last chunk of a chunked response674+ if lines:645- pending = lines[-1]675+ pending = lines[-1]676+ #if pending is a complete line, give it baack677+ if pending[-1] == '\n':678+ yield pending.rstrip()679+ pending = None680+ else:681+ pending = None682 683 # Yield the last line684 if pending is not None:685 yield pending.rstrip()58 unmodified linesmodified:test_requests.py+2 added-2 removed
606 unmodified lines607 608 lines = (0, 2, 10, 100)609 610 for i in lines:611-611 r = requests.get(httpbin('stream', str(i)), prefetch=False)613- len_lines = len([l for l in r.iter_lines()])612+ lines = list(r.iter_lines())613+ len_lines = len(lines)614 615 self.assertEqual(i, len_lines)616 617 # Test 'dangling' fragment in responses that do not terminate in41 unmodified lines