Skip to content

fix(http): ask If-Range so a file replaced mid-download is never stitched - #631

Merged
linroid merged 1 commit into
mainfrom
fix/http-if-range
Oct 23, 2026
Merged

linroid merged 1 commit into
mainfrom
fix/http-if-range

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Found while testing what happens when a file changes on the server during a segmented download: Ketch reported success with a corrupt file.

A local server served version A for its first two requests and version B (same size, new ETag) afterwards. ketch downloaded with 4 connections and completed with a file matching neither version: segment 1 from A, segments 3–4 from B, and segment 2 mixed. Nothing caught it, since no digest was published and no validator was sent with the ranges.

This happens whenever a server replaces a file in place while it's being downloaded: a release re-uploaded under the same name, a nightly build, a log or dump being rotated.

What changes

  • HttpDownloadSource sends If-Range on every ranged request. Its value is the file's strong ETag, else its Last-Modified, as the download started (from the resolution, or the saved state on resume). Per RFC 9110, a server answers a range whose validator no longer matches with the whole current file (200).
  • That 200 reaches fix(http): download from servers that ignore ranges or fail HEAD #613's fallback (RangeIgnoredException): the run restarts over one connection from byte zero and saves the new file, consistent. If the restart's whole-file answer has another size too, the file changed size, and the download fails with KetchError.FileChanged ("The file changed on the server while it downloaded") instead of Unsupported.
  • KtorHttpEngine keeps If-Range across cross-origin redirects (GitHub → its asset CDN, mirror redirects). It holds no credentials, and the validator is the final target's own, read by the HEAD through the same hops.
  • Weak ETags (W/…) are never sent. Servers that don't implement If-Range ignore it and behave as before. If-Match was rejected on purpose: server farms whose nodes give different ETags for the same file would make it fail every download, while If-Range only costs a restart there.

Verification

  • Same local test, from this branch: the output equals version B exactly (before: parts of A and B). Unchanged files still download over 4 connections with no restart.
  • Real downloads: openSUSE's 273 MB ISO through its mirror redirect, 4 connections, no restart, SHA-256 matches the published one. The GitHub release asset (cross-origin redirect) works too.
  • New HttpDownloadSourceTest cases: download_ranges_askIfRangeWithTheFilesValidator (strong ETag, else Last-Modified), download_fileReplacedWhileDownloading_savesTheNewFileWhole and download_fileReplacedBySizeWhileDownloading_failsAsChanged, over a fake server that honors If-Range.
  • ./gradlew :library:core:jvmTest :library:ktor:jvmTest passes.

…ched

A file the server replaced while Ketch downloaded it in segments was saved
as parts of both versions, and the download reported success: its ranges
carried no validator. Ranges now ask If-Range with the file's strong ETag,
else its Last-Modified, as the download started, so a replaced file comes
back whole; the run restarts over one connection and saves the new file,
or fails with FileChanged when its size changed too. If-Range is kept
across cross-origin redirects, as it holds no credentials.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-23T17:14:28.419117Z c31ec66 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 200 files  ± 0   2 228 suites  ±0   57m 12s ⏱️ ±0s
15 349 tests +12  15 081 ✅ +12  268 💤 ±0  0 ❌ ±0 
18 708 runs  +12  18 440 ✅ +12  268 💤 ±0  0 ❌ ±0 

Results for commit c31ec66. ± Comparison against base commit 884208d.

@linroid
linroid merged commit d3c7122 into main Oct 23, 2026
15 checks passed
@linroid
linroid deleted the fix/http-if-range branch October 23, 2026 18:15
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