From 8e9138f37f1e28413c1df814e65089105a7eea22 Mon Sep 17 00:00:00 2001 From: lixiaoyong Date: Fri, 10 May 2024 05:29:04 -0400 Subject: [PATCH] Fix HTTPResponse.read(0) when underlying buffer is empty --- ...ad-0-when-underlying-buffer-is-empty.patch | 60 +++++++++++++++++++ python-urllib3.spec | 9 ++- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 backport-Fix-HTTPResponse.read-0-when-underlying-buffer-is-empty.patch diff --git a/backport-Fix-HTTPResponse.read-0-when-underlying-buffer-is-empty.patch b/backport-Fix-HTTPResponse.read-0-when-underlying-buffer-is-empty.patch new file mode 100644 index 0000000..749fc56 --- /dev/null +++ b/backport-Fix-HTTPResponse.read-0-when-underlying-buffer-is-empty.patch @@ -0,0 +1,60 @@ +From 02ae65a45654bb3ced12b9ad22278c11e214aaf8 Mon Sep 17 00:00:00 2001 +From: Quentin Pradet +Date: Sun, 30 Apr 2023 00:29:17 +0400 +Subject: [PATCH] Fix HTTPResponse.read(0) when underlying buffer is empty + (#2998) + +Reference: https://github.com/urllib3/urllib3/commit/02ae65a45654bb3ced12b9ad22278c11e214aaf8 +--- + changelog/2998.bugfix.rst | 1 + + src/urllib3/response.py | 4 +++- + test/test_response.py | 3 +++ + 3 files changed, 7 insertions(+), 1 deletion(-) + create mode 100644 changelog/2998.bugfix.rst + +diff --git a/changelog/2998.bugfix.rst b/changelog/2998.bugfix.rst +new file mode 100644 +index 00000000..584f309a +--- /dev/null ++++ b/changelog/2998.bugfix.rst +@@ -0,0 +1 @@ ++Fixed ``HTTPResponse.read(0)`` call when underlying buffer is empty. +diff --git a/src/urllib3/response.py b/src/urllib3/response.py +index 69e1bd66..d8506875 100644 +--- a/src/urllib3/response.py ++++ b/src/urllib3/response.py +@@ -244,7 +244,9 @@ class BytesQueueBuffer: + self._size += len(data) + + def get(self, n: int) -> bytes: +- if not self.buffer: ++ if n == 0: ++ return b"" ++ elif not self.buffer: + raise RuntimeError("buffer is empty") + elif n < 0: + raise ValueError("n should be > 0") +diff --git a/test/test_response.py b/test/test_response.py +index 1a296b68..6b5b1a17 100644 +--- a/test/test_response.py ++++ b/test/test_response.py +@@ -44,6 +44,8 @@ class TestBytesQueueBuffer: + with pytest.raises(RuntimeError, match="buffer is empty"): + assert buffer.get(10) + ++ assert buffer.get(0) == b"" ++ + buffer.put(b"foo") + with pytest.raises(ValueError, match="n should be > 0"): + buffer.get(-1) +@@ -181,6 +183,7 @@ class TestResponse: + fp = BytesIO(b"foo") + r = HTTPResponse(fp, preload_content=False) + ++ assert r.read(0) == b"" + assert r.read(1) == b"f" + assert r.read(2) == b"oo" + assert r.read() == b"" +-- +2.18.2 + diff --git a/python-urllib3.spec b/python-urllib3.spec index 48222ee..5741aad 100644 --- a/python-urllib3.spec +++ b/python-urllib3.spec @@ -3,7 +3,7 @@ Name: python-%{srcname} Version: 1.26.18 -Release: 1 +Release: 2 Summary: Sanity-friendly HTTP client for Python License: MIT URL: https://urllib3.readthedocs.io @@ -11,6 +11,7 @@ Source0: https://github.com/urllib3/urllib3/archive/refs/tags/%{version}. Source1: ssl_match_hostname_py3.py Patch0001: remove_mock.patch +Patch0002: backport-Fix-HTTPResponse.read-0-when-underlying-buffer-is-empty.patch BuildArch: noarch @@ -76,6 +77,12 @@ PYTHONPATH=%{buildroot}%{python3_sitelib}:%{python3_sitelib} %{__python3} -m pyt %{python3_sitelib}/urllib3-*.egg-info %changelog +* Fri May 10 2024 lixiaoyong - 1.26.18-2 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:Fix HTTPResponse.read(0) when underlying buffer is empty + * Tue Feb 06 2024 chengyechun - 1.26.18-1 - Type:enhancement - ID:NA -- Gitee