Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6a9be55

Browse files
committedMay 4, 2015
core/resolve: addressed code review
- pass the nodes context instead of TODO when running commands through the http interface - error out instead of panic on /ipns/.. resolve in offline mode - setup ResolveLink timeout for each link - some minor tweaks to make golint happy
1 parent 72ee5d5 commit 6a9be55

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed
 

‎commands/http/handler.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12+
1213
cmds "github.com/ipfs/go-ipfs/commands"
1314
u "github.com/ipfs/go-ipfs/util"
1415
)
@@ -48,11 +49,6 @@ func NewHandler(ctx cmds.Context, root *cmds.Command, origin string) *Handler {
4849
}
4950

5051
func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
51-
// create a context.Context to pass into the commands.
52-
ctx, cancel := context.WithCancel(context.TODO())
53-
defer cancel()
54-
i.ctx.Context = ctx
55-
5652
log.Debug("Incoming API request: ", r.URL)
5753

5854
// error on external referers (to prevent CSRF attacks)
@@ -84,6 +80,27 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8480
w.Write([]byte(err.Error()))
8581
return
8682
}
83+
84+
// get the node's context to pass into the commands.
85+
node, err := i.ctx.GetNode()
86+
if err != nil {
87+
err = fmt.Errorf("cmds/http: couldn't GetNode(): %s", err)
88+
http.Error(w, err.Error(), http.StatusInternalServerError)
89+
return
90+
}
91+
ctx, cancel := context.WithCancel(node.Context())
92+
defer cancel()
93+
/*
94+
TODO(cryptix): the next line looks very fishy to me..
95+
It looks like the the context for the command request beeing prepared here is shared across all incoming requests..
96+
97+
I assume it really isn't because ServeHTTP() doesn't take a pointer receiver, but it's really subtule..
98+
99+
Shouldn't the context be just put on the command request?
100+
101+
ps: take note of the name clash - commands.Context != context.Context
102+
*/
103+
i.ctx.Context = ctx
87104
req.SetContext(i.ctx)
88105

89106
// call the command

‎core/pathresolver.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import (
1313

1414
const maxLinks = 32
1515

16-
var ErrTooManyLinks = errors.New("exceeded maximum number of links in ipns entry")
16+
// errors returned by Resolve function
17+
var (
18+
ErrTooManyLinks = errors.New("core/resolve: exceeded maximum number of links in ipns entry")
19+
ErrNoNamesys = errors.New("core/resolve: no Namesys on IpfsNode - can't resolve ipns entry")
20+
)
1721

18-
// Resolves the given path by parsing out /ipns/ entries and then going
22+
// Resolve resolves the given path by parsing out /ipns/ entries and then going
1923
// through the /ipfs/ entries and returning the final merkledage node.
2024
// Effectively enables /ipns/ in CLI commands.
2125
func Resolve(ctx context.Context, n *IpfsNode, p path.Path) (*merkledag.Node, error) {
@@ -39,6 +43,10 @@ func (r *resolver) resolveRecurse(depth int) (*merkledag.Node, error) {
3943
// to be an ipfs or an ipns resolution?
4044

4145
if strings.HasPrefix(r.p.String(), "/ipns/") {
46+
// TODO(cryptix): we sould be able to query the local cache for the path
47+
if r.n.Namesys == nil {
48+
return nil, ErrNoNamesys
49+
}
4250
// if it's an ipns path, try to resolve it.
4351
// if we can't, we can give that error back to the user.
4452
seg := r.p.Segments()

‎path/resolver.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// package path implements utilities for resolving paths within ipfs.
1+
// Package path implements utilities for resolving paths within ipfs.
22
package path
33

44
import (
@@ -99,9 +99,6 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd *merkledag.Node, names
9999
result = append(result, ndd)
100100
nd := ndd // dup arg workaround
101101

102-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
103-
defer cancel()
104-
105102
// for each of the path components
106103
for _, name := range names {
107104

@@ -123,11 +120,8 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd *merkledag.Node, names
123120

124121
if nlink.Node == nil {
125122
// fetch object for link and assign to nd
126-
/*
127-
TODO(cryptix): we wrapped a new context for each name iteration
128-
is this correct?
129-
moved it out of the loop for now
130-
*/
123+
ctx, cancel := context.WithTimeout(ctx, time.Minute)
124+
defer cancel()
131125
nd, err := s.DAG.Get(ctx, next)
132126
if err != nil {
133127
return append(result, nd), err

0 commit comments

Comments
 (0)
Please sign in to comment.