fix: correct cache-control in car responses
Context: https://github.com/ipfs/specs/pull/295
Marcin Rataj committed
Jul 5, 2022 at 01:06 UTC
289e465f0589a33c3333efe6eab26695e3a78df4
2 files changed
+12
-10
core/corehttp/gateway_handler_car.go
+10
-8
@@ -43,8 +43,14 @@ func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r
43
}
44
setContentDispositionHeader(w, name, "attachment")
45
46
- // Weak Etag W/ because we can't guarantee byte-for-byte identical responses
47
- // (CAR is streamed, and in theory, blocks may arrive from datastore in non-deterministic order)
46
+ // Set Cache-Control (same logic as for a regular files)
47
+ addCacheControlHeaders(w, r, contentPath, rootCid)
48
+
49
+ // Weak Etag W/ because we can't guarantee byte-for-byte identical
50
+ // responses, but still want to benefit from HTTP Caching. Two CAR
51
+ // responses for the same CID and selector will be logically equivalent,
52
+ // but when CAR is streamed, then in theory, blocks may arrive from
53
+ // datastore in non-deterministic order.
54
etag := `W/` + getEtag(r, rootCid)
55
w.Header().Set("Etag", etag)
56
@@ -55,14 +61,10 @@ func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r
61
}
62
63
// Make it clear we don't support range-requests over a car stream
58
- // Partial downloads and resumes should be handled using
59
- // IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769
64
+ // Partial downloads and resumes should be handled using requests for
65
+ // sub-DAGs and IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769
66
w.Header().Set("Accept-Ranges", "none")
67
62
- // Explicit Cache-Control to ensure fresh stream on retry.
63
- // CAR stream could be interrupted, and client should be able to resume and get full response, not the truncated one
64
- w.Header().Set("Cache-Control", "no-cache, no-transform")
65
-
68
w.Header().Set("Content-Type", "application/vnd.ipld.car; version=1")
69
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^)
70
test/sharness/t0118-gateway-car.sh
+2
-2
@@ -128,8 +128,8 @@ test_launch_ipfs_daemon_without_network
128
grep "< X-Ipfs-Roots" curl_output
129
'
130
131
- test_expect_success "GET response for application/vnd.ipld.car includes expected Cache-Control" '
132
- grep "< Cache-Control: no-cache, no-transform" curl_output
131
+ test_expect_success "GET response for application/vnd.ipld.car includes same Cache-Control as a block or a file" '
132
+ grep "< Cache-Control: public, max-age=29030400, immutable" curl_output
133
'
134
135
test_kill_ipfs_daemon