fix(dl): accept a clamped 206 and report audio-description-only titles

RFC 9110 clamps a range end past the resource to its last byte; the guard
rejected that valid response. The descriptive-audio filter also had no
emptiness check, so a title with only described audio failed with a
misleading missing-language error.
This commit is contained in:
imSp4rky
2026-08-23 16:32:30 -06:00
parent 1aa6315dc3
commit 5cece9672a
3 changed files with 38 additions and 1 deletions
@@ -76,6 +76,11 @@ class _Server:
self.send_206(conn, BODY[5:], f"bytes 5-{len(BODY) - 1}/{len(BODY)}") self.send_206(conn, BODY[5:], f"bytes 5-{len(BODY) - 1}/{len(BODY)}")
elif self.mode == "resume_missing_content_range": elif self.mode == "resume_missing_content_range":
self.send_206(conn, BODY) self.send_206(conn, BODY)
elif self.mode == "range_clamped_206":
# RFC 9110: a range end at or past the last byte clamps to it, so the whole slice arrives
start, _, end = headers["range"].removeprefix("bytes=").partition("-")
start, end = int(start), min(int(end), len(BODY) - 1)
self.send_206(conn, BODY[start : end + 1], f"bytes {start}-{end}/{len(BODY)}")
elif self.mode == "range_capped_206": elif self.mode == "range_capped_206":
# honors the requested start but caps the span, as size-capping CDNs do # honors the requested start but caps the span, as size-capping CDNs do
start, _, end = headers["range"].removeprefix("bytes=").partition("-") start, _, end = headers["range"].removeprefix("bytes=").partition("-")
@@ -203,6 +208,21 @@ def test_slice_segment_rejects_a_206_for_the_wrong_range(tmp_path, server):
assert len(server.requests) == downloader.MAX_ATTEMPTS assert len(server.requests) == downloader.MAX_ATTEMPTS
@pytest.mark.parametrize("server", ["range_clamped_206"], indirect=True)
def test_slice_segment_accepts_a_206_clamped_to_the_last_byte(tmp_path, server):
# some manifests author a range end past the resource; the clamped 206 is the whole slice
save_path = tmp_path / "0.mp4"
list(
download(
url=server.url,
save_path=save_path,
session=rq.Session(),
headers={"Range": f"bytes=0-{len(BODY)}"},
)
)
assert save_path.read_bytes() == BODY
@pytest.mark.parametrize("server", ["range_capped_206"], indirect=True) @pytest.mark.parametrize("server", ["range_capped_206"], indirect=True)
def test_slice_segment_rejects_a_206_capped_short_of_the_requested_end(tmp_path, server): def test_slice_segment_rejects_a_206_capped_short_of_the_requested_end(tmp_path, server):
# the body is a valid prefix of the slice, so only the Content-Range end catches it; # the body is a valid prefix of the slice, so only the Content-Range end catches it;
+3
View File
@@ -2660,6 +2660,9 @@ class dl:
if keep_audio and len(title.tracks.audio) > 0: if keep_audio and len(title.tracks.audio) > 0:
if not audio_description: if not audio_description:
title.tracks.select_audio(lambda x: not x.descriptive) title.tracks.select_audio(lambda x: not x.descriptive)
if not title.tracks.audio:
self.log.error("Audio description only, use --audio-description...")
sys.exit(1)
if acodec: if acodec:
title.tracks.select_audio(lambda x: x.codec in acodec) title.tracks.select_audio(lambda x: x.codec in acodec)
if not title.tracks.audio: if not title.tracks.audio:
+15 -1
View File
@@ -427,6 +427,16 @@ def parse_content_range(value: Any) -> Optional[tuple[int, int, Optional[int]]]:
return int(match.group(1)), int(match.group(2)), None if match.group(3) == "*" else int(match.group(3)) return int(match.group(1)), int(match.group(2)), None if match.group(3) == "*" else int(match.group(3))
def range_clamped_to_end(content_range: tuple[int, int, Optional[int]], slice_end: int) -> bool:
"""Whether a 206 stops early only because the requested range ends past the resource.
RFC 9110 clamps a range end at or beyond the complete length to the last byte, so the
response still carries the whole slice. Some manifests author such ranges.
"""
_, end, total = content_range
return total is not None and end == total - 1 and slice_end >= total
def request_range(headers: MutableMapping[str, Any]) -> Optional[tuple[int, Optional[int]]]: def request_range(headers: MutableMapping[str, Any]) -> Optional[tuple[int, Optional[int]]]:
""" """
The request's ``Range`` as ``(start, end)``. Gives None when absent or not ``bytes=start-...``. The request's ``Range`` as ``(start, end)``. Gives None when absent or not ``bytes=start-...``.
@@ -821,7 +831,11 @@ def download(
if ( if (
content_range is None content_range is None
or content_range[0] != slice_start or content_range[0] != slice_start
or (slice_end is not None and content_range[1] != slice_end) or (
slice_end is not None
and content_range[1] != slice_end
and not range_clamped_to_end(content_range, slice_end)
)
): ):
raise IOError( raise IOError(
f"byte-range segment got Content-Range {content_range!r}, " f"byte-range segment got Content-Range {content_range!r}, "