Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 14 additions & 5 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
# <https://github.com/httpie/cli/issues/423>
try:
total_size = int(final_response.headers['Content-Length'])
except (KeyError, ValueError, TypeError):
# When Content-Encoding is set (e.g. gzip), the requests library
# transparently decompresses the response body. This means the
# actual bytes written will differ from Content-Length (which
# reflects the compressed size). In this case, we cannot rely
# on Content-Length for progress/completeness tracking.
# See: https://github.com/httpie/cli/issues/423
# See: https://github.com/httpie/cli/issues/1642
content_encoding = final_response.headers.get('Content-Encoding')
if content_encoding:
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
30 changes: 30 additions & 0 deletions tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,36 @@ def test_download_resumed(self, mock_env, httpbin_both):
downloader.chunk_downloaded(b'45')
downloader.finish()

def test_download_with_Content_Encoding_gzip_not_interrupted(self, mock_env, httpbin_both):
"""
When Content-Encoding is set (e.g. gzip), the requests library
transparently decompresses the body, so the downloaded bytes
will differ from Content-Length (which is the compressed size).
httpie should NOT flag this as an interrupted download.

Regression test for https://github.com/httpie/cli/issues/1642
"""
with open(os.devnull, 'w') as devnull:
downloader = Downloader(mock_env, output_file=devnull)
# Simulate a gzip-encoded response where Content-Length is
# the compressed size (100 bytes) but the decompressed body
# is larger (500 bytes).
downloader.start(
initial_url='/',
final_response=Response(
url=httpbin_both.url + '/',
headers={
'Content-Length': 100,
'Content-Encoding': 'gzip',
}
)
)
# Simulate receiving decompressed data (larger than Content-Length)
downloader.chunk_downloaded(b'x' * 500)
downloader.finish()
# Should NOT be flagged as interrupted
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
Loading