Skip to content

feat(engine): replace a live session's request headers without a rebuild (#792) - #793

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
dlev02:feat/792-live-request-headers
Oct 15, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
dlev02:feat/792-live-request-headers

Conversation

@dlev02

@dlev02 dlev02 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Closes part 1 of #792.

What

updateRequestHeaders(_:) -> RequestHeaderUpdateOutcome replaces a live session's request headers. The next request from each of the session's source readers carries the new set. Nothing is torn down.

The motivating case is a media server whose bearer tokens expire under a long title. Until now the only way to get a renewed token onto the wire was reloadAtCurrentPosition(applying:), which is a visible rebuild at the playhead to change a value only the next request reads.

How

  • RequestHeaderStore: a lock-protected box, one per load, created from options.httpHeaders.
  • AVIOReader takes an optional store and reads extraHeaders from it per request. Every request path already goes through headers(for:), so the redirect policy, the held connection and the probe and chunk delegates all see the current value with no other change. Readers opened without a store (probes, sidecars, stills, prewarm) behave exactly as before.
  • DemuxerOpenProfile.requestHeaders / withRequestHeaders(_:) carry the store through Demuxer.open. Its signature is unchanged.
  • The store is wired into the session's reads of the source: the probe demuxer that becomes the playback demuxer, the software and audio loaders' opens, the HLSVideoEngine start, restart-reopen and live-reopen opens and the I-frame side reader (sourceHTTPHeaders is now read from the store), and the subtitle prefetch and native-subtitle side readers.
  • setLoadedHTTPHeaders(_:) is the fifth narrow write into loadedOptions, so rebuilds (an audio switch, a background return, reloadAtCurrentPosition()) replay the new set.
  • Routes whose requests are fixed at load answer .installedForNextRebuild: .remoteBypass, an AVPlayer-hosted audio item, a custom IOReader, and a remote disc image (HTTPDiscIOReader is not wired). A call with nothing loaded answers .noActiveSession and installs nothing.
  • The log names the replaced header names, never their values.

Part 2 of #792 (a typed sourceRefused and a shorter ladder for a mid-session 401) is not in this PR. It changes how the revive arm classifies failures, so it seemed better shaped by you.

Test plan

macOS 27 (arm64), Xcode 27 release toolchain, swift build and swift test.

  • New LiveRequestHeaderTests:
    • A loopback origin serves the opening range under token "first" and refuses it with 401 past that offset. After the first refusal the test replaces the store with "second". The same reader (never reopened) heals and delivers the full target, and the healing request carries "second". With the store removed (requestHeaderStore: nil), the same test fails after 83 s: the reader spends its 12-step reconnect ladder on the refused token and the read fails. That is the stall the issue describes.
    • A reader opened without a store keeps its opened headers.
    • The outcome table per route.
    • .noActiveSession installs nothing.
  • Full suite: 4899 tests. 4897 passed. Two hit their time limits on a machine running other heavy jobs at the time: "LAN peers cannot take the slots kept for loopback" and "Vision recognizes clean synthetic subtitle text". Both suites pass on their own, as do PublicAPIDocumentationTests and DocumentedConstantsTests.
  • Scripts/check-doc-links.py: all links resolve.
  • Not tested on device or against real media. The change is in the request path, not decode, but a tvOS run with a token-protected MKV would be the meaningful field check.

…ild (superuser404notfound#792)

updateRequestHeaders(_:) hands the session a new header set and the next
request each of its source readers makes carries it. One RequestHeaderStore
per load feeds the playback demuxer, the producer's reopens and revives, and
the I-frame and embedded-subtitle side readers, which used to freeze a copy of
LoadOptions.httpHeaders at their open. A reconnect already backing off over a
refused credential sends the new set on its next attempt.

The set is also installed into loadedOptions so rebuilds replay it. Routes
whose requests are fixed at load (remoteBypass, AVPlayer audio, custom
IOReader, remote disc image) report .installedForNextRebuild.
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged in 4768000, thank you. The tests pass on top of current main (4907), and the three places I flagged hold: the warm-connection check compares against the store through the computed extraHeaders, and the I-frame side reader reads the store.

One gap I closed in a follow-up, 83aefbf: makeFrameExtractor() built its extractor from loadedOptions.httpHeaders when the host asked for it, and that extractor opens its demuxer lazily (first scrub, and again after an idle close), so scrub stills after a rotation still asked with the refused token. It now reads the session's store when it opens, with a test that fails without the change.

Not released yet; it goes out with the next engine release. Part 2 of #792 is next on my side.

@dlev02

dlev02 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Perfect, thanks for the quick turnaround as always!

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.

2 participants