mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
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:
parent
dca6657edb
commit
6dbde1f889
3 changed files with 42 additions and 14 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue