blob: 962c06a391c237b6a7cdc6766e7cf5541b96bd8b [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package websocket
6
7import (
khenaidoo59ce9dd2019-11-11 13:05:32 -05008 "bytes"
khenaidoo26721882021-08-11 17:42:52 -04009 "context"
khenaidoo59ce9dd2019-11-11 13:05:32 -050010 "crypto/tls"
khenaidoo59ce9dd2019-11-11 13:05:32 -050011 "errors"
12 "io"
13 "io/ioutil"
14 "net"
15 "net/http"
khenaidoo26721882021-08-11 17:42:52 -040016 "net/http/httptrace"
khenaidoo59ce9dd2019-11-11 13:05:32 -050017 "net/url"
18 "strings"
19 "time"
20)
21
22// ErrBadHandshake is returned when the server response to opening handshake is
23// invalid.
24var ErrBadHandshake = errors.New("websocket: bad handshake")
25
26var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
27
28// NewClient creates a new client connection using the given net connection.
29// The URL u specifies the host and request URI. Use requestHeader to specify
30// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
31// (Cookie). Use the response.Header to get the selected subprotocol
32// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
33//
34// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
35// non-nil *http.Response so that callers can handle redirects, authentication,
36// etc.
37//
38// Deprecated: Use Dialer instead.
39func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
40 d := Dialer{
41 ReadBufferSize: readBufSize,
42 WriteBufferSize: writeBufSize,
43 NetDial: func(net, addr string) (net.Conn, error) {
44 return netConn, nil
45 },
46 }
47 return d.Dial(u.String(), requestHeader)
48}
49
50// A Dialer contains options for connecting to WebSocket server.
51type Dialer struct {
52 // NetDial specifies the dial function for creating TCP connections. If
53 // NetDial is nil, net.Dial is used.
54 NetDial func(network, addr string) (net.Conn, error)
55
khenaidoo26721882021-08-11 17:42:52 -040056 // NetDialContext specifies the dial function for creating TCP connections. If
57 // NetDialContext is nil, net.DialContext is used.
58 NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
59
khenaidoo59ce9dd2019-11-11 13:05:32 -050060 // Proxy specifies a function to return a proxy for a given
61 // Request. If the function returns a non-nil error, the
62 // request is aborted with the provided error.
63 // If Proxy is nil or returns a nil *URL, no proxy is used.
64 Proxy func(*http.Request) (*url.URL, error)
65
66 // TLSClientConfig specifies the TLS configuration to use with tls.Client.
67 // If nil, the default configuration is used.
68 TLSClientConfig *tls.Config
69
70 // HandshakeTimeout specifies the duration for the handshake to complete.
71 HandshakeTimeout time.Duration
72
khenaidoo26721882021-08-11 17:42:52 -040073 // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
khenaidoo59ce9dd2019-11-11 13:05:32 -050074 // size is zero, then a useful default size is used. The I/O buffer sizes
75 // do not limit the size of the messages that can be sent or received.
76 ReadBufferSize, WriteBufferSize int
77
khenaidoo26721882021-08-11 17:42:52 -040078 // WriteBufferPool is a pool of buffers for write operations. If the value
79 // is not set, then write buffers are allocated to the connection for the
80 // lifetime of the connection.
81 //
82 // A pool is most useful when the application has a modest volume of writes
83 // across a large number of connections.
84 //
85 // Applications should use a single pool for each unique value of
86 // WriteBufferSize.
87 WriteBufferPool BufferPool
88
khenaidoo59ce9dd2019-11-11 13:05:32 -050089 // Subprotocols specifies the client's requested subprotocols.
90 Subprotocols []string
91
92 // EnableCompression specifies if the client should attempt to negotiate
93 // per message compression (RFC 7692). Setting this value to true does not
94 // guarantee that compression will be supported. Currently only "no context
95 // takeover" modes are supported.
96 EnableCompression bool
97
98 // Jar specifies the cookie jar.
99 // If Jar is nil, cookies are not sent in requests and ignored
100 // in responses.
101 Jar http.CookieJar
102}
103
khenaidoo26721882021-08-11 17:42:52 -0400104// Dial creates a new client connection by calling DialContext with a background context.
105func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
106 return d.DialContext(context.Background(), urlStr, requestHeader)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500107}
108
khenaidoo26721882021-08-11 17:42:52 -0400109var errMalformedURL = errors.New("malformed ws or wss URL")
110
khenaidoo59ce9dd2019-11-11 13:05:32 -0500111func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
112 hostPort = u.Host
113 hostNoPort = u.Host
114 if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
115 hostNoPort = hostNoPort[:i]
116 } else {
117 switch u.Scheme {
118 case "wss":
119 hostPort += ":443"
120 case "https":
121 hostPort += ":443"
122 default:
123 hostPort += ":80"
124 }
125 }
126 return hostPort, hostNoPort
127}
128
khenaidoo26721882021-08-11 17:42:52 -0400129// DefaultDialer is a dialer with all fields set to the default values.
khenaidoo59ce9dd2019-11-11 13:05:32 -0500130var DefaultDialer = &Dialer{
khenaidoo26721882021-08-11 17:42:52 -0400131 Proxy: http.ProxyFromEnvironment,
132 HandshakeTimeout: 45 * time.Second,
khenaidoo59ce9dd2019-11-11 13:05:32 -0500133}
134
khenaidoo26721882021-08-11 17:42:52 -0400135// nilDialer is dialer to use when receiver is nil.
136var nilDialer = *DefaultDialer
137
138// DialContext creates a new client connection. Use requestHeader to specify the
khenaidoo59ce9dd2019-11-11 13:05:32 -0500139// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
140// Use the response.Header to get the selected subprotocol
141// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
142//
khenaidoo26721882021-08-11 17:42:52 -0400143// The context will be used in the request and in the Dialer.
144//
khenaidoo59ce9dd2019-11-11 13:05:32 -0500145// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
146// non-nil *http.Response so that callers can handle redirects, authentication,
147// etcetera. The response body may not contain the entire response and does not
148// need to be closed by the application.
khenaidoo26721882021-08-11 17:42:52 -0400149func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
khenaidoo59ce9dd2019-11-11 13:05:32 -0500150 if d == nil {
khenaidoo26721882021-08-11 17:42:52 -0400151 d = &nilDialer
khenaidoo59ce9dd2019-11-11 13:05:32 -0500152 }
153
154 challengeKey, err := generateChallengeKey()
155 if err != nil {
156 return nil, nil, err
157 }
158
khenaidoo26721882021-08-11 17:42:52 -0400159 u, err := url.Parse(urlStr)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500160 if err != nil {
161 return nil, nil, err
162 }
163
164 switch u.Scheme {
165 case "ws":
166 u.Scheme = "http"
167 case "wss":
168 u.Scheme = "https"
169 default:
170 return nil, nil, errMalformedURL
171 }
172
173 if u.User != nil {
174 // User name and password are not allowed in websocket URIs.
175 return nil, nil, errMalformedURL
176 }
177
178 req := &http.Request{
179 Method: "GET",
180 URL: u,
181 Proto: "HTTP/1.1",
182 ProtoMajor: 1,
183 ProtoMinor: 1,
184 Header: make(http.Header),
185 Host: u.Host,
186 }
khenaidoo26721882021-08-11 17:42:52 -0400187 req = req.WithContext(ctx)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500188
189 // Set the cookies present in the cookie jar of the dialer
190 if d.Jar != nil {
191 for _, cookie := range d.Jar.Cookies(u) {
192 req.AddCookie(cookie)
193 }
194 }
195
196 // Set the request headers using the capitalization for names and values in
197 // RFC examples. Although the capitalization shouldn't matter, there are
198 // servers that depend on it. The Header.Set method is not used because the
199 // method canonicalizes the header names.
200 req.Header["Upgrade"] = []string{"websocket"}
201 req.Header["Connection"] = []string{"Upgrade"}
202 req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
203 req.Header["Sec-WebSocket-Version"] = []string{"13"}
204 if len(d.Subprotocols) > 0 {
205 req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
206 }
207 for k, vs := range requestHeader {
208 switch {
209 case k == "Host":
210 if len(vs) > 0 {
211 req.Host = vs[0]
212 }
213 case k == "Upgrade" ||
214 k == "Connection" ||
215 k == "Sec-Websocket-Key" ||
216 k == "Sec-Websocket-Version" ||
217 k == "Sec-Websocket-Extensions" ||
218 (k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
219 return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
khenaidoo26721882021-08-11 17:42:52 -0400220 case k == "Sec-Websocket-Protocol":
221 req.Header["Sec-WebSocket-Protocol"] = vs
khenaidoo59ce9dd2019-11-11 13:05:32 -0500222 default:
223 req.Header[k] = vs
224 }
225 }
226
227 if d.EnableCompression {
khenaidoo26721882021-08-11 17:42:52 -0400228 req.Header["Sec-WebSocket-Extensions"] = []string{"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
229 }
230
231 if d.HandshakeTimeout != 0 {
232 var cancel func()
233 ctx, cancel = context.WithTimeout(ctx, d.HandshakeTimeout)
234 defer cancel()
235 }
236
237 // Get network dial function.
238 var netDial func(network, add string) (net.Conn, error)
239
240 if d.NetDialContext != nil {
241 netDial = func(network, addr string) (net.Conn, error) {
242 return d.NetDialContext(ctx, network, addr)
243 }
244 } else if d.NetDial != nil {
245 netDial = d.NetDial
246 } else {
247 netDialer := &net.Dialer{}
248 netDial = func(network, addr string) (net.Conn, error) {
249 return netDialer.DialContext(ctx, network, addr)
250 }
251 }
252
253 // If needed, wrap the dial function to set the connection deadline.
254 if deadline, ok := ctx.Deadline(); ok {
255 forwardDial := netDial
256 netDial = func(network, addr string) (net.Conn, error) {
257 c, err := forwardDial(network, addr)
258 if err != nil {
259 return nil, err
260 }
261 err = c.SetDeadline(deadline)
262 if err != nil {
263 c.Close()
264 return nil, err
265 }
266 return c, nil
267 }
268 }
269
270 // If needed, wrap the dial function to connect through a proxy.
271 if d.Proxy != nil {
272 proxyURL, err := d.Proxy(req)
273 if err != nil {
274 return nil, nil, err
275 }
276 if proxyURL != nil {
277 dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
278 if err != nil {
279 return nil, nil, err
280 }
281 netDial = dialer.Dial
282 }
khenaidoo59ce9dd2019-11-11 13:05:32 -0500283 }
284
285 hostPort, hostNoPort := hostPortNoPort(u)
khenaidoo26721882021-08-11 17:42:52 -0400286 trace := httptrace.ContextClientTrace(ctx)
287 if trace != nil && trace.GetConn != nil {
288 trace.GetConn(hostPort)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500289 }
290
khenaidoo26721882021-08-11 17:42:52 -0400291 netConn, err := netDial("tcp", hostPort)
292 if trace != nil && trace.GotConn != nil {
293 trace.GotConn(httptrace.GotConnInfo{
294 Conn: netConn,
295 })
khenaidoo59ce9dd2019-11-11 13:05:32 -0500296 }
khenaidoo59ce9dd2019-11-11 13:05:32 -0500297 if err != nil {
298 return nil, nil, err
299 }
300
301 defer func() {
302 if netConn != nil {
303 netConn.Close()
304 }
305 }()
306
khenaidoo59ce9dd2019-11-11 13:05:32 -0500307 if u.Scheme == "https" {
308 cfg := cloneTLSConfig(d.TLSClientConfig)
309 if cfg.ServerName == "" {
310 cfg.ServerName = hostNoPort
311 }
312 tlsConn := tls.Client(netConn, cfg)
313 netConn = tlsConn
khenaidoo26721882021-08-11 17:42:52 -0400314
315 var err error
316 if trace != nil {
317 err = doHandshakeWithTrace(trace, tlsConn, cfg)
318 } else {
319 err = doHandshake(tlsConn, cfg)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500320 }
khenaidoo26721882021-08-11 17:42:52 -0400321
322 if err != nil {
323 return nil, nil, err
khenaidoo59ce9dd2019-11-11 13:05:32 -0500324 }
325 }
326
khenaidoo26721882021-08-11 17:42:52 -0400327 conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize, d.WriteBufferPool, nil, nil)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500328
329 if err := req.Write(netConn); err != nil {
330 return nil, nil, err
331 }
332
khenaidoo26721882021-08-11 17:42:52 -0400333 if trace != nil && trace.GotFirstResponseByte != nil {
334 if peek, err := conn.br.Peek(1); err == nil && len(peek) == 1 {
335 trace.GotFirstResponseByte()
336 }
337 }
338
khenaidoo59ce9dd2019-11-11 13:05:32 -0500339 resp, err := http.ReadResponse(conn.br, req)
340 if err != nil {
341 return nil, nil, err
342 }
343
344 if d.Jar != nil {
345 if rc := resp.Cookies(); len(rc) > 0 {
346 d.Jar.SetCookies(u, rc)
347 }
348 }
349
350 if resp.StatusCode != 101 ||
351 !strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
352 !strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
353 resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
354 // Before closing the network connection on return from this
355 // function, slurp up some of the response to aid application
356 // debugging.
357 buf := make([]byte, 1024)
358 n, _ := io.ReadFull(resp.Body, buf)
359 resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
360 return nil, resp, ErrBadHandshake
361 }
362
363 for _, ext := range parseExtensions(resp.Header) {
364 if ext[""] != "permessage-deflate" {
365 continue
366 }
367 _, snct := ext["server_no_context_takeover"]
368 _, cnct := ext["client_no_context_takeover"]
369 if !snct || !cnct {
370 return nil, resp, errInvalidCompression
371 }
372 conn.newCompressionWriter = compressNoContextTakeover
373 conn.newDecompressionReader = decompressNoContextTakeover
374 break
375 }
376
377 resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
378 conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
379
380 netConn.SetDeadline(time.Time{})
381 netConn = nil // to avoid close in defer.
382 return conn, resp, nil
383}
khenaidoo26721882021-08-11 17:42:52 -0400384
385func doHandshake(tlsConn *tls.Conn, cfg *tls.Config) error {
386 if err := tlsConn.Handshake(); err != nil {
387 return err
388 }
389 if !cfg.InsecureSkipVerify {
390 if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
391 return err
392 }
393 }
394 return nil
395}