You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
5.2 KiB
191 lines
5.2 KiB
9 years ago
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||
|
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
9 years ago
|
package json2
|
||
9 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gorilla/rpc/v2"
|
||
|
)
|
||
|
|
||
|
var null = json.RawMessage([]byte("null"))
|
||
9 years ago
|
var Version = "2.0"
|
||
9 years ago
|
|
||
|
// ----------------------------------------------------------------------------
|
||
|
// Request and Response
|
||
|
// ----------------------------------------------------------------------------
|
||
|
|
||
|
// serverRequest represents a JSON-RPC request received by the server.
|
||
|
type serverRequest struct {
|
||
9 years ago
|
// JSON-RPC protocol.
|
||
|
Version string `json:"jsonrpc"`
|
||
|
|
||
9 years ago
|
// A String containing the name of the method to be invoked.
|
||
|
Method string `json:"method"`
|
||
9 years ago
|
|
||
|
// A Structured value to pass as arguments to the method.
|
||
9 years ago
|
Params *json.RawMessage `json:"params"`
|
||
9 years ago
|
|
||
|
// The request id. MUST be a string, number or null.
|
||
|
// Our implementation will not do type checking for id.
|
||
|
// It will be copied as it is.
|
||
9 years ago
|
Id *json.RawMessage `json:"id"`
|
||
|
}
|
||
|
|
||
|
// serverResponse represents a JSON-RPC response returned by the server.
|
||
|
type serverResponse struct {
|
||
9 years ago
|
// JSON-RPC protocol.
|
||
|
Version string `json:"jsonrpc"`
|
||
|
|
||
9 years ago
|
// The Object that was returned by the invoked method. This must be null
|
||
|
// in case there was an error invoking the method.
|
||
9 years ago
|
// As per spec the member will be omitted if there was an error.
|
||
|
Result interface{} `json:"result,omitempty"`
|
||
|
|
||
9 years ago
|
// An Error object if there was an error invoking the method. It must be
|
||
|
// null if there was no error.
|
||
9 years ago
|
// As per spec the member will be omitted if there was no error.
|
||
|
Error *Error `json:"error,omitempty"`
|
||
|
|
||
9 years ago
|
// This must be the same id as the request it is responding to.
|
||
|
Id *json.RawMessage `json:"id"`
|
||
|
}
|
||
|
|
||
|
// ----------------------------------------------------------------------------
|
||
|
// Codec
|
||
|
// ----------------------------------------------------------------------------
|
||
|
|
||
9 years ago
|
// NewcustomCodec returns a new JSON Codec based on passed encoder selector.
|
||
|
func NewCustomCodec(encSel rpc.EncoderSelector) *Codec {
|
||
|
return &Codec{encSel: encSel}
|
||
|
}
|
||
|
|
||
9 years ago
|
// NewCodec returns a new JSON Codec.
|
||
|
func NewCodec() *Codec {
|
||
9 years ago
|
return NewCustomCodec(rpc.DefaultEncoderSelector)
|
||
9 years ago
|
}
|
||
|
|
||
|
// Codec creates a CodecRequest to process each request.
|
||
|
type Codec struct {
|
||
9 years ago
|
encSel rpc.EncoderSelector
|
||
9 years ago
|
}
|
||
|
|
||
|
// NewRequest returns a CodecRequest.
|
||
|
func (c *Codec) NewRequest(r *http.Request) rpc.CodecRequest {
|
||
9 years ago
|
return newCodecRequest(r, c.encSel.Select(r))
|
||
9 years ago
|
}
|
||
|
|
||
|
// ----------------------------------------------------------------------------
|
||
|
// CodecRequest
|
||
|
// ----------------------------------------------------------------------------
|
||
|
|
||
|
// newCodecRequest returns a new CodecRequest.
|
||
9 years ago
|
func newCodecRequest(r *http.Request, encoder rpc.Encoder) rpc.CodecRequest {
|
||
9 years ago
|
// Decode the request body and check if RPC method is valid.
|
||
|
req := new(serverRequest)
|
||
|
err := json.NewDecoder(r.Body).Decode(req)
|
||
9 years ago
|
if err != nil {
|
||
|
err = &Error{
|
||
|
Code: E_PARSE,
|
||
|
Message: err.Error(),
|
||
|
Data: req,
|
||
|
}
|
||
|
}
|
||
|
if req.Version != Version {
|
||
|
err = &Error{
|
||
|
Code: E_INVALID_REQ,
|
||
|
Message: "jsonrpc must be " + Version,
|
||
|
Data: req,
|
||
|
}
|
||
|
}
|
||
9 years ago
|
r.Body.Close()
|
||
9 years ago
|
return &CodecRequest{request: req, err: err, encoder: encoder}
|
||
9 years ago
|
}
|
||
|
|
||
|
// CodecRequest decodes and encodes a single request.
|
||
|
type CodecRequest struct {
|
||
|
request *serverRequest
|
||
|
err error
|
||
9 years ago
|
encoder rpc.Encoder
|
||
9 years ago
|
}
|
||
|
|
||
|
// Method returns the RPC method for the current request.
|
||
|
//
|
||
|
// The method uses a dotted notation as in "Service.Method".
|
||
|
func (c *CodecRequest) Method() (string, error) {
|
||
|
if c.err == nil {
|
||
|
return c.request.Method, nil
|
||
|
}
|
||
|
return "", c.err
|
||
|
}
|
||
|
|
||
9 years ago
|
// ReadRe<quest fills the request object for the RPC method.
|
||
9 years ago
|
func (c *CodecRequest) ReadRequest(args interface{}) error {
|
||
|
if c.err == nil {
|
||
|
if c.request.Params != nil {
|
||
9 years ago
|
// JSON params structured object. Unmarshal to the args object.
|
||
|
err := json.Unmarshal(*c.request.Params, args)
|
||
|
if err != nil {
|
||
|
c.err = &Error{
|
||
|
Code: E_INVALID_REQ,
|
||
|
Message: err.Error(),
|
||
|
Data: c.request.Params,
|
||
|
}
|
||
|
}
|
||
9 years ago
|
} else {
|
||
9 years ago
|
c.err = &Error{
|
||
|
Code: E_INVALID_REQ,
|
||
|
Message: "rpc: method request ill-formed: missing params field",
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
|
return c.err
|
||
|
}
|
||
|
|
||
|
// WriteResponse encodes the response and writes it to the ResponseWriter.
|
||
|
func (c *CodecRequest) WriteResponse(w http.ResponseWriter, reply interface{}) {
|
||
9 years ago
|
res := &serverResponse{
|
||
|
Version: Version,
|
||
|
Result: reply,
|
||
|
Id: c.request.Id,
|
||
9 years ago
|
}
|
||
9 years ago
|
c.writeServerResponse(w, res)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func (c *CodecRequest) WriteError(w http.ResponseWriter, status int, err error) {
|
||
|
jsonErr, ok := err.(*Error)
|
||
|
if !ok {
|
||
|
jsonErr = &Error{
|
||
|
Code: E_SERVER,
|
||
|
Message: err.Error(),
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
res := &serverResponse{
|
||
|
Version: Version,
|
||
|
Error: jsonErr,
|
||
|
Id: c.request.Id,
|
||
9 years ago
|
}
|
||
9 years ago
|
c.writeServerResponse(w, res)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func (c *CodecRequest) writeServerResponse(w http.ResponseWriter, res *serverResponse) {
|
||
|
// Id is null for notifications and they don't have a response.
|
||
|
if c.request.Id != nil {
|
||
9 years ago
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
9 years ago
|
encoder := json.NewEncoder(c.encoder.Encode(w))
|
||
|
err := encoder.Encode(res)
|
||
|
|
||
9 years ago
|
// Not sure in which case will this happen. But seems harmless.
|
||
9 years ago
|
if err != nil {
|
||
|
rpc.WriteError(w, 400, err.Error())
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
type EmptyResponse struct {
|
||
|
}
|