Skip to content

Commit

Permalink
fix: correct cache-control in car responses
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel authored and Jorropo committed Jul 6, 2022
1 parent 90b48db commit 289e465
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions core/corehttp/gateway_handler_car.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 :^)

Expand Down
4 changes: 2 additions & 2 deletions test/sharness/t0118-gateway-car.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 289e465

Please sign in to comment.