Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions httpie/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,21 @@ def start(
"""
assert not self.status.time_started

# FIXME: some servers still might sent Content-Encoding: gzip
# When Content-Encoding is present (e.g. gzip), requests/urllib3
# transparently decompresses the body. Content-Length reflects the
# *compressed* size per RFC 9110 §8.6, so it must be ignored for
# download progress tracking — otherwise we flag a complete
# download as "Incomplete download".
# <https://github.com/httpie/cli/issues/1642>
# <https://github.com/httpie/cli/issues/423>
try:
total_size = int(final_response.headers['Content-Length'])
except (KeyError, ValueError, TypeError):
content_encoding = final_response.headers.get('Content-Encoding')
if content_encoding and content_encoding.lower() != 'identity':
total_size = None
else:
try:
total_size = int(final_response.headers['Content-Length'])
except (KeyError, ValueError, TypeError):
total_size = None

if not self._output_file:
self._output_file = self._get_output_file_from_response(
Expand Down
70 changes: 70 additions & 0 deletions tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,76 @@ def test_download_resumed(self, mock_env, httpbin_both):
downloader.chunk_downloaded(b'45')
downloader.finish()

def test_download_with_content_encoding_gzip(self, mock_env, httpbin_both):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

praise: The added tests make the distinction between the gzip and identity paths much easier to understand and help clarify the intent behind the new logic.

"""When Content-Encoding: gzip is set, Content-Length reflects the
compressed size but the body is transparently decompressed by
requests/urllib3. The download should not be flagged as incomplete.

<https://github.com/httpie/cli/issues/1642>
"""
with open(os.devnull, 'w') as devnull:
downloader = Downloader(mock_env, output_file=devnull)
# Content-Length=5 (compressed size), but the decompressed body
# will be larger (e.g. 100 bytes).
downloader.start(
initial_url='/',
final_response=Response(
url=httpbin_both.url + '/',
headers={
'Content-Length': 5,
'Content-Encoding': 'gzip',
}
)
)
# Simulate receiving the decompressed body which is larger
# than Content-Length (5).
downloader.chunk_downloaded(b'x' * 100)
downloader.finish()
assert not downloader.interrupted

def test_download_with_content_encoding_identity_uses_content_length(
self, mock_env, httpbin_both
):
"""When Content-Encoding is 'identity', Content-Length should
still be used for progress tracking (no decompression)."""
with open(os.devnull, 'w') as devnull:
downloader = Downloader(mock_env, output_file=devnull)
downloader.start(
initial_url='/',
final_response=Response(
url=httpbin_both.url + '/',
headers={
'Content-Length': 10,
'Content-Encoding': 'identity',
}
)
)
downloader.chunk_downloaded(b'12345')
downloader.chunk_downloaded(b'12345')
downloader.finish()
assert not downloader.interrupted

def test_download_with_content_encoding_gzip_interrupted(self, mock_env, httpbin_both):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

question: The test name and docstring suggest that an interrupted gzip download should be flagged, but the assertion expects downloader.interrupted to be False. Is the current behavior intentional, or should the expectation be updated?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The behavior is intentional — when Content-Encoding is set, Content-Length reflects the compressed size, so we set total_size=None. With no expected size, the download tracker cannot determine the download was interrupted, hence interrupted=False. Updated the test docstring to clarify this in the latest push.

"""Even with Content-Encoding, an actually interrupted download
(no chunks received) should still be flagged."""
with open(os.devnull, 'w') as devnull:
downloader = Downloader(mock_env, output_file=devnull)
downloader.start(
initial_url='/',
final_response=Response(
url=httpbin_both.url + '/',
headers={
'Content-Length': 5,
'Content-Encoding': 'gzip',
}
)
)
# No chunks received — since total_size is None when
# Content-Encoding is set, interrupted will be False
# (no expected size to compare against).
downloader.finish()
assert not downloader.interrupted

def test_download_with_redirect_original_url_used_for_filename(self, httpbin):
# Redirect from `/redirect/1` to `/get`.
expected_filename = '1.json'
Expand Down