|
|
@ -110,35 +110,35 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han |
|
|
|
var request types.RPCRequest |
|
|
|
err := json.Unmarshal(b, &request) |
|
|
|
if err != nil { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusBadRequest, types.NewRPCResponse("", nil, fmt.Sprintf("Error unmarshalling request: %v", err.Error()))) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCParseError("")) |
|
|
|
return |
|
|
|
} |
|
|
|
if len(r.URL.Path) > 1 { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusNotFound, types.NewRPCResponse(request.ID, nil, fmt.Sprintf("Invalid JSONRPC endpoint %s", r.URL.Path))) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInvalidRequestError(request.ID)) |
|
|
|
return |
|
|
|
} |
|
|
|
rpcFunc := funcMap[request.Method] |
|
|
|
if rpcFunc == nil { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusNotFound, types.NewRPCResponse(request.ID, nil, "RPC method unknown: "+request.Method)) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCMethodNotFoundError(request.ID)) |
|
|
|
return |
|
|
|
} |
|
|
|
if rpcFunc.ws { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusMethodNotAllowed, types.NewRPCResponse(request.ID, nil, "RPC method is only for websockets: "+request.Method)) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInternalError(request.ID)) |
|
|
|
return |
|
|
|
} |
|
|
|
args, err := jsonParamsToArgsRPC(rpcFunc, request.Params) |
|
|
|
if err != nil { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusBadRequest, types.NewRPCResponse(request.ID, nil, fmt.Sprintf("Error converting json params to arguments: %v", err.Error()))) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInvalidParamsError(request.ID)) |
|
|
|
return |
|
|
|
} |
|
|
|
returns := rpcFunc.f.Call(args) |
|
|
|
logger.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns) |
|
|
|
result, err := unreflectResult(returns) |
|
|
|
if err != nil { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusInternalServerError, types.NewRPCResponse(request.ID, result, err.Error())) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInternalError(request.ID)) |
|
|
|
return |
|
|
|
} |
|
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse(request.ID, result, "")) |
|
|
|
WriteRPCResponseHTTP(w, types.NewRPCSuccessResponse(request.ID, result)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -229,7 +229,7 @@ func makeHTTPHandler(rpcFunc *RPCFunc, logger log.Logger) func(http.ResponseWrit |
|
|
|
// Exception for websocket endpoints
|
|
|
|
if rpcFunc.ws { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusMethodNotAllowed, types.NewRPCResponse("", nil, "This RPC method is only for websockets")) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInternalError("")) |
|
|
|
} |
|
|
|
} |
|
|
|
// All other endpoints
|
|
|
@ -237,17 +237,17 @@ func makeHTTPHandler(rpcFunc *RPCFunc, logger log.Logger) func(http.ResponseWrit |
|
|
|
logger.Debug("HTTP HANDLER", "req", r) |
|
|
|
args, err := httpParamsToArgs(rpcFunc, r) |
|
|
|
if err != nil { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusBadRequest, types.NewRPCResponse("", nil, fmt.Sprintf("Error converting http params to args: %v", err.Error()))) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInvalidParamsError("")) |
|
|
|
return |
|
|
|
} |
|
|
|
returns := rpcFunc.f.Call(args) |
|
|
|
logger.Info("HTTPRestRPC", "method", r.URL.Path, "args", args, "returns", returns) |
|
|
|
result, err := unreflectResult(returns) |
|
|
|
if err != nil { |
|
|
|
WriteRPCResponseHTTPError(w, http.StatusInternalServerError, types.NewRPCResponse("", nil, err.Error())) |
|
|
|
WriteRPCResponseHTTP(w, types.RPCInternalError("")) |
|
|
|
return |
|
|
|
} |
|
|
|
WriteRPCResponseHTTP(w, types.NewRPCResponse("", result, "")) |
|
|
|
WriteRPCResponseHTTP(w, types.NewRPCSuccessResponse("", result)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -509,8 +509,7 @@ func (wsc *wsConnection) readRoutine() { |
|
|
|
var request types.RPCRequest |
|
|
|
err = json.Unmarshal(in, &request) |
|
|
|
if err != nil { |
|
|
|
errStr := fmt.Sprintf("Error unmarshaling data: %s", err.Error()) |
|
|
|
wsc.WriteRPCResponse(types.NewRPCResponse(request.ID, nil, errStr)) |
|
|
|
wsc.WriteRPCResponse(types.RPCParseError("")) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
@ -518,7 +517,7 @@ func (wsc *wsConnection) readRoutine() { |
|
|
|
|
|
|
|
rpcFunc := wsc.funcMap[request.Method] |
|
|
|
if rpcFunc == nil { |
|
|
|
wsc.WriteRPCResponse(types.NewRPCResponse(request.ID, nil, "RPC method unknown: "+request.Method)) |
|
|
|
wsc.WriteRPCResponse(types.RPCMethodNotFoundError(request.ID)) |
|
|
|
continue |
|
|
|
} |
|
|
|
var args []reflect.Value |
|
|
@ -529,7 +528,7 @@ func (wsc *wsConnection) readRoutine() { |
|
|
|
args, err = jsonParamsToArgsRPC(rpcFunc, request.Params) |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
wsc.WriteRPCResponse(types.NewRPCResponse(request.ID, nil, err.Error())) |
|
|
|
wsc.WriteRPCResponse(types.RPCInternalError(request.ID)) |
|
|
|
continue |
|
|
|
} |
|
|
|
returns := rpcFunc.f.Call(args) |
|
|
@ -539,10 +538,10 @@ func (wsc *wsConnection) readRoutine() { |
|
|
|
|
|
|
|
result, err := unreflectResult(returns) |
|
|
|
if err != nil { |
|
|
|
wsc.WriteRPCResponse(types.NewRPCResponse(request.ID, nil, err.Error())) |
|
|
|
wsc.WriteRPCResponse(types.RPCInternalError(request.ID)) |
|
|
|
continue |
|
|
|
} else { |
|
|
|
wsc.WriteRPCResponse(types.NewRPCResponse(request.ID, result, "")) |
|
|
|
wsc.WriteRPCResponse(types.NewRPCSuccessResponse(request.ID, result)) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|