Skip to content

Commit

Permalink
chore: apply feedback suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
hacdias and lidel committed Jun 26, 2023
1 parent cec853f commit 57bbd67
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestHeaders(t *testing.T) {
dagCborRoots = dirRoots + "," + dagCborCID
)

t.Run("Control-Cache-Immutable is not immutable for directories", func(t *testing.T) {
t.Run("Cache-Control is not immutable on generated /ipfs/ HTML dir listings", func(t *testing.T) {
req := mustNewRequest(t, http.MethodGet, ts.URL+"/ipfs/"+root.String()+"/", nil)
res := mustDoWithoutRedirect(t, req)

Expand All @@ -163,7 +163,7 @@ func TestHeaders(t *testing.T) {
}
})

t.Run("ETag contains expected values", func(t *testing.T) {
t.Run("ETag is based on CID and response format", func(t *testing.T) {
test := func(responseFormat string, path string, format string, args ...any) {
t.Run(responseFormat, func(t *testing.T) {
url := ts.URL + path
Expand Down
34 changes: 23 additions & 11 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,20 @@ type requestData struct {

// Defined if resolution has already happened.
pathMetadata *ContentPathMetadata
resolvedPath *ImmutablePath
}

func (rq *requestData) maybeResolvedPath() ImmutablePath {
if rq.resolvedPath != nil {
return *rq.resolvedPath
// mostlyResolvedPath is an opportunistic optimization that returns the mostly
// resolved version of ImmutablePath available. It does not guarantee it is fully
// resolved, nor that it is the original.
func (rq *requestData) mostlyResolvedPath() ImmutablePath {
if rq.pathMetadata != nil {
imPath, err := NewImmutablePath(rq.pathMetadata.LastSegment)
if err != nil {
// This will never happen. This error has previously been checked in
// [handleIfNoneMatch] and the request will have returned 500.
panic(err)
}
return imPath
}
return rq.immutablePath
}
Expand Down Expand Up @@ -469,7 +477,12 @@ func setContentDispositionHeader(w http.ResponseWriter, filename string, disposi

// setIpfsRootsHeader sets the X-Ipfs-Roots header with logical CID array for
// efficient HTTP cache invalidation.
func setIpfsRootsHeader(w http.ResponseWriter, pathMetadata ContentPathMetadata) {
func setIpfsRootsHeader(w http.ResponseWriter, rq *requestData, md *ContentPathMetadata) {
// Update requestData with the latest ContentPathMetadata if it wasn't set yet.
if rq.pathMetadata == nil {
rq.pathMetadata = md
}

// These are logical roots where each CID represent one path segment
// and resolves to either a directory or the root block of a file.
// The main purpose of this header is allow HTTP caches to do smarter decisions
Expand All @@ -492,10 +505,10 @@ func setIpfsRootsHeader(w http.ResponseWriter, pathMetadata ContentPathMetadata)
// the last root (responsible for specific article) may not change at all.

var pathRoots []string
for _, c := range pathMetadata.PathSegmentRoots {
for _, c := range rq.pathMetadata.PathSegmentRoots {
pathRoots = append(pathRoots, c.String())
}
pathRoots = append(pathRoots, pathMetadata.LastSegment.Cid().String())
pathRoots = append(pathRoots, rq.pathMetadata.LastSegment.Cid().String())
rootCidList := strings.Join(pathRoots, ",") // convention from rfc2616#sec4.2

w.Header().Set("X-Ipfs-Roots", rootCidList)
Expand Down Expand Up @@ -686,8 +699,7 @@ func (i *handler) handleIfNoneMatch(w http.ResponseWriter, r *http.Request, rq *
return true
}

resolvedPath := pathMetadata.LastSegment
pathCid := resolvedPath.Cid()
pathCid := pathMetadata.LastSegment.Cid()

// Checks against both file, dir listing, and dag index Etags.
// This is an inexpensive check, and it happens before we do any I/O.
Expand All @@ -701,14 +713,14 @@ func (i *handler) handleIfNoneMatch(w http.ResponseWriter, r *http.Request, rq *
return true
}

resolvedImPath, err := NewImmutablePath(resolvedPath)
// Check if the resolvedPath is an immutable path.
_, err = NewImmutablePath(pathMetadata.LastSegment)
if err != nil {
i.webError(w, r, err, http.StatusInternalServerError)
return true
}

rq.pathMetadata = &pathMetadata
rq.resolvedPath = &resolvedImPath
return false
}

Expand Down
7 changes: 2 additions & 5 deletions gateway/handler_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ func (i *handler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *h
ctx, span := spanTrace(ctx, "Handler.ServeRawBlock", trace.WithAttributes(attribute.String("path", rq.immutablePath.String())))
defer span.End()

pathMetadata, data, err := i.backend.GetBlock(ctx, rq.maybeResolvedPath())
pathMetadata, data, err := i.backend.GetBlock(ctx, rq.mostlyResolvedPath())
if !i.handleRequestErrors(w, r, rq.contentPath, err) {
return false
}
if rq.pathMetadata == nil {
rq.pathMetadata = &pathMetadata
}
defer data.Close()

setIpfsRootsHeader(w, *rq.pathMetadata)
setIpfsRootsHeader(w, rq, &pathMetadata)

blockCid := pathMetadata.LastSegment.Cid()

Expand Down
2 changes: 1 addition & 1 deletion gateway/handler_car.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (i *handler) serveCAR(ctx context.Context, w http.ResponseWriter, r *http.R
return false
}
defer carFile.Close()
setIpfsRootsHeader(w, md)
setIpfsRootsHeader(w, rq, &md)

// Make it clear we don't support range-requests over a car stream
// Partial downloads and resumes should be handled using requests for
Expand Down
7 changes: 2 additions & 5 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,13 @@ func (i *handler) serveCodec(ctx context.Context, w http.ResponseWriter, r *http
ctx, span := spanTrace(ctx, "Handler.ServeCodec", trace.WithAttributes(attribute.String("path", rq.immutablePath.String()), attribute.String("requestedContentType", rq.responseFormat)))
defer span.End()

pathMetadata, data, err := i.backend.GetBlock(ctx, rq.maybeResolvedPath())
pathMetadata, data, err := i.backend.GetBlock(ctx, rq.mostlyResolvedPath())
if !i.handleRequestErrors(w, r, rq.contentPath, err) {
return false
}
if rq.pathMetadata == nil {
rq.pathMetadata = &pathMetadata
}
defer data.Close()

setIpfsRootsHeader(w, *rq.pathMetadata)
setIpfsRootsHeader(w, rq, &pathMetadata)
return i.renderCodec(ctx, w, r, rq, data)
}

Expand Down
11 changes: 4 additions & 7 deletions gateway/handler_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (i *handler) serveDefaults(ctx context.Context, w http.ResponseWriter, r *h
switch r.Method {
case http.MethodHead:
var data files.Node
pathMetadata, data, err = i.backend.Head(ctx, rq.maybeResolvedPath())
pathMetadata, data, err = i.backend.Head(ctx, rq.mostlyResolvedPath())
if !i.handleRequestErrors(w, r, rq.contentPath, err) {
return false
}
Expand Down Expand Up @@ -62,10 +62,10 @@ func (i *handler) serveDefaults(ctx context.Context, w http.ResponseWriter, r *h
// allow backend to find providers for parents, even when internal
// CIDs are not announced, and will provide better key for caching
// related DAGs.
pathMetadata, getResp, err = i.backend.Get(ctx, rq.maybeResolvedPath(), ranges...)
pathMetadata, getResp, err = i.backend.Get(ctx, rq.mostlyResolvedPath(), ranges...)
if err != nil {
if isWebRequest(rq.responseFormat) {
forwardedPath, continueProcessing := i.handleWebRequestErrors(w, r, rq.maybeResolvedPath(), rq.immutablePath, rq.contentPath, err, rq.logger)
forwardedPath, continueProcessing := i.handleWebRequestErrors(w, r, rq.mostlyResolvedPath(), rq.immutablePath, rq.contentPath, err, rq.logger)
if !continueProcessing {
return false
}
Expand Down Expand Up @@ -94,10 +94,7 @@ func (i *handler) serveDefaults(ctx context.Context, w http.ResponseWriter, r *h
return false
}

if rq.pathMetadata == nil {
rq.pathMetadata = &pathMetadata
}
setIpfsRootsHeader(w, *rq.pathMetadata)
setIpfsRootsHeader(w, rq, &pathMetadata)

resolvedPath := pathMetadata.LastSegment
switch mc.Code(resolvedPath.Cid().Prefix().Codec) {
Expand Down
7 changes: 2 additions & 5 deletions gateway/handler_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ func (i *handler) serveTAR(ctx context.Context, w http.ResponseWriter, r *http.R
defer cancel()

// Get Unixfs file (or directory)
pathMetadata, file, err := i.backend.GetAll(ctx, rq.maybeResolvedPath())
pathMetadata, file, err := i.backend.GetAll(ctx, rq.mostlyResolvedPath())
if !i.handleRequestErrors(w, r, rq.contentPath, err) {
return false
}
if rq.pathMetadata == nil {
rq.pathMetadata = &pathMetadata
}
defer file.Close()

setIpfsRootsHeader(w, *rq.pathMetadata)
setIpfsRootsHeader(w, rq, &pathMetadata)
rootCid := pathMetadata.LastSegment.Cid()

// Set Cache-Control and read optional Last-Modified time
Expand Down

0 comments on commit 57bbd67

Please sign in to comment.