This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
72 lines (53 loc) · 1.6 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package fastjsonrpc
import (
"context"
"encoding/json"
"github.com/valyala/fasthttp"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fastjson"
)
// RequestHandler must process incoming requests.
type RequestHandler func(ctx *RequestCtx)
// RequestCtx contains incoming request and manages outgoing response.
type RequestCtx struct {
fasthttpCtx *fasthttp.RequestCtx
arena fastjson.Arena
id []byte
method []byte
params *fastjson.Value
paramsBytes *bytebufferpool.ByteBuffer
response *bytebufferpool.ByteBuffer
bytebufferpool *bytebufferpool.Pool
}
// Arena returns fastjson.Arena for current request.
//
// RequestHandler should avoid holding references to Arena and/or constructed Values after the return.
func (ctx *RequestCtx) Arena() *fastjson.Arena {
return &ctx.arena
}
// ID returns "id" field of JSON-RPC 2.0 request.
func (ctx *RequestCtx) ID() []byte {
return ctx.id
}
// Method returns matched method.
func (ctx *RequestCtx) Method() []byte {
return ctx.method
}
// Params returns request parameters already unmarshalled with valyala/fastjson.
func (ctx *RequestCtx) Params() *fastjson.Value {
return ctx.params
}
// ParamsBytes returns raw bytes of request's "params" field.
func (ctx *RequestCtx) ParamsBytes() []byte {
return ctx.paramsBytes.B
}
// ParamsUnmarshal parses request param and stores the result in the value pointed to by v.
func (ctx *RequestCtx) ParamsUnmarshal(v interface{}) *Error {
if json.Unmarshal(ctx.ParamsBytes(), v) != nil {
return ErrInvalidParams()
}
return nil
}
func (ctx *RequestCtx) Context() context.Context {
return ctx.fasthttpCtx
}