-
Notifications
You must be signed in to change notification settings - Fork 43
/
transport.go
154 lines (138 loc) · 3.27 KB
/
transport.go
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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/gorilla/websocket"
)
// NewTransport creates a transport that supports the given endpoint. The
// endpoint is a URI with a scheme and an optional mode, for example
// "https+get://infura.io/".
func NewTransport(endpoint string, timeout time.Duration) (Transport, error) {
url, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
scheme, mode := url.Scheme, ""
if parts := strings.Split(scheme, "+"); len(parts) > 1 {
scheme, mode = parts[0], parts[1]
}
var t Transport
switch scheme {
case "http", "https":
url.Scheme = scheme
t = &httpTransport{
Client: http.Client{Timeout: timeout},
endpoint: url.String(),
contentType: "application/json",
bodyReader: func(body io.ReadCloser) ([]byte, error) {
defer body.Close()
return ioutil.ReadAll(body)
},
}
case "ws", "wss":
conn, _, err := websocket.DefaultDialer.Dial(url.String(), nil)
if err != nil {
return nil, fmt.Errorf("Got: %s when connecting to ws", err)
}
t = &websocketTransport{
ws: conn,
}
case "noop":
t = &noopTransport{}
default:
return nil, fmt.Errorf("unsupported transport: %s", scheme)
}
if mode == "" {
return t, nil
}
if modalTransport, ok := t.(Modal); ok {
return t, modalTransport.Mode(mode)
}
return nil, fmt.Errorf("transport is not modal: %s", scheme)
}
// Modal is a type of Transport that has multiple modes for interpreting the
// payloads sent to it. Not all transports support modes.
type Modal interface {
Mode(string) error
}
type Transport interface {
// TODO: Add context?
// TODO: Should this be: Do(Request) (Response, error)?
Send(body []byte) ([]byte, error)
}
type httpTransport struct {
http.Client
contentType string
endpoint string
getHost string
getPath string
bodyReader func(io.ReadCloser) ([]byte, error)
}
func (t *httpTransport) Mode(m string) error {
switch strings.ToLower(m) {
case "post":
t.getHost = ""
case "get":
url, err := url.Parse(t.endpoint)
if err != nil {
return err
}
t.getPath = url.Path
if t.getPath == "" {
t.getPath = "/"
}
url.Path = ""
t.getHost = url.String()
default:
return fmt.Errorf("invalid mode for http transport: %s", m)
}
return nil
}
func (t *httpTransport) Send(body []byte) ([]byte, error) {
var resp *http.Response
var err error
if t.getHost != "" {
url := t.getHost + path.Join(t.getPath, string(body))
resp, err = t.Client.Get(url)
} else {
resp, err = t.Client.Post(t.endpoint, t.contentType, bytes.NewReader(body))
}
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode)
}
if t.bodyReader == nil {
resp.Body.Close()
return nil, nil
}
// TODO: Avoid reading the whole body into memory
return t.bodyReader(resp.Body)
}
type websocketTransport struct {
ws *websocket.Conn
}
func (t *websocketTransport) Send(body []byte) ([]byte, error) {
err := t.ws.WriteMessage(websocket.TextMessage, body)
if err != nil {
return nil, err
}
_, message, err := t.ws.ReadMessage()
_ = message
if err != nil {
return nil, err
}
return nil, nil
}
type noopTransport struct{}
func (t *noopTransport) Send(body []byte) ([]byte, error) {
return nil, nil
}