Skip to content

fix(content): a resume with the If-Range of another TAF gets the whole file - #612

Open
Yunaido wants to merge 2 commits into
toniebox-reverse-engineering:developfrom
Yunaido:fix-local-if-range
Open

Yunaido wants to merge 2 commits into
toniebox-reverse-engineering:developfrom
Yunaido:fix-local-if-range

Conversation

@Yunaido

@Yunaido Yunaido commented Nov 17, 2026

Copy link
Copy Markdown

What

A box that resumes a download sends Range: bytes=<n>- and, in If-Range, the audio id (decimal) of the TAF it
has the first part of. TeddyCloud served local content (a cached tonie, a library TAF, a custom source) from the
Range on without looking at If-Range. If the tag's source was changed to another TAF in the meantime, the box
completed the start of the old TAF with the rest of the new one, leaving a mix of two TAFs on its SD card.

Now a Range request whose If-Range is a decimal audio id other than the one of the file being sent gets the whole
file: 200, no Content-Range. HTTP prescribes this for an If-Range whose validator does not match (RFC 9110
13.1.5). A matching or absent If-Range is served as before, and so is an If-Range that is not a decimal number.

Why

Found before it hit a real box: a box had the first 30.4 MB of a tag's TAF on its card when the tag's source was
to be changed to another TAF. Its next resume would be Range: bytes=30425088- with If-Range: <old audio id>,
and TeddyCloud would answer 206 from 30.4 MB of the new file: the repro test below shows exactly that answer.

How

ignoreRangeOfAnotherTaf() in src/handler_cloud.c, on the "Serve local content" path just before
httpExtSendResponseStream. When If-Range is a decimal number that fits an audio id and is not the audio id in the
header of the file being served (tonieInfo->tafHeader), it drops the Range (Range.valid = false, start and end
0). The same validator therefore holds:

  • with skip_header: the Range counts in the audio, the audio id is still the file's;
  • for a V3 chapter: the chapter is cut from that file;
  • for a random TAP pool: pickPoolTaf already picks the TAF the If-Range names, so the check matches.

Only a decimal number is compared, because that is what the boxes send: the stock CC3200 does, and develop's
random-pool code assumes the same. Any other value is not ours to judge, and the Range is served as before. That
covers an ETag, a date, a number with text after it, or one too large for an audio id. TeddyCloud sends no
validator for this content, and what other clients (an ESP32 box, a browser) might send is unknown. A client whose
validator cannot be compared should not lose its resume; on a slow link a resume that restarts at 0 may never
finish.

Not changed:

  • Live streams (an ffmpeg source, a TAP stream) have their own branches and already decide about a Range there.
  • Nothing under src/cyclone/.

Tested

  • New IfRange cases in tests/py/test_taf_serving.py. The local-content setup moved into a base class
    TafContent, so the new class does not run the existing tests again.
  • The repro was committed first as an expected failure: a tag served CONTENT, then its source was changed to
    another TAF in the library, and a request came with Range 20000- and If-Range of the old audio id. Before the
    fix: 206, bytes 20000-28671/28672 with the other TAF's bytes. After: 200 with the whole new file.
  • More cases:
    • If-Range matching the served file: 206 with the rest;
    • no If-Range: 206 as before;
    • an If-Range that is no decimal audio id (an ETag, a date, <id>abc, 99999999999): 206 as before;
    • skip_header with a matching If-Range: 206 of the audio; with a mismatching one: 200 with all of the audio;
    • a V3 chapter with Range: 206 for a match, 200 with the whole chapter for a mismatch.
  • test_tap_random, test_http_range, test_taf_redownload and full make test pass. Every pytest entry was
    also run with its server log kept: no ASan/UBSan report apart from the known protobuf-c shift in test_box_sim.

…e other TAF

A box resumes a download with Range and, in If-Range, the audio id of the
TAF it has the first part of. When the tag's source was changed to another
TAF meanwhile, local content is served from the Range on regardless: the
box gets the rest of the new TAF after the start of the old one. Marked
as an expected failure until fixed.
…e file

A box resumes a download with Range and the audio id of the TAF it has
the first part of in If-Range. Local content was served from the Range on
regardless, so when the tag's source had been changed to another TAF, the
box completed the start of the old TAF with the rest of the new one.

As for any If-Range whose validator does not match the representation
(RFC 9110 13.1.5), the Range is now ignored and the whole file is sent
with 200. The validator is the audio id in the header of the file that is
sent, also with skip_header and for a V3 chapter. Only an If-Range that
is a decimal number fitting an audio id is compared, as the boxes send
it. Any other value (an ETag, a date, a number with text after it) is not
TeddyCloud's to judge, and the Range is served as before, so that a
client does not lose its resume over it; so is a matching or absent
If-Range. Live streams (ffmpeg, TAP streams) are not changed: they decide
about a Range themselves. A random TAP pool already uses If-Range to pick
the TAF it resumes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant