diff options
author | Balint Gabor <balint.g@gmail.com> | 2018-02-23 06:15:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-23 06:15:13 +0800 |
commit | a1984ce727642b1989657b9eb430562d47f89c70 (patch) | |
tree | 82b799d32555f0943d033dd89079cf71b041d24d /swarm/api/api.go | |
parent | 8522b312210865022618489a46db87df44923d4a (diff) | |
parent | 6a9730edaa3c398ef1e9fe084f9b16de4d3ef78e (diff) | |
download | dexon-a1984ce727642b1989657b9eb430562d47f89c70.tar dexon-a1984ce727642b1989657b9eb430562d47f89c70.tar.gz dexon-a1984ce727642b1989657b9eb430562d47f89c70.tar.bz2 dexon-a1984ce727642b1989657b9eb430562d47f89c70.tar.lz dexon-a1984ce727642b1989657b9eb430562d47f89c70.tar.xz dexon-a1984ce727642b1989657b9eb430562d47f89c70.tar.zst dexon-a1984ce727642b1989657b9eb430562d47f89c70.zip |
Merge pull request #15748 from janos/multiple-ens-endpoints
swarm: repeated --ens-api CLI flag (#14386)
Diffstat (limited to 'swarm/api/api.go')
-rw-r--r-- | swarm/api/api.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/swarm/api/api.go b/swarm/api/api.go index 8c4bca2ec..fdf76d390 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "net/http" + "path" "regexp" "strings" "sync" @@ -40,6 +41,81 @@ type Resolver interface { Resolve(string) (common.Hash, error) } +// NoResolverError is returned by MultiResolver.Resolve if no resolver +// can be found for the address. +type NoResolverError struct { + TLD string +} + +func NewNoResolverError(tld string) *NoResolverError { + return &NoResolverError{TLD: tld} +} + +func (e *NoResolverError) Error() string { + if e.TLD == "" { + return "no ENS resolver" + } + return fmt.Sprintf("no ENS endpoint configured to resolve .%s TLD names", e.TLD) +} + +// MultiResolver is used to resolve URL addresses based on their TLDs. +// Each TLD can have multiple resolvers, and the resoluton from the +// first one in the sequence will be returned. +type MultiResolver struct { + resolvers map[string][]Resolver +} + +// MultiResolverOption sets options for MultiResolver and is used as +// arguments for its constructor. +type MultiResolverOption func(*MultiResolver) + +// MultiResolverOptionWithResolver adds a Resolver to a list of resolvers +// for a specific TLD. If TLD is an empty string, the resolver will be added +// to the list of default resolver, the ones that will be used for resolution +// of addresses which do not have their TLD resolver specified. +func MultiResolverOptionWithResolver(r Resolver, tld string) MultiResolverOption { + return func(m *MultiResolver) { + m.resolvers[tld] = append(m.resolvers[tld], r) + } +} + +// NewMultiResolver creates a new instance of MultiResolver. +func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) { + m = &MultiResolver{ + resolvers: make(map[string][]Resolver), + } + for _, o := range opts { + o(m) + } + return m +} + +// Resolve resolves address by choosing a Resolver by TLD. +// If there are more default Resolvers, or for a specific TLD, +// the Hash from the the first one which does not return error +// will be returned. +func (m MultiResolver) Resolve(addr string) (h common.Hash, err error) { + rs := m.resolvers[""] + tld := path.Ext(addr) + if tld != "" { + tld = tld[1:] + rstld, ok := m.resolvers[tld] + if ok { + rs = rstld + } + } + if rs == nil { + return h, NewNoResolverError(tld) + } + for _, r := range rs { + h, err = r.Resolve(addr) + if err == nil { + return + } + } + return +} + /* Api implements webserver/file system related content storage and retrieval on top of the dpa |