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"
  • Loading branch information
radoering committed Feb 26, 2024
1 parent 036dcf1 commit b99e004
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 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
1 change: 1 addition & 0 deletions tests/inspection/test_lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def handle_request(
None,
(codes.method_not_allowed, b"Method not allowed"),
(codes.requested_range_not_satisfiable, b"Requested range not satisfiable"),
(codes.internal_server_error, b"Internal server error"),
(codes.not_implemented, b"Unsupported client range"),
(NEGATIVE_OFFSET_AS_POSITIVE, b"handle negative offset as positive"),
],
Expand Down

0 comments on commit b99e004

Please sign in to comment.