p2p/simulations: support GET requests with query string in URL

This commit is contained in:
Fabio Barone 2017-04-24 18:19:02 -05:00 committed by Lewis Marshall
parent 20e91384bf
commit 9fc8097768
2 changed files with 19 additions and 4 deletions

View file

@ -2,6 +2,8 @@ package simulations
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
@ -72,7 +74,13 @@ func handle(w http.ResponseWriter, r *http.Request, c Controller) {
return
}
// on return we close the request Body so we assume it is read synchronously
response, err := handler(r.Body)
var params io.ReadCloser
if r.Method == "GET" && len(r.URL.RawQuery) > 0 {
params = ioutil.NopCloser(strings.NewReader(r.URL.RawQuery))
} else {
params = r.Body
}
response, err := handler(params)
if err != nil {
http.Error(w, fmt.Sprintf("handler error: %v", err), http.StatusBadRequest)
return

View file

@ -166,9 +166,16 @@ func nethook(conf *simulations.NetworkConfig) (simulations.NetworkControl, *simu
&simulations.ResourceHandlers{
Retrieve: &simulations.ResourceHandler{
Handle: func(msg interface{}, parent *simulations.ResourceController) (interface{}, error) {
id := msg.(string)
ids := msg.([]string)
var results []string
for _, id := range ids {
if len(id) != 128 {
return nil, fmt.Errorf("Nodes controller expects 128 bytes size hex id")
}
pp := net.GetNode(adapters.NewNodeIdFromHex(id)).Adapter().(*SimNode).hive
return pp.String(), nil
results = append(results, pp.String())
}
return results, nil
},
Type: reflect.TypeOf([]string{}), // this is input not output param structure
},