2019-06-28 09:03:28 +00:00
|
|
|
package rpclib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2019-07-02 13:49:10 +00:00
|
|
|
const (
|
|
|
|
rpcParseError = -32700
|
|
|
|
rpcMethodNotFound = -32601
|
|
|
|
rpcInvalidParams = -32602
|
|
|
|
)
|
|
|
|
|
2019-06-28 09:03:28 +00:00
|
|
|
type rpcHandler struct {
|
|
|
|
paramReceivers []reflect.Type
|
|
|
|
nParams int
|
|
|
|
|
|
|
|
receiver reflect.Value
|
|
|
|
handlerFunc reflect.Value
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
hasCtx int
|
|
|
|
|
2019-06-28 09:03:28 +00:00
|
|
|
errOut int
|
|
|
|
valOut int
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// RPCServer provides a jsonrpc 2.0 http server handler
|
2019-06-28 09:03:28 +00:00
|
|
|
type RPCServer struct {
|
|
|
|
methods map[string]rpcHandler
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// NewServer creates new RPCServer instance
|
2019-06-28 09:03:28 +00:00
|
|
|
func NewServer() *RPCServer {
|
|
|
|
return &RPCServer{
|
|
|
|
methods: map[string]rpcHandler{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type param struct {
|
2019-06-28 13:49:34 +00:00
|
|
|
data []byte // from unmarshal
|
|
|
|
|
|
|
|
v reflect.Value // to marshal
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *param) UnmarshalJSON(raw []byte) error {
|
|
|
|
p.data = make([]byte, len(raw))
|
|
|
|
copy(p.data, raw)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
func (p *param) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(p.v.Interface())
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:03:28 +00:00
|
|
|
type request struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
2019-07-02 19:08:30 +00:00
|
|
|
ID *int64 `json:"id,omitempty"`
|
2019-06-28 09:03:28 +00:00
|
|
|
Method string `json:"method"`
|
|
|
|
Params []param `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type respError struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:49:10 +00:00
|
|
|
func (e *respError) Error() string {
|
|
|
|
if e.Code >= -32768 && e.Code <= -32000 {
|
|
|
|
return fmt.Sprintf("RPC error (%d): %s", e.Code, e.Message)
|
|
|
|
}
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:03:28 +00:00
|
|
|
type response struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
2019-06-28 13:49:34 +00:00
|
|
|
Result interface{} `json:"result,omitempty"`
|
2019-07-02 19:08:30 +00:00
|
|
|
ID int64 `json:"id"`
|
2019-06-28 09:03:28 +00:00
|
|
|
Error *respError `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-07-02 12:40:25 +00:00
|
|
|
// TODO: return errors to clients per spec
|
2019-06-28 09:03:28 +00:00
|
|
|
func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req request
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2019-07-02 13:49:10 +00:00
|
|
|
s.rpcError(w, &req, rpcParseError, err)
|
2019-06-28 09:03:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, ok := s.methods[req.Method]
|
|
|
|
if !ok {
|
2019-07-02 13:49:10 +00:00
|
|
|
s.rpcError(w, &req, rpcMethodNotFound, fmt.Errorf("method '%s' not found", req.Method))
|
2019-06-28 09:03:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
if len(req.Params) != handler.nParams {
|
2019-07-02 13:49:10 +00:00
|
|
|
s.rpcError(w, &req, rpcInvalidParams, fmt.Errorf("wrong param count"))
|
2019-06-28 13:49:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
callParams := make([]reflect.Value, 1+handler.hasCtx+handler.nParams)
|
2019-06-28 09:03:28 +00:00
|
|
|
callParams[0] = handler.receiver
|
2019-06-28 14:53:01 +00:00
|
|
|
if handler.hasCtx == 1 {
|
|
|
|
callParams[1] = reflect.ValueOf(r.Context())
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:03:28 +00:00
|
|
|
for i := 0; i < handler.nParams; i++ {
|
|
|
|
rp := reflect.New(handler.paramReceivers[i])
|
|
|
|
if err := json.NewDecoder(bytes.NewReader(req.Params[i].data)).Decode(rp.Interface()); err != nil {
|
2019-07-02 13:49:10 +00:00
|
|
|
s.rpcError(w, &req, rpcParseError, err)
|
2019-06-28 09:03:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
callParams[i+1+handler.hasCtx] = reflect.ValueOf(rp.Elem().Interface())
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callResult := handler.handlerFunc.Call(callParams)
|
2019-07-02 19:08:30 +00:00
|
|
|
if req.ID == nil {
|
2019-06-28 09:03:28 +00:00
|
|
|
return // notification
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := response{
|
|
|
|
Jsonrpc: "2.0",
|
2019-07-02 19:08:30 +00:00
|
|
|
ID: *req.ID,
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if handler.errOut != -1 {
|
|
|
|
err := callResult[handler.errOut].Interface()
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = &respError{
|
|
|
|
Code: 1,
|
|
|
|
Message: err.(error).Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if handler.valOut != -1 {
|
|
|
|
resp.Result = callResult[handler.valOut].Interface()
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
2019-07-02 12:40:25 +00:00
|
|
|
fmt.Println(err)
|
2019-06-28 13:49:34 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 13:49:10 +00:00
|
|
|
func (s *RPCServer) rpcError(w http.ResponseWriter, req *request, code int, err error) {
|
|
|
|
w.WriteHeader(500)
|
2019-07-02 19:08:30 +00:00
|
|
|
if req.ID == nil { // notification
|
2019-07-02 13:49:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := response{
|
|
|
|
Jsonrpc: "2.0",
|
2019-07-02 19:08:30 +00:00
|
|
|
ID: *req.ID,
|
2019-07-02 13:49:10 +00:00
|
|
|
Error: &respError{
|
|
|
|
Code: code,
|
2019-07-02 17:30:37 +00:00
|
|
|
Message: err.Error(),
|
2019-07-02 13:49:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = json.NewEncoder(w).Encode(resp)
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// Register registers new RPC handler
|
|
|
|
//
|
|
|
|
// Handler is any value with methods defined
|
2019-06-29 09:19:06 +00:00
|
|
|
func (s *RPCServer) Register(namespace string, r interface{}) {
|
2019-06-28 09:03:28 +00:00
|
|
|
val := reflect.ValueOf(r)
|
|
|
|
//TODO: expect ptr
|
|
|
|
|
|
|
|
for i := 0; i < val.NumMethod(); i++ {
|
|
|
|
method := val.Type().Method(i)
|
|
|
|
|
|
|
|
funcType := method.Func.Type()
|
2019-06-28 14:53:01 +00:00
|
|
|
hasCtx := 0
|
|
|
|
if funcType.NumIn() >= 2 && funcType.In(1) == contextType {
|
|
|
|
hasCtx = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
ins := funcType.NumIn() - 1 - hasCtx
|
2019-06-28 09:03:28 +00:00
|
|
|
recvs := make([]reflect.Type, ins)
|
|
|
|
for i := 0; i < ins; i++ {
|
2019-06-28 14:53:01 +00:00
|
|
|
recvs[i] = method.Type.In(i + 1 + hasCtx)
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
valOut, errOut, _ := processFuncOut(funcType)
|
2019-06-28 09:03:28 +00:00
|
|
|
|
2019-07-01 20:00:22 +00:00
|
|
|
fmt.Println(namespace + "." + method.Name)
|
2019-06-29 09:19:06 +00:00
|
|
|
|
|
|
|
s.methods[namespace+"."+method.Name] = rpcHandler{
|
2019-06-28 09:03:28 +00:00
|
|
|
paramReceivers: recvs,
|
|
|
|
nParams: ins,
|
|
|
|
|
|
|
|
handlerFunc: method.Func,
|
|
|
|
receiver: val,
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
hasCtx: hasCtx,
|
|
|
|
|
2019-06-28 09:03:28 +00:00
|
|
|
errOut: errOut,
|
|
|
|
valOut: valOut,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 13:49:34 +00:00
|
|
|
|
|
|
|
func processFuncOut(funcType reflect.Type) (valOut int, errOut int, n int) {
|
|
|
|
errOut = -1
|
|
|
|
valOut = -1
|
|
|
|
n = funcType.NumOut()
|
|
|
|
|
|
|
|
switch n {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
2019-06-28 14:53:01 +00:00
|
|
|
if funcType.Out(0) == errorType {
|
2019-06-28 13:49:34 +00:00
|
|
|
errOut = 0
|
|
|
|
} else {
|
|
|
|
valOut = 0
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
valOut = 0
|
|
|
|
errOut = 1
|
2019-06-28 14:53:01 +00:00
|
|
|
if funcType.Out(1) != errorType {
|
2019-06-28 13:49:34 +00:00
|
|
|
panic("expected error as second return value")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("too many error values")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-07-02 13:49:10 +00:00
|
|
|
|
|
|
|
var _ error = &respError{}
|