diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 264f074d9..5a7d3b79a 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -202,7 +202,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr // and only if it's /ipfs! // TODO: break this out when we split /ipfs /ipns routes. modtime := time.Now() - if strings.HasPrefix(urlPath, ipfsPathPrefix) { + if strings.HasPrefix(urlPath, ipfsPathPrefix) && !dir { w.Header().Set("Etag", etag) w.Header().Set("Cache-Control", "public, max-age=29030400, immutable") diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index 8f307b2c7..55c0b15ee 100644 --- a/core/corehttp/gateway_test.go +++ b/core/corehttp/gateway_test.go @@ -23,6 +23,9 @@ import ( id "gx/ipfs/QmeWJwi61vii5g8zQUB9UGegfUbmhTKHgeDFP9XuSp5jZ4/go-libp2p/p2p/protocol/identify" ) +// `ipfs object new unixfs-dir` +var emptyDir = "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" + type mockNamesys map[string]path.Path func (m mockNamesys) Resolve(ctx context.Context, name string) (value path.Path, err error) { @@ -461,6 +464,32 @@ func TestIPNSHostnameBacklinks(t *testing.T) { } } +func TestCacheControlImmutable(t *testing.T) { + ts, _ := newTestServerAndNode(t, nil) + t.Logf("test server url: %s", ts.URL) + defer ts.Close() + + req, err := http.NewRequest("GET", ts.URL+emptyDir+"/", nil) + if err != nil { + t.Fatal(err) + } + + res, err := doWithoutRedirect(req) + if err != nil { + t.Fatal(err) + } + + // check the immutable tag isn't set + hdrs, ok := res.Header["Cache-Control"] + if ok { + for _, hdr := range hdrs { + if strings.Contains(hdr, "immutable") { + t.Fatalf("unexpected Cache-Control: immutable on directory listing: %s", hdr) + } + } + } +} + func TestVersion(t *testing.T) { config.CurrentCommit = "theshortcommithash"