Files
tendermint/rpc/jsonrpc/server/http_uri_handler.go
M. J. Fromberger da1b871808 Unify RPC method signatures and parameter decoding (#8397)
Pass all parameters from JSON-RPC requests to their corresponding handlers
using struct types instead of positional parameters. This allows us to control
encoding of arguments using only the standard library, and to eliminate the
remaining special-purpose JSON encoding hooks in the server.

To support existing use, the server still allows arguments to be encoded in
JSON as either an array or an object.

Related changes:

- Rework the RPCFunc constructor to reduce reflection during RPC call service.
- Add request parameter wrappers for each RPC service method.
- Update the RPC Environment methods to use these types.
- Update the interfaces and shims derived from Environment to the new
  signatures.
- Update and extend test cases.
2022-04-27 07:53:51 -07:00

98 lines
2.6 KiB
Go

package server
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/tendermint/tendermint/libs/log"
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)
// uriReqID is a placeholder ID used for GET requests, which do not receive a
// JSON-RPC request ID from the caller.
const uriReqID = -1
// convert from a function name to the http handler
func makeHTTPHandler(rpcFunc *RPCFunc, logger log.Logger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
ctx := rpctypes.WithCallInfo(req.Context(), &rpctypes.CallInfo{
HTTPRequest: req,
})
args, err := parseURLParams(rpcFunc.argNames, req)
if err != nil {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err.Error())
return
}
jreq := rpctypes.NewRequest(uriReqID)
result, err := rpcFunc.Call(ctx, args)
if err == nil {
writeHTTPResponse(w, logger, jreq.MakeResponse(result))
} else {
writeHTTPResponse(w, logger, jreq.MakeError(err))
}
}
}
func parseURLParams(argNames []string, req *http.Request) ([]byte, error) {
if err := req.ParseForm(); err != nil {
return nil, fmt.Errorf("invalid HTTP request: %w", err)
}
getArg := func(name string) (string, bool) {
if req.Form.Has(name) {
return req.Form.Get(name), true
}
return "", false
}
params := make(map[string]interface{})
for _, name := range argNames {
v, ok := getArg(name)
if !ok {
continue
}
if z, err := decodeInteger(v); err == nil {
params[name] = z
} else if b, err := strconv.ParseBool(v); err == nil {
params[name] = b
} else if lc := strings.ToLower(v); strings.HasPrefix(lc, "0x") {
dec, err := hex.DecodeString(lc[2:])
if err != nil {
return nil, fmt.Errorf("invalid hex string: %w", err)
} else if len(dec) == 0 {
return nil, errors.New("invalid empty hex string")
}
params[name] = dec
} else if isQuotedString(v) {
var dec string
if err := json.Unmarshal([]byte(v), &dec); err != nil {
return nil, fmt.Errorf("invalid quoted string: %w", err)
}
params[name] = dec
} else {
params[name] = v
}
}
return json.Marshal(params)
}
// isQuotedString reports whether s is enclosed in double quotes.
func isQuotedString(s string) bool {
return len(s) >= 2 && strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`)
}
// decodeInteger decodes s into an int64. If s is "double quoted" the quotes
// are removed; otherwise s must be a base-10 digit string.
func decodeInteger(s string) (int64, error) {
if isQuotedString(s) {
s = s[1 : len(s)-1]
}
return strconv.ParseInt(s, 10, 64)
}