Skip to content

Commit 160bc6a

Browse files
committed
fix: handle missing Content-Length header in download_file
1 parent 0abb3b9 commit 160bc6a

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

comfy_cli/file_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ def download_file(url: str, local_filepath: pathlib.Path, headers: dict | None =
6969

7070
with httpx.stream("GET", url, follow_redirects=True, headers=headers) as response:
7171
if response.status_code == 200:
72-
total = int(response.headers["Content-Length"])
72+
content_length = response.headers.get("Content-Length")
73+
total = int(content_length) if content_length is not None else None
74+
if total is not None:
75+
description = f"Downloading {total // 1024 // 1024} MB"
76+
else:
77+
description = "Downloading..."
7378
try:
7479
with open(local_filepath, "wb") as f:
7580
for data in ui.show_progress(
7681
response.iter_bytes(),
7782
total,
78-
description=f"Downloading {total // 1024 // 1024} MB",
83+
description=description,
7984
):
8085
f.write(data)
8186
except KeyboardInterrupt:

tests/test_file_utils_network.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ def test_download_file_success(mock_stream, tmp_path):
8585
assert test_file.read_bytes() == b"test data"
8686

8787

88+
@patch("httpx.stream")
89+
def test_download_file_success_without_content_length(mock_stream, tmp_path):
90+
"""Download should succeed when Content-Length header is missing (e.g. chunked/gzip responses)."""
91+
mock_response = Mock()
92+
mock_response.status_code = 200
93+
mock_response.headers = {}
94+
mock_response.iter_bytes.return_value = [b"chunk1", b"chunk2"]
95+
mock_response.__enter__ = Mock(return_value=mock_response)
96+
mock_response.__exit__ = Mock(return_value=None)
97+
mock_stream.return_value = mock_response
98+
99+
test_file = tmp_path / "test.txt"
100+
download_file("http://example.com", test_file)
101+
102+
assert test_file.exists()
103+
assert test_file.read_bytes() == b"chunk1chunk2"
104+
105+
88106
@patch("httpx.stream")
89107
def test_download_file_failure(mock_stream):
90108
mock_response = Mock()

0 commit comments

Comments
 (0)