forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
217 lines (194 loc) · 5.22 KB
/
server.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package jsonrpc2
import (
"encoding/json"
"errors"
"fmt"
"io"
"sync"
"github.com/micro/go-micro/codec"
)
type serverCodec struct {
encmutex sync.Mutex // protects enc
dec *json.Decoder // for reading JSON values
enc *json.Encoder // for writing JSON values
c io.Closer
// temporary work space
req serverRequest
// JSON-RPC clients can use arbitrary json values as request IDs.
// Package rpc expects uint64 request IDs.
// We assign uint64 sequence numbers to incoming requests
// but save the original request ID in the pending map.
// When rpc responds, we use the sequence number in
// the response to find the original request ID.
mutex sync.Mutex // protects seq, pending
seq uint64
pending map[interface{}]*json.RawMessage
}
func newServerCodec(conn io.ReadWriteCloser) *serverCodec {
return &serverCodec{
dec: json.NewDecoder(conn),
enc: json.NewEncoder(conn),
c: conn,
pending: make(map[interface{}]*json.RawMessage),
}
}
type serverRequest struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params *json.RawMessage `json:"params"`
ID *json.RawMessage `json:"id"`
}
func (r *serverRequest) reset() {
r.Version = ""
r.Method = ""
r.Params = nil
r.ID = nil
}
func (r *serverRequest) UnmarshalJSON(raw []byte) error {
r.reset()
type req *serverRequest
if err := json.Unmarshal(raw, req(r)); err != nil {
return errors.New("bad request")
}
var o = make(map[string]*json.RawMessage)
if err := json.Unmarshal(raw, &o); err != nil {
return errors.New("bad request")
}
if o["jsonrpc"] == nil || o["method"] == nil {
return errors.New("bad request")
}
_, okID := o["id"]
_, okParams := o["params"]
if len(o) == 3 && !(okID || okParams) || len(o) == 4 && !(okID && okParams) || len(o) > 4 {
return errors.New("bad request")
}
if r.Version != "2.0" {
return errors.New("bad request")
}
if okParams {
if r.Params == nil || len(*r.Params) == 0 {
return errors.New("bad request")
}
switch []byte(*r.Params)[0] {
case '[', '{':
default:
return errors.New("bad request")
}
}
if okID && r.ID == nil {
r.ID = &null
}
if okID {
if len(*r.ID) == 0 {
return errors.New("bad request")
}
switch []byte(*r.ID)[0] {
case 't', 'f', '{', '[':
return errors.New("bad request")
}
}
return nil
}
type serverResponse struct {
Version string `json:"jsonrpc"`
ID *json.RawMessage `json:"id"`
Result interface{} `json:"result,omitempty"`
Error interface{} `json:"error,omitempty"`
}
func (c *serverCodec) ReadHeader(m *codec.Message) (err error) {
// If return error:
// - codec will be closed
// So, try to send error reply to client before returning error.
c.req.reset()
var raw json.RawMessage
if err := c.dec.Decode(&raw); err != nil {
c.encmutex.Lock()
c.enc.Encode(serverResponse{Version: "2.0", ID: &null, Error: errParse})
c.encmutex.Unlock()
return err
}
if err := json.Unmarshal(raw, &c.req); err != nil {
if err.Error() == "bad request" {
c.encmutex.Lock()
c.enc.Encode(serverResponse{Version: "2.0", ID: &null, Error: errRequest})
c.encmutex.Unlock()
}
return err
}
m.Endpoint = c.req.Method
// JSON request id can be any JSON value;
// RPC package expects uint64. Translate to
// internal uint64 and save JSON on the side.
c.mutex.Lock()
c.seq++
c.pending[c.seq] = c.req.ID
c.req.ID = nil
m.Id = fmt.Sprintf("%d", c.seq)
c.mutex.Unlock()
return nil
}
func (c *serverCodec) ReadBody(x interface{}) error {
// If x!=nil and return error e:
// - WriteResponse() will be called with e.Error() in r.Error
if x == nil {
return nil
}
if c.req.Params == nil {
return nil
}
if err := json.Unmarshal(*c.req.Params, x); err != nil {
return NewError(errParams.Code, err.Error())
}
return nil
}
var null = json.RawMessage([]byte("null"))
func (c *serverCodec) Write(m *codec.Message, x interface{}) error {
// If return error: nothing happens.
// In r.Error will be "" or .Error() of error returned by:
// - ReadRequestBody()
// - called RPC method
c.mutex.Lock()
b, ok := c.pending[m.Id]
if !ok {
c.mutex.Unlock()
fmt.Println("invalid sequence number in response", m.Id)
return errors.New("invalid sequence number in response")
}
c.mutex.Unlock()
if replies, ok := x.(*[]*json.RawMessage); m.Endpoint == "JSONRPC2.Batch" && ok {
if len(*replies) == 0 {
return nil
}
c.encmutex.Lock()
defer c.encmutex.Unlock()
return c.enc.Encode(replies)
}
if b == nil {
// Notification. Do not respond.
return nil
}
resp := serverResponse{Version: "2.0", ID: b}
if m.Error == "" {
if x == nil {
resp.Result = &null
} else {
resp.Result = x
}
} else if m.Error[0] == '{' && m.Error[len(m.Error)-1] == '}' {
// Well… this check for '{'…'}' isn't too strict, but I
// suppose we're trusting our own RPC methods (this way they
// can force sending wrong reply or many replies instead
// of one) and normal errors won't be formatted this way.
raw := json.RawMessage(m.Error)
resp.Error = &raw
} else {
raw := json.RawMessage(newError(m.Error).Error())
resp.Error = &raw
}
c.encmutex.Lock()
defer c.encmutex.Unlock()
return c.enc.Encode(resp)
}
func (c *serverCodec) Close() error {
return c.c.Close()
}