question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DownloadLengthMismatchError because requests automatically decodes gzip files

See original GitHub issue

Description of issue or feature request:

If target is a gzip file, served with a Content-Encoding: gzip header, requests will automatically decode the file.

Current behavior:

As a result, the default fetcher for tuf ngclient raises a DownloadLengthMismatchError:

tuf.api.exceptions.DownloadLengthMismatchError: Downloaded x bytes exceeding the maximum allowed length of y

Expected behavior:

From the requests docs:

An important note about using Response.iter_content versus Response.raw. Response.iter_content will automatically decode the gzip and deflate transfer-encodings. Response.raw is a raw stream of bytes – it does not transform the response content. If you really need access to the bytes as they were returned, use Response.raw.

I would expect tuf to access the Reponse.raw instead of Response.iter_content here:

https://github.com/theupdateframework/python-tuf/blob/b5e952cee3edb1c3363a8a7859cb548b13ab71f3/tuf/ngclient/_internal/requests_fetcher.py#L99

Note that Response.raw is a urllib3.response.HTTPResponse, which also decodes gzip encoded content by default, but requests disables this by setting decode_content=False.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lukpuehcommented, Jul 20, 2022
0reactions
dennisvangcommented, Jul 7, 2022

@joshuagl I believe PR #2048 does the job, although I’m not entirely sure my method of testing is acceptable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Downloading gzip files is broken · Issue #4346 · psf/requests
I'm trying to download a binary gzip file with a GET request. ... It seems that requests is not only automatically decoding the...
Read more >
Having Trouble Getting requests==2.7.0 to Automatically ...
In Python 3 you can handle decoding as a stream by using the gzip module and streaming the response: import gzip import requests...
Read more >
gzip — Support for gzip files — Python 3.11.1 documentation
The GzipFile class reads and writes gzip-format files, automatically compressing or decompressing the data so that it looks like an ordinary file object....
Read more >
Compressed HTTP Requests with Curl and PHP • PHP.Watch
How to make fast and efficient HTTP(S) requests via Curl using transfer encodings such as gzip, zstd, Brotli, and deflate.
Read more >
Spring application with Gzip compression
As you can see, the client automatically decodes the response according to the gzip encoding specified by Content-Encoding . Since the response ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found