Skip to content

Commit

Permalink
refactor: rename HasDNSLinkRecord to GetDNSLinkRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 6, 2023
1 parent 51d5002 commit fd4daf7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions examples/gateway/common/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"context"
"errors"
"fmt"
"net/http"
gopath "path"
Expand Down Expand Up @@ -143,6 +144,10 @@ func (api *BlocksGateway) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte,
return nil, routing.ErrNotSupported
}

func (api *BlocksGateway) GetDNSLinkRecord(ctx context.Context, host string) (ifacepath.Path, error) {
return nil, errors.New("not implemented")
}

func (api *BlocksGateway) IsCached(ctx context.Context, p ifacepath.Path) bool {
rp, err := api.ResolvePath(ctx, p)
if err != nil {
Expand Down Expand Up @@ -188,11 +193,6 @@ func (api *BlocksGateway) ResolvePath(ctx context.Context, p ifacepath.Path) (if
return ifacepath.NewResolvedPath(ipath, node, root, gopath.Join(rest...)), nil
}

func (api *BlocksGateway) HasDNSLinkRecord(ctx context.Context, host string) bool {
// Not implemented.
return false
}

func (api *BlocksGateway) resolveNode(ctx context.Context, p ifacepath.Path) (format.Node, error) {
rp, err := api.ResolvePath(ctx, p)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ type API interface {
// from the routing system.
GetIPNSRecord(context.Context, cid.Cid) ([]byte, error)

// GetDNSLinkRecord returns the DNSLink TXT record for the provided FQDN.
// Unlike ResolvePath, it does not perform recursive resolution. It only
// checks for the existence of a DNSLink TXT record with path starting with
// /ipfs/ or /ipns/ and returns the path as-is.
GetDNSLinkRecord(context.Context, string) (path.Path, error)

// IsCached returns whether or not the path exists locally.
IsCached(context.Context, path.Path) bool

// ResolvePath resolves the path using UnixFS resolver. If the path does not
// exist due to a missing link, it should return an error of type:
// https://pkg.go.dev/github.com/ipfs/[email protected]/resolver#ErrNoLink
ResolvePath(context.Context, path.Path) (path.Resolved, error)

// HasDNSLinkRecord returns if the provided path has a DNSLink TXT record.
// It does not perform any validation, only checks for the existence of
// a DNSLink TXT record.
HasDNSLinkRecord(context.Context, string) bool
}

// A helper function to clean up a set of headers:
Expand Down
9 changes: 6 additions & 3 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func (m *mockApi) ResolvePath(context.Context, ipath.Path) (ipath.Resolved, erro
return nil, errors.New("not implemented")
}

func (m *mockApi) HasDNSLinkRecord(ctx context.Context, hostname string) bool {
_, err := m.ns.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
return err == nil || err == namesys.ErrResolveRecursion
func (m *mockApi) GetDNSLinkRecord(ctx context.Context, hostname string) (ipath.Path, error) {
p, err := m.ns.Resolve(ctx, "/ipns/"+hostname, nsopts.Depth(1))
if err == namesys.ErrResolveRecursion {
err = nil
}
return ipath.New(p.String()), err
}
3 changes: 2 additions & 1 deletion gateway/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ func hasDNSLinkRecord(ctx context.Context, api API, host string) bool {
return false
}

return api.HasDNSLinkRecord(ctx, dnslinkName)
_, err := api.GetDNSLinkRecord(ctx, dnslinkName)
return err == nil
}

func isSubdomainNamespace(ns string) bool {
Expand Down

0 comments on commit fd4daf7

Please sign in to comment.