tendermint/rpc/lib/client/http_client.go

236 lines
6.1 KiB
Go
Raw Permalink Normal View History

2016-01-12 13:50:06 -08:00
package rpcclient
import (
"bytes"
2016-01-13 15:37:35 -08:00
"encoding/json"
"fmt"
2016-01-12 13:50:06 -08:00
"io/ioutil"
2016-02-18 13:07:49 -08:00
"net"
2016-01-12 13:50:06 -08:00
"net/http"
"net/url"
"reflect"
"strings"
2016-01-12 13:50:06 -08:00
2017-03-09 07:00:05 -08:00
"github.com/pkg/errors"
2018-04-05 15:45:11 -07:00
"github.com/tendermint/go-amino"
types "github.com/tendermint/tendermint/rpc/lib/types"
2016-01-12 13:50:06 -08:00
)
const (
protoHTTP = "http"
protoHTTPS = "https"
protoWSS = "wss"
protoWS = "ws"
protoTCP = "tcp"
)
// HTTPClient is a common interface for JSONRPCClient and URIClient.
type HTTPClient interface {
Call(method string, params map[string]interface{}, result interface{}) (interface{}, error)
2018-04-05 15:45:11 -07:00
Codec() *amino.Codec
2018-04-05 21:19:14 -07:00
SetCodec(*amino.Codec)
}
// TODO: Deprecate support for IP:PORT or /path/to/socket
func makeHTTPDialer(remoteAddr string) (string, string, func(string, string) (net.Conn, error)) {
// protocol to use for http operations, to support both http and https
clientProtocol := protoHTTP
parts := strings.SplitN(remoteAddr, "://", 2)
var protocol, address string
if len(parts) == 1 {
// default to tcp if nothing specified
protocol, address = protoTCP, remoteAddr
} else if len(parts) == 2 {
protocol, address = parts[0], parts[1]
} else {
// return a invalid message
msg := fmt.Sprintf("Invalid addr: %s", remoteAddr)
return clientProtocol, msg, func(_ string, _ string) (net.Conn, error) {
return nil, errors.New(msg)
}
}
// accept http as an alias for tcp and set the client protocol
switch protocol {
case protoHTTP, protoHTTPS:
clientProtocol = protocol
protocol = protoTCP
case protoWS, protoWSS:
clientProtocol = protocol
}
2016-02-18 13:07:49 -08:00
// replace / with . for http requests (kvstore domain)
2017-10-17 00:59:05 -07:00
trimmedAddress := strings.Replace(address, "/", ".", -1)
return clientProtocol, trimmedAddress, func(proto, addr string) (net.Conn, error) {
return net.Dial(protocol, address)
2016-02-18 13:07:49 -08:00
}
2016-01-12 13:50:06 -08:00
}
// We overwrite the http.Client.Dial so we can do http over tcp or unix.
// remoteAddr should be fully featured (eg. with tcp:// or unix://)
func makeHTTPClient(remoteAddr string) (string, *http.Client) {
protocol, address, dialer := makeHTTPDialer(remoteAddr)
return protocol + "://" + address, &http.Client{
Transport: &http.Transport{
Dial: dialer,
},
2016-02-18 13:07:49 -08:00
}
2016-01-12 13:50:06 -08:00
}
2016-02-18 13:07:49 -08:00
//------------------------------------------------------------------------------------
// JSONRPCClient takes params as a slice
type JSONRPCClient struct {
address string
client *http.Client
2018-04-05 15:45:11 -07:00
cdc *amino.Codec
2016-01-12 13:50:06 -08:00
}
2017-10-17 00:59:05 -07:00
// NewJSONRPCClient returns a JSONRPCClient pointed at the given address.
func NewJSONRPCClient(remote string) *JSONRPCClient {
address, client := makeHTTPClient(remote)
return &JSONRPCClient{
address: address,
client: client,
2018-04-05 15:45:11 -07:00
cdc: amino.NewCodec(),
2016-01-12 13:50:06 -08:00
}
}
func (c *JSONRPCClient) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
2018-04-05 15:45:11 -07:00
request, err := types.MapToRequest(c.cdc, "jsonrpc-client", method, params)
if err != nil {
return nil, err
2016-01-12 13:50:06 -08:00
}
requestBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
2017-03-09 12:00:25 -08:00
// log.Info(string(requestBytes))
2016-01-12 13:50:06 -08:00
requestBuf := bytes.NewBuffer(requestBytes)
2016-05-04 07:39:43 -07:00
// log.Info(Fmt("RPC request to %v (%v): %v", c.remote, method, string(requestBytes)))
httpResponse, err := c.client.Post(c.address, "text/json", requestBuf)
2016-01-12 13:50:06 -08:00
if err != nil {
return nil, err
}
2017-10-03 15:49:20 -07:00
defer httpResponse.Body.Close() // nolint: errcheck
2017-09-21 06:55:06 -07:00
2016-01-12 13:50:06 -08:00
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
2016-02-18 13:07:49 -08:00
// log.Info(Fmt("RPC response: %v", string(responseBytes)))
2018-04-05 15:45:11 -07:00
return unmarshalResponseBytes(c.cdc, responseBytes, result)
}
func (c *JSONRPCClient) Codec() *amino.Codec {
return c.cdc
2016-01-12 13:50:06 -08:00
}
2018-04-05 21:19:14 -07:00
func (c *JSONRPCClient) SetCodec(cdc *amino.Codec) {
c.cdc = cdc
}
2016-02-18 13:07:49 -08:00
//-------------------------------------------------------------
// URI takes params as a map
type URIClient struct {
address string
client *http.Client
2018-04-05 15:45:11 -07:00
cdc *amino.Codec
2016-02-18 13:07:49 -08:00
}
func NewURIClient(remote string) *URIClient {
address, client := makeHTTPClient(remote)
return &URIClient{
address: address,
client: client,
2018-04-05 15:45:11 -07:00
cdc: amino.NewCodec(),
2016-02-18 13:07:49 -08:00
}
}
func (c *URIClient) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
2018-04-05 15:45:11 -07:00
values, err := argsToURLValues(c.cdc, params)
2016-01-12 13:50:06 -08:00
if err != nil {
return nil, err
}
// log.Info(Fmt("URI request to %v (%v): %v", c.address, method, values))
resp, err := c.client.PostForm(c.address+"/"+method, values)
2016-01-12 13:50:06 -08:00
if err != nil {
return nil, err
}
2017-10-03 15:49:20 -07:00
defer resp.Body.Close() // nolint: errcheck
2017-09-21 06:55:06 -07:00
2016-01-12 13:50:06 -08:00
responseBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
2018-04-05 15:45:11 -07:00
return unmarshalResponseBytes(c.cdc, responseBytes, result)
}
func (c *URIClient) Codec() *amino.Codec {
return c.cdc
2016-01-12 13:50:06 -08:00
}
2018-04-05 21:19:14 -07:00
func (c *URIClient) SetCodec(cdc *amino.Codec) {
c.cdc = cdc
}
2016-01-12 13:50:06 -08:00
//------------------------------------------------
2018-04-05 15:45:11 -07:00
func unmarshalResponseBytes(cdc *amino.Codec, responseBytes []byte, result interface{}) (interface{}, error) {
// Read response. If rpc/core/types is imported, the result will unmarshal
// into the correct type.
// log.Notice("response", "response", string(responseBytes))
2016-01-12 13:50:06 -08:00
var err error
response := &types.RPCResponse{}
2016-01-13 15:37:35 -08:00
err = json.Unmarshal(responseBytes, response)
2016-01-12 13:50:06 -08:00
if err != nil {
2017-03-09 07:00:05 -08:00
return nil, errors.Errorf("Error unmarshalling rpc response: %v", err)
2016-01-12 13:50:06 -08:00
}
if response.Error != nil {
return nil, errors.Errorf("Response error: %v", response.Error)
2016-01-12 13:50:06 -08:00
}
2018-04-05 15:45:11 -07:00
// Unmarshal the RawMessage into the result.
err = cdc.UnmarshalJSON(response.Result, result)
if err != nil {
2017-03-09 07:00:05 -08:00
return nil, errors.Errorf("Error unmarshalling rpc response result: %v", err)
}
return result, nil
2016-01-12 13:50:06 -08:00
}
2018-04-05 15:45:11 -07:00
func argsToURLValues(cdc *amino.Codec, args map[string]interface{}) (url.Values, error) {
2016-01-12 13:50:06 -08:00
values := make(url.Values)
if len(args) == 0 {
return values, nil
}
2018-04-05 15:45:11 -07:00
err := argsToJSON(cdc, args)
2016-01-12 13:50:06 -08:00
if err != nil {
return nil, err
}
for key, val := range args {
values.Set(key, val.(string))
}
return values, nil
}
2018-04-05 15:45:11 -07:00
func argsToJSON(cdc *amino.Codec, args map[string]interface{}) error {
2016-01-12 13:50:06 -08:00
for k, v := range args {
rt := reflect.TypeOf(v)
isByteSlice := rt.Kind() == reflect.Slice && rt.Elem().Kind() == reflect.Uint8
if isByteSlice {
bytes := reflect.ValueOf(v).Bytes()
args[k] = fmt.Sprintf("0x%X", bytes)
continue
}
2018-04-05 15:45:11 -07:00
data, err := cdc.MarshalJSON(v)
2016-01-12 13:50:06 -08:00
if err != nil {
return err
}
args[k] = string(data)
2016-01-12 13:50:06 -08:00
}
return nil
}