Skip to content

Commit 37437dd

Browse files
committed
return car with enough data to prove path does not resolve
1 parent ec79bf8 commit 37437dd

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

gateway/backend_car.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,17 +1123,31 @@ func (api *CarBackend) GetCAR(ctx context.Context, p path.ImmutablePath, params
11231123
}
11241124
l := getLinksystem(teeBlock)
11251125

1126+
var isNotFound bool
1127+
11261128
// First resolve the path since we always need to.
11271129
md, terminalBlk, err := resolvePathWithRootsAndBlock(ctx, p, l)
11281130
if err != nil {
1129-
return err
1131+
if isErrNotFound(err) {
1132+
isNotFound = true
1133+
} else {
1134+
return err
1135+
}
11301136
}
1137+
11311138
if len(md.LastSegmentRemainder) > 0 {
11321139
return nil
11331140
}
11341141

11351142
if cw == nil {
1136-
cw, err = storage.NewWritable(w, []cid.Cid{md.LastSegment.RootCid()}, carv2.WriteAsCarV1(true), carv2.AllowDuplicatePuts(params.Duplicates.Bool()))
1143+
var roots []cid.Cid
1144+
if isNotFound {
1145+
roots = emptyRoot
1146+
} else {
1147+
roots = []cid.Cid{md.LastSegment.RootCid()}
1148+
}
1149+
1150+
cw, err = storage.NewWritable(w, roots, carv2.WriteAsCarV1(true), carv2.AllowDuplicatePuts(params.Duplicates.Bool()))
11371151
if err != nil {
11381152
// io.PipeWriter.CloseWithError always returns nil.
11391153
_ = w.CloseWithError(err)
@@ -1149,12 +1163,14 @@ func (api *CarBackend) GetCAR(ctx context.Context, p path.ImmutablePath, params
11491163
blockBuffer = nil
11501164
}
11511165

1152-
params.Duplicates = DuplicateBlocksIncluded
1153-
err = walkGatewaySimpleSelector(ctx, terminalBlk.Cid(), terminalBlk, []string{}, params, l)
1154-
// err = walkGatewaySimpleSelector2(ctx, terminalBlk, params.Scope, params.Range, l)
1155-
if err != nil {
1156-
return err
1166+
if !isNotFound {
1167+
params.Duplicates = DuplicateBlocksIncluded
1168+
err = walkGatewaySimpleSelector(ctx, terminalBlk.Cid(), terminalBlk, []string{}, params, l)
1169+
if err != nil {
1170+
return err
1171+
}
11571172
}
1173+
11581174
return nil
11591175
})
11601176

gateway/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ipfs/boxo/path/resolver"
1414
"github.com/ipfs/go-cid"
1515
"github.com/ipld/go-ipld-prime/datamodel"
16+
"github.com/ipld/go-ipld-prime/schema"
1617
)
1718

1819
var (
@@ -210,7 +211,7 @@ func webError(w http.ResponseWriter, r *http.Request, c *Config, err error, defa
210211
// isErrNotFound returns true for IPLD errors that should return 4xx errors (e.g. the path doesn't exist, the data is
211212
// the wrong type, etc.), rather than issues with just finding and retrieving the data.
212213
func isErrNotFound(err error) bool {
213-
if errors.Is(err, &resolver.ErrNoLink{}) {
214+
if errors.Is(err, &resolver.ErrNoLink{}) || errors.Is(err, schema.ErrNoSuchField{}) {
214215
return true
215216
}
216217

0 commit comments

Comments
 (0)