Skip to content

Commit

Permalink
lazy-wheel: handle unexpected status codes as "negative offsets not s…
Browse files Browse the repository at this point in the history
…upported"

not all servers return "method not allowed" or "not implemented", but for example also "internal server error" or "not found"

(cherry picked from commit 8c9a80e)
  • Loading branch information
radoering authored and abn committed Feb 26, 2024
1 parent ed8cb8b commit 465aa8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
35 changes: 17 additions & 18 deletions src/poetry/inspection/lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,22 +608,10 @@ def _extract_content_length(
# Initial range request for just the end of the file.
file_length, tail = self._try_initial_chunk_request(initial_chunk_size)
except HTTPError as e:
# Our initial request using a negative byte range was not supported.
resp = e.response
code = resp.status_code if resp is not None else None
# Our initial request using a negative byte range was not supported.
if code in [codes.not_implemented, codes.method_not_allowed]:
# pypi notably does not support negative byte ranges: see
# https://github.com/pypi/warehouse/issues/12823.
logger.debug(
"Negative byte range not supported for domain '%s': "
"using HEAD request before lazy wheel from now on",
domain,
)
# Avoid trying a negative byte range request against this domain for the
# rest of the resolve.
self._domains_without_negative_range.add(domain)
# Apply a HEAD request to get the real size, and nothing else for now.
return (self._content_length_from_head(), None)

# This indicates that the requested range from the end was larger than the
# actual file size: https://www.rfc-editor.org/rfc/rfc9110#status.416.
if code == codes.requested_range_not_satisfiable:
Expand All @@ -633,10 +621,21 @@ def _extract_content_length(
file_length = self._parse_full_length_from_content_range(
resp.headers["Content-Range"]
)
return (file_length, None)
# If we get some other error, then we expect that non-range requests will
# also fail, so we error out here and let the user figure it out.
raise
return file_length, None

# pypi notably does not support negative byte ranges: see
# https://github.com/pypi/warehouse/issues/12823.
logger.debug(
"Negative byte range not supported for domain '%s': "
"using HEAD request before lazy wheel from now on (code: %s)",
domain,
code,
)
# Avoid trying a negative byte range request against this domain for the
# rest of the resolve.
self._domains_without_negative_range.add(domain)
# Apply a HEAD request to get the real size, and nothing else for now.
return self._content_length_from_head(), None

# Some servers that do not support negative offsets,
# handle a negative offset like "-10" as "0-10".
Expand Down
4 changes: 3 additions & 1 deletion tests/inspection/test_lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ def handle_request(
"negative_offset_error",
[
None,
(codes.not_found, b"Not found"), # Nexus
(codes.method_not_allowed, b"Method not allowed"),
(codes.requested_range_not_satisfiable, b"Requested range not satisfiable"),
(codes.not_implemented, b"Unsupported client range"),
(codes.internal_server_error, b"Internal server error"), # GAR
(codes.not_implemented, b"Unsupported client range"), # PyPI
(NEGATIVE_OFFSET_AS_POSITIVE, b"handle negative offset as positive"),
],
)
Expand Down

0 comments on commit 465aa8c

Please sign in to comment.