Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the git blob id of the dir-index bundle in the ETag #6465

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ include $(dir)/Rules.mk
dir := filestore/pb
include $(dir)/Rules.mk

dir := assets
include $(dir)/Rules.mk

# -------------------- #
# universal rules #
Expand Down
1 change: 1 addition & 0 deletions assets/Rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assets-bindata-blob:=$(shell git hash-object $(dir)/bindata.go)
2 changes: 1 addition & 1 deletion cmd/ipfs/Rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 18 additions & 4 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
"github.com/multiformats/go-multibase"
)

// 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

const (
ipfsPathPrefix = "/ipfs/"
ipnsPathPrefix = "/ipns/"
Expand Down Expand Up @@ -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 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 != "" {
responseEtag = "\"DirIndex-" + AssetsBindataBlob + "_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)

// Suborigin header, sandboxes apps from each other in the browser (even
// though they are served from the same gateway domain).
Expand Down