From 6dbde1f8897319fd84b94b95e55b07be5d776d0c Mon Sep 17 00:00:00 2001 From: nolash Date: Wed, 18 Jan 2017 23:06:36 +0200 Subject: [PATCH] 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: ""}) --- swarm/api/api.go | 3 +++ swarm/api/http/server.go | 6 ++++- swarm/api/http/server_test.go | 47 +++++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/swarm/api/api.go b/swarm/api/api.go index 10f2597f39..f92a14653d 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -89,6 +89,9 @@ func (self *Api) Resolve(hostPort string, nameresolver bool) (storage.Key, error return contentHash[:], err } func Parse(uri string) (hostPort, path string) { + if uri == "" { + return + } parts := slashes.Split(uri, 3) var i int if len(parts) == 0 { diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index 85505776d0..afd867efc8 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -32,8 +32,8 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/swarm/api" - "github.com/rs/cors" "github.com/ethereum/go-ethereum/swarm/storage" + "github.com/rs/cors" ) const ( @@ -195,6 +195,10 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) { } case r.Method == "GET" || r.Method == "HEAD": path = trailingSlashes.ReplaceAllString(path, "") + if path == "" { + http.Error(w, "Empty path not allowed", http.StatusBadRequest) + return + } if raw { var reader storage.LazySectionReader parsedurl, _ := api.Parse(path) diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go index 2d088dfd5c..90791c7089 100644 --- a/swarm/api/http/server_test.go +++ b/swarm/api/http/server_test.go @@ -9,15 +9,17 @@ import ( "time" "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/storage" ) -func TestBzzrGetPath(t *testing.T) { +func init() { + //glog.SetToStderr(true) + //glog.SetV(6) +} - glog.SetToStderr(true) - glog.SetV(6) +func TestBzzrGetPath(t *testing.T) { var err error @@ -33,6 +35,10 @@ func TestBzzrGetPath(t *testing.T) { testrequests["/"] = 0 testrequests["/a"] = 1 testrequests["/a/b"] = 2 + testrequests["/x"] = 0 + testrequests[""] = 0 + + expectedfailrequests := []string{"", "/x"} reader := [3]*bytes.Reader{} @@ -73,10 +79,10 @@ func TestBzzrGetPath(t *testing.T) { a := api.NewApi(dpa, nil) // 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 // 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++ { _, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a") if i == maxproxyattempts { @@ -90,19 +96,34 @@ func TestBzzrGetPath(t *testing.T) { } for k, v := range testrequests { - var body []byte 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) resp, err = http.Get(url) defer resp.Body.Close() - body, err = ioutil.ReadAll(resp.Body) + respbody, err = ioutil.ReadAll(resp.Body) - if string(body) != testmanifest[v] { - t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(body)) + if string(respbody) != testmanifest[v] { + 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)) } }