Skip to content

Commit

Permalink
Merge pull request #3870 from ipfs/fix/directory-cache-control
Browse files Browse the repository at this point in the history
gateway: fix erroneous Cache-Control: immutable on dir listings
  • Loading branch information
whyrusleeping authored Apr 20, 2017
2 parents db43aab + 2c4e643 commit 87d07a9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
29 changes: 29 additions & 0 deletions core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 87d07a9

Please sign in to comment.