HttpStaticServer ignores If-Range, so resuming a download of a changed file returns a corrupted mix of old and new bytes
What version of Effect is running?
effect 4.0.0, @effect/platform-node 4.0.0 (same code on main at 5e6f756)
What steps can reproduce the bug?
// ifrange.mjs — npm i [email protected] @effect/[email protected] && node ifrange.mjs
import { Effect, Layer } from "effect"
import { HttpServer, HttpStaticServer } from "effect/http"
import { NodeHttpServer } from "@effect/platform-node"
import { createServer } from "node:http"
import { mkdtempSync, writeFileSync, utimesSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
const root = mkdtempSync(join(tmpdir(), "if-range-"))
const file = join(root, "data.txt")
writeFileSync(file, "old-old-old-old-old-") // version 1, 20 bytes
utimesSync(file, new Date("2026-01-01T00:00:00Z"), new Date("2026-01-01T00:00:00Z"))
const port = 34567
const url = `http://localhost:${port}/data.txt`
const ServerLive = Layer.unwrap(Effect.map(
HttpStaticServer.make({ root }),
(app) => HttpServer.serve(app)
)).pipe(Layer.provide(NodeHttpServer.layer(createServer, { port })))
const client = Effect.promise(async () => {
// 1. Download the first 10 bytes of version 1 and keep its validators
const first = await fetch(url, { headers: { Range: "bytes=0-9" } })
const etag = first.headers.get("etag")
const lastModified = first.headers.get("last-modified")
const part1 = await first.text()
console.log("first", first.status, JSON.stringify(part1), "etag:", etag)
// 2. The file changes on the server
writeFileSync(file, "NEW-NEW-NEW-NEW-NEW-NEW-") // version 2, 24 bytes
utimesSync(file, new Date("2026-02-01T00:00:00Z"), new Date("2026-02-01T00:00:00Z"))
// 3. Resume with If-Range set to the old validator
for (const [label, ifRange] of [["If-Range: <old etag>", etag], ["If-Range: <old date>", lastModified]]) {
const resumed = await fetch(url, { headers: { Range: "bytes=10-", "If-Range": ifRange } })
const body = await resumed.text()
console.log(label, resumed.status, JSON.stringify(body), "etag:", resumed.headers.get("etag"),
resumed.status === 206 ? `-> client assembles ${JSON.stringify(part1 + body)}` : "")
}
})
Effect.runPromise(Effect.scoped(Effect.flatMap(Layer.build(ServerLive), () => client)))
.then(() => process.exit(0), (e) => { console.error(e); process.exit(1) })
Output:
first 206 "old-old-ol" etag: "14-19b76daa800"
If-Range: <old etag> 206 "W-NEW-NEW-NEW-" etag: "18-19c167fcc00" -> client assembles "old-old-olW-NEW-NEW-NEW-"
If-Range: <old date> 206 "W-NEW-NEW-NEW-" etag: "18-19c167fcc00" -> client assembles "old-old-olW-NEW-NEW-NEW-"
What is the expected behavior?
Both resume requests should get 200 with the whole new file, so the client starts over.
RFC 9110 §13.1.5 defines If-Range for this case: send the requested range only if the representation is unchanged, and otherwise send the whole thing. §13.2.2 step 5 requires an origin server to evaluate it before honoring Range. The condition is false when the validator doesn't match. When it is false, the server must ignore Range and send 200:
- an entity-tag matches only by strong comparison, so a weak ETag never matches;
- an HTTP-date matches only when it equals
Last-Modifiedexactly.
What do you see instead?
206 with a slice of the new version. The client stitches it onto the bytes it already has from the old version and gets a file that never existed on the server, here "old-old-olW-NEW-NEW-NEW-". Nothing reports an error, because the Content-Range total is consistent with the new file.
Browsers, curl -C -, download managers and media players send If-Range when they resume. HttpStaticServer advertises Accept-Ranges: bytes, and HttpPlatform.fileResponse sends ETag and Last-Modified, so clients will rely on it.
Additional information
serveFile in packages/effect/src/http/HttpStaticServer.ts evaluates If-None-Match / If-Modified-Since and then applies Range, but nothing reads if-range. A grep for if-range over packages/*/src finds no handler.
A possible fix is to check If-Range before parseRange, against the etag / last-modified headers of the full file response that getFullResponse() already builds. When it doesn't match, return getFullResponse(). Two details:
- Strong comparison: a value with a
W/prefix, or a weakETagfromEtag.layerWeak, never matches. - Dates:
Date.parse(ifRange) === Date.parse(lastModified).
I'm happy to open a PR with the fix and tests.