aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api
diff options
context:
space:
mode:
authorholisticode <holistic.computing@gmail.com>2017-04-13 17:06:19 +0800
committerFelix Lange <fjl@users.noreply.github.com>2017-04-13 17:06:19 +0800
commit409b61fe3c8046dc26f3442846e9f7032bebb9c5 (patch)
tree041d051d192165fdf1d56391ec63c67822492476 /swarm/api
parentd5d910e8b68f6c6b29ca85f5a9fa1b72b2cc08c1 (diff)
downloadgo-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.tar
go-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.tar.gz
go-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.tar.bz2
go-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.tar.lz
go-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.tar.xz
go-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.tar.zst
go-tangerine-409b61fe3c8046dc26f3442846e9f7032bebb9c5.zip
swarm/api: better name resolver handling (#3754)
Fixes #3608
Diffstat (limited to 'swarm/api')
-rw-r--r--swarm/api/api.go28
-rw-r--r--swarm/api/http/server_test.go28
2 files changed, 43 insertions, 13 deletions
diff --git a/swarm/api/api.go b/swarm/api/api.go
index f58b7a53d..26a9445d5 100644
--- a/swarm/api/api.go
+++ b/swarm/api/api.go
@@ -17,7 +17,6 @@
package api
import (
- "errors"
"fmt"
"io"
"net/http"
@@ -84,25 +83,28 @@ type ErrResolve error
// DNS Resolver
func (self *Api) Resolve(uri *URI) (storage.Key, error) {
log.Trace(fmt.Sprintf("Resolving : %v", uri.Addr))
+
+ var err error
+ if !uri.Immutable() {
+ if self.dns != nil {
+ resolved, err := self.dns.Resolve(uri.Addr)
+ if err == nil {
+ return resolved[:], nil
+ }
+ } else {
+ err = fmt.Errorf("no DNS to resolve name")
+ }
+ }
if hashMatcher.MatchString(uri.Addr) {
- log.Trace(fmt.Sprintf("addr is a hash: %q", uri.Addr))
return storage.Key(common.Hex2Bytes(uri.Addr)), nil
}
- if uri.Immutable() {
- return nil, errors.New("refusing to resolve immutable address")
- }
- if self.dns == nil {
- return nil, fmt.Errorf("unable to resolve addr %q, resolver not configured", uri.Addr)
- }
- hash, err := self.dns.Resolve(uri.Addr)
if err != nil {
- log.Warn(fmt.Sprintf("DNS error resolving addr %q: %s", uri.Addr, err))
- return nil, ErrResolve(err)
+ return nil, fmt.Errorf("'%s' does not resolve: %v but is not a content hash", uri.Addr, err)
}
- log.Trace(fmt.Sprintf("addr lookup: %v -> %v", uri.Addr, hash))
- return hash[:], nil
+ return nil, fmt.Errorf("'%s' is not a content hash", uri.Addr)
}
+
// Put provides singleton manifest creation on top of dpa store
func (self *Api) Put(content, contentType string) (storage.Key, error) {
r := strings.NewReader(content)
diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go
index 942f3ba0b..ceb8db75b 100644
--- a/swarm/api/http/server_test.go
+++ b/swarm/api/http/server_test.go
@@ -99,4 +99,32 @@ func TestBzzrGetPath(t *testing.T) {
}
}
+ nonhashtests := []string{
+ srv.URL + "/bzz:/name",
+ srv.URL + "/bzzi:/nonhash",
+ srv.URL + "/bzzr:/nonhash",
+ }
+
+ nonhashresponses := []string{
+ "error resolving name: 'name' does not resolve: no DNS to resolve name but is not a content hash\n",
+ "error resolving nonhash: 'nonhash' is not a content hash\n",
+ "error resolving nonhash: 'nonhash' does not resolve: no DNS to resolve name but is not a content hash\n",
+ }
+
+ for i, url := range nonhashtests {
+ var resp *http.Response
+ var respbody []byte
+
+ resp, err = http.Get(url)
+
+ if err != nil {
+ t.Fatalf("Request failed: %v", err)
+ }
+ defer resp.Body.Close()
+ respbody, err = ioutil.ReadAll(resp.Body)
+ if string(respbody) != nonhashresponses[i] {
+ t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
+ }
+ }
+
}