Skip to content

Commit

Permalink
fix: double reading error on urllib3 2.0
Browse files Browse the repository at this point in the history
Fixes psf#295

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed May 18, 2023
1 parent 27d3993 commit 738e005
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions cachecontrol/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def dumps(self, request, response, body=None):
# sure it acts as though it was never read.
body = response.read(decode_content=False)
response._fp = io.BytesIO(body)
response.length_remaining = len(body)

# NOTE: This is all a bit weird, but it's really important that on
# Python 2.x these objects are unicode and not str, even when
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ def stream(self, env, start_response):
for i in range(10):
yield pformat(i).encode("utf8")

def fixed_length(self, env, start_response):
body = b"0123456789"
headers = [
("Content-Type", "text/plain"),
("Cache-Control", "max-age=5000"),
("Content-Length", str(len(body)))
]
start_response("200 OK", headers)
return [body]

def __call__(self, env, start_response):
func = self.dispatch(env)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ def test_no_body_creates_response_file_handle_on_dumps(self, url):
# handle. Reading it again proves we're resetting the internal
# file handle with a buffer.
assert original_resp.raw.read()

def test_no_incomplete_read_on_dumps(self, url):
resp = requests.get(url + "fixed_length", stream=True)
self.serializer.dumps(resp.request, resp.raw)

assert resp.content == b"0123456789"

0 comments on commit 738e005

Please sign in to comment.