Skip to content

feat(engine): restart a download when the file on the server changes - #2

Merged
troke12 merged 3 commits into
troke12:mainfrom
rigaz29:engine-if-range
Oct 19, 2026
Merged

troke12 merged 3 commits into
troke12:mainfrom
rigaz29:engine-if-range

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ODM stores the probe's ETag and Last-Modified with the download and sends one of them as If-Range on 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

  • Which validator goes into If-Range: a strong ETag, otherwise Last-Modified, otherwise none. A weak ETag (W/…) is never sent (RFC 9110 §13.1.5), because servers answer it with a full 200.
  • What a 200 to a request with If-Range means:
    • its validator differs or is missing → the file changed → restart.
    • same validator → a server ignoring Range. The body is kept when the range covered the whole file; otherwise the error is server ignored Range, as on main. fix(engine): continue on one connection when a range is ignored #10 will turn this case into a one-connection continue.
  • On resume: the file is probed at FinalURL with the saved cookies via guardedClient(), 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

Test plan

  • go test ./... and go test -race ./internal/engine
  • TestWeakETagFreshMultiConnection (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 uses d.URL.
  • TestSubrangeOKWithSameETagDoesNotWrite: a same-ETag 200 reports server ignored Range and keeps stored bytes. Fails if every 200 counts as changed.
  • Existing: restart on ETag change (resume and mid-transfer), full-file 200 kept, representationDiffers.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)

@troke12 troke12 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  • revalidate probes the original d.URL without the stored cookies; main resumed straight from FinalURL. 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.
@troke12
troke12 force-pushed the engine-if-range branch 2 times, most recently from 58f5968 to f2b611c Compare October 19, 2026 03:04
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)
@troke12

troke12 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Rebased onto current main and applied the review: a weak ETag is never sent as If-Range (Last-Modified or nothing instead; tested against http.ServeContent), a 200 is classified by its own validator (different → restart, same → "server ignored Range" as on main, ready for #10), and the resume probe uses FinalURL with the saved cookies through guardedClient(), non-fatal on failure. Your commits are kept with you as author; the review changes are a separate commit on top. Thanks!

@troke12
troke12 merged commit 84823de into troke12:main Oct 19, 2026
5 checks passed
troke12 pushed a commit to rigaz29/odm that referenced this pull request Oct 19, 2026
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)
troke12 pushed a commit that referenced this pull request Oct 19, 2026
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)
@rigaz29
rigaz29 deleted the engine-if-range branch October 19, 2026 05:01
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