-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpconnection.go
201 lines (167 loc) · 4.88 KB
/
httpconnection.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
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
package signalr
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"nhooyr.io/websocket"
)
// Doer is the *http.Client interface
type Doer interface {
Do(req *http.Request) (*http.Response, error)
}
type httpConnection struct {
client Doer
headers func() http.Header
transports []TransportType
}
// WithHTTPClient sets the http client used to connect to the signalR server.
// The client is only used for http requests. It is not used for the websocket connection.
func WithHTTPClient(client Doer) func(*httpConnection) error {
return func(c *httpConnection) error {
c.client = client
return nil
}
}
// WithHTTPHeaders sets the function for providing request headers for HTTP and websocket requests
func WithHTTPHeaders(headers func() http.Header) func(*httpConnection) error {
return func(c *httpConnection) error {
c.headers = headers
return nil
}
}
func WithTransports(transports ...TransportType) func(*httpConnection) error {
return func(c *httpConnection) error {
for _, transport := range transports {
switch transport {
case TransportWebSockets, TransportServerSentEvents:
// Supported
default:
return fmt.Errorf("unsupported transport %s", transport)
}
}
c.transports = transports
return nil
}
}
// NewHTTPConnection creates a signalR HTTP Connection for usage with a Client.
// ctx can be used to cancel the SignalR negotiation during the creation of the Connection
// but not the Connection itself.
func NewHTTPConnection(ctx context.Context, address string, options ...func(*httpConnection) error) (Connection, error) {
httpConn := &httpConnection{}
for _, option := range options {
if option != nil {
if err := option(httpConn); err != nil {
return nil, err
}
}
}
if httpConn.client == nil {
httpConn.client = http.DefaultClient
}
if len(httpConn.transports) == 0 {
httpConn.transports = []TransportType{TransportWebSockets, TransportServerSentEvents}
}
reqURL, err := url.Parse(address)
if err != nil {
return nil, err
}
negotiateURL := *reqURL
negotiateURL.Path = path.Join(negotiateURL.Path, "negotiate")
req, err := http.NewRequestWithContext(ctx, "POST", negotiateURL.String(), nil)
if err != nil {
return nil, err
}
if httpConn.headers != nil {
req.Header = httpConn.headers()
}
resp, err := httpConn.client.Do(req)
if err != nil {
return nil, err
}
defer func() { closeResponseBody(resp.Body) }()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%v %v -> %v", req.Method, req.URL.String(), resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
negotiateResponse := negotiateResponse{}
if err := json.Unmarshal(body, &negotiateResponse); err != nil {
return nil, err
}
q := reqURL.Query()
switch negotiateResponse.NegotiateVersion {
case 0:
q.Set("id", negotiateResponse.ConnectionID)
case 1:
q.Set("id", negotiateResponse.ConnectionToken)
}
reqURL.RawQuery = q.Encode()
// Select the best connection
var conn Connection
switch {
case negotiateResponse.hasTransport("WebTransports"):
// TODO
case httpConn.hasTransport(TransportWebSockets) && negotiateResponse.hasTransport(TransportWebSockets):
wsURL := reqURL
// switch to wss for secure connection
if reqURL.Scheme == "https" {
wsURL.Scheme = "wss"
} else {
wsURL.Scheme = "ws"
}
opts := &websocket.DialOptions{}
if httpConn.headers != nil {
opts.HTTPHeader = httpConn.headers()
} else {
opts.HTTPHeader = http.Header{}
}
for _, cookie := range resp.Cookies() {
opts.HTTPHeader.Add("Cookie", cookie.String())
}
ws, _, err := websocket.Dial(ctx, wsURL.String(), opts)
if err != nil {
return nil, err
}
// TODO think about if the API should give the possibility to cancel this connection
conn = newWebSocketConnection(context.Background(), negotiateResponse.ConnectionID, ws)
case httpConn.hasTransport(TransportServerSentEvents) && negotiateResponse.hasTransport(TransportServerSentEvents):
req, err := http.NewRequest("GET", reqURL.String(), nil)
if err != nil {
return nil, err
}
if httpConn.headers != nil {
req.Header = httpConn.headers()
}
req.Header.Set("Accept", "text/event-stream")
resp, err := httpConn.client.Do(req)
if err != nil {
return nil, err
}
conn, err = newClientSSEConnection(address, negotiateResponse.ConnectionID, resp.Body)
if err != nil {
return nil, err
}
}
return conn, nil
}
// closeResponseBody reads a http response body to the end and closes it
// See https://blog.cubieserver.de/2022/http-connection-reuse-in-go-clients/
// The body needs to be fully read and closed, otherwise the connection will not be reused
func closeResponseBody(body io.ReadCloser) {
_, _ = io.Copy(io.Discard, body)
_ = body.Close()
}
func (h *httpConnection) hasTransport(transport TransportType) bool {
for _, t := range h.transports {
if transport == t {
return true
}
}
return false
}