diff --git a/tests/downloaders/test_failure_surfacing.py b/tests/downloaders/test_failure_surfacing.py index 176b9d4b..691f34c3 100644 --- a/tests/downloaders/test_failure_surfacing.py +++ b/tests/downloaders/test_failure_surfacing.py @@ -76,6 +76,11 @@ class _Server: self.send_206(conn, BODY[5:], f"bytes 5-{len(BODY) - 1}/{len(BODY)}") elif self.mode == "resume_missing_content_range": 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": # honors the requested start but caps the span, as size-capping CDNs do 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 +@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) 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; diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index 91663c6b..c7618aa4 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -2660,6 +2660,9 @@ class dl: if keep_audio and len(title.tracks.audio) > 0: if not audio_description: 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: title.tracks.select_audio(lambda x: x.codec in acodec) if not title.tracks.audio: diff --git a/unshackle/core/downloaders/requests.py b/unshackle/core/downloaders/requests.py index e01f9650..3a485913 100644 --- a/unshackle/core/downloaders/requests.py +++ b/unshackle/core/downloaders/requests.py @@ -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)) +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]]]: """ The request's ``Range`` as ``(start, end)``. Gives None when absent or not ``bytes=start-...``. @@ -821,7 +831,11 @@ def download( if ( content_range is None 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( f"byte-range segment got Content-Range {content_range!r}, "