Repository navigation
fix(http): ask If-Range so a file replaced mid-download is never stitched - #631
Merged
Merged
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
ketchdownloaded 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
HttpDownloadSourcesendsIf-Rangeon every ranged request. Its value is the file's strong ETag, else itsLast-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).200reaches 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 withKetchError.FileChanged("The file changed on the server while it downloaded") instead ofUnsupported.KtorHttpEnginekeepsIf-Rangeacross 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.W/…) are never sent. Servers that don't implementIf-Rangeignore it and behave as before.If-Matchwas rejected on purpose: server farms whose nodes give different ETags for the same file would make it fail every download, whileIf-Rangeonly costs a restart there.Verification
HttpDownloadSourceTestcases:download_ranges_askIfRangeWithTheFilesValidator(strong ETag, else Last-Modified),download_fileReplacedWhileDownloading_savesTheNewFileWholeanddownload_fileReplacedBySizeWhileDownloading_failsAsChanged, over a fake server that honorsIf-Range../gradlew :library:core:jvmTest :library:ktor:jvmTestpasses.