Skip to content

Commit

Permalink
fix: correct cache-control in car responses
Browse files Browse the repository at this point in the history
Context: ipfs/specs#295


This commit was moved from ipfs/kubo@289e465
  • Loading branch information
lidel authored and Jorropo committed Jul 6, 2022
1 parent 6704be1 commit 2871589
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions gateway/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

0 comments on commit 2871589

Please sign in to comment.