From f9d8af2f005beef68565adf61fc9af67ecd2a2ef Mon Sep 17 00:00:00 2001 From: a <a@tuxpa.in> Date: Sun, 4 Dec 2022 01:03:07 -0600 Subject: [PATCH] change request --- handler.go | 4 +-- reflect_handler.go | 4 +-- router_mux.go | 2 +- router_request.go | 64 +++++++++++++++++++++++++-------------------- router_response.go | 2 +- testservice_test.go | 2 +- 6 files changed, 43 insertions(+), 35 deletions(-) diff --git a/handler.go b/handler.go index 3937b2f..22ac056 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 f9748f1..ce3fd58 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 d8a4e3e..62148b3 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 0549ac8..4c2208e 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 6093f98..863a920 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 ac24fca..43e4706 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 { -- GitLab