http.go 3.0 KB
Newer Older
1 2 3
package rpc

import (
T
Taylor Gerring 已提交
4
	"encoding/json"
T
Taylor Gerring 已提交
5
	"io"
T
Taylor Gerring 已提交
6
	"io/ioutil"
7 8 9 10 11 12 13 14
	"net/http"

	"github.com/ethereum/go-ethereum/logger"
	"github.com/ethereum/go-ethereum/xeth"
)

var rpchttplogger = logger.NewLogger("RPC-HTTP")

15 16 17 18 19
const (
	jsonrpcver       = "2.0"
	maxSizeReqLength = 1024 * 1024 // 1MB
)

20 21 22 23 24
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
	api := NewEthereumApi(pipe, dataDir)

	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
T
Taylor Gerring 已提交
25
		// TODO this needs to be configurable
26 27
		w.Header().Set("Access-Control-Allow-Origin", "*")

T
Taylor Gerring 已提交
28
		// Limit request size to resist DoS
29
		if req.ContentLength > maxSizeReqLength {
T
Taylor Gerring 已提交
30
			jsonerr := &RpcErrorObject{-32700, "Request too large"}
T
Taylor Gerring 已提交
31
			Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
32 33 34
			return
		}

T
Taylor Gerring 已提交
35
		// Read request body
T
Taylor Gerring 已提交
36 37 38 39
		defer req.Body.Close()
		body, err := ioutil.ReadAll(req.Body)
		if err != nil {
			jsonerr := &RpcErrorObject{-32700, "Could not read request body"}
T
Taylor Gerring 已提交
40
			Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
41 42
		}

T
Taylor Gerring 已提交
43 44 45 46
		// Try to parse the request as a single
		var reqSingle RpcRequest
		if err := json.Unmarshal(body, &reqSingle); err == nil {
			response := RpcResponse(api, &reqSingle)
T
Taylor Gerring 已提交
47
			Send(w, &response)
T
Taylor Gerring 已提交
48
			return
T
Taylor Gerring 已提交
49 50 51 52 53 54 55 56 57 58 59
		}

		// Try to parse the request to batch
		var reqBatch []RpcRequest
		if err := json.Unmarshal(body, &reqBatch); err == nil {
			// Build response batch
			resBatch := make([]*interface{}, len(reqBatch))
			for i, request := range reqBatch {
				response := RpcResponse(api, &request)
				resBatch[i] = response
			}
T
Taylor Gerring 已提交
60
			Send(w, resBatch)
61 62 63
			return
		}

T
Taylor Gerring 已提交
64 65
		// Not a batch or single request, error
		jsonerr := &RpcErrorObject{-32600, "Could not decode request"}
T
Taylor Gerring 已提交
66
		Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
67 68
	})
}
T
Taylor Gerring 已提交
69 70 71 72 73 74

func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} {
	var reply, response interface{}
	reserr := api.GetRequestReply(request, &reply)
	switch reserr.(type) {
	case nil:
T
Taylor Gerring 已提交
75
		response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply}
T
Taylor Gerring 已提交
76 77
	case *NotImplementedError:
		jsonerr := &RpcErrorObject{-32601, reserr.Error()}
T
Taylor Gerring 已提交
78
		response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
T
Taylor Gerring 已提交
79 80
	case *DecodeParamError, *InsufficientParamsError, *ValidationError:
		jsonerr := &RpcErrorObject{-32602, reserr.Error()}
T
Taylor Gerring 已提交
81
		response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
T
Taylor Gerring 已提交
82 83
	default:
		jsonerr := &RpcErrorObject{-32603, reserr.Error()}
T
Taylor Gerring 已提交
84
		response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
T
Taylor Gerring 已提交
85 86 87 88 89
	}

	rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
	return &response
}
T
Taylor Gerring 已提交
90 91 92 93 94 95 96 97 98 99 100 101

func Send(writer io.Writer, v interface{}) (n int, err error) {
	var payload []byte
	payload, err = json.MarshalIndent(v, "", "\t")
	if err != nil {
		rpclogger.Fatalln("Error marshalling JSON", err)
		return 0, err
	}
	rpclogger.DebugDetailf("Sending payload: %s", payload)

	return writer.Write(payload)
}