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
Peter Rabbitson committed
Jun 23, 2019 at 21:17 UTC
2d5f8b4ebe015817b2ea285a4d589e5d796bf8a3
3 files changed
+22
-4
assets/assets.go
+2
@@ -1,6 +1,8 @@
1
//go:generate git submodule update --init ./dir-index-html
2
//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
3
//go:generate gofmt -w bindata.go
4
+//go:generate sh -c "sed -i \"s/.*BindataVersionHash.*/BindataVersionHash=\\\"$(git hash-object bindata.go)\\\"/\" bindata_version_hash.go"
5
+//go:generate gofmt -w bindata_version_hash.go
6
package assets
7
8
import (
assets/bindata_version_hash.go
new
+5
@@ -0,0 +1,5 @@
1
+package assets
2
+
3
+const (
4
+ BindataVersionHash = "c1aa0601ac3eac2c50b296cf618a6747eeba8579"
5
+)
core/corehttp/gateway_handler.go
+15
-4
@@ -19,6 +19,7 @@ import (
19
"github.com/gabriel-vasile/mimetype"
20
"github.com/ipfs/go-cid"
21
files "github.com/ipfs/go-ipfs-files"
22
+ assets "github.com/ipfs/go-ipfs/assets"
23
dag "github.com/ipfs/go-merkledag"
24
mfs "github.com/ipfs/go-mfs"
25
path "github.com/ipfs/go-path"
@@ -222,16 +223,26 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
223
224
defer dr.Close()
225
225
- // Check etag send back to us
226
- etag := "\"" + resolvedPath.Cid().String() + "\""
227
- if r.Header.Get("If-None-Match") == etag || r.Header.Get("If-None-Match") == "W/"+etag {
226
+ var responseEtag string
227
+
228
+ // we need to figure out whether this is a directory before doing most of the heavy lifting below
229
+ _, ok := dr.(files.Directory)
230
+
231
+ if ok && assets.BindataVersionHash != "" {
232
+ responseEtag = `"DirIndex-` + assets.BindataVersionHash + `_CID-` + resolvedPath.Cid().String() + `"`
233
+ } else {
234
+ responseEtag = `"` + resolvedPath.Cid().String() + `"`
235
+ }
236
+
237
+ // Check etag sent back to us
238
+ if r.Header.Get("If-None-Match") == responseEtag || r.Header.Get("If-None-Match") == `W/`+responseEtag {
239
w.WriteHeader(http.StatusNotModified)
240
return
241
}
242
243
i.addUserHeaders(w) // ok, _now_ write user's headers.
244
w.Header().Set("X-IPFS-Path", urlPath)
234
- w.Header().Set("Etag", etag)
245
+ w.Header().Set("Etag", responseEtag)
246
247
// set these headers _after_ the error, for we may just not have it
248
// and don't want the client to cache a 500 response...