server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.JSXEth, port int) (*RpcHttpServer, error) {
  26. sport := fmt.Sprintf(":%d", 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. }, nil
  37. }
  38. type RpcHttpServer struct {
  39. quit chan bool
  40. listener net.Listener
  41. pipe *xeth.JSXEth
  42. port int
  43. }
  44. func (s *RpcHttpServer) exitHandler() {
  45. out:
  46. for {
  47. select {
  48. case <-s.quit:
  49. s.listener.Close()
  50. break out
  51. }
  52. }
  53. rpchttplogger.Infoln("Shutdown RPC-HTTP server")
  54. }
  55. func (s *RpcHttpServer) Stop() {
  56. close(s.quit)
  57. }
  58. func (s *RpcHttpServer) Start() {
  59. rpchttplogger.Infof("Starting RPC-HTTP server on port %d", s.port)
  60. go s.exitHandler()
  61. api := rpc.NewEthereumApi(s.pipe)
  62. h := s.apiHandler(api)
  63. http.Handle("/", h)
  64. err := http.Serve(s.listener, nil)
  65. // FIX Complains on shutdown due to listner already being closed
  66. if err != nil {
  67. rpchttplogger.Errorln("Error on RPC-HTTP interface:", err)
  68. }
  69. }
  70. func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
  71. fn := func(w http.ResponseWriter, req *http.Request) {
  72. w.Header().Set("Access-Control-Allow-Origin", "*")
  73. rpchttplogger.Debugln("Handling request")
  74. reqParsed, reqerr := JSON.ParseRequestBody(req)
  75. if reqerr != nil {
  76. JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest})
  77. return
  78. }
  79. var response interface{}
  80. reserr := api.GetRequestReply(&reqParsed, &response)
  81. if reserr != nil {
  82. rpchttplogger.Errorln(reserr)
  83. JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
  84. return
  85. }
  86. rpchttplogger.Debugf("Generated response: %T %s", response, response)
  87. JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response})
  88. }
  89. return http.HandlerFunc(fn)
  90. }