2015-03-10 00:00:27 +02:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 {
|
|
|
|
var json JsonWrapper
|
|
|
|
api := NewEthereumApi(pipe, dataDir)
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
rpchttplogger.DebugDetailln("Handling request")
|
|
|
|
|
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-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
|
2015-03-10 00:25:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:00:27 +02:00
|
|
|
reqParsed, reqerr := json.ParseRequestBody(req)
|
2015-03-13 02:07:03 +02:00
|
|
|
switch reqerr.(type) {
|
|
|
|
case nil:
|
|
|
|
break
|
2015-03-13 16:03:48 +02:00
|
|
|
case *DecodeParamError, *InsufficientParamsError, *ValidationError:
|
2015-03-13 02:07:03 +02:00
|
|
|
jsonerr := &RpcErrorObject{-32602, reqerr.Error()}
|
2015-03-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
|
2015-03-13 02:07:03 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
jsonerr := &RpcErrorObject{-32700, "Could not parse request"}
|
2015-03-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
|
2015-03-10 00:00:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var response interface{}
|
|
|
|
reserr := api.GetRequestReply(&reqParsed, &response)
|
2015-03-13 02:07:03 +02:00
|
|
|
switch reserr.(type) {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
case *NotImplementedError:
|
|
|
|
jsonerr := &RpcErrorObject{-32601, reserr.Error()}
|
2015-03-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr})
|
2015-03-13 02:07:03 +02:00
|
|
|
return
|
2015-03-13 16:03:48 +02:00
|
|
|
case *DecodeParamError, *InsufficientParamsError, *ValidationError:
|
2015-03-13 02:07:03 +02:00
|
|
|
jsonerr := &RpcErrorObject{-32602, reserr.Error()}
|
2015-03-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr})
|
2015-03-13 02:07:03 +02:00
|
|
|
return
|
|
|
|
default:
|
2015-03-10 00:00:27 +02:00
|
|
|
jsonerr := &RpcErrorObject{-32603, reserr.Error()}
|
2015-03-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr})
|
2015-03-10 00:00:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
|
2015-03-15 08:21:54 +02:00
|
|
|
json.Send(w, &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Result: response})
|
2015-03-10 00:00:27 +02:00
|
|
|
})
|
|
|
|
}
|