json.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "reflect"
  25. "strings"
  26. "sync"
  27. "time"
  28. )
  29. const (
  30. vsn = "2.0"
  31. serviceMethodSeparator = "_"
  32. subscribeMethodSuffix = "_subscribe"
  33. unsubscribeMethodSuffix = "_unsubscribe"
  34. notificationMethodSuffix = "_subscription"
  35. defaultWriteTimeout = 10 * time.Second // used if context has no deadline
  36. )
  37. var null = json.RawMessage("null")
  38. type subscriptionResult struct {
  39. ID string `json:"subscription"`
  40. Result json.RawMessage `json:"result,omitempty"`
  41. }
  42. // A value of this type can a JSON-RPC request, notification, successful response or
  43. // error response. Which one it is depends on the fields.
  44. type jsonrpcMessage struct {
  45. Version string `json:"jsonrpc,omitempty"`
  46. ID json.RawMessage `json:"id,omitempty"`
  47. Method string `json:"method,omitempty"`
  48. Params json.RawMessage `json:"params,omitempty"`
  49. Error *jsonError `json:"error,omitempty"`
  50. Result json.RawMessage `json:"result,omitempty"`
  51. }
  52. func (msg *jsonrpcMessage) isNotification() bool {
  53. return msg.ID == nil && msg.Method != ""
  54. }
  55. func (msg *jsonrpcMessage) isCall() bool {
  56. return msg.hasValidID() && msg.Method != ""
  57. }
  58. func (msg *jsonrpcMessage) isResponse() bool {
  59. return msg.hasValidID() && msg.Method == "" && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
  60. }
  61. func (msg *jsonrpcMessage) hasValidID() bool {
  62. return len(msg.ID) > 0 && msg.ID[0] != '{' && msg.ID[0] != '['
  63. }
  64. func (msg *jsonrpcMessage) isSubscribe() bool {
  65. return strings.HasSuffix(msg.Method, subscribeMethodSuffix)
  66. }
  67. func (msg *jsonrpcMessage) isUnsubscribe() bool {
  68. return strings.HasSuffix(msg.Method, unsubscribeMethodSuffix)
  69. }
  70. func (msg *jsonrpcMessage) namespace() string {
  71. elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2)
  72. return elem[0]
  73. }
  74. func (msg *jsonrpcMessage) String() string {
  75. b, _ := json.Marshal(msg)
  76. return string(b)
  77. }
  78. func (msg *jsonrpcMessage) errorResponse(err error) *jsonrpcMessage {
  79. resp := errorMessage(err)
  80. resp.ID = msg.ID
  81. return resp
  82. }
  83. func (msg *jsonrpcMessage) response(result interface{}) *jsonrpcMessage {
  84. enc, err := json.Marshal(result)
  85. if err != nil {
  86. // TODO: wrap with 'internal server error'
  87. return msg.errorResponse(err)
  88. }
  89. return &jsonrpcMessage{Version: vsn, ID: msg.ID, Result: enc}
  90. }
  91. func errorMessage(err error) *jsonrpcMessage {
  92. msg := &jsonrpcMessage{Version: vsn, ID: null, Error: &jsonError{
  93. Code: defaultErrorCode,
  94. Message: err.Error(),
  95. }}
  96. ec, ok := err.(Error)
  97. if ok {
  98. msg.Error.Code = ec.ErrorCode()
  99. }
  100. return msg
  101. }
  102. type jsonError struct {
  103. Code int `json:"code"`
  104. Message string `json:"message"`
  105. Data interface{} `json:"data,omitempty"`
  106. }
  107. func (err *jsonError) Error() string {
  108. if err.Message == "" {
  109. return fmt.Sprintf("json-rpc error %d", err.Code)
  110. }
  111. return err.Message
  112. }
  113. func (err *jsonError) ErrorCode() int {
  114. return err.Code
  115. }
  116. // Conn is a subset of the methods of net.Conn which are sufficient for ServerCodec.
  117. type Conn interface {
  118. io.ReadWriteCloser
  119. SetWriteDeadline(time.Time) error
  120. }
  121. // ConnRemoteAddr wraps the RemoteAddr operation, which returns a description
  122. // of the peer address of a connection. If a Conn also implements ConnRemoteAddr, this
  123. // description is used in log messages.
  124. type ConnRemoteAddr interface {
  125. RemoteAddr() string
  126. }
  127. // connWithRemoteAddr overrides the remote address of a connection.
  128. type connWithRemoteAddr struct {
  129. Conn
  130. addr string
  131. }
  132. func (c connWithRemoteAddr) RemoteAddr() string { return c.addr }
  133. // jsonCodec reads and writes JSON-RPC messages to the underlying connection. It also has
  134. // support for parsing arguments and serializing (result) objects.
  135. type jsonCodec struct {
  136. remoteAddr string
  137. closer sync.Once // close closed channel once
  138. closed chan interface{} // closed on Close
  139. decode func(v interface{}) error // decoder to allow multiple transports
  140. encMu sync.Mutex // guards the encoder
  141. encode func(v interface{}) error // encoder to allow multiple transports
  142. conn Conn
  143. }
  144. // NewCodec creates a new RPC server codec with support for JSON-RPC 2.0 based
  145. // on explicitly given encoding and decoding methods.
  146. func NewCodec(conn Conn, encode, decode func(v interface{}) error) ServerCodec {
  147. codec := &jsonCodec{
  148. closed: make(chan interface{}),
  149. encode: encode,
  150. decode: decode,
  151. conn: conn,
  152. }
  153. if ra, ok := conn.(ConnRemoteAddr); ok {
  154. codec.remoteAddr = ra.RemoteAddr()
  155. }
  156. return codec
  157. }
  158. // NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0.
  159. func NewJSONCodec(conn Conn) ServerCodec {
  160. enc := json.NewEncoder(conn)
  161. dec := json.NewDecoder(conn)
  162. dec.UseNumber()
  163. return NewCodec(conn, enc.Encode, dec.Decode)
  164. }
  165. func (c *jsonCodec) RemoteAddr() string {
  166. return c.remoteAddr
  167. }
  168. func (c *jsonCodec) Read() (msg []*jsonrpcMessage, batch bool, err error) {
  169. // Decode the next JSON object in the input stream.
  170. // This verifies basic syntax, etc.
  171. var rawmsg json.RawMessage
  172. if err := c.decode(&rawmsg); err != nil {
  173. return nil, false, err
  174. }
  175. msg, batch = parseMessage(rawmsg)
  176. return msg, batch, nil
  177. }
  178. // Write sends a message to client.
  179. func (c *jsonCodec) Write(ctx context.Context, v interface{}) error {
  180. c.encMu.Lock()
  181. defer c.encMu.Unlock()
  182. deadline, ok := ctx.Deadline()
  183. if !ok {
  184. deadline = time.Now().Add(defaultWriteTimeout)
  185. }
  186. c.conn.SetWriteDeadline(deadline)
  187. return c.encode(v)
  188. }
  189. // Close the underlying connection
  190. func (c *jsonCodec) Close() {
  191. c.closer.Do(func() {
  192. close(c.closed)
  193. c.conn.Close()
  194. })
  195. }
  196. // Closed returns a channel which will be closed when Close is called
  197. func (c *jsonCodec) Closed() <-chan interface{} {
  198. return c.closed
  199. }
  200. // parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
  201. // checks in this function because the raw message has already been syntax-checked when it
  202. // is called. Any non-JSON-RPC messages in the input return the zero value of
  203. // jsonrpcMessage.
  204. func parseMessage(raw json.RawMessage) ([]*jsonrpcMessage, bool) {
  205. if !isBatch(raw) {
  206. msgs := []*jsonrpcMessage{{}}
  207. json.Unmarshal(raw, &msgs[0])
  208. return msgs, false
  209. }
  210. dec := json.NewDecoder(bytes.NewReader(raw))
  211. dec.Token() // skip '['
  212. var msgs []*jsonrpcMessage
  213. for dec.More() {
  214. msgs = append(msgs, new(jsonrpcMessage))
  215. dec.Decode(&msgs[len(msgs)-1])
  216. }
  217. return msgs, true
  218. }
  219. // isBatch returns true when the first non-whitespace characters is '['
  220. func isBatch(raw json.RawMessage) bool {
  221. for _, c := range raw {
  222. // skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt)
  223. if c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d {
  224. continue
  225. }
  226. return c == '['
  227. }
  228. return false
  229. }
  230. // parsePositionalArguments tries to parse the given args to an array of values with the
  231. // given types. It returns the parsed values or an error when the args could not be
  232. // parsed. Missing optional arguments are returned as reflect.Zero values.
  233. func parsePositionalArguments(rawArgs json.RawMessage, types []reflect.Type) ([]reflect.Value, error) {
  234. dec := json.NewDecoder(bytes.NewReader(rawArgs))
  235. var args []reflect.Value
  236. tok, err := dec.Token()
  237. switch {
  238. case err == io.EOF || tok == nil && err == nil:
  239. // "params" is optional and may be empty. Also allow "params":null even though it's
  240. // not in the spec because our own client used to send it.
  241. case err != nil:
  242. return nil, err
  243. case tok == json.Delim('['):
  244. // Read argument array.
  245. if args, err = parseArgumentArray(dec, types); err != nil {
  246. return nil, err
  247. }
  248. default:
  249. return nil, errors.New("non-array args")
  250. }
  251. // Set any missing args to nil.
  252. for i := len(args); i < len(types); i++ {
  253. if types[i].Kind() != reflect.Ptr {
  254. return nil, fmt.Errorf("missing value for required argument %d", i)
  255. }
  256. args = append(args, reflect.Zero(types[i]))
  257. }
  258. return args, nil
  259. }
  260. func parseArgumentArray(dec *json.Decoder, types []reflect.Type) ([]reflect.Value, error) {
  261. args := make([]reflect.Value, 0, len(types))
  262. for i := 0; dec.More(); i++ {
  263. if i >= len(types) {
  264. return args, fmt.Errorf("too many arguments, want at most %d", len(types))
  265. }
  266. argval := reflect.New(types[i])
  267. if err := dec.Decode(argval.Interface()); err != nil {
  268. return args, fmt.Errorf("invalid argument %d: %v", i, err)
  269. }
  270. if argval.IsNil() && types[i].Kind() != reflect.Ptr {
  271. return args, fmt.Errorf("missing value for required argument %d", i)
  272. }
  273. args = append(args, argval.Elem())
  274. }
  275. // Read end of args array.
  276. _, err := dec.Token()
  277. return args, err
  278. }
  279. // parseSubscriptionName extracts the subscription name from an encoded argument array.
  280. func parseSubscriptionName(rawArgs json.RawMessage) (string, error) {
  281. dec := json.NewDecoder(bytes.NewReader(rawArgs))
  282. if tok, _ := dec.Token(); tok != json.Delim('[') {
  283. return "", errors.New("non-array args")
  284. }
  285. v, _ := dec.Token()
  286. method, ok := v.(string)
  287. if !ok {
  288. return "", errors.New("expected subscription name as first argument")
  289. }
  290. return method, nil
  291. }