Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: assume binary payload on missing `Content-Type` (#1820)

NTFSvolumeabc3655e67e4
diff --git a/cyberdrop_dl/clients/downloads.py b/cyberdrop_dl/clients/downloads.py
index d1604aa6f..b1bebad5d 100644
--- a/cyberdrop_dl/clients/downloads.py
+++ b/cyberdrop_dl/clients/downloads.py
@@ -89,8 +89,8 @@ class DownloadClient:
etag.check(resp.headers)
await self.http_client.check_http_status(resp)
- if not media_item.is_segment:
- _check_content_type(_get_content_type(resp.headers), media_item.ext)
+ if not media_item.is_segment and (content_type := _get_content_type(resp.headers)):
+ _check_content_type(content_type, media_item.ext)
media_item.filesize = int(resp.headers.get("Content-Length", "0")) or None
if not media_item.path:
@@ -364,11 +364,10 @@ def _check_content_type(content_type: str, ext: str) -> str | None:
raise InvalidContentTypeError(message=msg)
-def _get_content_type(headers: Mapping[str, str]) -> str:
+def _get_content_type(headers: Mapping[str, str]) -> str | None:
content_type = headers.get("Content-Type")
if not content_type:
- msg = "No content type in response headers"
- raise InvalidContentTypeError(message=msg)
+ return
override_key = next((name for name in _CONTENT_TYPES_OVERRIDES if name in content_type), "<NO_OVERRIDE>")
return _CONTENT_TYPES_OVERRIDES.get(override_key) or content_type
diff --git a/cyberdrop_dl/clients/response.py b/cyberdrop_dl/clients/response.py
index 3d09d4313..816e4e3cf 100644
--- a/cyberdrop_dl/clients/response.py
+++ b/cyberdrop_dl/clients/response.py
@@ -92,7 +92,7 @@ class AbstractResponse(ABC, Generic[_ResponseT]):
return BeautifulSoup(self._text, "html.parser").prettify(formatter="html")
if not ("json" in self.content_type or "html" in self.content_type):
- return f"<{self.content_type} payload>"
+ return f"<{self.content_type or 'application/octet-stream'} payload>"
return self._text
diff --git a/tests/test_download_client.py b/tests/test_download_client.py
index be76ab561..1ba0abbd9 100644
--- a/tests/test_download_client.py
+++ b/tests/test_download_client.py
@@ -28,18 +28,21 @@ def test_chunk_size_is_never_greater_that_speed_limit(manager: Manager, limit: i
def test_get_content_type() -> None:
def get(value: str) -> str:
- return _get_content_type(CIMultiDict({"content-type": value}))
+ content_type = _get_content_type(CIMultiDict({"content-type": value}))
+ assert content_type
+ return content_type
assert get("text/vnd.trolltech.linguist") == "video/MP2T"
assert get("text/HTML") == "text/HTML"
assert get("application/json") == "application/json"
- with pytest.raises(InvalidContentTypeError):
- _ = get("")
-
def test_check_content_type() -> None:
_check_content_type("text/html", ".txt")
with pytest.raises(InvalidContentTypeError, match="Received 'text/html', was expecting binary payload"):
_check_content_type("text/html", ".mp4")
+
+
+def test_get_content_type_missing_headers() -> None:
+ assert _get_content_type({}) is None