-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
80 lines (64 loc) · 1.69 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
73
74
75
76
77
78
79
80
package jsonrpc
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
// Request represents a JSON-RPC request received by the server.
type Request struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
ID interface{} `json:"id"`
parsingError bool
}
// Unmarshal unmarshal req params to specified structure.
// Unmarshal will set parse error if unmarshaling failed.
func (r *Request) Unmarshal(v interface{}) error {
if err := json.Unmarshal(r.Params, v); err != nil {
r.parsingError = true
return err
}
return nil
}
// parseRequest parses a HTTP request to JSON-RPC request.
func parseRequest(r *http.Request) (requests []*Request, reqParseError *respError) {
// check for content type
if !strings.HasPrefix(r.Header.Get(contentTypeKey), contentTypeValue) {
return requests, errInvalidRequest()
}
defer func(r *http.Request) {
if err := r.Body.Close(); err != nil {
reqParseError = errInternal()
}
}(r)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return requests, errInvalidRequest()
}
// check for batch
if bytes.ContainsRune(b[:1], batchRequestKey) {
requests, reqParseError = unmarshalBatchReq(b)
} else {
requests, reqParseError = unmarshalReq(b)
}
return requests, reqParseError
}
func unmarshalBatchReq(b []byte) ([]*Request, *respError) {
var rs []*Request
if err := json.Unmarshal(b, &rs); err != nil {
return nil, errParse()
}
return rs, nil
}
func unmarshalReq(b []byte) ([]*Request, *respError) {
rs := make([]*Request, 1)
req := new(Request)
if err := json.Unmarshal(b, &req); err != nil {
return nil, errParse()
}
rs[0] = req
return rs, nil
}