|
@ -14,8 +14,8 @@ import ( |
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket" |
|
|
"github.com/gorilla/websocket" |
|
|
"github.com/pkg/errors" |
|
|
"github.com/pkg/errors" |
|
|
types "github.com/tendermint/tendermint/rpc/types" |
|
|
|
|
|
wire "github.com/tendermint/go-wire" |
|
|
wire "github.com/tendermint/go-wire" |
|
|
|
|
|
types "github.com/tendermint/tendermint/rpc/types" |
|
|
cmn "github.com/tendermint/tmlibs/common" |
|
|
cmn "github.com/tendermint/tmlibs/common" |
|
|
events "github.com/tendermint/tmlibs/events" |
|
|
events "github.com/tendermint/tmlibs/events" |
|
|
) |
|
|
) |
|
@ -108,32 +108,32 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc) http.HandlerFunc { |
|
|
var request types.RPCRequest |
|
|
var request types.RPCRequest |
|
|
err := json.Unmarshal(b, &request) |
|
|
err := json.Unmarshal(b, &request) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", nil, fmt.Sprintf("Error unmarshalling request: %v", err.Error()))) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusBadRequest, types.NewRPCResponse("", nil, fmt.Sprintf("Error unmarshalling request: %v", err.Error()))) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
if len(r.URL.Path) > 1 { |
|
|
if len(r.URL.Path) > 1 { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, nil, fmt.Sprintf("Invalid JSONRPC endpoint %s", r.URL.Path))) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusNotFound, types.NewRPCResponse(request.ID, nil, fmt.Sprintf("Invalid JSONRPC endpoint %s", r.URL.Path))) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
rpcFunc := funcMap[request.Method] |
|
|
rpcFunc := funcMap[request.Method] |
|
|
if rpcFunc == nil { |
|
|
if rpcFunc == nil { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, nil, "RPC method unknown: "+request.Method)) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusNotFound, types.NewRPCResponse(request.ID, nil, "RPC method unknown: "+request.Method)) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
if rpcFunc.ws { |
|
|
if rpcFunc.ws { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, nil, "RPC method is only for websockets: "+request.Method)) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusMethodNotAllowed, types.NewRPCResponse(request.ID, nil, "RPC method is only for websockets: "+request.Method)) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
args, err := jsonParamsToArgsRPC(rpcFunc, request.Params) |
|
|
args, err := jsonParamsToArgsRPC(rpcFunc, request.Params) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, nil, fmt.Sprintf("Error converting json params to arguments: %v", err.Error()))) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusBadRequest, types.NewRPCResponse(request.ID, nil, fmt.Sprintf("Error converting json params to arguments: %v", err.Error()))) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
returns := rpcFunc.f.Call(args) |
|
|
returns := rpcFunc.f.Call(args) |
|
|
log.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns) |
|
|
log.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns) |
|
|
result, err := unreflectResult(returns) |
|
|
result, err := unreflectResult(returns) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, result, err.Error())) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusInternalServerError, types.NewRPCResponse(request.ID, result, err.Error())) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, result, "")) |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, result, "")) |
|
@ -221,7 +221,7 @@ func makeHTTPHandler(rpcFunc *RPCFunc) func(http.ResponseWriter, *http.Request) |
|
|
// Exception for websocket endpoints
|
|
|
// Exception for websocket endpoints
|
|
|
if rpcFunc.ws { |
|
|
if rpcFunc.ws { |
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", nil, "This RPC method is only for websockets")) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusMethodNotAllowed, types.NewRPCResponse("", nil, "This RPC method is only for websockets")) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// All other endpoints
|
|
|
// All other endpoints
|
|
@ -229,14 +229,14 @@ func makeHTTPHandler(rpcFunc *RPCFunc) func(http.ResponseWriter, *http.Request) |
|
|
log.Debug("HTTP HANDLER", "req", r) |
|
|
log.Debug("HTTP HANDLER", "req", r) |
|
|
args, err := httpParamsToArgs(rpcFunc, r) |
|
|
args, err := httpParamsToArgs(rpcFunc, r) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", nil, fmt.Sprintf("Error converting http params to args: %v", err.Error()))) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusBadRequest, types.NewRPCResponse("", nil, fmt.Sprintf("Error converting http params to args: %v", err.Error()))) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
returns := rpcFunc.f.Call(args) |
|
|
returns := rpcFunc.f.Call(args) |
|
|
log.Info("HTTPRestRPC", "method", r.URL.Path, "args", args, "returns", returns) |
|
|
log.Info("HTTPRestRPC", "method", r.URL.Path, "args", args, "returns", returns) |
|
|
result, err := unreflectResult(returns) |
|
|
result, err := unreflectResult(returns) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", nil, err.Error())) |
|
|
|
|
|
|
|
|
WriteRPCResponseHTTPError(w, http.StatusInternalServerError, types.NewRPCResponse("", nil, err.Error())) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", result, "")) |
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", result, "")) |
|
|