From 3419bd94b9ce643b34ac88e3223e1c75f35a5218 Mon Sep 17 00:00:00 2001 From: divingpetrel Date: Wed, 14 Apr 2021 10:15:56 +0100 Subject: [PATCH 1/2] disable-last-modified-behaviour-ipns-routes --- core/corehttp/gateway_handler.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index ccec95b0151..798ee69efbc 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -299,8 +299,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // set these headers _after_ the error, for we may just not have it // and don't want the client to cache a 500 response... // and only if it's /ipfs! - // TODO: break this out when we split /ipfs /ipns routes. - modtime := time.Now() + + // ipns routes do not require LastModified header, disable by passing zero date to ServeContent + modtime := time.Time{} if f, ok := dr.(files.File); ok { if strings.HasPrefix(urlPath, ipfsPathPrefix) { From ea99e13ab0b4f5cba2f9fba64b82875142b69736 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Tue, 27 Apr 2021 01:18:02 +0200 Subject: [PATCH 2/2] test(gw): http headers related to caching --- core/corehttp/gateway_handler.go | 3 ++- test/sharness/t0110-gateway.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 798ee69efbc..fe01819c4fd 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -301,13 +301,14 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request // and only if it's /ipfs! // ipns routes do not require LastModified header, disable by passing zero date to ServeContent + // (ipfs routes have modtime set in a separate code block below) modtime := time.Time{} if f, ok := dr.(files.File); ok { if strings.HasPrefix(urlPath, ipfsPathPrefix) { w.Header().Set("Cache-Control", "public, max-age=29030400, immutable") - // set modtime to a really long time ago, since files are immutable and should stay cached + // set modtime for /ipfs/ to a really long time ago, since files are immutable and should stay cached modtime = time.Unix(1, 0) } diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index 84bc963410d..578acf4176c 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -108,6 +108,34 @@ test_expect_success "GET /ipfs/ipns/{peerid} returns redirect to the valid path" test_should_contain "" response_with_ipfs_ipns_ns ' +# Headers related to caching +# Cache-Control: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control +# Last-Modified: https://github.com/ipfs/go-ipfs/issues/7968 +# Etag: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag + +# /ipfs/* +test_expect_success "GET for /ipfs/ should include the ancient Last-Modified header" " + curl -svX GET 'http://127.0.0.1:$port/ipfs/$HASH' > curl_out 2>&1 && + grep -i 'Last-Modified: Thu, 01 Jan 1970 00:00:01 GMT' curl_out +" +test_expect_success "GET for /ipfs/ should include maxed out Cache-Control header" " + grep -i 'Cache-Control: public, max-age=29030400, immutable' curl_out +" +test_expect_success "GET for /ipfs/ should include Etag header set to the CID" " + grep -i 'Etag: \"${HASH}\"' curl_out +" + +# /ipns/* +test_expect_success "GET for /ipns/ should not include Last-Modified header" " + curl -svX GET 'http://127.0.0.1:$port/ipns/$PEERID' > curl_out 2>&1 && + test_must_fail grep -i 'Last-Modified' curl_out +" +test_expect_success "GET for /ipns/ should include Etag header set to the current CID" " + grep -i 'Etag: \"${HASH}\"' curl_out +" +# TODO: set Cache-Control for /ipns/ based on the TTL of IPNS or DNSLink record + + test_expect_success "GET invalid IPFS path errors" ' test_must_fail curl -sf "http://127.0.0.1:$port/ipfs/12345" '