rpc: be less restrictive on the request id

This commit is contained in:
Bas van Kervel 2016-03-01 11:58:34 +01:00
parent c3a4874e5e
commit f756020b0f
7 changed files with 73 additions and 45 deletions

View file

@ -37,7 +37,7 @@ func NewJeth(re *jsre.JSRE, client rpc.Client) *Jeth {
return &Jeth{re, client} return &Jeth{re, client}
} }
func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id *int64) (response otto.Value) { func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) {
m := rpc.JSONErrResponse{ m := rpc.JSONErrResponse{
Version: "2.0", Version: "2.0",
Id: id, Id: id,

View file

@ -24,6 +24,9 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"errors"
"strconv"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
) )
@ -40,14 +43,14 @@ const (
type JSONRequest struct { type JSONRequest struct {
Method string `json:"method"` Method string `json:"method"`
Version string `json:"jsonrpc"` Version string `json:"jsonrpc"`
Id *int64 `json:"id,omitempty"` Id json.RawMessage `json:"id"`
Payload json.RawMessage `json:"params,omitempty"` Payload json.RawMessage `json:"params,omitempty"`
} }
// JSON-RPC response // JSON-RPC response
type JSONSuccessResponse struct { type JSONSuccessResponse struct {
Version string `json:"jsonrpc"` Version string `json:"jsonrpc"`
Id int64 `json:"id"` Id interface{} `json:"id,omitempty"`
Result interface{} `json:"result"` Result interface{} `json:"result"`
} }
@ -60,9 +63,9 @@ type JSONError struct {
// JSON-RPC error response // JSON-RPC error response
type JSONErrResponse struct { type JSONErrResponse struct {
Version string `json:"jsonrpc"` Version string `json:"jsonrpc"`
Id *int64 `json:"id,omitempty"` Id interface{} `json:"id,omitempty"`
Error JSONError `json:"error"` Error JSONError `json:"error"`
} }
// JSON-RPC notification payload // JSON-RPC notification payload
@ -123,21 +126,46 @@ func (c *jsonCodec) ReadRequestHeaders() ([]rpcRequest, bool, RPCError) {
return parseRequest(incomingMsg) return parseRequest(incomingMsg)
} }
// checkId returns an error when req.Id isn't valid for RPC method calls
// according to the JSON-RPC 2.0 spec.
func checkId(req *JSONRequest) error {
if req.Id == nil {
return errors.New("RPC method expects request id")
}
id := string(req.Id)
if id == "null" {
return nil
}
if _, err := strconv.ParseFloat(id, 64); err == nil {
return nil
}
var str string
if err := json.Unmarshal(req.Id, &str); err == nil {
return nil
}
return errors.New("invalid request id")
}
// parseRequest will parse a single request from the given RawMessage. It will return the parsed request, an indication // parseRequest will parse a single request from the given RawMessage. It will return the parsed request, an indication
// if the request was a batch or an error when the request could not be parsed. // if the request was a batch or an error when the request could not be parsed.
func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) { func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) {
var in JSONRequest var in *JSONRequest
if err := json.Unmarshal(incomingMsg, &in); err != nil { if err := json.Unmarshal(incomingMsg, &in); err != nil {
return nil, false, &invalidMessageError{err.Error()} return nil, false, &invalidMessageError{err.Error()}
} }
if in.Id == nil { if err := checkId(in); err != nil {
return nil, false, &invalidMessageError{"Server cannot handle notifications"} return nil, false, &invalidRequestError{err.Error()}
} }
// subscribe are special, they will always use `subscribeMethod` as service method // subscribe are special, they will always use `subscribeMethod` as service method
if in.Method == subscribeMethod { if in.Method == subscribeMethod {
reqs := []rpcRequest{rpcRequest{id: *in.Id, isPubSub: true}} reqs := []rpcRequest{rpcRequest{id: &in.Id, isPubSub: true}}
if len(in.Payload) > 0 { if len(in.Payload) > 0 {
// first param must be subscription name // first param must be subscription name
var subscribeMethod [1]string var subscribeMethod [1]string
@ -155,7 +183,7 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) {
} }
if in.Method == unsubscribeMethod { if in.Method == unsubscribeMethod {
return []rpcRequest{rpcRequest{id: *in.Id, isPubSub: true, return []rpcRequest{rpcRequest{id: &in.Id, isPubSub: true,
method: unsubscribeMethod, params: in.Payload}}, false, nil method: unsubscribeMethod, params: in.Payload}}, false, nil
} }
@ -166,29 +194,29 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) {
} }
if len(in.Payload) == 0 { if len(in.Payload) == 0 {
return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: *in.Id}}, false, nil return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: &in.Id}}, false, nil
} }
return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: *in.Id, params: in.Payload}}, false, nil return []rpcRequest{rpcRequest{service: elems[0], method: elems[1], id: &in.Id, params: in.Payload}}, false, nil
} }
// parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication // parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication
// if the request was a batch or an error when the request could not be read. // if the request was a batch or an error when the request could not be read.
func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) { func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCError) {
var in []JSONRequest var in []*JSONRequest
if err := json.Unmarshal(incomingMsg, &in); err != nil { if err := json.Unmarshal(incomingMsg, &in); err != nil {
return nil, false, &invalidMessageError{err.Error()} return nil, false, &invalidMessageError{err.Error()}
} }
requests := make([]rpcRequest, len(in)) requests := make([]rpcRequest, len(in))
for i, r := range in { for i, r := range in {
if r.Id == nil { if err := checkId(r); err != nil {
return nil, true, &invalidMessageError{"Server cannot handle notifications"} return nil, true, &invalidRequestError{err.Error()}
} }
// (un)subscribe are special, they will always use the same service.method // (un)subscribe are special, they will always use the same service.method
if r.Method == subscribeMethod { if r.Method == subscribeMethod {
requests[i] = rpcRequest{id: *r.Id, isPubSub: true} requests[i] = rpcRequest{id: &r.Id, isPubSub: true}
if len(r.Payload) > 0 { if len(r.Payload) > 0 {
var subscribeMethod [1]string var subscribeMethod [1]string
if err := json.Unmarshal(r.Payload, &subscribeMethod); err != nil { if err := json.Unmarshal(r.Payload, &subscribeMethod); err != nil {
@ -206,7 +234,7 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCErro
} }
if r.Method == unsubscribeMethod { if r.Method == unsubscribeMethod {
requests[i] = rpcRequest{id: *r.Id, isPubSub: true, method: unsubscribeMethod, params: r.Payload} requests[i] = rpcRequest{id: &r.Id, isPubSub: true, method: unsubscribeMethod, params: r.Payload}
continue continue
} }
@ -216,9 +244,9 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, RPCErro
} }
if len(r.Payload) == 0 { if len(r.Payload) == 0 {
requests[i] = rpcRequest{service: elems[0], method: elems[1], id: *r.Id, params: nil} requests[i] = rpcRequest{service: elems[0], method: elems[1], id: &r.Id, params: nil}
} else { } else {
requests[i] = rpcRequest{service: elems[0], method: elems[1], id: *r.Id, params: r.Payload} requests[i] = rpcRequest{service: elems[0], method: elems[1], id: &r.Id, params: r.Payload}
} }
} }
@ -294,7 +322,7 @@ func parsePositionalArguments(args json.RawMessage, argTypes []reflect.Type) ([]
} }
// CreateResponse will create a JSON-RPC success response with the given id and reply as result. // CreateResponse will create a JSON-RPC success response with the given id and reply as result.
func (c *jsonCodec) CreateResponse(id int64, reply interface{}) interface{} { func (c *jsonCodec) CreateResponse(id interface{}, reply interface{}) interface{} {
if isHexNum(reflect.TypeOf(reply)) { if isHexNum(reflect.TypeOf(reply)) {
return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: fmt.Sprintf(`%#x`, reply)} return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: fmt.Sprintf(`%#x`, reply)}
} }
@ -302,13 +330,13 @@ func (c *jsonCodec) CreateResponse(id int64, reply interface{}) interface{} {
} }
// CreateErrorResponse will create a JSON-RPC error response with the given id and error. // CreateErrorResponse will create a JSON-RPC error response with the given id and error.
func (c *jsonCodec) CreateErrorResponse(id *int64, err RPCError) interface{} { func (c *jsonCodec) CreateErrorResponse(id interface{}, err RPCError) interface{} {
return &JSONErrResponse{Version: jsonRPCVersion, Id: id, Error: JSONError{Code: err.Code(), Message: err.Error()}} return &JSONErrResponse{Version: jsonRPCVersion, Id: id, Error: JSONError{Code: err.Code(), Message: err.Error()}}
} }
// CreateErrorResponseWithInfo will create a JSON-RPC error response with the given id and error. // CreateErrorResponseWithInfo will create a JSON-RPC error response with the given id and error.
// info is optional and contains additional information about the error. When an empty string is passed it is ignored. // info is optional and contains additional information about the error. When an empty string is passed it is ignored.
func (c *jsonCodec) CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} { func (c *jsonCodec) CreateErrorResponseWithInfo(id interface{}, err RPCError, info interface{}) interface{} {
return &JSONErrResponse{Version: jsonRPCVersion, Id: id, return &JSONErrResponse{Version: jsonRPCVersion, Id: id,
Error: JSONError{Code: err.Code(), Message: err.Error(), Data: info}} Error: JSONError{Code: err.Code(), Message: err.Error(), Data: info}}
} }

View file

@ -51,8 +51,8 @@ func TestJSONRequestParsing(t *testing.T) {
t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method) t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method)
} }
if requests[0].id != 1234 { if id, ok := requests[0].id.(float64); !ok || id != 1234 {
t.Fatalf("Expected id 1234 but got %d", requests[0].id) t.Fatalf("Expected id 1234 but got %v", requests[0].id)
} }
var arg int var arg int

View file

@ -160,11 +160,11 @@ func (s *Server) ServeCodec(codec ServerCodec) {
if batch { if batch {
resps := make([]interface{}, len(reqs)) resps := make([]interface{}, len(reqs))
for i, r := range reqs { for i, r := range reqs {
resps[i] = codec.CreateErrorResponse(&r.id, err) resps[i] = codec.CreateErrorResponse(r.id, err)
} }
codec.Write(resps) codec.Write(resps)
} else { } else {
codec.Write(codec.CreateErrorResponse(&reqs[0].id, err)) codec.Write(codec.CreateErrorResponse(reqs[0].id, err))
} }
break break
} }
@ -279,7 +279,7 @@ func (s *Server) unsubscribe(subid string) bool {
// handle executes a request and returns the response from the callback. // handle executes a request and returns the response from the callback.
func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverRequest) interface{} { func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverRequest) interface{} {
if req.err != nil { if req.err != nil {
return codec.CreateErrorResponse(&req.id, req.err) return codec.CreateErrorResponse(req.id, req.err)
} }
if req.isUnsubscribe { // first param must be the subscription id if req.isUnsubscribe { // first param must be the subscription id
@ -288,17 +288,17 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
if s.unsubscribe(subid) { if s.unsubscribe(subid) {
return codec.CreateResponse(req.id, true) return codec.CreateResponse(req.id, true)
} else { } else {
return codec.CreateErrorResponse(&req.id, return codec.CreateErrorResponse(req.id,
&callbackError{fmt.Sprintf("subscription '%s' not found", subid)}) &callbackError{fmt.Sprintf("subscription '%s' not found", subid)})
} }
} }
return codec.CreateErrorResponse(&req.id, &invalidParamsError{"Expected subscription id as argument"}) return codec.CreateErrorResponse(req.id, &invalidParamsError{"Expected subscription id as argument"})
} }
if req.callb.isSubscribe { if req.callb.isSubscribe {
subid, err := s.createSubscription(codec, req) subid, err := s.createSubscription(codec, req)
if err != nil { if err != nil {
return codec.CreateErrorResponse(&req.id, &callbackError{err.Error()}) return codec.CreateErrorResponse(req.id, &callbackError{err.Error()})
} }
return codec.CreateResponse(req.id, subid) return codec.CreateResponse(req.id, subid)
} }
@ -308,7 +308,7 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
rpcErr := &invalidParamsError{fmt.Sprintf("%s%s%s expects %d parameters, got %d", rpcErr := &invalidParamsError{fmt.Sprintf("%s%s%s expects %d parameters, got %d",
req.svcname, serviceMethodSeparator, req.callb.method.Name, req.svcname, serviceMethodSeparator, req.callb.method.Name,
len(req.callb.argTypes), len(req.args))} len(req.callb.argTypes), len(req.args))}
return codec.CreateErrorResponse(&req.id, rpcErr) return codec.CreateErrorResponse(req.id, rpcErr)
} }
arguments := []reflect.Value{req.callb.rcvr} arguments := []reflect.Value{req.callb.rcvr}
@ -328,7 +328,7 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
if req.callb.errPos >= 0 { // test if method returned an error if req.callb.errPos >= 0 { // test if method returned an error
if !reply[req.callb.errPos].IsNil() { if !reply[req.callb.errPos].IsNil() {
e := reply[req.callb.errPos].Interface().(error) e := reply[req.callb.errPos].Interface().(error)
res := codec.CreateErrorResponse(&req.id, &callbackError{e.Error()}) res := codec.CreateErrorResponse(req.id, &callbackError{e.Error()})
return res return res
} }
} }
@ -339,7 +339,7 @@ func (s *Server) handle(ctx context.Context, codec ServerCodec, req *serverReque
func (s *Server) exec(ctx context.Context, codec ServerCodec, req *serverRequest) { func (s *Server) exec(ctx context.Context, codec ServerCodec, req *serverRequest) {
var response interface{} var response interface{}
if req.err != nil { if req.err != nil {
response = codec.CreateErrorResponse(&req.id, req.err) response = codec.CreateErrorResponse(req.id, req.err)
} else { } else {
response = s.handle(ctx, codec, req) response = s.handle(ctx, codec, req)
} }
@ -355,7 +355,7 @@ func (s *Server) execBatch(ctx context.Context, codec ServerCodec, requests []*s
responses := make([]interface{}, len(requests)) responses := make([]interface{}, len(requests))
for i, req := range requests { for i, req := range requests {
if req.err != nil { if req.err != nil {
responses[i] = codec.CreateErrorResponse(&req.id, req.err) responses[i] = codec.CreateErrorResponse(req.id, req.err)
} else { } else {
responses[i] = s.handle(ctx, codec, req) responses[i] = s.handle(ctx, codec, req)
} }

View file

@ -109,7 +109,7 @@ func (c *ServerTestCodec) ReadRequestHeaders() ([]rpcRequest, bool, RPCError) {
if c.counter == 1 { if c.counter == 1 {
var req JSONRequest var req JSONRequest
json.Unmarshal(c.input, &req) json.Unmarshal(c.input, &req)
return []rpcRequest{rpcRequest{id: *req.Id, isPubSub: false, service: "test", method: req.Method, params: req.Payload}}, false, nil return []rpcRequest{rpcRequest{id: req.Id, isPubSub: false, service: "test", method: req.Method, params: req.Payload}}, false, nil
} }
// requests are executes in parallel, wait a bit before returning an error so that the previous request has time to // requests are executes in parallel, wait a bit before returning an error so that the previous request has time to
@ -172,15 +172,15 @@ func (c *ServerTestCodec) ParseRequestArguments(argTypes []reflect.Type, payload
return argValues, nil return argValues, nil
} }
func (c *ServerTestCodec) CreateResponse(id int64, reply interface{}) interface{} { func (c *ServerTestCodec) CreateResponse(id interface{}, reply interface{}) interface{} {
return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: reply} return &JSONSuccessResponse{Version: jsonRPCVersion, Id: id, Result: reply}
} }
func (c *ServerTestCodec) CreateErrorResponse(id *int64, err RPCError) interface{} { func (c *ServerTestCodec) CreateErrorResponse(id interface{}, err RPCError) interface{} {
return &JSONErrResponse{Version: jsonRPCVersion, Id: id, Error: JSONError{Code: err.Code(), Message: err.Error()}} return &JSONErrResponse{Version: jsonRPCVersion, Id: id, Error: JSONError{Code: err.Code(), Message: err.Error()}}
} }
func (c *ServerTestCodec) CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} { func (c *ServerTestCodec) CreateErrorResponseWithInfo(id interface{}, err RPCError, info interface{}) interface{} {
return &JSONErrResponse{Version: jsonRPCVersion, Id: id, return &JSONErrResponse{Version: jsonRPCVersion, Id: id,
Error: JSONError{Code: err.Code(), Message: err.Error(), Data: info}} Error: JSONError{Code: err.Code(), Message: err.Error(), Data: info}}
} }

View file

@ -57,7 +57,7 @@ type service struct {
// serverRequest is an incoming request // serverRequest is an incoming request
type serverRequest struct { type serverRequest struct {
id int64 id interface{}
svcname string svcname string
rcvr reflect.Value rcvr reflect.Value
callb *callback callb *callback
@ -86,7 +86,7 @@ type Server struct {
type rpcRequest struct { type rpcRequest struct {
service string service string
method string method string
id int64 id interface{}
isPubSub bool isPubSub bool
params interface{} params interface{}
} }
@ -108,11 +108,11 @@ type ServerCodec interface {
// Parse request argument to the given types // Parse request argument to the given types
ParseRequestArguments([]reflect.Type, interface{}) ([]reflect.Value, RPCError) ParseRequestArguments([]reflect.Type, interface{}) ([]reflect.Value, RPCError)
// Assemble success response // Assemble success response
CreateResponse(int64, interface{}) interface{} CreateResponse(interface{}, interface{}) interface{}
// Assemble error response // Assemble error response
CreateErrorResponse(*int64, RPCError) interface{} CreateErrorResponse(interface{}, RPCError) interface{}
// Assemble error response with extra information about the error through info // Assemble error response with extra information about the error through info
CreateErrorResponseWithInfo(id *int64, err RPCError, info interface{}) interface{} CreateErrorResponseWithInfo(id interface{}, err RPCError, info interface{}) interface{}
// Create notification response // Create notification response
CreateNotification(string, interface{}) interface{} CreateNotification(string, interface{}) interface{}
// Write msg to client. // Write msg to client.

View file

@ -218,7 +218,7 @@ func newSubscriptionId() (string, error) {
// on which the given client connects. // on which the given client connects.
func SupportedModules(client Client) (map[string]string, error) { func SupportedModules(client Client) (map[string]string, error) {
req := JSONRequest{ req := JSONRequest{
Id: new(int64), Id: []byte("1"),
Version: "2.0", Version: "2.0",
Method: "rpc_modules", Method: "rpc_modules",
} }