2015-03-10 00:00:27 +02:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2015-03-17 23:38:05 +02:00
|
|
|
"encoding/json"
|
2015-03-18 02:14:19 +02:00
|
|
|
"io"
|
2015-03-17 23:38:05 +02:00
|
|
|
"io/ioutil"
|
2015-03-10 00:00:27 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/xeth"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rpchttplogger = logger.NewLogger("RPC-HTTP")
|
|
|
|
|
2015-03-10 00:25:46 +02:00
|
|
|
const (
|
|
|
|
jsonrpcver = "2.0"
|
|
|
|
maxSizeReqLength = 1024 * 1024 // 1MB
|
|
|
|
)
|
|
|
|
|
2015-03-10 00:00:27 +02:00
|
|
|
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
|
|
|
|
func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
|
|
|
|
api := NewEthereumApi(pipe, dataDir)
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2015-03-17 23:38:05 +02:00
|
|
|
// TODO this needs to be configurable
|
2015-03-10 00:00:27 +02:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
2015-03-17 23:38:05 +02:00
|
|
|
// Limit request size to resist DoS
|
2015-03-10 00:25:46 +02:00
|
|
|
if req.ContentLength > maxSizeReqLength {
|
2015-03-13 02:07:03 +02:00
|
|
|
jsonerr := &RpcErrorObject{-32700, "Request too large"}
|
2015-03-18 02:14:19 +02:00
|
|
|
Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
|
2015-03-10 00:25:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-18 02:14:19 +02:00
|
|
|
// Read request body
|
2015-03-17 23:38:05 +02:00
|
|
|
defer req.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
jsonerr := &RpcErrorObject{-32700, "Could not read request body"}
|
2015-03-18 02:14:19 +02:00
|
|
|
Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
|
2015-03-10 00:00:27 +02:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:38:05 +02:00
|
|
|
// Try to parse the request as a single
|
|
|
|
var reqSingle RpcRequest
|
|
|
|
if err := json.Unmarshal(body, &reqSingle); err == nil {
|
|
|
|
response := RpcResponse(api, &reqSingle)
|
2015-03-18 02:14:19 +02:00
|
|
|
Send(w, &response)
|
2015-03-13 02:07:03 +02:00
|
|
|
return
|
2015-03-17 23:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2015-03-18 02:14:19 +02:00
|
|
|
Send(w, resBatch)
|
2015-03-10 00:00:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-17 23:38:05 +02:00
|
|
|
// Not a batch or single request, error
|
|
|
|
jsonerr := &RpcErrorObject{-32600, "Could not decode request"}
|
2015-03-18 02:14:19 +02:00
|
|
|
Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
|
2015-03-10 00:00:27 +02:00
|
|
|
})
|
|
|
|
}
|
2015-03-17 23:38:05 +02:00
|
|
|
|
|
|
|
func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} {
|
|
|
|
var reply, response interface{}
|
|
|
|
reserr := api.GetRequestReply(request, &reply)
|
|
|
|
switch reserr.(type) {
|
|
|
|
case nil:
|
2015-03-17 23:46:22 +02:00
|
|
|
response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply}
|
2015-03-17 23:38:05 +02:00
|
|
|
case *NotImplementedError:
|
|
|
|
jsonerr := &RpcErrorObject{-32601, reserr.Error()}
|
2015-03-17 23:46:22 +02:00
|
|
|
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
|
2015-03-17 23:38:05 +02:00
|
|
|
case *DecodeParamError, *InsufficientParamsError, *ValidationError:
|
|
|
|
jsonerr := &RpcErrorObject{-32602, reserr.Error()}
|
2015-03-17 23:46:22 +02:00
|
|
|
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
|
2015-03-17 23:38:05 +02:00
|
|
|
default:
|
|
|
|
jsonerr := &RpcErrorObject{-32603, reserr.Error()}
|
2015-03-17 23:46:22 +02:00
|
|
|
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
|
2015-03-17 23:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
|
|
|
|
return &response
|
|
|
|
}
|
2015-03-18 02:14:19 +02:00
|
|
|
|
|
|
|
func Send(writer io.Writer, v interface{}) (n int, err error) {
|
|
|
|
var payload []byte
|
|
|
|
payload, err = json.MarshalIndent(v, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
rpclogger.Fatalln("Error marshalling JSON", err)
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
rpclogger.DebugDetailf("Sending payload: %s", payload)
|
|
|
|
|
|
|
|
return writer.Write(payload)
|
|
|
|
}
|