swarm/api: bzzr get with path

Finally a hopefully clean commit for this PR
Added check for empty path to avoid SIGSEGV in path parser and resolver
Added requested tests for empty path and non-existing manifest. However signature for StartHTTPServer had changed in the last time so now it's hacked as so:

	StartHttpServer(api.API, &Server{Addr: "127.0.0.1:8504", CorsString: ""})
This commit is contained in:
nolash 2017-01-18 23:06:36 +02:00
parent dca6657edb
commit 6dbde1f889
3 changed files with 42 additions and 14 deletions

View file

@ -89,6 +89,9 @@ func (self *Api) Resolve(hostPort string, nameresolver bool) (storage.Key, error
return contentHash[:], err return contentHash[:], err
} }
func Parse(uri string) (hostPort, path string) { func Parse(uri string) (hostPort, path string) {
if uri == "" {
return
}
parts := slashes.Split(uri, 3) parts := slashes.Split(uri, 3)
var i int var i int
if len(parts) == 0 { if len(parts) == 0 {

View file

@ -32,8 +32,8 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/swarm/api" "github.com/ethereum/go-ethereum/swarm/api"
"github.com/rs/cors"
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
"github.com/rs/cors"
) )
const ( const (
@ -195,6 +195,10 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
} }
case r.Method == "GET" || r.Method == "HEAD": case r.Method == "GET" || r.Method == "HEAD":
path = trailingSlashes.ReplaceAllString(path, "") path = trailingSlashes.ReplaceAllString(path, "")
if path == "" {
http.Error(w, "Empty path not allowed", http.StatusBadRequest)
return
}
if raw { if raw {
var reader storage.LazySectionReader var reader storage.LazySectionReader
parsedurl, _ := api.Parse(path) parsedurl, _ := api.Parse(path)

View file

@ -9,15 +9,17 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/logger/glog" //"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/swarm/api" "github.com/ethereum/go-ethereum/swarm/api"
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
) )
func TestBzzrGetPath(t *testing.T) { func init() {
//glog.SetToStderr(true)
//glog.SetV(6)
}
glog.SetToStderr(true) func TestBzzrGetPath(t *testing.T) {
glog.SetV(6)
var err error var err error
@ -33,6 +35,10 @@ func TestBzzrGetPath(t *testing.T) {
testrequests["/"] = 0 testrequests["/"] = 0
testrequests["/a"] = 1 testrequests["/a"] = 1
testrequests["/a/b"] = 2 testrequests["/a/b"] = 2
testrequests["/x"] = 0
testrequests[""] = 0
expectedfailrequests := []string{"", "/x"}
reader := [3]*bytes.Reader{} reader := [3]*bytes.Reader{}
@ -73,10 +79,10 @@ func TestBzzrGetPath(t *testing.T) {
a := api.NewApi(dpa, nil) a := api.NewApi(dpa, nil)
// iterate port numbers up if fail // iterate port numbers up if fail
StartHttpServer(a, "8504") StartHttpServer(a, &Server{Addr: "127.0.0.1:8504", CorsString: ""})
// how to wait for ListenAndServe to have initialized? This is pretty cruuuude // how to wait for ListenAndServe to have initialized? This is pretty cruuuude
// if we fix it we don't need maxproxyattempts anymore either // if we fix it we don't need maxproxyattempts anymore either
time.Sleep(100 * time.Millisecond) time.Sleep(1000 * time.Millisecond)
for i := 0; i <= maxproxyattempts; i++ { for i := 0; i <= maxproxyattempts; i++ {
_, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a") _, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
if i == maxproxyattempts { if i == maxproxyattempts {
@ -90,19 +96,34 @@ func TestBzzrGetPath(t *testing.T) {
} }
for k, v := range testrequests { for k, v := range testrequests {
var body []byte
var resp *http.Response var resp *http.Response
url := "http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain" var respbody []byte
url := "http://127.0.0.1:8504/bzzr:/"
if k[:] != "" {
url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
}
t.Logf("Sending proxy GET: %v", url) t.Logf("Sending proxy GET: %v", url)
resp, err = http.Get(url) resp, err = http.Get(url)
defer resp.Body.Close() defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body) respbody, err = ioutil.ReadAll(resp.Body)
if string(body) != testmanifest[v] { if string(respbody) != testmanifest[v] {
t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(body)) isexpectedfailrequest := false
for _, r := range expectedfailrequests {
if k[:] == r {
isexpectedfailrequest = true
}
}
if isexpectedfailrequest {
t.Logf("Expected fail request failed as expected: %v", string(respbody))
} else {
t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
}
} else {
t.Logf("Response body OK: %v", string(respbody))
} }
t.Log(string(body))
} }
} }