Skip to content

Accept-Encoding: gzip (take 2)#93

Open
shamilbi wants to merge 1 commit into
python-morgan:mainfrom
shamilbi:gzip2
Open

Accept-Encoding: gzip (take 2)#93
shamilbi wants to merge 1 commit into
python-morgan:mainfrom
shamilbi:gzip2

Conversation

@shamilbi

@shamilbi shamilbi commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

use "Accept-Encoding": "gzip" to reduce traffic
(take1: #86)

@shamilbi shamilbi mentioned this pull request Jul 8, 2026
Comment thread morgan/__init__.py
Mirrorer(args).copy_server()


def download_req(index_url: str, req_name: str) -> tuple[dict, str]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread morgan/__init__.py


def download_req(index_url: str, req_name: str) -> tuple[dict, str]:
# get information about this package from the Simple API in JSON

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As this method is only used by Mirrorer, please make it a Mirrorer method, not a global function

Comment thread morgan/__init__.py
},
)

response_url = ""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
response_url = ""

This line here is unnecessary

Comment thread morgan/__init__.py
response_url = ""
with urllib.request.urlopen(request) as response: # noqa: S310
# Check if response is gzip-encoded
if response.headers.get("Content-Encoding") == "gzip":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comparison should be case-insensitive (GZIP is a valid encoding), and you could strip whitespaces in the process.

Comment thread morgan/__init__.py
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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 err

and then expanding except (urllib.error.HTTPError, MetadataError) as err: in mirror().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants