From 226cb424b51dbf52c32c3058d4d15bf7769c605f Mon Sep 17 00:00:00 2001 From: Mib Kd743naq Date: Sun, 23 Jun 2019 19:17:39 +0000 Subject: [PATCH 1/2] 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 License: MIT Signed-off-by: Mib Kd743naq --- Rules.mk | 2 ++ assets/Rules.mk | 1 + cmd/ipfs/Rules.mk | 2 +- core/corehttp/gateway_handler.go | 22 ++++++++++++++++++---- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 assets/Rules.mk diff --git a/Rules.mk b/Rules.mk index d4bdf361ac7..15bc05567bc 100644 --- a/Rules.mk +++ b/Rules.mk @@ -54,6 +54,8 @@ include $(dir)/Rules.mk dir := filestore/pb include $(dir)/Rules.mk +dir := assets +include $(dir)/Rules.mk # -------------------- # # universal rules # diff --git a/assets/Rules.mk b/assets/Rules.mk new file mode 100644 index 00000000000..e9a5bb86aba --- /dev/null +++ b/assets/Rules.mk @@ -0,0 +1 @@ +assets-bindata-blob:=$(shell git hash-object $(dir)/bindata.go) diff --git a/cmd/ipfs/Rules.mk b/cmd/ipfs/Rules.mk index 2a768b5fc8a..81c2b0861c2 100644 --- a/cmd/ipfs/Rules.mk +++ b/cmd/ipfs/Rules.mk @@ -13,7 +13,7 @@ PATH := $(realpath $(d)):$(PATH) # DEPS_OO_$(d) += merkledag/pb/merkledag.pb.go namesys/pb/namesys.pb.go # DEPS_OO_$(d) += pin/internal/pb/header.pb.go unixfs/pb/unixfs.pb.go -$(d)_flags =-ldflags="-X "github.com/ipfs/go-ipfs".CurrentCommit=$(git-hash)" +$(d)_flags =-ldflags="-X "github.com/ipfs/go-ipfs".CurrentCommit=$(git-hash) -X "github.com/ipfs/go-ipfs/core/corehttp".AssetsBindataBlob=$(assets-bindata-blob)" $(d)-try-build $(IPFS_BIN_$(d)): GOFLAGS += $(cmd/ipfs_flags) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 1aa343a5502..1984388df03 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -32,6 +32,10 @@ import ( "github.com/multiformats/go-multibase" ) +// This is set to the git object id of github.com/ipfs/go-ipfs/assets/bindata.go +// during build time and is critical during directory-view ETag construction +var AssetsBindataBlob string + const ( ipfsPathPrefix = "/ipfs/" ipnsPathPrefix = "/ipns/" @@ -178,16 +182,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 response_etag 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 && AssetsBindataBlob != "" { + response_etag = "\"DirIndex-" + AssetsBindataBlob + "_CID-" + resolvedPath.Cid().String() + "\"" + } else { + response_etag = "\"" + resolvedPath.Cid().String() + "\"" + } + + // Check etag sent back to us + if r.Header.Get("If-None-Match") == response_etag || r.Header.Get("If-None-Match") == "W/"+response_etag { 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", response_etag) // Suborigin header, sandboxes apps from each other in the browser (even // though they are served from the same gateway domain). From c32ba7e5288e903ce65f7321c8329a3fc14d2e2d Mon Sep 17 00:00:00 2001 From: Mib Kd743naq Date: Tue, 25 Jun 2019 07:10:16 +0000 Subject: [PATCH 2/2] Fix complaints from CodeClimate License: MIT Signed-off-by: Mib Kd743naq --- core/corehttp/gateway_handler.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 1984388df03..81d9a988465 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -32,7 +32,7 @@ import ( "github.com/multiformats/go-multibase" ) -// This is set to the git object id of github.com/ipfs/go-ipfs/assets/bindata.go +// AssetsBindataBlob is set to the git object id of github.com/ipfs/go-ipfs/assets/bindata.go // during build time and is critical during directory-view ETag construction var AssetsBindataBlob string @@ -182,26 +182,26 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request defer dr.Close() - var response_etag string + 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 && AssetsBindataBlob != "" { - response_etag = "\"DirIndex-" + AssetsBindataBlob + "_CID-" + resolvedPath.Cid().String() + "\"" + responseEtag = "\"DirIndex-" + AssetsBindataBlob + "_CID-" + resolvedPath.Cid().String() + "\"" } else { - response_etag = "\"" + resolvedPath.Cid().String() + "\"" + responseEtag = "\"" + resolvedPath.Cid().String() + "\"" } // Check etag sent back to us - if r.Header.Get("If-None-Match") == response_etag || r.Header.Get("If-None-Match") == "W/"+response_etag { + 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", response_etag) + w.Header().Set("Etag", responseEtag) // Suborigin header, sandboxes apps from each other in the browser (even // though they are served from the same gateway domain).