From 974f9287b84ecc83b2765a208c144b4bc25197b8 Mon Sep 17 00:00:00 2001 From: manushkin Date: Thu, 26 Dec 2024 10:45:02 +0100 Subject: [PATCH 01/10] Fixed http read_chunked for amt with negative value Param value `amt` can be negative, e.x. -1. In that case we read all data, with chunk separators, instead of correct reading. P.S. example of using -1 here: https://github.com/Textualize/rich/blob/master/rich/progress.py#L247 --- Lib/http/client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/http/client.py b/Lib/http/client.py index fab90a0ba4eb83..87f078dbfaaac0 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -590,6 +590,8 @@ def _get_chunk_left(self): def _read_chunked(self, amt=None): assert self.chunked != _UNKNOWN + if amt < 0: + amt = None value = [] try: while (chunk_left := self._get_chunk_left()) is not None: From 2685fe661363bb53e3c18f2dd7cedc3a5d425778 Mon Sep 17 00:00:00 2001 From: manushkin Date: Thu, 26 Dec 2024 11:28:00 +0100 Subject: [PATCH 02/10] Fixed http read_chunked for amt with negative value --- Lib/http/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index 87f078dbfaaac0..1c0332d82bd20b 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -590,7 +590,7 @@ def _get_chunk_left(self): def _read_chunked(self, amt=None): assert self.chunked != _UNKNOWN - if amt < 0: + if amt is not None and amt < 0: amt = None value = [] try: From 343a888b536d43081a9486eb31d11c0d5cdb55f1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 26 Dec 2024 11:00:04 +0000 Subject: [PATCH 03/10] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst b/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst new file mode 100644 index 00000000000000..64aeafeae44c96 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst @@ -0,0 +1,2 @@ +Fix incorrect handling of negative read sizes in :meth:`HTTPResponse.read +`. Patch by manushkin@gmail.com From f082c532f78a462b96da2702138e5c462123aec6 Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Thu, 26 Dec 2024 12:27:31 +0100 Subject: [PATCH 04/10] Added test --- Lib/test/test_httplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 89963dadeb152b..fbe36cd02edc2e 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1082,7 +1082,7 @@ def test_chunked(self): resp.close() # Various read sizes - for n in range(1, 12): + for n in list(range(1, 12)) + [-1, None]: sock = FakeSocket(chunked_start + last_chunk + chunked_end) resp = client.HTTPResponse(sock, method="GET") resp.begin() From 2cd0ead7358e9e665af8b0120a80fe38a47f5acc Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Thu, 26 Dec 2024 12:39:49 +0100 Subject: [PATCH 05/10] Improved test --- Lib/test/test_httplib.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index fbe36cd02edc2e..4e8255d10a04a9 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1081,8 +1081,17 @@ def test_chunked(self): self.assertEqual(resp.read(), expected) resp.close() + # explicit full read + for n in (-1, None): + with self.subTest('full read', n=n): + sock = FakeSocket(chunked_start + last_chunk + chunked_end) + resp = client.HTTPResponse(sock, method="GET") + resp.begin() + self.assertEqual(resp.read(n), expected) + resp.close() + # Various read sizes - for n in list(range(1, 12)) + [-1, None]: + for n in range(1, 12): sock = FakeSocket(chunked_start + last_chunk + chunked_end) resp = client.HTTPResponse(sock, method="GET") resp.begin() From 48bec8a7c5d0c624956d50830742ed5fc3a552e7 Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Thu, 26 Dec 2024 13:31:03 +0100 Subject: [PATCH 06/10] Simple commit to change email for cpython-cla-bot --- Lib/test/test_httplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 4e8255d10a04a9..ef6bf2c97d5d96 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1081,7 +1081,7 @@ def test_chunked(self): self.assertEqual(resp.read(), expected) resp.close() - # explicit full read + # Explicit full read for n in (-1, None): with self.subTest('full read', n=n): sock = FakeSocket(chunked_start + last_chunk + chunked_end) From 26b7b2e401ea4fd8aad612012efd55f5767761dc Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Fri, 27 Dec 2024 09:13:42 +0100 Subject: [PATCH 07/10] Improved test --- Lib/test/test_httplib.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index ef6bf2c97d5d96..035a3e58aa93e6 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1082,14 +1082,25 @@ def test_chunked(self): resp.close() # Explicit full read - for n in (-1, None): + for n in (-123, -1, None): with self.subTest('full read', n=n): sock = FakeSocket(chunked_start + last_chunk + chunked_end) resp = client.HTTPResponse(sock, method="GET") resp.begin() + self.assertTrue(resp.chunked) self.assertEqual(resp.read(n), expected) resp.close() + # Read first chunk + for n in (-1,): + with self.subTest('full read', n=n): + sock = FakeSocket(chunked_start + last_chunk + chunked_end) + resp = client.HTTPResponse(sock, method="GET") + resp.begin() + self.assertTrue(resp.chunked) + self.assertEqual(resp.read1(n), b"hello worl") + resp.close() + # Various read sizes for n in range(1, 12): sock = FakeSocket(chunked_start + last_chunk + chunked_end) From 4cd1232ac523afb7e8091093c40351a968e705da Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Fri, 27 Dec 2024 16:04:20 +0100 Subject: [PATCH 08/10] Improved test --- Lib/test/test_httplib.py | 15 +++++++-------- ...2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 035a3e58aa93e6..3b8baf837e805c 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1092,14 +1092,13 @@ def test_chunked(self): resp.close() # Read first chunk - for n in (-1,): - with self.subTest('full read', n=n): - sock = FakeSocket(chunked_start + last_chunk + chunked_end) - resp = client.HTTPResponse(sock, method="GET") - resp.begin() - self.assertTrue(resp.chunked) - self.assertEqual(resp.read1(n), b"hello worl") - resp.close() + with self.subTest('read1(-1)'): + sock = FakeSocket(chunked_start + last_chunk + chunked_end) + resp = client.HTTPResponse(sock, method="GET") + resp.begin() + self.assertTrue(resp.chunked) + self.assertEqual(resp.read1(-1), b"hello worl") + resp.close() # Various read sizes for n in range(1, 12): diff --git a/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst b/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst index 64aeafeae44c96..e885add7b68c0f 100644 --- a/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst +++ b/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst @@ -1,2 +1,2 @@ -Fix incorrect handling of negative read sizes in :meth:`HTTPResponse.read -`. Patch by manushkin@gmail.com +Fix incorrect handling of negative read sizes in :meth:`HTTPResponse.read +`. Patch by Yury Manushkin. From e348cec715aed8ddd95284cd5ea36606b1716c64 Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Fri, 27 Dec 2024 20:24:16 +0100 Subject: [PATCH 09/10] Improved case for reading not chunked data --- Lib/http/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/client.py b/Lib/http/client.py index 1c0332d82bd20b..33a858d34ae1ba 100644 --- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -472,7 +472,7 @@ def read(self, amt=None): if self.chunked: return self._read_chunked(amt) - if amt is not None: + if amt is not None and amt >= 0: if self.length is not None and amt > self.length: # clip the read to the "end of response" amt = self.length From 4120d98c1a3ba363244289017c7dfa795bf959f5 Mon Sep 17 00:00:00 2001 From: Yury Manushkin Date: Tue, 28 Jan 2025 12:13:05 +0100 Subject: [PATCH 10/10] Renamed gh-issue-128271 to gh-issue-112064 --- ....mCcw3B.rst => 2024-12-26-11-00-03.gh-issue-112064.mCcw3B.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst => 2024-12-26-11-00-03.gh-issue-112064.mCcw3B.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst b/Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-112064.mCcw3B.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-128271.mCcw3B.rst rename to Misc/NEWS.d/next/Library/2024-12-26-11-00-03.gh-issue-112064.mCcw3B.rst