aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/http.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-03-10 18:04:11 +0800
committerobscuren <geffobscura@gmail.com>2015-03-10 18:04:11 +0800
commit05c9351659e0482aeb29f80903286aefc671f0c5 (patch)
tree56132f55a3f826ce852c5d053c7e8984932e7878 /rpc/http.go
parent0db4a0e898d09ffa7b6b1289e9a334edc0001cfa (diff)
parent80985f97da8174576ee227909035a364af2fd6c9 (diff)
downloadgo-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.tar
go-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.tar.gz
go-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.tar.bz2
go-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.tar.lz
go-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.tar.xz
go-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.tar.zst
go-tangerine-05c9351659e0482aeb29f80903286aefc671f0c5.zip
Merge branch 'accounts-integration' of https://github.com/fjl/go-ethereum into fjl-accounts-integration
Diffstat (limited to 'rpc/http.go')
-rw-r--r--rpc/http.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/rpc/http.go b/rpc/http.go
new file mode 100644
index 000000000..857cf3221
--- /dev/null
+++ b/rpc/http.go
@@ -0,0 +1,52 @@
+package rpc
+
+import (
+ "net/http"
+
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/xeth"
+)
+
+var rpchttplogger = logger.NewLogger("RPC-HTTP")
+
+const (
+ jsonrpcver = "2.0"
+ maxSizeReqLength = 1024 * 1024 // 1MB
+)
+
+// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
+func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
+ var json JsonWrapper
+ api := NewEthereumApi(pipe, dataDir)
+
+ return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+
+ rpchttplogger.DebugDetailln("Handling request")
+
+ if req.ContentLength > maxSizeReqLength {
+ jsonerr := &RpcErrorObject{-32700, "Error: Request too large"}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
+ return
+ }
+
+ reqParsed, reqerr := json.ParseRequestBody(req)
+ if reqerr != nil {
+ jsonerr := &RpcErrorObject{-32700, "Error: Could not parse request"}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
+ return
+ }
+
+ var response interface{}
+ reserr := api.GetRequestReply(&reqParsed, &response)
+ if reserr != nil {
+ rpchttplogger.Warnln(reserr)
+ jsonerr := &RpcErrorObject{-32603, reserr.Error()}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
+ return
+ }
+
+ rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
+ json.Send(w, &RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
+ })
+}