Repository navigation
feat(engine): restart a download when the file on the server changes - #2
Conversation
troke12
left a comment
There was a problem hiding this comment.
The feature is worth having, but there is a blocking regression:
Weak ETags. The stored ETag is sent as If-Range verbatim, including W/. RFC 9110 doesn't allow a weak validator in If-Range, and Go's own net/http (fs.go, checkIfRange → etagStrongMatch) answers a full 200 to it. Every sub-range request then hits errRepresentationChanged; the re-probe sees the same ETag, the next pass gets 200 again and fails with "file on the server changed". This breaks fresh multi-connection downloads from such servers, not only resumes. Please never send a W/ ETag (fall back to Last-Modified or nothing) and add a test using http.ServeContent.
Also:
revalidateprobes the originald.URLwithout the stored cookies;mainresumed straight fromFinalURL. Short-lived redirect/token links that resume today would fail at the probe.- A server that ignores Range on GET now reports "file on the server changed" instead of "server ignored Range".
- This contradicts #10 on what a 200 means. Suggested rule: validator differs → restart (#2); validator matches or no If-Range sent → continue on one connection (#10).
Note for the whole set (#1–#11): thanks a lot for these! They are all branched from 5350f79 independently, so every pair conflicts on CHANGELOG.md and most on download.go. main has also moved (e830f5d adds a stream downloader that touches download.go). Could you rebase onto current main and stack them in this order: #1 → #2 → #10 → #8 → #5/#6/#7/#11? #2, #3 and #10 each add their own pass loop / transferCancel in Start; one shared design would be much easier to merge.
Save the probe ETag and Last-Modified, and send one of them as If-Range on later range requests. A resume whose validator or size no longer matches, and a transfer whose If-Range is rejected, discards the partial file and fetches the new representation from the first byte.
A range that already covers the whole file can be answered with 200 while the ETag or Last-Modified is unchanged. That body is the file. A 200 for any narrower range, or a different validator or length, still discards the partial and starts again.
58f5968 to
f2b611c
Compare
Review changes on top of the previous two commits: - A weak ETag (W/) is never sent as If-Range; Last-Modified is, or nothing. RFC 9110 does not allow a weak one there and Go's http.ServeContent answers it with a full 200, so every sub-range of a fresh multi-connection download looked like a changed file and failed. TestWeakETagFreshMultiConnection and TestWeakETagFallsBackToLastModified use http.ServeContent. - A 200 to a request with If-Range is told apart by its own validator: a different (or missing) one is a changed file and restarts; the same one is a server ignoring Range, kept when the range covered the whole file and otherwise reported as "server ignored Range", as on main. - revalidate probes FinalURL with the saved cookies through guardedClient, as the transfer itself does: a token link that only works there resumes as before. A probe failure is not fatal (If-Range on each request still guards), except a local-network refusal. A probe that sets no cookie keeps the saved ones. - Tests reuse closeEngineFile; the ignored-Range test sets retryBase. ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
|
Rebased onto current |
Review changes on top of the previous commit, which ports troke12#10 onto the pass loop If-Range (troke12#2) added, as one design: - The one-connection path for a whole-file 200 runs only when its validator matches the one sent as If-Range, or none was sent. A replaced file of the same size used to be joined to the old prefix; it now restarts (TestSubrangeOKWithOtherETagSameSizeRestarts). - A resume probe that finds ranges gone is no longer a changed file by itself: with the same validator the prefix is kept and the download continues on one connection, as when a transfer notices it. representationDiffers loses its range clause. - discardPrefix reads through the idle timer, so a stall while skipping the stored prefix is retried (TestDiscardPrefixStallRetries). - Tests: the ignored-range resume fixture answers the resume probe (bytes=0-); troke12#2's same-ETag subrange test now expects completion on one connection with the stored prefix kept. ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
Review changes on top of the previous commit, which ports #10 onto the pass loop If-Range (#2) added, as one design: - The one-connection path for a whole-file 200 runs only when its validator matches the one sent as If-Range, or none was sent. A replaced file of the same size used to be joined to the old prefix; it now restarts (TestSubrangeOKWithOtherETagSameSizeRestarts). - A resume probe that finds ranges gone is no longer a changed file by itself: with the same validator the prefix is kept and the download continues on one connection, as when a transfer notices it. representationDiffers loses its range clause. - discardPrefix reads through the idle timer, so a stall while skipping the stored prefix is retried (TestDiscardPrefixStallRetries). - Tests: the ignored-range resume fixture answers the resume probe (bytes=0-); #2's same-ETag subrange test now expects completion on one connection with the stored prefix kept. ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
Summary
ODM stores the probe's
ETagandLast-Modifiedwith the download and sends one of them asIf-Rangeon every ranged request. When the file on the server changes, during a transfer or between a pause and a resume, the partial file is discarded and the download starts over from byte 0 (one extra pass; a second change is reported as an error). Before this, a changed file was silently stitched together from two versions.Rules
ETag, otherwiseLast-Modified, otherwise none. A weakETag(W/…) is never sent (RFC 9110 §13.1.5), because servers answer it with a full 200.Range. The body is kept when the range covered the whole file; otherwise the error isserver ignored Range, as onmain. fix(engine): continue on one connection when a range is ignored #10 will turn this case into a one-connection continue.FinalURLwith the saved cookies viaguardedClient(), the same way the transfer itself requests it. A different validator or size, or a server that no longer accepts ranges, restarts the download. A failed probe is not fatal (If-Range still guards each request), except a local-network refusal.Commits
be0a6e6,9696603(Ryan Bagas): the original change, rebased onto currentmain(streams,guardedClient, fix(engine): retry a connection that stops sending #1/fix(engine): request raw bytes for a ranged download #5/fix(engine): retry when a connection closes early #7).f2b611c: review changes. No weak ETag in If-Range, a 200 classified by its own validator, revalidate via FinalURL + cookies + guarded client, docs.Test plan
go test ./...andgo test -race ./internal/engineTestWeakETagFreshMultiConnection(http.ServeContent+W/ETag, 4 connections) completes. Fails if the weak ETag is sent.TestWeakETagFallsBackToLastModified: If-Range carries the date and ServeContent answers 206.TestResumeRevalidatesFinalURLWithCookies: the token link resumes, and the expired original URL is never probed. Fails if revalidate usesd.URL.TestSubrangeOKWithSameETagDoesNotWrite: a same-ETag 200 reportsserver ignored Rangeand keeps stored bytes. Fails if every 200 counts as changed.representationDiffers.✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)