client.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.
  4. package websocket
  5. import (
  6. "bytes"
  7. "crypto/tls"
  8. "errors"
  9. "io"
  10. "io/ioutil"
  11. "net"
  12. "net/http"
  13. "net/url"
  14. "strings"
  15. "time"
  16. )
  17. // ErrBadHandshake is returned when the server response to opening handshake is
  18. // invalid.
  19. var ErrBadHandshake = errors.New("websocket: bad handshake")
  20. var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
  21. // NewClient creates a new client connection using the given net connection.
  22. // The URL u specifies the host and request URI. Use requestHeader to specify
  23. // the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
  24. // (Cookie). Use the response.Header to get the selected subprotocol
  25. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  26. //
  27. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  28. // non-nil *http.Response so that callers can handle redirects, authentication,
  29. // etc.
  30. //
  31. // Deprecated: Use Dialer instead.
  32. func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
  33. d := Dialer{
  34. ReadBufferSize: readBufSize,
  35. WriteBufferSize: writeBufSize,
  36. NetDial: func(net, addr string) (net.Conn, error) {
  37. return netConn, nil
  38. },
  39. }
  40. return d.Dial(u.String(), requestHeader)
  41. }
  42. // A Dialer contains options for connecting to WebSocket server.
  43. type Dialer struct {
  44. // NetDial specifies the dial function for creating TCP connections. If
  45. // NetDial is nil, net.Dial is used.
  46. NetDial func(network, addr string) (net.Conn, error)
  47. // Proxy specifies a function to return a proxy for a given
  48. // Request. If the function returns a non-nil error, the
  49. // request is aborted with the provided error.
  50. // If Proxy is nil or returns a nil *URL, no proxy is used.
  51. Proxy func(*http.Request) (*url.URL, error)
  52. // TLSClientConfig specifies the TLS configuration to use with tls.Client.
  53. // If nil, the default configuration is used.
  54. TLSClientConfig *tls.Config
  55. // HandshakeTimeout specifies the duration for the handshake to complete.
  56. HandshakeTimeout time.Duration
  57. // ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
  58. // size is zero, then a useful default size is used. The I/O buffer sizes
  59. // do not limit the size of the messages that can be sent or received.
  60. ReadBufferSize, WriteBufferSize int
  61. // WriteBufferPool is a pool of buffers for write operations. If the value
  62. // is not set, then write buffers are allocated to the connection for the
  63. // lifetime of the connection.
  64. //
  65. // A pool is most useful when the application has a modest volume of writes
  66. // across a large number of connections.
  67. //
  68. // Applications should use a single pool for each unique value of
  69. // WriteBufferSize.
  70. WriteBufferPool BufferPool
  71. // Subprotocols specifies the client's requested subprotocols.
  72. Subprotocols []string
  73. // EnableCompression specifies if the client should attempt to negotiate
  74. // per message compression (RFC 7692). Setting this value to true does not
  75. // guarantee that compression will be supported. Currently only "no context
  76. // takeover" modes are supported.
  77. EnableCompression bool
  78. // Jar specifies the cookie jar.
  79. // If Jar is nil, cookies are not sent in requests and ignored
  80. // in responses.
  81. Jar http.CookieJar
  82. }
  83. var errMalformedURL = errors.New("malformed ws or wss URL")
  84. func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
  85. hostPort = u.Host
  86. hostNoPort = u.Host
  87. if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
  88. hostNoPort = hostNoPort[:i]
  89. } else {
  90. switch u.Scheme {
  91. case "wss":
  92. hostPort += ":443"
  93. case "https":
  94. hostPort += ":443"
  95. default:
  96. hostPort += ":80"
  97. }
  98. }
  99. return hostPort, hostNoPort
  100. }
  101. // DefaultDialer is a dialer with all fields set to the default values.
  102. var DefaultDialer = &Dialer{
  103. Proxy: http.ProxyFromEnvironment,
  104. HandshakeTimeout: 45 * time.Second,
  105. }
  106. // nilDialer is dialer to use when receiver is nil.
  107. var nilDialer Dialer = *DefaultDialer
  108. // Dial creates a new client connection. Use requestHeader to specify the
  109. // origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
  110. // Use the response.Header to get the selected subprotocol
  111. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  112. //
  113. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  114. // non-nil *http.Response so that callers can handle redirects, authentication,
  115. // etcetera. The response body may not contain the entire response and does not
  116. // need to be closed by the application.
  117. func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
  118. if d == nil {
  119. d = &nilDialer
  120. }
  121. challengeKey, err := generateChallengeKey()
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. u, err := url.Parse(urlStr)
  126. if err != nil {
  127. return nil, nil, err
  128. }
  129. switch u.Scheme {
  130. case "ws":
  131. u.Scheme = "http"
  132. case "wss":
  133. u.Scheme = "https"
  134. default:
  135. return nil, nil, errMalformedURL
  136. }
  137. if u.User != nil {
  138. // User name and password are not allowed in websocket URIs.
  139. return nil, nil, errMalformedURL
  140. }
  141. req := &http.Request{
  142. Method: "GET",
  143. URL: u,
  144. Proto: "HTTP/1.1",
  145. ProtoMajor: 1,
  146. ProtoMinor: 1,
  147. Header: make(http.Header),
  148. Host: u.Host,
  149. }
  150. // Set the cookies present in the cookie jar of the dialer
  151. if d.Jar != nil {
  152. for _, cookie := range d.Jar.Cookies(u) {
  153. req.AddCookie(cookie)
  154. }
  155. }
  156. // Set the request headers using the capitalization for names and values in
  157. // RFC examples. Although the capitalization shouldn't matter, there are
  158. // servers that depend on it. The Header.Set method is not used because the
  159. // method canonicalizes the header names.
  160. req.Header["Upgrade"] = []string{"websocket"}
  161. req.Header["Connection"] = []string{"Upgrade"}
  162. req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
  163. req.Header["Sec-WebSocket-Version"] = []string{"13"}
  164. if len(d.Subprotocols) > 0 {
  165. req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
  166. }
  167. for k, vs := range requestHeader {
  168. switch {
  169. case k == "Host":
  170. if len(vs) > 0 {
  171. req.Host = vs[0]
  172. }
  173. case k == "Upgrade" ||
  174. k == "Connection" ||
  175. k == "Sec-Websocket-Key" ||
  176. k == "Sec-Websocket-Version" ||
  177. k == "Sec-Websocket-Extensions" ||
  178. (k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
  179. return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
  180. case k == "Sec-Websocket-Protocol":
  181. req.Header["Sec-WebSocket-Protocol"] = vs
  182. default:
  183. req.Header[k] = vs
  184. }
  185. }
  186. if d.EnableCompression {
  187. req.Header["Sec-WebSocket-Extensions"] = []string{"permessage-deflate; server_no_context_takeover; client_no_context_takeover"}
  188. }
  189. var deadline time.Time
  190. if d.HandshakeTimeout != 0 {
  191. deadline = time.Now().Add(d.HandshakeTimeout)
  192. }
  193. // Get network dial function.
  194. netDial := d.NetDial
  195. if netDial == nil {
  196. netDialer := &net.Dialer{Deadline: deadline}
  197. netDial = netDialer.Dial
  198. }
  199. // If needed, wrap the dial function to set the connection deadline.
  200. if !deadline.Equal(time.Time{}) {
  201. forwardDial := netDial
  202. netDial = func(network, addr string) (net.Conn, error) {
  203. c, err := forwardDial(network, addr)
  204. if err != nil {
  205. return nil, err
  206. }
  207. err = c.SetDeadline(deadline)
  208. if err != nil {
  209. c.Close()
  210. return nil, err
  211. }
  212. return c, nil
  213. }
  214. }
  215. // If needed, wrap the dial function to connect through a proxy.
  216. if d.Proxy != nil {
  217. proxyURL, err := d.Proxy(req)
  218. if err != nil {
  219. return nil, nil, err
  220. }
  221. if proxyURL != nil {
  222. dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
  223. if err != nil {
  224. return nil, nil, err
  225. }
  226. netDial = dialer.Dial
  227. }
  228. }
  229. hostPort, hostNoPort := hostPortNoPort(u)
  230. netConn, err := netDial("tcp", hostPort)
  231. if err != nil {
  232. return nil, nil, err
  233. }
  234. defer func() {
  235. if netConn != nil {
  236. netConn.Close()
  237. }
  238. }()
  239. if u.Scheme == "https" {
  240. cfg := cloneTLSConfig(d.TLSClientConfig)
  241. if cfg.ServerName == "" {
  242. cfg.ServerName = hostNoPort
  243. }
  244. tlsConn := tls.Client(netConn, cfg)
  245. netConn = tlsConn
  246. if err := tlsConn.Handshake(); err != nil {
  247. return nil, nil, err
  248. }
  249. if !cfg.InsecureSkipVerify {
  250. if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
  251. return nil, nil, err
  252. }
  253. }
  254. }
  255. conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize, d.WriteBufferPool, nil, nil)
  256. if err := req.Write(netConn); err != nil {
  257. return nil, nil, err
  258. }
  259. resp, err := http.ReadResponse(conn.br, req)
  260. if err != nil {
  261. return nil, nil, err
  262. }
  263. if d.Jar != nil {
  264. if rc := resp.Cookies(); len(rc) > 0 {
  265. d.Jar.SetCookies(u, rc)
  266. }
  267. }
  268. if resp.StatusCode != 101 ||
  269. !strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
  270. !strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
  271. resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
  272. // Before closing the network connection on return from this
  273. // function, slurp up some of the response to aid application
  274. // debugging.
  275. buf := make([]byte, 1024)
  276. n, _ := io.ReadFull(resp.Body, buf)
  277. resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
  278. return nil, resp, ErrBadHandshake
  279. }
  280. for _, ext := range parseExtensions(resp.Header) {
  281. if ext[""] != "permessage-deflate" {
  282. continue
  283. }
  284. _, snct := ext["server_no_context_takeover"]
  285. _, cnct := ext["client_no_context_takeover"]
  286. if !snct || !cnct {
  287. return nil, resp, errInvalidCompression
  288. }
  289. conn.newCompressionWriter = compressNoContextTakeover
  290. conn.newDecompressionReader = decompressNoContextTakeover
  291. break
  292. }
  293. resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
  294. conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
  295. netConn.SetDeadline(time.Time{})
  296. netConn = nil // to avoid close in defer.
  297. return conn, resp, nil
  298. }