-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmessage.go
More file actions
216 lines (190 loc) · 5.25 KB
/
message.go
File metadata and controls
216 lines (190 loc) · 5.25 KB
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
// Package fdpass implements JSON-RPC 2.0 with file descriptor passing over Unix domain sockets.
package fdpass
import (
"encoding/json"
"os"
)
// JSONRPCVersion is the JSON-RPC protocol version.
const JSONRPCVersion = "2.0"
// FDsKey is the JSON key for the file descriptor count field.
const FDsKey = "fds"
// FileDescriptorErrorCode is the error code for FD-related protocol errors.
const FileDescriptorErrorCode = -32050
// Request represents a JSON-RPC 2.0 request.
type Request struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
ID interface{} `json:"id"`
// Fds is the number of file descriptors attached to this message.
Fds *int `json:"fds,omitempty"`
}
// Response represents a JSON-RPC 2.0 response.
type Response struct {
JsonRpc string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
ID interface{} `json:"id"`
// Fds is the number of file descriptors attached to this message.
Fds *int `json:"fds,omitempty"`
}
// Notification represents a JSON-RPC 2.0 notification (a request without an ID).
type Notification struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
// Fds is the number of file descriptors attached to this message.
Fds *int `json:"fds,omitempty"`
}
// Error represents a JSON-RPC 2.0 error object.
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
func (e *Error) Error() string {
return e.Message
}
// MessageWithFds wraps a JSON-RPC message with associated file descriptors.
type MessageWithFds struct {
// Message is the JSON-RPC message (Request, Response, or Notification).
Message interface{}
// FileDescriptors are the file descriptors to pass with this message.
// The order corresponds to indices 0..N-1 matching the message's fds count.
FileDescriptors []*os.File
}
// NewRequest creates a new JSON-RPC request.
func NewRequest(method string, params interface{}, id interface{}) *Request {
return &Request{
JsonRpc: JSONRPCVersion,
Method: method,
Params: params,
ID: id,
}
}
// NewResponse creates a new successful JSON-RPC response.
func NewResponse(result interface{}, id interface{}) *Response {
return &Response{
JsonRpc: JSONRPCVersion,
Result: result,
ID: id,
}
}
// NewErrorResponse creates a new error JSON-RPC response.
func NewErrorResponse(err *Error, id interface{}) *Response {
return &Response{
JsonRpc: JSONRPCVersion,
Error: err,
ID: id,
}
}
// NewNotification creates a new JSON-RPC notification.
func NewNotification(method string, params interface{}) *Notification {
return &Notification{
JsonRpc: JSONRPCVersion,
Method: method,
Params: params,
}
}
// GetFDCount reads the file descriptor count from a JSON value.
// Returns 0 if the `fds` field is absent or not a valid number.
func GetFDCount(value map[string]interface{}) int {
if fds, ok := value[FDsKey]; ok {
switch v := fds.(type) {
case float64:
return int(v)
case int:
return v
}
}
return 0
}
// FileDescriptorError creates a standard FD error for protocol violations.
func FileDescriptorError() *Error {
return &Error{
Code: FileDescriptorErrorCode,
Message: "File Descriptor Error",
}
}
// SetFDs sets the fds count on a Request.
func (r *Request) SetFDs(count int) {
if count > 0 {
r.Fds = &count
} else {
r.Fds = nil
}
}
// GetFDs returns the fds count from a Request.
func (r *Request) GetFDs() int {
if r.Fds != nil {
return *r.Fds
}
return 0
}
// SetFDs sets the fds count on a Response.
func (r *Response) SetFDs(count int) {
if count > 0 {
r.Fds = &count
} else {
r.Fds = nil
}
}
// GetFDs returns the fds count from a Response.
func (r *Response) GetFDs() int {
if r.Fds != nil {
return *r.Fds
}
return 0
}
// SetFDs sets the fds count on a Notification.
func (n *Notification) SetFDs(count int) {
if count > 0 {
n.Fds = &count
} else {
n.Fds = nil
}
}
// GetFDs returns the fds count from a Notification.
func (n *Notification) GetFDs() int {
if n.Fds != nil {
return *n.Fds
}
return 0
}
// ParseMessage parses a raw JSON message into the appropriate type.
// It returns one of *Request, *Response, or *Notification.
func ParseMessage(data []byte) (interface{}, error) {
// First parse as a generic map to determine type
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil, err
}
// Determine message type based on fields present
_, hasMethod := raw["method"]
_, hasID := raw["id"]
_, hasResult := raw["result"]
_, hasError := raw["error"]
if hasMethod && hasID {
// Request
var req Request
if err := json.Unmarshal(data, &req); err != nil {
return nil, err
}
return &req, nil
} else if hasResult || hasError {
// Response
var resp Response
if err := json.Unmarshal(data, &resp); err != nil {
return nil, err
}
return &resp, nil
} else if hasMethod {
// Notification
var notif Notification
if err := json.Unmarshal(data, ¬if); err != nil {
return nil, err
}
return ¬if, nil
}
return nil, &Error{Code: -32600, Message: "Invalid JSON-RPC message"}
}