From ce911c3fed7e6404fba7de3868b1000425984b1e Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Thu, 17 Mar 2016 06:42:48 +0000 Subject: Issue #26499: Fixes to HTTPResponse.readline() and read1(), by Silent Ghost --- Lib/http/client.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'Lib/http') diff --git a/Lib/http/client.py b/Lib/http/client.py index 24231b51b7..2d6e07bbfc 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -635,6 +635,8 @@ class HTTPResponse(io.BufferedIOBase): return b"" if self.chunked: return self._read1_chunked(n) + if self.length is not None and (n < 0 or n > self.length): + n = self.length try: result = self.fp.read1(n) except ValueError: @@ -645,6 +647,8 @@ class HTTPResponse(io.BufferedIOBase): result = self.fp.read1(16*1024) if not result and n: self._close_conn() + elif self.length is not None: + self.length -= len(result) return result def peek(self, n=-1): @@ -662,9 +666,13 @@ class HTTPResponse(io.BufferedIOBase): if self.chunked: # Fallback to IOBase readline which uses peek() and read() return super().readline(limit) + if self.length is not None and (limit < 0 or limit > self.length): + limit = self.length result = self.fp.readline(limit) if not result and limit: self._close_conn() + elif self.length is not None: + self.length -= len(result) return result def _read1_chunked(self, n): -- cgit v1.2.1