fix: correct cache-control in car responses

Context: https://github.com/ipfs/specs/pull/295
This commit is contained in:
Marcin Rataj 2022-07-05 01:06:01 +02:00 committed by Jorropo
parent 90b48db172
commit 289e465f05
2 changed files with 12 additions and 10 deletions

View File

@ -43,8 +43,14 @@ func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r
}
setContentDispositionHeader(w, name, "attachment")
// Weak Etag W/ because we can't guarantee byte-for-byte identical responses
// (CAR is streamed, and in theory, blocks may arrive from datastore in non-deterministic order)
// Set Cache-Control (same logic as for a regular files)
addCacheControlHeaders(w, r, contentPath, rootCid)
// Weak Etag W/ because we can't guarantee byte-for-byte identical
// responses, but still want to benefit from HTTP Caching. Two CAR
// responses for the same CID and selector will be logically equivalent,
// but when CAR is streamed, then in theory, blocks may arrive from
// datastore in non-deterministic order.
etag := `W/` + getEtag(r, rootCid)
w.Header().Set("Etag", etag)
@ -55,14 +61,10 @@ func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r
}
// Make it clear we don't support range-requests over a car stream
// Partial downloads and resumes should be handled using
// IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769
// Partial downloads and resumes should be handled using requests for
// sub-DAGs and IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769
w.Header().Set("Accept-Ranges", "none")
// Explicit Cache-Control to ensure fresh stream on retry.
// CAR stream could be interrupted, and client should be able to resume and get full response, not the truncated one
w.Header().Set("Cache-Control", "no-cache, no-transform")
w.Header().Set("Content-Type", "application/vnd.ipld.car; version=1")
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^)

View File

@ -128,8 +128,8 @@ test_launch_ipfs_daemon_without_network
grep "< X-Ipfs-Roots" curl_output
'
test_expect_success "GET response for application/vnd.ipld.car includes expected Cache-Control" '
grep "< Cache-Control: no-cache, no-transform" curl_output
test_expect_success "GET response for application/vnd.ipld.car includes same Cache-Control as a block or a file" '
grep "< Cache-Control: public, max-age=29030400, immutable" curl_output
'
test_kill_ipfs_daemon