1 // 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.
22 // ErrBadHandshake is returned when the server response to opening handshake is
24 var ErrBadHandshake = errors.New("websocket: bad handshake")
26 var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
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).
34 // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
35 // non-nil *http.Response so that callers can handle redirects, authentication,
38 // Deprecated: Use Dialer instead.
39 func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
41 ReadBufferSize: readBufSize,
42 WriteBufferSize: writeBufSize,
43 NetDial: func(net, addr string) (net.Conn, error) {
47 return d.Dial(u.String(), requestHeader)
50 // A Dialer contains options for connecting to WebSocket server.
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)
56 // 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)
60 // 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)
66 // TLSClientConfig specifies the TLS configuration to use with tls.Client.
67 // If nil, the default configuration is used.
68 TLSClientConfig *tls.Config
70 // HandshakeTimeout specifies the duration for the handshake to complete.
71 HandshakeTimeout time.Duration
73 // ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
74 // 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
78 // 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.
82 // A pool is most useful when the application has a modest volume of writes
83 // across a large number of connections.
85 // Applications should use a single pool for each unique value of
87 WriteBufferPool BufferPool
89 // Subprotocols specifies the client's requested subprotocols.
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
98 // Jar specifies the cookie jar.
99 // If Jar is nil, cookies are not sent in requests and ignored
104 // Dial creates a new client connection by calling DialContext with a background context.
105 func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
106 return d.DialContext(context.Background(), urlStr, requestHeader)
109 var errMalformedURL = errors.New("malformed ws or wss URL")
111 func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
114 if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
115 hostNoPort = hostNoPort[:i]
126 return hostPort, hostNoPort
129 // DefaultDialer is a dialer with all fields set to the default values.
130 var DefaultDialer = &Dialer{
131 Proxy: http.ProxyFromEnvironment,
132 HandshakeTimeout: 45 * time.Second,
135 // nilDialer is dialer to use when receiver is nil.
136 var nilDialer = *DefaultDialer
138 // DialContext creates a new client connection. Use requestHeader to specify the
139 // 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).
143 // The context will be used in the request and in the Dialer.
145 // 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.
149 func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
154 challengeKey, err := generateChallengeKey()
159 u, err := url.Parse(urlStr)
170 return nil, nil, errMalformedURL
174 // User name and password are not allowed in websocket URIs.
175 return nil, nil, errMalformedURL
178 req := &http.Request{
184 Header: make(http.Header),
187 req = req.WithContext(ctx)
189 // Set the cookies present in the cookie jar of the dialer
191 for _, cookie := range d.Jar.Cookies(u) {
192 req.AddCookie(cookie)
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, ", ")}
207 for k, vs := range requestHeader {
213 case k == "Upgrade" ||
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)
220 case k == "Sec-Websocket-Protocol":
221 req.Header["Sec-WebSocket-Protocol"] = vs
227 if d.EnableCompression {
228 req.Header["Sec-WebSocket-Extensions"] = []string{"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
231 if d.HandshakeTimeout != 0 {
233 ctx, cancel = context.WithTimeout(ctx, d.HandshakeTimeout)
237 // Get network dial function.
238 var netDial func(network, add string) (net.Conn, error)
240 if d.NetDialContext != nil {
241 netDial = func(network, addr string) (net.Conn, error) {
242 return d.NetDialContext(ctx, network, addr)
244 } else if d.NetDial != nil {
247 netDialer := &net.Dialer{}
248 netDial = func(network, addr string) (net.Conn, error) {
249 return netDialer.DialContext(ctx, network, addr)
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)
261 err = c.SetDeadline(deadline)
270 // If needed, wrap the dial function to connect through a proxy.
272 proxyURL, err := d.Proxy(req)
277 dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
281 netDial = dialer.Dial
285 hostPort, hostNoPort := hostPortNoPort(u)
286 trace := httptrace.ContextClientTrace(ctx)
287 if trace != nil && trace.GetConn != nil {
288 trace.GetConn(hostPort)
291 netConn, err := netDial("tcp", hostPort)
292 if trace != nil && trace.GotConn != nil {
293 trace.GotConn(httptrace.GotConnInfo{
307 if u.Scheme == "https" {
308 cfg := cloneTLSConfig(d.TLSClientConfig)
309 if cfg.ServerName == "" {
310 cfg.ServerName = hostNoPort
312 tlsConn := tls.Client(netConn, cfg)
317 err = doHandshakeWithTrace(trace, tlsConn, cfg)
319 err = doHandshake(tlsConn, cfg)
327 conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize, d.WriteBufferPool, nil, nil)
329 if err := req.Write(netConn); err != nil {
333 if trace != nil && trace.GotFirstResponseByte != nil {
334 if peek, err := conn.br.Peek(1); err == nil && len(peek) == 1 {
335 trace.GotFirstResponseByte()
339 resp, err := http.ReadResponse(conn.br, req)
345 if rc := resp.Cookies(); len(rc) > 0 {
346 d.Jar.SetCookies(u, rc)
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
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
363 for _, ext := range parseExtensions(resp.Header) {
364 if ext[""] != "permessage-deflate" {
367 _, snct := ext["server_no_context_takeover"]
368 _, cnct := ext["client_no_context_takeover"]
370 return nil, resp, errInvalidCompression
372 conn.newCompressionWriter = compressNoContextTakeover
373 conn.newDecompressionReader = decompressNoContextTakeover
377 resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
378 conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
380 netConn.SetDeadline(time.Time{})
381 netConn = nil // to avoid close in defer.
382 return conn, resp, nil
385 func doHandshake(tlsConn *tls.Conn, cfg *tls.Config) error {
386 if err := tlsConn.Handshake(); err != nil {
389 if !cfg.InsecureSkipVerify {
390 if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {