gateway: fix erroneous Cache-Control: immutable on dir listings
License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
Lars Gierth committed
Apr 19, 2017 at 05:10 UTC
2c4e6434adadb9ab0f0c1be9f9046d2761d8fc0f
2 files changed
+30
-1
core/corehttp/gateway_handler.go
+1
-1
@@ -202,7 +202,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
202
// and only if it's /ipfs!
203
// TODO: break this out when we split /ipfs /ipns routes.
204
modtime := time.Now()
205
- if strings.HasPrefix(urlPath, ipfsPathPrefix) {
205
+ if strings.HasPrefix(urlPath, ipfsPathPrefix) && !dir {
206
w.Header().Set("Etag", etag)
207
w.Header().Set("Cache-Control", "public, max-age=29030400, immutable")
208
core/corehttp/gateway_test.go
+29
@@ -23,6 +23,9 @@ import (
23
id "gx/ipfs/QmeWJwi61vii5g8zQUB9UGegfUbmhTKHgeDFP9XuSp5jZ4/go-libp2p/p2p/protocol/identify"
24
)
25
26
+// `ipfs object new unixfs-dir`
27
+var emptyDir = "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
28
+
29
type mockNamesys map[string]path.Path
30
31
func (m mockNamesys) Resolve(ctx context.Context, name string) (value path.Path, err error) {
@@ -461,6 +464,32 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
464
}
465
}
466
467
+func TestCacheControlImmutable(t *testing.T) {
468
+ ts, _ := newTestServerAndNode(t, nil)
469
+ t.Logf("test server url: %s", ts.URL)
470
+ defer ts.Close()
471
+
472
+ req, err := http.NewRequest("GET", ts.URL+emptyDir+"/", nil)
473
+ if err != nil {
474
+ t.Fatal(err)
475
+ }
476
+
477
+ res, err := doWithoutRedirect(req)
478
+ if err != nil {
479
+ t.Fatal(err)
480
+ }
481
+
482
+ // check the immutable tag isn't set
483
+ hdrs, ok := res.Header["Cache-Control"]
484
+ if ok {
485
+ for _, hdr := range hdrs {
486
+ if strings.Contains(hdr, "immutable") {
487
+ t.Fatalf("unexpected Cache-Control: immutable on directory listing: %s", hdr)
488
+ }
489
+ }
490
+ }
491
+}
492
+
493
func TestVersion(t *testing.T) {
494
config.CurrentCommit = "theshortcommithash"
495