json.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. type deadlineCloser interface {
  122. io.Closer
  123. SetWriteDeadline(time.Time) error
  124. }
  125. // ConnRemoteAddr wraps the RemoteAddr operation, which returns a description
  126. // of the peer address of a connection. If a Conn also implements ConnRemoteAddr, this
  127. // description is used in log messages.
  128. type ConnRemoteAddr interface {
  129. RemoteAddr() string
  130. }
  131. // connWithRemoteAddr overrides the remote address of a connection.
  132. type connWithRemoteAddr struct {
  133. Conn
  134. addr string
  135. }
  136. func (c connWithRemoteAddr) RemoteAddr() string { return c.addr }
  137. // jsonCodec reads and writes JSON-RPC messages to the underlying connection. It also has
  138. // support for parsing arguments and serializing (result) objects.
  139. type jsonCodec struct {
  140. remoteAddr string
  141. closer sync.Once // close closed channel once
  142. closed chan interface{} // closed on Close
  143. decode func(v interface{}) error // decoder to allow multiple transports
  144. encMu sync.Mutex // guards the encoder
  145. encode func(v interface{}) error // encoder to allow multiple transports
  146. conn deadlineCloser
  147. }
  148. func newCodec(conn deadlineCloser, encode, decode func(v interface{}) error) ServerCodec {
  149. codec := &jsonCodec{
  150. closed: make(chan interface{}),
  151. encode: encode,
  152. decode: decode,
  153. conn: conn,
  154. }
  155. if ra, ok := conn.(ConnRemoteAddr); ok {
  156. codec.remoteAddr = ra.RemoteAddr()
  157. }
  158. return codec
  159. }
  160. // NewJSONCodec creates a codec that reads from the given connection. If conn implements
  161. // ConnRemoteAddr, log messages will use it to include the remote address of the
  162. // connection.
  163. func NewJSONCodec(conn Conn) ServerCodec {
  164. enc := json.NewEncoder(conn)
  165. dec := json.NewDecoder(conn)
  166. dec.UseNumber()
  167. return newCodec(conn, enc.Encode, dec.Decode)
  168. }
  169. func (c *jsonCodec) RemoteAddr() string {
  170. return c.remoteAddr
  171. }
  172. func (c *jsonCodec) Read() (msg []*jsonrpcMessage, batch bool, err error) {
  173. // Decode the next JSON object in the input stream.
  174. // This verifies basic syntax, etc.
  175. var rawmsg json.RawMessage
  176. if err := c.decode(&rawmsg); err != nil {
  177. return nil, false, err
  178. }
  179. msg, batch = parseMessage(rawmsg)
  180. return msg, batch, nil
  181. }
  182. // Write sends a message to client.
  183. func (c *jsonCodec) Write(ctx context.Context, v interface{}) error {
  184. c.encMu.Lock()
  185. defer c.encMu.Unlock()
  186. deadline, ok := ctx.Deadline()
  187. if !ok {
  188. deadline = time.Now().Add(defaultWriteTimeout)
  189. }
  190. c.conn.SetWriteDeadline(deadline)
  191. return c.encode(v)
  192. }
  193. // Close the underlying connection
  194. func (c *jsonCodec) Close() {
  195. c.closer.Do(func() {
  196. close(c.closed)
  197. c.conn.Close()
  198. })
  199. }
  200. // Closed returns a channel which will be closed when Close is called
  201. func (c *jsonCodec) Closed() <-chan interface{} {
  202. return c.closed
  203. }
  204. // parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
  205. // checks in this function because the raw message has already been syntax-checked when it
  206. // is called. Any non-JSON-RPC messages in the input return the zero value of
  207. // jsonrpcMessage.
  208. func parseMessage(raw json.RawMessage) ([]*jsonrpcMessage, bool) {
  209. if !isBatch(raw) {
  210. msgs := []*jsonrpcMessage{{}}
  211. json.Unmarshal(raw, &msgs[0])
  212. return msgs, false
  213. }
  214. dec := json.NewDecoder(bytes.NewReader(raw))
  215. dec.Token() // skip '['
  216. var msgs []*jsonrpcMessage
  217. for dec.More() {
  218. msgs = append(msgs, new(jsonrpcMessage))
  219. dec.Decode(&msgs[len(msgs)-1])
  220. }
  221. return msgs, true
  222. }
  223. // isBatch returns true when the first non-whitespace characters is '['
  224. func isBatch(raw json.RawMessage) bool {
  225. for _, c := range raw {
  226. // skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt)
  227. if c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d {
  228. continue
  229. }
  230. return c == '['
  231. }
  232. return false
  233. }
  234. // parsePositionalArguments tries to parse the given args to an array of values with the
  235. // given types. It returns the parsed values or an error when the args could not be
  236. // parsed. Missing optional arguments are returned as reflect.Zero values.
  237. func parsePositionalArguments(rawArgs json.RawMessage, types []reflect.Type) ([]reflect.Value, error) {
  238. dec := json.NewDecoder(bytes.NewReader(rawArgs))
  239. var args []reflect.Value
  240. tok, err := dec.Token()
  241. switch {
  242. case err == io.EOF || tok == nil && err == nil:
  243. // "params" is optional and may be empty. Also allow "params":null even though it's
  244. // not in the spec because our own client used to send it.
  245. case err != nil:
  246. return nil, err
  247. case tok == json.Delim('['):
  248. // Read argument array.
  249. if args, err = parseArgumentArray(dec, types); err != nil {
  250. return nil, err
  251. }
  252. default:
  253. return nil, errors.New("non-array args")
  254. }
  255. // Set any missing args to nil.
  256. for i := len(args); i < len(types); i++ {
  257. if types[i].Kind() != reflect.Ptr {
  258. return nil, fmt.Errorf("missing value for required argument %d", i)
  259. }
  260. args = append(args, reflect.Zero(types[i]))
  261. }
  262. return args, nil
  263. }
  264. func parseArgumentArray(dec *json.Decoder, types []reflect.Type) ([]reflect.Value, error) {
  265. args := make([]reflect.Value, 0, len(types))
  266. for i := 0; dec.More(); i++ {
  267. if i >= len(types) {
  268. return args, fmt.Errorf("too many arguments, want at most %d", len(types))
  269. }
  270. argval := reflect.New(types[i])
  271. if err := dec.Decode(argval.Interface()); err != nil {
  272. return args, fmt.Errorf("invalid argument %d: %v", i, err)
  273. }
  274. if argval.IsNil() && types[i].Kind() != reflect.Ptr {
  275. return args, fmt.Errorf("missing value for required argument %d", i)
  276. }
  277. args = append(args, argval.Elem())
  278. }
  279. // Read end of args array.
  280. _, err := dec.Token()
  281. return args, err
  282. }
  283. // parseSubscriptionName extracts the subscription name from an encoded argument array.
  284. func parseSubscriptionName(rawArgs json.RawMessage) (string, error) {
  285. dec := json.NewDecoder(bytes.NewReader(rawArgs))
  286. if tok, _ := dec.Token(); tok != json.Delim('[') {
  287. return "", errors.New("non-array args")
  288. }
  289. v, _ := dec.Token()
  290. method, ok := v.(string)
  291. if !ok {
  292. return "", errors.New("expected subscription name as first argument")
  293. }
  294. return method, nil
  295. }