2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-16 20:41:47 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2015-05-09 00:59:31 +03:00
|
|
|
"bytes"
|
2018-08-24 23:45:12 +03:00
|
|
|
"context"
|
2013-12-14 09:30:22 +04:00
|
|
|
"crypto/tls"
|
2013-10-16 20:41:47 +04:00
|
|
|
"errors"
|
2022-06-21 20:54:14 +03:00
|
|
|
"fmt"
|
2015-05-09 00:59:31 +03:00
|
|
|
"io"
|
2023-08-26 23:01:45 +03:00
|
|
|
"log"
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-08-24 23:45:12 +03:00
|
|
|
"net/http/httptrace"
|
2013-10-16 20:41:47 +04:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2013-12-14 09:30:22 +04:00
|
|
|
"time"
|
2023-08-26 23:01:45 +03:00
|
|
|
|
|
|
|
"golang.org/x/net/proxy"
|
2013-10-16 20:41:47 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrBadHandshake is returned when the server response to opening handshake is
|
|
|
|
// invalid.
|
|
|
|
var ErrBadHandshake = errors.New("websocket: bad handshake")
|
|
|
|
|
2016-09-30 07:52:15 +03:00
|
|
|
var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// NewClient creates a new client connection using the given net connection.
|
|
|
|
// The URL u specifies the host and request URI. Use requestHeader to specify
|
2013-12-14 09:30:22 +04:00
|
|
|
// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
|
2013-10-16 20:41:47 +04:00
|
|
|
// (Cookie). Use the response.Header to get the selected subprotocol
|
|
|
|
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
|
|
|
//
|
|
|
|
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
|
|
|
// non-nil *http.Response so that callers can handle redirects, authentication,
|
|
|
|
// etc.
|
2015-10-20 20:44:58 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Use Dialer instead.
|
2013-10-16 20:41:47 +04:00
|
|
|
func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
|
2015-10-20 20:44:58 +03:00
|
|
|
d := Dialer{
|
|
|
|
ReadBufferSize: readBufSize,
|
|
|
|
WriteBufferSize: writeBufSize,
|
|
|
|
NetDial: func(net, addr string) (net.Conn, error) {
|
|
|
|
return netConn, nil
|
|
|
|
},
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2015-10-20 20:44:58 +03:00
|
|
|
return d.Dial(u.String(), requestHeader)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
|
2013-12-24 00:08:49 +04:00
|
|
|
// A Dialer contains options for connecting to WebSocket server.
|
2021-12-16 22:07:50 +03:00
|
|
|
//
|
|
|
|
// It is safe to call Dialer's methods concurrently.
|
2013-12-14 09:30:22 +04:00
|
|
|
type Dialer struct {
|
|
|
|
// NetDial specifies the dial function for creating TCP connections. If
|
|
|
|
// NetDial is nil, net.Dial is used.
|
|
|
|
NetDial func(network, addr string) (net.Conn, error)
|
|
|
|
|
2018-08-24 23:45:12 +03:00
|
|
|
// NetDialContext specifies the dial function for creating TCP connections. If
|
2022-01-04 04:59:52 +03:00
|
|
|
// NetDialContext is nil, NetDial is used.
|
2018-08-24 23:45:12 +03:00
|
|
|
NetDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
2022-01-04 04:59:52 +03:00
|
|
|
// NetDialTLSContext specifies the dial function for creating TLS/TCP connections. If
|
|
|
|
// NetDialTLSContext is nil, NetDialContext is used.
|
|
|
|
// If NetDialTLSContext is set, Dial assumes the TLS handshake is done there and
|
|
|
|
// TLSClientConfig is ignored.
|
|
|
|
NetDialTLSContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
2015-10-20 12:29:17 +03:00
|
|
|
// Proxy specifies a function to return a proxy for a given
|
|
|
|
// Request. If the function returns a non-nil error, the
|
|
|
|
// request is aborted with the provided error.
|
|
|
|
// If Proxy is nil or returns a nil *URL, no proxy is used.
|
|
|
|
Proxy func(*http.Request) (*url.URL, error)
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
// TLSClientConfig specifies the TLS configuration to use with tls.Client.
|
|
|
|
// If nil, the default configuration is used.
|
2022-01-04 04:59:52 +03:00
|
|
|
// If either NetDialTLS or NetDialTLSContext are set, Dial assumes the TLS handshake
|
|
|
|
// is done there and TLSClientConfig is ignored.
|
2013-12-14 09:30:22 +04:00
|
|
|
TLSClientConfig *tls.Config
|
|
|
|
|
|
|
|
// HandshakeTimeout specifies the duration for the handshake to complete.
|
|
|
|
HandshakeTimeout time.Duration
|
|
|
|
|
2018-10-30 17:45:53 +03:00
|
|
|
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
|
2017-03-03 01:46:13 +03:00
|
|
|
// size is zero, then a useful default size is used. The I/O buffer sizes
|
|
|
|
// do not limit the size of the messages that can be sent or received.
|
2013-12-14 09:30:22 +04:00
|
|
|
ReadBufferSize, WriteBufferSize int
|
2013-12-14 19:48:43 +04:00
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
// WriteBufferPool is a pool of buffers for write operations. If the value
|
|
|
|
// is not set, then write buffers are allocated to the connection for the
|
|
|
|
// lifetime of the connection.
|
|
|
|
//
|
|
|
|
// A pool is most useful when the application has a modest volume of writes
|
|
|
|
// across a large number of connections.
|
|
|
|
//
|
|
|
|
// Applications should use a single pool for each unique value of
|
|
|
|
// WriteBufferSize.
|
|
|
|
WriteBufferPool BufferPool
|
|
|
|
|
2013-12-14 19:48:43 +04:00
|
|
|
// Subprotocols specifies the client's requested subprotocols.
|
|
|
|
Subprotocols []string
|
2016-09-30 07:52:15 +03:00
|
|
|
|
2016-10-18 03:30:22 +03:00
|
|
|
// EnableCompression specifies if the client should attempt to negotiate
|
|
|
|
// per message compression (RFC 7692). Setting this value to true does not
|
2016-09-30 07:52:15 +03:00
|
|
|
// guarantee that compression will be supported. Currently only "no context
|
|
|
|
// takeover" modes are supported.
|
2016-10-18 03:30:22 +03:00
|
|
|
EnableCompression bool
|
2016-10-17 21:19:52 +03:00
|
|
|
|
|
|
|
// Jar specifies the cookie jar.
|
|
|
|
// If Jar is nil, cookies are not sent in requests and ignored
|
|
|
|
// in responses.
|
|
|
|
Jar http.CookieJar
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
|
2018-08-24 23:45:12 +03:00
|
|
|
// Dial creates a new client connection by calling DialContext with a background context.
|
|
|
|
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
|
2018-08-25 18:15:06 +03:00
|
|
|
return d.DialContext(context.Background(), urlStr, requestHeader)
|
2018-08-24 23:45:12 +03:00
|
|
|
}
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
var errMalformedURL = errors.New("malformed ws or wss URL")
|
|
|
|
|
2014-05-08 22:21:56 +04:00
|
|
|
func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
|
|
|
|
hostPort = u.Host
|
|
|
|
hostNoPort = u.Host
|
|
|
|
if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
|
|
|
|
hostNoPort = hostNoPort[:i]
|
|
|
|
} else {
|
2015-10-20 12:29:17 +03:00
|
|
|
switch u.Scheme {
|
|
|
|
case "wss":
|
2014-05-08 22:21:56 +04:00
|
|
|
hostPort += ":443"
|
2015-10-20 12:29:17 +03:00
|
|
|
case "https":
|
|
|
|
hostPort += ":443"
|
|
|
|
default:
|
2014-05-08 22:21:56 +04:00
|
|
|
hostPort += ":80"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hostPort, hostNoPort
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
// DefaultDialer is a dialer with all fields set to the default values.
|
2015-10-20 12:29:17 +03:00
|
|
|
var DefaultDialer = &Dialer{
|
2018-03-04 14:17:01 +03:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2018-03-06 02:35:16 +03:00
|
|
|
HandshakeTimeout: 45 * time.Second,
|
2015-10-20 12:29:17 +03:00
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
|
2018-03-06 20:21:16 +03:00
|
|
|
// nilDialer is dialer to use when receiver is nil.
|
2018-08-25 00:03:26 +03:00
|
|
|
var nilDialer = *DefaultDialer
|
2018-03-06 20:21:16 +03:00
|
|
|
|
2018-08-24 23:45:12 +03:00
|
|
|
// DialContext creates a new client connection. Use requestHeader to specify the
|
2013-12-14 09:30:22 +04:00
|
|
|
// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
|
|
|
|
// Use the response.Header to get the selected subprotocol
|
|
|
|
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
|
|
|
|
//
|
2018-10-12 05:01:31 +03:00
|
|
|
// The context will be used in the request and in the Dialer.
|
2018-08-24 23:45:12 +03:00
|
|
|
//
|
2013-12-14 09:30:22 +04:00
|
|
|
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
|
|
|
|
// non-nil *http.Response so that callers can handle redirects, authentication,
|
2015-05-09 00:59:31 +03:00
|
|
|
// etcetera. The response body may not contain the entire response and does not
|
|
|
|
// need to be closed by the application.
|
2018-08-25 18:15:06 +03:00
|
|
|
func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
|
2015-10-20 20:44:58 +03:00
|
|
|
if d == nil {
|
2018-03-06 20:21:16 +03:00
|
|
|
d = &nilDialer
|
2015-10-20 20:44:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
challengeKey, err := generateChallengeKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:07:11 +03:00
|
|
|
u, err := url.Parse(urlStr)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
switch u.Scheme {
|
|
|
|
case "ws":
|
|
|
|
u.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
u.Scheme = "https"
|
|
|
|
default:
|
|
|
|
return nil, nil, errMalformedURL
|
|
|
|
}
|
2014-05-08 22:21:56 +04:00
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
if u.User != nil {
|
|
|
|
// User name and password are not allowed in websocket URIs.
|
|
|
|
return nil, nil, errMalformedURL
|
|
|
|
}
|
|
|
|
|
2023-10-08 17:29:10 +03:00
|
|
|
host := requestHeader.Get("Host")
|
|
|
|
if host == "" {
|
|
|
|
host = u.Host
|
|
|
|
}
|
2015-10-20 20:44:58 +03:00
|
|
|
req := &http.Request{
|
2021-12-19 19:21:45 +03:00
|
|
|
Method: http.MethodGet,
|
2015-10-20 20:44:58 +03:00
|
|
|
URL: u,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: make(http.Header),
|
2023-10-08 17:29:10 +03:00
|
|
|
Host: host,
|
2015-10-20 20:44:58 +03:00
|
|
|
}
|
2018-08-24 23:45:12 +03:00
|
|
|
req = req.WithContext(ctx)
|
2015-10-20 20:44:58 +03:00
|
|
|
|
2016-10-17 21:19:52 +03:00
|
|
|
// Set the cookies present in the cookie jar of the dialer
|
|
|
|
if d.Jar != nil {
|
|
|
|
for _, cookie := range d.Jar.Cookies(u) {
|
|
|
|
req.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
// Set the request headers using the capitalization for names and values in
|
|
|
|
// RFC examples. Although the capitalization shouldn't matter, there are
|
|
|
|
// servers that depend on it. The Header.Set method is not used because the
|
|
|
|
// method canonicalizes the header names.
|
|
|
|
req.Header["Upgrade"] = []string{"websocket"}
|
|
|
|
req.Header["Connection"] = []string{"Upgrade"}
|
|
|
|
req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
|
|
|
|
req.Header["Sec-WebSocket-Version"] = []string{"13"}
|
|
|
|
if len(d.Subprotocols) > 0 {
|
|
|
|
req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
2015-10-20 20:44:58 +03:00
|
|
|
for k, vs := range requestHeader {
|
2015-11-02 19:39:02 +03:00
|
|
|
switch {
|
|
|
|
case k == "Host":
|
2015-10-20 20:44:58 +03:00
|
|
|
if len(vs) > 0 {
|
|
|
|
req.Host = vs[0]
|
|
|
|
}
|
2015-11-02 19:39:02 +03:00
|
|
|
case k == "Upgrade" ||
|
|
|
|
k == "Connection" ||
|
|
|
|
k == "Sec-Websocket-Key" ||
|
|
|
|
k == "Sec-Websocket-Version" ||
|
2023-08-26 23:01:45 +03:00
|
|
|
//#nosec G101 (CWE-798): Potential HTTP request smuggling via parameter pollution
|
2016-09-30 07:52:15 +03:00
|
|
|
k == "Sec-Websocket-Extensions" ||
|
2015-11-02 19:39:02 +03:00
|
|
|
(k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
|
|
|
|
return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
|
2018-03-06 16:56:47 +03:00
|
|
|
case k == "Sec-Websocket-Protocol":
|
|
|
|
req.Header["Sec-WebSocket-Protocol"] = vs
|
2015-11-02 19:39:02 +03:00
|
|
|
default:
|
2015-10-20 20:44:58 +03:00
|
|
|
req.Header[k] = vs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 03:30:22 +03:00
|
|
|
if d.EnableCompression {
|
2018-04-20 20:16:12 +03:00
|
|
|
req.Header["Sec-WebSocket-Extensions"] = []string{"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
|
2016-09-30 07:52:15 +03:00
|
|
|
}
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
if d.HandshakeTimeout != 0 {
|
2018-08-24 23:45:12 +03:00
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, d.HandshakeTimeout)
|
|
|
|
defer cancel()
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
// Get network dial function.
|
2018-08-24 23:45:12 +03:00
|
|
|
var netDial func(network, add string) (net.Conn, error)
|
|
|
|
|
2022-01-04 04:59:52 +03:00
|
|
|
switch u.Scheme {
|
|
|
|
case "http":
|
|
|
|
if d.NetDialContext != nil {
|
|
|
|
netDial = func(network, addr string) (net.Conn, error) {
|
|
|
|
return d.NetDialContext(ctx, network, addr)
|
|
|
|
}
|
|
|
|
} else if d.NetDial != nil {
|
|
|
|
netDial = d.NetDial
|
2018-08-24 23:45:12 +03:00
|
|
|
}
|
2022-01-04 04:59:52 +03:00
|
|
|
case "https":
|
|
|
|
if d.NetDialTLSContext != nil {
|
|
|
|
netDial = func(network, addr string) (net.Conn, error) {
|
|
|
|
return d.NetDialTLSContext(ctx, network, addr)
|
|
|
|
}
|
|
|
|
} else if d.NetDialContext != nil {
|
|
|
|
netDial = func(network, addr string) (net.Conn, error) {
|
|
|
|
return d.NetDialContext(ctx, network, addr)
|
|
|
|
}
|
|
|
|
} else if d.NetDial != nil {
|
|
|
|
netDial = d.NetDial
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, nil, errMalformedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if netDial == nil {
|
2018-08-24 23:45:12 +03:00
|
|
|
netDialer := &net.Dialer{}
|
|
|
|
netDial = func(network, addr string) (net.Conn, error) {
|
|
|
|
return netDialer.DialContext(ctx, network, addr)
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
// If needed, wrap the dial function to set the connection deadline.
|
2018-08-24 23:45:12 +03:00
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
2017-12-01 04:43:01 +03:00
|
|
|
forwardDial := netDial
|
|
|
|
netDial = func(network, addr string) (net.Conn, error) {
|
|
|
|
c, err := forwardDial(network, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = c.SetDeadline(deadline)
|
|
|
|
if err != nil {
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
log.Printf("websocket: failed to close network connection: %v", err)
|
|
|
|
}
|
2017-12-01 04:43:01 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If needed, wrap the dial function to connect through a proxy.
|
|
|
|
if d.Proxy != nil {
|
|
|
|
proxyURL, err := d.Proxy(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if proxyURL != nil {
|
2023-08-26 23:01:45 +03:00
|
|
|
dialer, err := proxy.FromURL(proxyURL, netDialerFunc(netDial))
|
2017-12-01 04:43:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
netDial = dialer.Dial
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPort, hostNoPort := hostPortNoPort(u)
|
2018-08-24 23:45:12 +03:00
|
|
|
trace := httptrace.ContextClientTrace(ctx)
|
|
|
|
if trace != nil && trace.GetConn != nil {
|
|
|
|
trace.GetConn(hostPort)
|
|
|
|
}
|
|
|
|
|
2017-12-01 04:43:01 +03:00
|
|
|
netConn, err := netDial("tcp", hostPort)
|
2022-07-06 11:19:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-08-24 23:45:12 +03:00
|
|
|
if trace != nil && trace.GotConn != nil {
|
|
|
|
trace.GotConn(httptrace.GotConnInfo{
|
|
|
|
Conn: netConn,
|
|
|
|
})
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if netConn != nil {
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := netConn.Close(); err != nil {
|
|
|
|
log.Printf("websocket: failed to close network connection: %v", err)
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-04 04:59:52 +03:00
|
|
|
if u.Scheme == "https" && d.NetDialTLSContext == nil {
|
|
|
|
// If NetDialTLSContext is set, assume that the TLS handshake has already been done
|
|
|
|
|
2016-04-24 01:46:58 +03:00
|
|
|
cfg := cloneTLSConfig(d.TLSClientConfig)
|
|
|
|
if cfg.ServerName == "" {
|
2023-10-08 17:29:10 +03:00
|
|
|
if host != "" {
|
|
|
|
cfg.ServerName = host
|
|
|
|
} else {
|
|
|
|
cfg.ServerName = hostNoPort
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
tlsConn := tls.Client(netConn, cfg)
|
|
|
|
netConn = tlsConn
|
2018-08-24 23:45:12 +03:00
|
|
|
|
2022-01-01 19:43:22 +03:00
|
|
|
if trace != nil && trace.TLSHandshakeStart != nil {
|
|
|
|
trace.TLSHandshakeStart()
|
|
|
|
}
|
|
|
|
err := doHandshake(ctx, tlsConn, cfg)
|
|
|
|
if trace != nil && trace.TLSHandshakeDone != nil {
|
|
|
|
trace.TLSHandshakeDone(tlsConn.ConnectionState(), err)
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
2018-08-24 23:45:12 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize, d.WriteBufferPool, nil, nil)
|
2015-05-08 20:33:57 +03:00
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
if err := req.Write(netConn); err != nil {
|
|
|
|
return nil, nil, err
|
2015-05-08 20:33:57 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 23:45:12 +03:00
|
|
|
if trace != nil && trace.GotFirstResponseByte != nil {
|
|
|
|
if peek, err := conn.br.Peek(1); err == nil && len(peek) == 1 {
|
|
|
|
trace.GotFirstResponseByte()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
resp, err := http.ReadResponse(conn.br, req)
|
2013-12-14 09:30:22 +04:00
|
|
|
if err != nil {
|
2022-06-21 20:54:14 +03:00
|
|
|
if d.TLSClientConfig != nil {
|
|
|
|
for _, proto := range d.TLSClientConfig.NextProtos {
|
|
|
|
if proto != "http/1.1" {
|
|
|
|
return nil, nil, fmt.Errorf(
|
|
|
|
"websocket: protocol %q was given but is not supported;"+
|
|
|
|
"sharing tls.Config with net/http Transport can cause this error: %w",
|
|
|
|
proto, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-20 20:44:58 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-10-17 21:19:52 +03:00
|
|
|
|
|
|
|
if d.Jar != nil {
|
|
|
|
if rc := resp.Cookies(); len(rc) > 0 {
|
|
|
|
d.Jar.SetCookies(u, rc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:44:58 +03:00
|
|
|
if resp.StatusCode != 101 ||
|
2020-08-20 16:43:18 +03:00
|
|
|
!tokenListContainsValue(resp.Header, "Upgrade", "websocket") ||
|
|
|
|
!tokenListContainsValue(resp.Header, "Connection", "upgrade") ||
|
2015-10-20 20:44:58 +03:00
|
|
|
resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
|
|
|
|
// Before closing the network connection on return from this
|
|
|
|
// function, slurp up some of the response to aid application
|
|
|
|
// debugging.
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
n, _ := io.ReadFull(resp.Body, buf)
|
2023-08-26 23:01:45 +03:00
|
|
|
resp.Body = io.NopCloser(bytes.NewReader(buf[:n]))
|
2015-10-20 20:44:58 +03:00
|
|
|
return nil, resp, ErrBadHandshake
|
2013-12-14 09:30:22 +04:00
|
|
|
}
|
|
|
|
|
2017-03-19 11:36:38 +03:00
|
|
|
for _, ext := range parseExtensions(resp.Header) {
|
2016-09-30 07:52:15 +03:00
|
|
|
if ext[""] != "permessage-deflate" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, snct := ext["server_no_context_takeover"]
|
|
|
|
_, cnct := ext["client_no_context_takeover"]
|
|
|
|
if !snct || !cnct {
|
|
|
|
return nil, resp, errInvalidCompression
|
|
|
|
}
|
|
|
|
conn.newCompressionWriter = compressNoContextTakeover
|
|
|
|
conn.newDecompressionReader = decompressNoContextTakeover
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-08-26 23:01:45 +03:00
|
|
|
resp.Body = io.NopCloser(bytes.NewReader([]byte{}))
|
2015-10-20 20:44:58 +03:00
|
|
|
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
|
|
|
|
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := netConn.SetDeadline(time.Time{}); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
netConn = nil // to avoid close in defer.
|
|
|
|
return conn, resp, nil
|
|
|
|
}
|
2022-01-03 02:53:55 +03:00
|
|
|
|
|
|
|
func cloneTLSConfig(cfg *tls.Config) *tls.Config {
|
|
|
|
if cfg == nil {
|
2023-08-26 23:01:45 +03:00
|
|
|
return &tls.Config{MinVersion: tls.VersionTLS12}
|
2022-01-03 02:53:55 +03:00
|
|
|
}
|
|
|
|
return cfg.Clone()
|
|
|
|
}
|