aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/comms/http.go
blob: ebee791bd1cdaa78fd1ab9d85f2d2b96e7eb7e07 (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
package comms

import (
    "fmt"
    "net/http"
    "strings"

    "bytes"
    "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"
)

var (
    // main HTTP rpc listener
    httpListener         *stoppableTCPListener
    listenerStoppedError = fmt.Errorf("Listener has stopped")
)

type HttpConfig struct {
    ListenAddress string
    ListenPort    uint
    CorsDomain    string
}

func StartHttp(cfg HttpConfig, codec codec.Codec, api shared.EthereumApi) error {
    if httpListener != nil {
        if fmt.Sprintf("%s:%d", cfg.ListenAddress, cfg.ListenPort) != httpListener.Addr().String() {
            return fmt.Errorf("RPC service already running on %s ", httpListener.Addr().String())
        }
        return nil // RPC service already running on given host/port
    }

    l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", cfg.ListenAddress, cfg.ListenPort))
    if err != nil {
        glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", cfg.ListenAddress, cfg.ListenPort, err)
        return err
    }
    httpListener = l

    var handler http.Handler
    if len(cfg.CorsDomain) > 0 {
        var opts cors.Options
        opts.AllowedMethods = []string{"POST"}
        opts.AllowedOrigins = strings.Split(cfg.CorsDomain, " ")

        c := cors.New(opts)
        handler = newStoppableHandler(c.Handler(gethHttpHandler(codec, api)), l.stop)
    } else {
        handler = newStoppableHandler(gethHttpHandler(codec, api), l.stop)
    }

    go http.Serve(l, handler)

    return nil
}

func StopHttp() {
    if httpListener != nil {
        httpListener.Stop()
        httpListener = nil
    }
}

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
}