Accept-Encoding: gzip (take 2)#93
Open
shamilbi wants to merge 1 commit into
Open
Conversation
Closed
grische
requested changes
Jul 10, 2026
| Mirrorer(args).copy_server() | ||
|
|
||
|
|
||
| def download_req(index_url: str, req_name: str) -> tuple[dict, str]: |
Contributor
There was a problem hiding this comment.
Suggested change
| def download_req(index_url: str, req_name: str) -> tuple[dict, str]: | |
| def fetch_package_metadata(index_url: str, req_name: str) -> tuple[dict, str]: |
Please use a more descriptive name, like fetch_package_metadata
|
|
||
|
|
||
| def download_req(index_url: str, req_name: str) -> tuple[dict, str]: | ||
| # get information about this package from the Simple API in JSON |
Contributor
There was a problem hiding this comment.
As this method is only used by Mirrorer, please make it a Mirrorer method, not a global function
| }, | ||
| ) | ||
|
|
||
| response_url = "" |
Contributor
There was a problem hiding this comment.
Suggested change
| response_url = "" |
This line here is unnecessary
| response_url = "" | ||
| with urllib.request.urlopen(request) as response: # noqa: S310 | ||
| # Check if response is gzip-encoded | ||
| if response.headers.get("Content-Encoding") == "gzip": |
Contributor
There was a problem hiding this comment.
The comparison should be case-insensitive (GZIP is a valid encoding), and you could strip whitespaces in the process.
| with urllib.request.urlopen(request) as response: # noqa: S310 | ||
| # Check if response is gzip-encoded | ||
| if response.headers.get("Content-Encoding") == "gzip": | ||
| with gzip.GzipFile(fileobj=response) as gzip_response: |
Contributor
There was a problem hiding this comment.
A broken server or a corrupted or truncated body can raise a gzip.BadGzipFile or EOFError. We need to have a better error handling in case
A suggestion:
class MetadataError(RuntimeError):
"""The index returned an unusable metadata response."""
# and then inside the function's `with urllib.request.urlopen(...)` block:
try:
if response.headers.get("Content-Encoding") == "gzip":
with gzip.GzipFile(fileobj=response) as gzip_response:
data = json.load(gzip_response)
else:
data = json.load(response)
except (gzip.BadGzipFile, EOFError, zlib.error, json.JSONDecodeError) as err:
msg = f"Failed decoding metadata from {response.url}: {err}"
raise MetadataError(msg) from errand then expanding except (urllib.error.HTTPError, MetadataError) as err: in mirror().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
use "Accept-Encoding": "gzip" to reduce traffic
(take1: #86)