Skip to content

Commit

Permalink
fix: path joinining
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Sep 27, 2023
1 parent 3f5d5bf commit f9799cb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
7 changes: 3 additions & 4 deletions namesys/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
return out
}

segments := p.Segments()
fqdn := segments[1]
fqdn := p.Segments()[1]
if _, ok := dns.IsDomainName(fqdn); !ok {
out <- ResolveResult{Err: fmt.Errorf("not a valid domain name: %q", fqdn)}
close(out)
Expand Down Expand Up @@ -90,7 +89,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
break
}
if subRes.Err == nil {
p, err := path.Join(subRes.Path, segments[2:]...)
p, err := joinPaths(subRes.Path, p)
emitOnceResult(ctx, out, ResolveResult{Path: p, Err: err})
// Return without waiting for rootRes, since this result
// (for "_dnslink."+fqdn) takes precedence
Expand All @@ -103,7 +102,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, options
break
}
if rootRes.Err == nil {
p, err := path.Join(rootRes.Path, segments[2:]...)
p, err := joinPaths(rootRes.Path, p)
emitOnceResult(ctx, out, ResolveResult{Path: p, Err: err})
// Do not return here. Wait for subRes so that it is
// output last if good, thereby giving subRes precedence.
Expand Down
9 changes: 4 additions & 5 deletions namesys/ipns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, option
ctx, cancel = context.WithTimeout(ctx, options.DhtTimeout)
}

segments := p.Segments()
name, err := ipns.NameFromString(segments[1])
name, err := ipns.NameFromString(p.Segments()[1])
if err != nil {
out <- ResolveResult{Err: err}
close(out)
Expand Down Expand Up @@ -104,13 +103,13 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, option
return
}

p, err := rec.Value()
resolvedBase, err := rec.Value()
if err != nil {
emitOnceResult(ctx, out, ResolveResult{Err: err})
return
}

p, err = path.Join(p, segments[2:]...)
resolvedBase, err = joinPaths(resolvedBase, p)
if err != nil {
emitOnceResult(ctx, out, ResolveResult{Err: err})
return
Expand All @@ -122,7 +121,7 @@ func (r *IPNSResolver) resolveOnceAsync(ctx context.Context, p path.Path, option
return
}

emitOnceResult(ctx, out, ResolveResult{Path: p, TTL: ttl})
emitOnceResult(ctx, out, ResolveResult{Path: resolvedBase, TTL: ttl})
case <-ctx.Done():
return
}
Expand Down
17 changes: 7 additions & 10 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
madns "github.com/multiformats/go-multiaddr-dns"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
)

// namesys is a multi-protocol [NameSystem] that implements generic IPFS naming.
Expand Down Expand Up @@ -162,8 +163,6 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
return out
}

// TODO

segments := p.Segments()
resolvablePath, err := path.NewPathFromSegments(segments[0], segments[1])
if err != nil {
Expand All @@ -172,10 +171,8 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
return out
}

if p, ttl, ok := ns.cacheGet(resolvablePath.String()); ok {
if len(segments) > 2 {
p, err = path.Join(p, segments[2:]...)
}
if resolvedBase, ttl, ok := ns.cacheGet(resolvablePath.String()); ok {
p, err = joinPaths(resolvedBase, p)
span.SetAttributes(attribute.Bool("CacheHit", true))
span.RecordError(err)
out <- ResolveResult{Path: p, TTL: ttl, Err: err}
Expand Down Expand Up @@ -218,10 +215,10 @@ func (ns *namesys) resolveOnceAsync(ctx context.Context, p path.Path, options Re
best = res
}

// Attach rest of the path
p := res.Path
if p != nil && len(segments) > 2 {
p, err = path.Join(p, segments[2:]...)
p, err := joinPaths(res.Path, p)
if err != nil {
// res.Err may already be defined, so just combine them
res.Err = multierr.Combine(err, res.Err)
}

emitOnceResult(ctx, out, ResolveResult{Path: p, TTL: res.TTL, Err: res.Err})
Expand Down
19 changes: 19 additions & 0 deletions namesys/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package namesys
import (
"context"
"fmt"
"strings"
"time"

"github.com/ipfs/boxo/path"
Expand Down Expand Up @@ -121,6 +122,24 @@ func emitResult(ctx context.Context, outCh chan<- ResolveResult, r ResolveResult
}
}

func joinPaths(resolvedBase, unresolvedPath path.Path) (path.Path, error) {
if resolvedBase == nil {
return nil, nil
}

segments := unresolvedPath.Segments()[2:]
if strings.HasSuffix(unresolvedPath.String(), "/") {
segments = append(segments, "")
}

// simple optimization
if len(segments) == 0 {
return resolvedBase, nil
}

return path.Join(resolvedBase, segments...)
}

var tracer = otel.Tracer("boxo/namesys")

func startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
Expand Down

0 comments on commit f9799cb

Please sign in to comment.