aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/comms/http.go
blob: c165aa27e11693bfd9724c5bc054d7a2072cc731 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package comms

import (
    "encoding/json"
    "fmt"
    "net"
    "net/http"
    "strings"
    "sync"
    "time"

    "bytes"
    "io"
    "io/ioutil"

    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/logger/glog"
    "github.com/ethereum/go-ethereum/rpc/codec"
    "github.com/ethereum/go-ethereum/rpc/shared"
    "github.com/rs/cors"
)

const (
    serverIdleTimeout  = 10 * time.Second // idle keep-alive connections
    serverReadTimeout  = 15 * time.Second // per-request read timeout
    serverWriteTimeout = 15 * time.Second // per-request read timeout
)

var (
    httpServerMu sync.Mutex
    httpServer   *stopServer
)

type HttpConfig struct {
    ListenAddress string
    ListenPort    uint
    CorsDomain    string
}

// stopServer augments http.Server with idle connection tracking.
// Idle keep-alive connections are shut down when Close is called.
type stopServer struct {
    *http.Server
    l net.Listener
    // connection tracking state
    mu       sync.Mutex
    shutdown bool // true when Stop has returned
    idle     map[net.Conn]struct{}
}

type handler struct {
    codec codec.Codec
    api   shared.EthereumApi
}

// StartHTTP starts listening for RPC requests sent via HTTP.
func StartHttp(cfg HttpConfig, codec codec.Codec, api shared.EthereumApi) error {
    httpServerMu.Lock()
    defer httpServerMu.Unlock()

    addr := fmt.Sprintf("%s:%d", cfg.ListenAddress, cfg.ListenPort)
    if httpServer != nil {
        if addr != httpServer.Addr {
            return fmt.Errorf("RPC service already running on %s ", httpServer.Addr)
        }
        return nil // RPC service already running on given host/port
    }
    // Set up the request handler, wrapping it with CORS headers if configured.
    handler := http.Handler(&handler{codec, api})
    if len(cfg.CorsDomain) > 0 {
        opts := cors.Options{
            AllowedMethods: []string{"POST"},
            AllowedOrigins: strings.Split(cfg.CorsDomain, " "),
        }
        handler = cors.New(opts).Handler(handler)
    }
    // Start the server.
    s, err := listenHTTP(addr, handler)
    if err != nil {
        glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", cfg.ListenAddress, cfg.ListenPort, err)
        return err
    }
    httpServer = s
    return nil
}

func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "application/json")

    // Limit request size to resist DoS
    if req.ContentLength > maxHttpSizeReqLength {
        err := fmt.Errorf("Request too large")
        response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err)
        sendJSON(w, &response)
        return
    }

    defer req.Body.Close()
    payload, err := ioutil.ReadAll(req.Body)
    if err != nil {
        err := fmt.Errorf("Could not read request body")
        response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err)
        sendJSON(w, &response)
        return
    }

    c := h.codec.New(nil)
    var rpcReq shared.Request
    if err = c.Decode(payload, &rpcReq); err == nil {
        reply, err := h.api.Execute(&rpcReq)
        res := shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
        sendJSON(w, &res)
        return
    }

    var reqBatch []shared.Request
    if err = c.Decode(payload, &reqBatch); err == nil {
        resBatch := make([]*interface{}, len(reqBatch))
        resCount := 0
        for i, rpcReq := range reqBatch {
            reply, err := h.api.Execute(&rpcReq)
            if rpcReq.Id != nil { // this leaves nil entries in the response batch for later removal
                resBatch[i] = shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
                resCount += 1
            }
        }
        // make response omitting nil entries
        sendJSON(w, resBatch[:resCount])
        return
    }

    // invalid request
    err = fmt.Errorf("Could not decode request")
    res := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32600, err)
    sendJSON(w, res)
}

func sendJSON(w io.Writer, v interface{}) {
    if glog.V(logger.Detail) {
        if payload, err := json.MarshalIndent(v, "", "\t"); err == nil {
            glog.Infof("Sending payload: %s", payload)
        }
    }
    if err := json.NewEncoder(w).Encode(v); err != nil {
        glog.V(logger.Error).Infoln("Error sending JSON:", err)
    }
}

// Stop closes all active HTTP connections and shuts down the server.
func StopHttp() {
    httpServerMu.Lock()
    defer httpServerMu.Unlock()
    if httpServer != nil {
        httpServer.Close()
        httpServer = nil
    }
}

func listenHTTP(addr string, h http.Handler) (*stopServer, error) {
    l, err := net.Listen("tcp", addr)
    if err != nil {
        return nil, err
    }
    s := &stopServer{l: l, idle: make(map[net.Conn]struct{})}
    s.Server = &http.Server{
        Addr:         addr,
        Handler:      h,
        ReadTimeout:  serverReadTimeout,
        WriteTimeout: serverWriteTimeout,
        ConnState:    s.connState,
    }
    go s.Serve(l)
    return s, nil
}

func (s *stopServer) connState(c net.Conn, state http.ConnState) {
    s.mu.Lock()
    defer s.mu.Unlock()
    // Close c immediately if we're past shutdown.
    if s.shutdown {
        if state != http.StateClosed {
            c.Close()
        }
        return
    }
    if state == http.StateIdle {
        s.idle[c] = struct{}{}
    } else {
        delete(s.idle, c)
    }
}

func (s *stopServer) Close() {
    s.mu.Lock()
    defer s.mu.Unlock()
    // Shut down the acceptor. No new connections can be created.
    s.l.Close()
    // Drop all idle connections. Non-idle connections will be
    // closed by connState as soon as they become idle.
    s.shutdown = true
    for c := range s.idle {
        glog.V(logger.Detail).Infof("closing idle connection %v", c.RemoteAddr())
        c.Close()
        delete(s.idle, c)
    }
}

type httpClient struct {
    address string
    port    uint
    codec   codec.ApiCoder
    lastRes interface{}
    lastErr error
}

// Create a new in process client
func NewHttpClient(cfg HttpConfig, c codec.Codec) *httpClient {
    return &httpClient{
        address: cfg.ListenAddress,
        port:    cfg.ListenPort,
        codec:   c.New(nil),
    }
}

func (self *httpClient) Close() {
    // do nothing
}

func (self *httpClient) Send(req interface{}) error {
    var body []byte
    var err error

    self.lastRes = nil
    self.lastErr = nil

    if body, err = self.codec.Encode(req); err != nil {
        return err
    }

    httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s:%d", self.address, self.port), bytes.NewBuffer(body))
    if err != nil {
        return err
    }
    httpReq.Header.Set("Content-Type", "application/json")

    client := http.Client{}
    resp, err := client.Do(httpReq)
    if err != nil {
        return err
    }

    defer resp.Body.Close()

    if resp.Status == "200 OK" {
        reply, _ := ioutil.ReadAll(resp.Body)
        var rpcSuccessResponse shared.SuccessResponse
        if err = self.codec.Decode(reply, &rpcSuccessResponse); err == nil {
            self.lastRes = rpcSuccessResponse.Result
            self.lastErr = err
            return nil
        } else {
            var rpcErrorResponse shared.ErrorResponse
            if err = self.codec.Decode(reply, &rpcErrorResponse); err == nil {
                self.lastRes = rpcErrorResponse.Error
                self.lastErr = err
                return nil
            } else {
                return err
            }
        }
    }

    return fmt.Errorf("Not implemented")
}

func (self *httpClient) Recv() (interface{}, error) {
    return self.lastRes, self.lastErr
}

func (self *httpClient) SupportedModules() (map[string]string, error) {
    var body []byte
    var err error

    payload := shared.Request{
        Id:      1,
        Jsonrpc: "2.0",
        Method:  "modules",
    }

    if body, err = self.codec.Encode(payload); err != nil {
        return nil, err
    }

    req, err := http.NewRequest("POST", fmt.Sprintf("%s:%d", self.address, self.port), bytes.NewBuffer(body))
    if err != nil {
        return nil, err
    }
    req.Header.Set("Content-Type", "application/json")

    client := http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return nil, err
    }

    defer resp.Body.Close()

    if resp.Status == "200 OK" {
        reply, _ := ioutil.ReadAll(resp.Body)
        var rpcRes shared.SuccessResponse
        if err = self.codec.Decode(reply, &rpcRes); err != nil {
            return nil, err
        }

        result := make(map[string]string)
        if modules, ok := rpcRes.Result.(map[string]interface{}); ok {
            for a, v := range modules {
                result[a] = fmt.Sprintf("%s", v)
            }
            return result, nil
        }
        err = fmt.Errorf("Unable to parse module response - %v", rpcRes.Result)
    } else {
        fmt.Printf("resp.Status = %s\n", resp.Status)
        fmt.Printf("err = %v\n", err)
    }

    return nil, err
}