diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-18 05:46:22 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-18 05:46:22 +0800 |
commit | c6f84325b1c21c33ca6470da9020c327d207f80a (patch) | |
tree | d5490cb7e590d371ff5cc324c72fbf5180b361dd /rpc/http.go | |
parent | 917050dc30d8717d7e0bba1257165c1aec44887f (diff) | |
parent | 8fd243ee2303946dd520dd87513bbe0ec4184301 (diff) | |
download | go-tangerine-c6f84325b1c21c33ca6470da9020c327d207f80a.tar.gz go-tangerine-c6f84325b1c21c33ca6470da9020c327d207f80a.tar.zst go-tangerine-c6f84325b1c21c33ca6470da9020c327d207f80a.zip |
Support JSON RPC batch requests
Diffstat (limited to 'rpc/http.go')
-rw-r--r-- | rpc/http.go | 85 |
1 files changed, 52 insertions, 33 deletions
diff --git a/rpc/http.go b/rpc/http.go index b6edb7cd7..0fe634dee 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -1,6 +1,8 @@ package rpc import ( + "encoding/json" + "io/ioutil" "net/http" "github.com/ethereum/go-ethereum/logger" @@ -16,54 +18,71 @@ const ( // JSONRPC returns a handler that implements the Ethereum JSON-RPC API. func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { - var json JsonWrapper + var jsw JsonWrapper api := NewEthereumApi(pipe, dataDir) return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + // TODO this needs to be configurable w.Header().Set("Access-Control-Allow-Origin", "*") - rpchttplogger.DebugDetailln("Handling request") - + // Limit request size to resist DoS if req.ContentLength > maxSizeReqLength { jsonerr := &RpcErrorObject{-32700, "Request too large"} - json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) + jsw.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) return } - reqParsed, reqerr := json.ParseRequestBody(req) - switch reqerr.(type) { - case nil: - break - case *DecodeParamError, *InsufficientParamsError, *ValidationError: - jsonerr := &RpcErrorObject{-32602, reqerr.Error()} - json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) - return - default: - jsonerr := &RpcErrorObject{-32700, "Could not parse request"} - json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) - return + defer req.Body.Close() + body, err := ioutil.ReadAll(req.Body) + if err != nil { + jsonerr := &RpcErrorObject{-32700, "Could not read request body"} + jsw.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) } - var response interface{} - reserr := api.GetRequestReply(&reqParsed, &response) - switch reserr.(type) { - case nil: - break - case *NotImplementedError: - jsonerr := &RpcErrorObject{-32601, reserr.Error()} - json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) + // Try to parse the request as a single + var reqSingle RpcRequest + if err := json.Unmarshal(body, &reqSingle); err == nil { + response := RpcResponse(api, &reqSingle) + jsw.Send(w, &response) return - case *DecodeParamError, *InsufficientParamsError, *ValidationError: - jsonerr := &RpcErrorObject{-32602, reserr.Error()} - json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) - return - default: - jsonerr := &RpcErrorObject{-32603, reserr.Error()} - json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) + } + + // Try to parse the request to batch + var reqBatch []RpcRequest + if err := json.Unmarshal(body, &reqBatch); err == nil { + // Build response batch + resBatch := make([]*interface{}, len(reqBatch)) + for i, request := range reqBatch { + response := RpcResponse(api, &request) + resBatch[i] = response + } + jsw.Send(w, resBatch) return } - rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) - json.Send(w, &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Result: response}) + // Not a batch or single request, error + jsonerr := &RpcErrorObject{-32600, "Could not decode request"} + jsw.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) }) } + +func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} { + var reply, response interface{} + reserr := api.GetRequestReply(request, &reply) + switch reserr.(type) { + case nil: + response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply} + case *NotImplementedError: + jsonerr := &RpcErrorObject{-32601, reserr.Error()} + response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr} + case *DecodeParamError, *InsufficientParamsError, *ValidationError: + jsonerr := &RpcErrorObject{-32602, reserr.Error()} + response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr} + default: + jsonerr := &RpcErrorObject{-32603, reserr.Error()} + response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr} + } + + rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) + return &response +} |