From 6d2ca5eb001f9c718d989f494308408d8d329217 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 19 Oct 2016 10:05:58 -0700 Subject: [PATCH] make path resolver no longer require whole node for construction License: MIT Signed-off-by: Jeromy This commit was moved from ipfs/kubo@67c2a4ec14a230818e0cd0052725ac001b048968 --- gateway/core/corehttp/gateway_handler.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index 99b59a1fa..c35431cbe 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -17,6 +17,7 @@ import ( dag "github.com/ipfs/go-ipfs/merkledag" dagutils "github.com/ipfs/go-ipfs/merkledag/utils" path "github.com/ipfs/go-ipfs/path" + ft "github.com/ipfs/go-ipfs/unixfs" uio "github.com/ipfs/go-ipfs/unixfs/io" humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize" @@ -153,7 +154,13 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request ipnsHostname = true } - nd, err := core.Resolve(ctx, i.node, path.Path(urlPath)) + p, err := path.ParsePath(urlPath) + if err != nil { + webError(w, "Invalid Path Error", err, http.StatusBadRequest) + return + } + + nd, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, p) // If node is in offline mode the error code and message should be different if err == core.ErrNoNamesys && !i.node.OnlineMode() { w.WriteHeader(http.StatusServiceUnavailable) @@ -240,8 +247,14 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request return } + p, err := path.ParsePath(urlPath + "/index.html") + if err != nil { + internalWebError(w, err) + return + } + // return index page instead. - nd, err := core.Resolve(ctx, i.node, path.Path(urlPath+"/index.html")) + nd, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, p) if err != nil { internalWebError(w, err) return @@ -356,7 +369,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { var newnode node.Node if rsegs[len(rsegs)-1] == "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" { - newnode = uio.NewEmptyDirectory() + newnode = ft.EmptyDirNode() } else { putNode, err := i.newDagFromReader(r.Body) if err != nil { @@ -372,7 +385,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { } var newcid *cid.Cid - rnode, err := core.Resolve(ctx, i.node, rootPath) + rnode, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, rootPath) switch ev := err.(type) { case path.ErrNoLink: // ev.Node < node where resolve failed @@ -397,7 +410,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { } e := dagutils.NewDagEditor(pbnd, i.node.DAG) - err = e.InsertNodeAtPath(ctx, newPath, newnode, uio.NewEmptyDirectory) + err = e.InsertNodeAtPath(ctx, newPath, newnode, ft.EmptyDirNode) if err != nil { webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError) return