提交 a90f4967 作者: Remco Bloemen

gateway: re-use resolved path

Instead of resolving a node, we resolve a path. This resolved path
is then re-used for Cat and Ls. This way, a resolve operation is
only done once.

The error messages for a failed resolve is changed from `ipfs cat …`
to `ipfs resolve …` to better reflect the API calls. The test is
updated accordingly.

License: MIT
Signed-off-by: 's avatarRemco Bloemen <remco@2π.com>
上级 8db6f86d
......@@ -164,35 +164,36 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
return
}
dr, err := i.api.Unixfs().Cat(ctx, parsedPath)
dir := false
// Resolve path to the final DAG node for the ETag
resolvedPath, err := i.api.ResolvePath(ctx, parsedPath)
switch err {
case nil:
// Cat() worked
defer dr.Close()
case coreiface.ErrIsDir:
dir = true
case coreiface.ErrOffline:
if !i.node.OnlineMode() {
webError(w, "ipfs cat "+urlPath, err, http.StatusServiceUnavailable)
webError(w, "ipfs resolve -r "+urlPath, err, http.StatusServiceUnavailable)
return
}
fallthrough
default:
webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound)
webError(w, "ipfs resolve -r "+urlPath, err, http.StatusNotFound)
return
}
// Resolve path to the final DAG node for the ETag
dagnode, err := i.api.ResolveNode(ctx, parsedPath)
if err != nil {
// Unixfs().Cat() also calls ResolveNode, so it should not fail here.
webError(w, "could not resolve ipfs path", err, http.StatusBadRequest)
dr, err := i.api.Unixfs().Cat(ctx, resolvedPath)
dir := false
switch err {
case nil:
// Cat() worked
defer dr.Close()
case coreiface.ErrIsDir:
dir = true
default:
webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound)
return
}
// Check etag send back to us
etag := "\"" + dagnode.Cid().String() + "\""
etag := "\"" + resolvedPath.Cid().String() + "\""
if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return
......@@ -225,7 +226,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
return
}
links, err := i.api.Unixfs().Ls(ctx, parsedPath)
links, err := i.api.Unixfs().Ls(ctx, resolvedPath)
if err != nil {
internalWebError(w, err)
return
......
......@@ -136,7 +136,7 @@ func TestGatewayGet(t *testing.T) {
{"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
{"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"},
{"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"},
{"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs cat /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
{"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
{"example.com", "/", http.StatusOK, "fnord"},
} {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论