From a88f609b8ab1e20eb2564d47605985d6b8f593e8 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 28 Jan 2015 10:22:37 -0600 Subject: [PATCH 1/2] Use custom Send methods --- rpc/ws/server.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/rpc/ws/server.go b/rpc/ws/server.go index 248ebc585d..e2b9184304 100644 --- a/rpc/ws/server.go +++ b/rpc/ws/server.go @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with go-ethereum. If not, see . */ -package ws +package rpcws import ( "fmt" @@ -30,6 +30,7 @@ import ( ) var wslogger = logger.NewLogger("RPC-WS") +var JSON rpc.JsonWrapper type WebSocketServer struct { eth *eth.Ethereum @@ -98,25 +99,28 @@ func (s *WebSocketServer) apiHandler(api *rpc.EthereumApi) http.Handler { func sockHandler(api *rpc.EthereumApi) websocket.Handler { fn := func(conn *websocket.Conn) { for { - wslogger.Debugln("Handling request") + wslogger.Debugln("Handling connection") var reqParsed rpc.RpcRequest + // reqParsed, reqerr := JSON.ParseRequestBody(conn.Request()) if err := websocket.JSON.Receive(conn, &reqParsed); err != nil { - wslogger.Debugln(rpc.ErrorParseRequest) - websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) + // if reqerr != nil { + JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) + // websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) continue } var response interface{} reserr := api.GetRequestReply(&reqParsed, &response) if reserr != nil { - wslogger.Errorln(reserr) - websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + // websocket.JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) continue } wslogger.Debugf("Generated response: %T %s", response, response) - websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) + JSON.Send(conn, &rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) + // websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) } } return websocket.Handler(fn) From d613bf69bf3fd0cfbe28a2f68c87421f7d5bccf9 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 11 Feb 2015 11:56:29 +0100 Subject: [PATCH 2/2] #295 Allow RPC ID to be string --- rpc/http/server.go | 2 +- rpc/message.go | 6 +++--- rpc/util.go | 2 +- rpc/ws/server.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rpc/http/server.go b/rpc/http/server.go index a34400a77c..10c8fa8138 100644 --- a/rpc/http/server.go +++ b/rpc/http/server.go @@ -102,7 +102,7 @@ func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler { if reserr != nil { rpchttplogger.Warnln(reserr) jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()} - JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: &reqParsed.ID, Error: jsonerr}) + JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) return } diff --git a/rpc/message.go b/rpc/message.go index 78dc6e2ffa..7983e003d2 100644 --- a/rpc/message.go +++ b/rpc/message.go @@ -34,20 +34,20 @@ const ( ) type RpcRequest struct { + ID interface{} `json:"id"` JsonRpc string `json:"jsonrpc"` - ID int `json:"id"` Method string `json:"method"` Params []json.RawMessage `json:"params"` } type RpcSuccessResponse struct { - ID int `json:"id"` + ID interface{} `json:"id"` JsonRpc string `json:"jsonrpc"` Result interface{} `json:"result"` } type RpcErrorResponse struct { - ID *int `json:"id"` + ID interface{} `json:"id"` JsonRpc string `json:"jsonrpc"` Error *RpcErrorObject `json:"error"` } diff --git a/rpc/util.go b/rpc/util.go index 509d9a17d3..679d83754d 100644 --- a/rpc/util.go +++ b/rpc/util.go @@ -47,7 +47,6 @@ func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) // Convert JSON to native types d := json.NewDecoder(req.Body) - // d.UseNumber() defer req.Body.Close() err := d.Decode(&reqParsed) @@ -55,6 +54,7 @@ func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) rpclogger.Errorln("Error decoding JSON: ", err) return reqParsed, err } + rpclogger.DebugDetailf("Parsed request: %s", reqParsed) return reqParsed, nil diff --git a/rpc/ws/server.go b/rpc/ws/server.go index fe6af042f6..100713c100 100644 --- a/rpc/ws/server.go +++ b/rpc/ws/server.go @@ -109,7 +109,7 @@ func sockHandler(api *rpc.EthereumApi) websocket.Handler { if reserr != nil { wslogger.Warnln(reserr) jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()} - JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: &reqParsed.ID, Error: jsonerr}) + JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) continue }