Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/poetry/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def _get(self, start: int = 0) -> Response:
)
try:
response.raise_for_status()
if start > 0 and response.status_code != 206:
raise ChunkedEncodingError(
f"Server ignored the Range request while resuming {self._url}."
)
Comment on lines 102 to +106

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): A resumed response with an HTTP error status such as 416 or 500 raises requests' HTTPError before the new status check runs, so it does not raise the promised ChunkedEncodingError for every non-206 resumed response.

Triggers: When the server responds to the resume request with a non-206 error status.

Suggested fix: Check response.status_code != 206 before raise_for_status() (or convert the resulting HTTPError to ChunkedEncodingError) while still closing the response.

Suggested change
response.raise_for_status()
if start > 0 and response.status_code != 206:
raise ChunkedEncodingError(
f"Server ignored the Range request while resuming {self._url}."
)
if start > 0 and response.status_code != 206:
raise ChunkedEncodingError(
f"Server ignored the Range request while resuming {self._url}."
)
response.raise_for_status()

return response
except BaseException:
response.close()
Expand Down
35 changes: 31 additions & 4 deletions tests/utils/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ def handle_request(request: PreparedRequest) -> HttpResponse:
assert http.calls[-1].request.headers["Range"] == f"bytes={file_length // 2}-"


def test_download_file_fails_if_server_ignores_resume_range(
http: responses.RequestsMock, tmp_path: Path
) -> None:
body = b"abcdefgh"
url = "https://foo.com/archive.tar.gz"

def handle_request(request: PreparedRequest) -> HttpResponse:
if "Range" not in request.headers:
return 200, {"Content-Length": "8", "Accept-Ranges": "bytes"}, body[:4]
return 200, {"Content-Length": "8"}, body

http.add_callback(responses.GET, url, callback=handle_request)
dest = tmp_path / "archive.tar.gz"

with pytest.raises(ChunkedEncodingError, match=r"ignored.*Range"):
download_file(url, dest, chunk_size=4, max_retries=1)

assert http.calls[-1].request.headers["Range"] == "bytes=4-"
assert not dest.exists()


def test_download_file_fail_when_no_range(
http: responses.RequestsMock, fixture_dir: FixtureDirGetter, tmp_path: Path
) -> None:
Expand Down Expand Up @@ -205,6 +226,9 @@ class _Resp:
"Content-Length": "8",
}

def __init__(self, status_code: int) -> None:
self.status_code = status_code

def raise_for_status(self) -> None: ...
def close(self) -> None: ...
def __enter__(self) -> _Resp:
Expand All @@ -220,8 +244,8 @@ def iter_content(self, chunk_size: int = 1) -> Iterator[bytes]:
raise exc("stub failure")

class _Session:
def get(self, *_: object, **__: object) -> _Resp:
return _Resp()
def get(self, *_: object, headers: dict[str, str], **__: object) -> _Resp:
return _Resp(status_code=206 if "Range" in headers else 200)

downloader = Downloader(
"https://example.invalid/x",
Expand Down Expand Up @@ -252,6 +276,9 @@ class _Resp:
"Content-Length": "8",
}

def __init__(self, status_code: int) -> None:
self.status_code = status_code

def raise_for_status(self) -> None: ...
def close(self) -> None: ...
def __enter__(self) -> _Resp:
Expand All @@ -266,8 +293,8 @@ def iter_content(self, chunk_size: int = 1) -> Iterator[bytes]:
raise requests.exceptions.ConnectionError("stub failure")

class _Session:
def get(self, *_: object, **__: object) -> _Resp:
return _Resp()
def get(self, *_: object, headers: dict[str, str], **__: object) -> _Resp:
return _Resp(status_code=206 if "Range" in headers else 200)

downloader = Downloader(
"https://example.invalid/x",
Expand Down
Loading