diff --git a/handler.go b/handler.go index 3937b2f52003fa828a7d96a3b18f2181a98adb4b..22ac0566bff1044f9d8bbe26b7f6c0a7573c2996 100644 --- a/handler.go +++ b/handler.go @@ -306,7 +306,7 @@ func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMes // Install notifier in context so the subscription handler can find it. n := &Notifier{h: h, namespace: namespace, idgen: randomIDGenerator()} cp.notifiers = append(cp.notifiers, n) - req := &Request{ctx: cp.ctx, msg: *msg, peer: h.peer} + req := NewMsgRequest(cp.ctx, h.peer, *msg) // now actually run the handler req = req.WithContext( context.WithValue(req.ctx, notifierKey{}, n), @@ -356,7 +356,7 @@ func (h *handler) addSubscriptions(nn []*Notifier) { func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage { callb := h.reg.Match(NewRouteContext(), msg.Method) - req := &Request{ctx: cp.ctx, msg: *msg, peer: h.peer} + req := NewMsgRequest(cp.ctx, h.peer, *msg) mw := NewReaderResponseWriterMsg(req) if msg.isSubscribe() { return h.handleSubscribe(cp, msg) diff --git a/reflect_handler.go b/reflect_handler.go index f9748f1e1d8c7daed3d32d4be246e448ea8146d3..ce3fd5857f3ea493bc4ec4d26918e69308b7c4da 100644 --- a/reflect_handler.go +++ b/reflect_handler.go @@ -82,7 +82,7 @@ type callback struct { // callback handler implements handler for the original receiver style that geth used func (e *callback) ServeRPC(w ResponseWriter, r *Request) { argTypes := append([]reflect.Type{}, e.argTypes...) - args, err := parsePositionalArguments(r.msg.Params, argTypes) + args, err := parsePositionalArguments(r.Params, argTypes) if err != nil { w.Send(nil, &invalidParamsError{err.Error()}) return @@ -102,7 +102,7 @@ func (e *callback) ServeRPC(w ResponseWriter, r *Request) { const size = 64 << 10 buf := make([]byte, size) buf = buf[:runtime.Stack(buf, false)] - log.Error().Str("method", r.msg.Method).Interface("err", err).Hex("buf", buf).Msg("crashed") + log.Error().Str("method", r.Method).Interface("err", err).Hex("buf", buf).Msg("crashed") // errRes := errors.New("method handler crashed: " + fmt.Sprint(err)) w.Send(nil, nil) return diff --git a/router_mux.go b/router_mux.go index d8a4e3ef79c6b3bc4a3a62ab6b461bf8c0d02ac6..62148b385875e2cfb523b70e6c18ce5c7fe2f53f 100644 --- a/router_mux.go +++ b/router_mux.go @@ -365,7 +365,7 @@ func (mx *Mux) routeRPC(w ResponseWriter, r *Request) { // The request routing path routePath := rctx.RoutePath if routePath == "" { - routePath = r.Method() + routePath = r.Method if routePath == "" { routePath = "_" } diff --git a/router_request.go b/router_request.go index 0549ac8b27c144bbaa3846f27d26e42719206787..4c2208ee8bdedb3d9640eda70d84a48226178614 100644 --- a/router_request.go +++ b/router_request.go @@ -8,40 +8,45 @@ import ( ) type Request struct { + ID *ID `json:"id,omitempty"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` + + Peer PeerInfo `json:"-"` + + Version version `json:"jsonrpc"` + ctx context.Context - msg jsonrpcMessage +} - peer PeerInfo +func NewMsgRequest(ctx context.Context, peer PeerInfo, msg jsonrpcMessage) *Request { + r := &Request{ctx: ctx} + r.ID = msg.ID + r.Method = msg.Method + r.Params = msg.Params + r.Peer = peer + r.ctx = ctx + return r } func NewRequest(ctx context.Context, id string, method string, params any) *Request { r := &Request{ctx: ctx} pms, _ := json.Marshal(params) - r.msg = jsonrpcMessage{ - ID: NewStringIDPtr(id), - Method: method, - Params: pms, - } + r.ID = NewStringIDPtr(id) + r.Method = method + r.Params = pms return r } -func (r *Request) Method() string { - return r.msg.Method -} - -func (r *Request) Params() json.RawMessage { - return r.msg.Params -} - func (r *Request) ParamSlice() []any { var params []any - json.Unmarshal(r.msg.Params, ¶ms) + json.Unmarshal(r.Params, ¶ms) return params } func (r *Request) ParamArray(a ...any) error { var params []json.RawMessage - json.Unmarshal(r.msg.Params, ¶ms) + json.Unmarshal(r.Params, ¶ms) for idx, v := range params { if len(v) > idx { err := json.Unmarshal(v, &a[idx]) @@ -56,19 +61,23 @@ func (r *Request) ParamArray(a ...any) error { } func (r *Request) ParamInto(v any) error { - return json.Unmarshal(r.msg.Params, &v) + return json.Unmarshal(r.Params, &v) } func (r *Request) Context() context.Context { return r.ctx } -func (r *Request) Remote() string { - return r.peer.RemoteAddr +func (r *Request) Msg() jsonrpcMessage { + return jsonrpcMessage{ + ID: r.ID, + Method: r.Method, + Params: r.Params, + } } -func (r *Request) Peer() PeerInfo { - return r.peer +func (r *Request) Remote() string { + return r.Peer.RemoteAddr } func (r *Request) WithContext(ctx context.Context) *Request { @@ -79,18 +88,17 @@ func (r *Request) WithContext(ctx context.Context) *Request { r2 := new(Request) *r2 = *r r2.ctx = ctx - r2.msg = r.msg + r2.ID = r.ID + r2.Method = r.Method + r2.Params = r.Params + r2.Peer = r.Peer return r2 } -func (r *Request) Msg() jsonrpcMessage { - return r.msg -} - var jpool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool() func (r *Request) Iter(fn func(j *jsoniter.Iterator) error) error { - it := jpool.BorrowIterator(r.Params()) + it := jpool.BorrowIterator(r.Params) defer jpool.ReturnIterator(it) return fn(it) } diff --git a/router_response.go b/router_response.go index 6093f989473a3f251ffb10174dd58acd8ba5b221..863a920502afc4d6763b71a2ce9827f1de9ef31c 100644 --- a/router_response.go +++ b/router_response.go @@ -39,7 +39,7 @@ func NewReaderResponseWriterMsg(r *Request) *ResponseWriterMsg { } func (w *ResponseWriterMsg) Header() http.Header { - wh := w.r.Peer().HTTP.WriteHeaders + wh := w.r.Peer.HTTP.WriteHeaders if wh == nil { wh = http.Header{} } diff --git a/testservice_test.go b/testservice_test.go index ac24fca48c168a81cd6191e1d69848f619472673..43e47062d8e04e3f5e8fa726ab176dd9d6a46bc6 100644 --- a/testservice_test.go +++ b/testservice_test.go @@ -27,7 +27,7 @@ import ( func newTestServer() *Server { server := NewServer() server.Router().HandleFunc("testservice_subscribe", func(w ResponseWriter, r *Request) { - log.Println(r.Params()) + log.Println(r.Params) sub, err := UpgradeToSubscription(w, r) w.Send(sub, err) if err != nil {