server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package rpchttp
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "github.com/ethereum/go-ethereum/logger"
  20. "github.com/ethereum/go-ethereum/rpc"
  21. "github.com/ethereum/go-ethereum/xeth"
  22. )
  23. var rpchttplogger = logger.NewLogger("RPC-HTTP")
  24. var JSON rpc.JsonWrapper
  25. func NewRpcHttpServer(pipe *xeth.XEth, address string, port int) (*RpcHttpServer, error) {
  26. sport := fmt.Sprintf("%s:%d", address, port)
  27. l, err := net.Listen("tcp", sport)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &RpcHttpServer{
  32. listener: l,
  33. quit: make(chan bool),
  34. pipe: pipe,
  35. port: port,
  36. addr: address,
  37. }, nil
  38. }
  39. type RpcHttpServer struct {
  40. quit chan bool
  41. listener net.Listener
  42. pipe *xeth.XEth
  43. port int
  44. addr string
  45. }
  46. func (s *RpcHttpServer) exitHandler() {
  47. out:
  48. for {
  49. select {
  50. case <-s.quit:
  51. s.listener.Close()
  52. break out
  53. }
  54. }
  55. rpchttplogger.Infoln("Shutdown RPC-HTTP server")
  56. }
  57. func (s *RpcHttpServer) Stop() {
  58. close(s.quit)
  59. }
  60. func (s *RpcHttpServer) Start() {
  61. rpchttplogger.Infof("Starting RPC-HTTP server on %s:%d", s.addr, s.port)
  62. go s.exitHandler()
  63. api := rpc.NewEthereumApi(s.pipe)
  64. h := s.apiHandler(api)
  65. http.Handle("/", h)
  66. err := http.Serve(s.listener, nil)
  67. // FIX Complains on shutdown due to listner already being closed
  68. if err != nil {
  69. rpchttplogger.Errorln("Error on RPC-HTTP interface:", err)
  70. }
  71. }
  72. func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
  73. var jsonrpcver string = "2.0"
  74. fn := func(w http.ResponseWriter, req *http.Request) {
  75. w.Header().Set("Access-Control-Allow-Origin", "*")
  76. rpchttplogger.DebugDetailln("Handling request")
  77. reqParsed, reqerr := JSON.ParseRequestBody(req)
  78. if reqerr != nil {
  79. jsonerr := &rpc.RpcErrorObject{-32700, "Error: Could not parse request"}
  80. JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
  81. return
  82. }
  83. var response interface{}
  84. reserr := api.GetRequestReply(&reqParsed, &response)
  85. if reserr != nil {
  86. rpchttplogger.Warnln(reserr)
  87. jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()}
  88. JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
  89. return
  90. }
  91. rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
  92. JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
  93. }
  94. return http.HandlerFunc(fn)
  95. }