diff options
Diffstat (limited to 'rpc/server.go')
-rw-r--r-- | rpc/server.go | 61 |
1 files changed, 49 insertions, 12 deletions
diff --git a/rpc/server.go b/rpc/server.go index 983dc6c33..a4cf1aa7c 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -1,10 +1,25 @@ +/* + This file is part of go-ethereum + + go-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + go-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +*/ package rpc import ( "fmt" "net" - "net/rpc" - "net/rpc/jsonrpc" + "net/http" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/xeth" @@ -38,17 +53,14 @@ func (s *JsonRpcServer) Stop() { func (s *JsonRpcServer) Start() { jsonlogger.Infoln("Starting JSON-RPC server") go s.exitHandler() - rpc.Register(&EthereumApi{pipe: s.pipe}) - rpc.HandleHTTP() - for { - conn, err := s.listener.Accept() - if err != nil { - jsonlogger.Infoln("Error starting JSON-RPC:", err) - break - } - jsonlogger.Debugln("Incoming request.") - go jsonrpc.ServeConn(conn) + h := apiHandler(&EthereumApi{pipe: s.pipe}) + http.Handle("/", h) + + err := http.Serve(s.listener, nil) + // FIX Complains on shutdown due to listner already being closed + if err != nil { + jsonlogger.Errorln("Error on JSON-RPC interface:", err) } } @@ -65,3 +77,28 @@ func NewJsonRpcServer(pipe *xeth.JSXEth, port int) (*JsonRpcServer, error) { pipe: pipe, }, nil } + +func apiHandler(xeth *EthereumApi) http.Handler { + fn := func(w http.ResponseWriter, req *http.Request) { + jsonlogger.Debugln("Handling request") + + reqParsed, reqerr := JSON.ParseRequestBody(req) + if reqerr != nil { + JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: ErrorParseRequest}) + return + } + + var response interface{} + reserr := xeth.GetRequestReply(&reqParsed, &response) + if reserr != nil { + jsonlogger.Errorln(reserr) + JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + return + } + + jsonlogger.Debugf("Generated response: %T %s", response, response) + JSON.Send(w, &RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) + } + + return http.HandlerFunc(fn) +} |