From 2d5f8b4ebe015817b2ea285a4d589e5d796bf8a3 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Sun, 23 Jun 2019 21:17:39 +0200 Subject: [PATCH] Include the git blob id of the dir-index bundle in the ETag While the content of raw files retrieved via the gateway should never change, the look and feel of the directory index can and will change between versions of go-ipfs. Incorporate the hash of assets/bindata.go into the ETag when appropriate --- assets/assets.go | 2 ++ assets/bindata_version_hash.go | 5 +++++ core/corehttp/gateway_handler.go | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 assets/bindata_version_hash.go diff --git a/assets/assets.go b/assets/assets.go index f3f5de069..51c3981c8 100644 --- a/assets/assets.go +++ b/assets/assets.go @@ -1,6 +1,8 @@ //go:generate git submodule update --init ./dir-index-html //go:generate go run github.com/go-bindata/go-bindata/go-bindata -pkg=assets init-doc dir-index-html/dir-index.html dir-index-html/knownIcons.txt //go:generate gofmt -w bindata.go +//go:generate sh -c "sed -i \"s/.*BindataVersionHash.*/BindataVersionHash=\\\"$(git hash-object bindata.go)\\\"/\" bindata_version_hash.go" +//go:generate gofmt -w bindata_version_hash.go package assets import ( diff --git a/assets/bindata_version_hash.go b/assets/bindata_version_hash.go new file mode 100644 index 000000000..60cece78f --- /dev/null +++ b/assets/bindata_version_hash.go @@ -0,0 +1,5 @@ +package assets + +const ( + BindataVersionHash = "c1aa0601ac3eac2c50b296cf618a6747eeba8579" +) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index ba264c095..a1549efdd 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -19,6 +19,7 @@ import ( "github.com/gabriel-vasile/mimetype" "github.com/ipfs/go-cid" files "github.com/ipfs/go-ipfs-files" + assets "github.com/ipfs/go-ipfs/assets" dag "github.com/ipfs/go-merkledag" mfs "github.com/ipfs/go-mfs" path "github.com/ipfs/go-path" @@ -222,16 +223,26 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request defer dr.Close() - // Check etag send back to us - etag := "\"" + resolvedPath.Cid().String() + "\"" - if r.Header.Get("If-None-Match") == etag || r.Header.Get("If-None-Match") == "W/"+etag { + var responseEtag string + + // we need to figure out whether this is a directory before doing most of the heavy lifting below + _, ok := dr.(files.Directory) + + if ok && assets.BindataVersionHash != "" { + responseEtag = `"DirIndex-` + assets.BindataVersionHash + `_CID-` + resolvedPath.Cid().String() + `"` + } else { + responseEtag = `"` + resolvedPath.Cid().String() + `"` + } + + // Check etag sent back to us + if r.Header.Get("If-None-Match") == responseEtag || r.Header.Get("If-None-Match") == `W/`+responseEtag { w.WriteHeader(http.StatusNotModified) return } i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("X-IPFS-Path", urlPath) - w.Header().Set("Etag", etag) + w.Header().Set("Etag", responseEtag) // set these headers _after_ the error, for we may just not have it // and don't want the client to cache a 500 response...