PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/gorilla/websocket/client.go

https://gitlab.com/leanlabsio/kanban
Go | 392 lines | 270 code | 52 blank | 70 comment | 84 complexity | f2641b861dfbcbb4b65cc9286422fc59 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-3.0, 0BSD, BSD-3-Clause
  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. "bufio"
  7. "bytes"
  8. "crypto/tls"
  9. "encoding/base64"
  10. "errors"
  11. "io"
  12. "io/ioutil"
  13. "net"
  14. "net/http"
  15. "net/url"
  16. "strings"
  17. "time"
  18. )
  19. // ErrBadHandshake is returned when the server response to opening handshake is
  20. // invalid.
  21. var ErrBadHandshake = errors.New("websocket: bad handshake")
  22. var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
  23. // NewClient creates a new client connection using the given net connection.
  24. // The URL u specifies the host and request URI. Use requestHeader to specify
  25. // the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
  26. // (Cookie). Use the response.Header to get the selected subprotocol
  27. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  28. //
  29. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  30. // non-nil *http.Response so that callers can handle redirects, authentication,
  31. // etc.
  32. //
  33. // Deprecated: Use Dialer instead.
  34. func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
  35. d := Dialer{
  36. ReadBufferSize: readBufSize,
  37. WriteBufferSize: writeBufSize,
  38. NetDial: func(net, addr string) (net.Conn, error) {
  39. return netConn, nil
  40. },
  41. }
  42. return d.Dial(u.String(), requestHeader)
  43. }
  44. // A Dialer contains options for connecting to WebSocket server.
  45. type Dialer struct {
  46. // NetDial specifies the dial function for creating TCP connections. If
  47. // NetDial is nil, net.Dial is used.
  48. NetDial func(network, addr string) (net.Conn, error)
  49. // Proxy specifies a function to return a proxy for a given
  50. // Request. If the function returns a non-nil error, the
  51. // request is aborted with the provided error.
  52. // If Proxy is nil or returns a nil *URL, no proxy is used.
  53. Proxy func(*http.Request) (*url.URL, error)
  54. // TLSClientConfig specifies the TLS configuration to use with tls.Client.
  55. // If nil, the default configuration is used.
  56. TLSClientConfig *tls.Config
  57. // HandshakeTimeout specifies the duration for the handshake to complete.
  58. HandshakeTimeout time.Duration
  59. // ReadBufferSize and WriteBufferSize specify I/O buffer sizes. If a buffer
  60. // size is zero, then a useful default size is used. The I/O buffer sizes
  61. // do not limit the size of the messages that can be sent or received.
  62. ReadBufferSize, WriteBufferSize int
  63. // Subprotocols specifies the client's requested subprotocols.
  64. Subprotocols []string
  65. // EnableCompression specifies if the client should attempt to negotiate
  66. // per message compression (RFC 7692). Setting this value to true does not
  67. // guarantee that compression will be supported. Currently only "no context
  68. // takeover" modes are supported.
  69. EnableCompression bool
  70. // Jar specifies the cookie jar.
  71. // If Jar is nil, cookies are not sent in requests and ignored
  72. // in responses.
  73. Jar http.CookieJar
  74. }
  75. var errMalformedURL = errors.New("malformed ws or wss URL")
  76. // parseURL parses the URL.
  77. //
  78. // This function is a replacement for the standard library url.Parse function.
  79. // In Go 1.4 and earlier, url.Parse loses information from the path.
  80. func parseURL(s string) (*url.URL, error) {
  81. // From the RFC:
  82. //
  83. // ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
  84. // wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
  85. var u url.URL
  86. switch {
  87. case strings.HasPrefix(s, "ws://"):
  88. u.Scheme = "ws"
  89. s = s[len("ws://"):]
  90. case strings.HasPrefix(s, "wss://"):
  91. u.Scheme = "wss"
  92. s = s[len("wss://"):]
  93. default:
  94. return nil, errMalformedURL
  95. }
  96. if i := strings.Index(s, "?"); i >= 0 {
  97. u.RawQuery = s[i+1:]
  98. s = s[:i]
  99. }
  100. if i := strings.Index(s, "/"); i >= 0 {
  101. u.Opaque = s[i:]
  102. s = s[:i]
  103. } else {
  104. u.Opaque = "/"
  105. }
  106. u.Host = s
  107. if strings.Contains(u.Host, "@") {
  108. // Don't bother parsing user information because user information is
  109. // not allowed in websocket URIs.
  110. return nil, errMalformedURL
  111. }
  112. return &u, nil
  113. }
  114. func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
  115. hostPort = u.Host
  116. hostNoPort = u.Host
  117. if i := strings.LastIndex(u.Host, ":"); i > strings.LastIndex(u.Host, "]") {
  118. hostNoPort = hostNoPort[:i]
  119. } else {
  120. switch u.Scheme {
  121. case "wss":
  122. hostPort += ":443"
  123. case "https":
  124. hostPort += ":443"
  125. default:
  126. hostPort += ":80"
  127. }
  128. }
  129. return hostPort, hostNoPort
  130. }
  131. // DefaultDialer is a dialer with all fields set to the default zero values.
  132. var DefaultDialer = &Dialer{
  133. Proxy: http.ProxyFromEnvironment,
  134. }
  135. // Dial creates a new client connection. Use requestHeader to specify the
  136. // origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
  137. // Use the response.Header to get the selected subprotocol
  138. // (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
  139. //
  140. // If the WebSocket handshake fails, ErrBadHandshake is returned along with a
  141. // non-nil *http.Response so that callers can handle redirects, authentication,
  142. // etcetera. The response body may not contain the entire response and does not
  143. // need to be closed by the application.
  144. func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
  145. if d == nil {
  146. d = &Dialer{
  147. Proxy: http.ProxyFromEnvironment,
  148. }
  149. }
  150. challengeKey, err := generateChallengeKey()
  151. if err != nil {
  152. return nil, nil, err
  153. }
  154. u, err := parseURL(urlStr)
  155. if err != nil {
  156. return nil, nil, err
  157. }
  158. switch u.Scheme {
  159. case "ws":
  160. u.Scheme = "http"
  161. case "wss":
  162. u.Scheme = "https"
  163. default:
  164. return nil, nil, errMalformedURL
  165. }
  166. if u.User != nil {
  167. // User name and password are not allowed in websocket URIs.
  168. return nil, nil, errMalformedURL
  169. }
  170. req := &http.Request{
  171. Method: "GET",
  172. URL: u,
  173. Proto: "HTTP/1.1",
  174. ProtoMajor: 1,
  175. ProtoMinor: 1,
  176. Header: make(http.Header),
  177. Host: u.Host,
  178. }
  179. // Set the cookies present in the cookie jar of the dialer
  180. if d.Jar != nil {
  181. for _, cookie := range d.Jar.Cookies(u) {
  182. req.AddCookie(cookie)
  183. }
  184. }
  185. // Set the request headers using the capitalization for names and values in
  186. // RFC examples. Although the capitalization shouldn't matter, there are
  187. // servers that depend on it. The Header.Set method is not used because the
  188. // method canonicalizes the header names.
  189. req.Header["Upgrade"] = []string{"websocket"}
  190. req.Header["Connection"] = []string{"Upgrade"}
  191. req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
  192. req.Header["Sec-WebSocket-Version"] = []string{"13"}
  193. if len(d.Subprotocols) > 0 {
  194. req.Header["Sec-WebSocket-Protocol"] = []string{strings.Join(d.Subprotocols, ", ")}
  195. }
  196. for k, vs := range requestHeader {
  197. switch {
  198. case k == "Host":
  199. if len(vs) > 0 {
  200. req.Host = vs[0]
  201. }
  202. case k == "Upgrade" ||
  203. k == "Connection" ||
  204. k == "Sec-Websocket-Key" ||
  205. k == "Sec-Websocket-Version" ||
  206. k == "Sec-Websocket-Extensions" ||
  207. (k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
  208. return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
  209. default:
  210. req.Header[k] = vs
  211. }
  212. }
  213. if d.EnableCompression {
  214. req.Header.Set("Sec-Websocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover")
  215. }
  216. hostPort, hostNoPort := hostPortNoPort(u)
  217. var proxyURL *url.URL
  218. // Check wether the proxy method has been configured
  219. if d.Proxy != nil {
  220. proxyURL, err = d.Proxy(req)
  221. }
  222. if err != nil {
  223. return nil, nil, err
  224. }
  225. var targetHostPort string
  226. if proxyURL != nil {
  227. targetHostPort, _ = hostPortNoPort(proxyURL)
  228. } else {
  229. targetHostPort = hostPort
  230. }
  231. var deadline time.Time
  232. if d.HandshakeTimeout != 0 {
  233. deadline = time.Now().Add(d.HandshakeTimeout)
  234. }
  235. netDial := d.NetDial
  236. if netDial == nil {
  237. netDialer := &net.Dialer{Deadline: deadline}
  238. netDial = netDialer.Dial
  239. }
  240. netConn, err := netDial("tcp", targetHostPort)
  241. if err != nil {
  242. return nil, nil, err
  243. }
  244. defer func() {
  245. if netConn != nil {
  246. netConn.Close()
  247. }
  248. }()
  249. if err := netConn.SetDeadline(deadline); err != nil {
  250. return nil, nil, err
  251. }
  252. if proxyURL != nil {
  253. connectHeader := make(http.Header)
  254. if user := proxyURL.User; user != nil {
  255. proxyUser := user.Username()
  256. if proxyPassword, passwordSet := user.Password(); passwordSet {
  257. credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
  258. connectHeader.Set("Proxy-Authorization", "Basic "+credential)
  259. }
  260. }
  261. connectReq := &http.Request{
  262. Method: "CONNECT",
  263. URL: &url.URL{Opaque: hostPort},
  264. Host: hostPort,
  265. Header: connectHeader,
  266. }
  267. connectReq.Write(netConn)
  268. // Read response.
  269. // Okay to use and discard buffered reader here, because
  270. // TLS server will not speak until spoken to.
  271. br := bufio.NewReader(netConn)
  272. resp, err := http.ReadResponse(br, connectReq)
  273. if err != nil {
  274. return nil, nil, err
  275. }
  276. if resp.StatusCode != 200 {
  277. f := strings.SplitN(resp.Status, " ", 2)
  278. return nil, nil, errors.New(f[1])
  279. }
  280. }
  281. if u.Scheme == "https" {
  282. cfg := cloneTLSConfig(d.TLSClientConfig)
  283. if cfg.ServerName == "" {
  284. cfg.ServerName = hostNoPort
  285. }
  286. tlsConn := tls.Client(netConn, cfg)
  287. netConn = tlsConn
  288. if err := tlsConn.Handshake(); err != nil {
  289. return nil, nil, err
  290. }
  291. if !cfg.InsecureSkipVerify {
  292. if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
  293. return nil, nil, err
  294. }
  295. }
  296. }
  297. conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
  298. if err := req.Write(netConn); err != nil {
  299. return nil, nil, err
  300. }
  301. resp, err := http.ReadResponse(conn.br, req)
  302. if err != nil {
  303. return nil, nil, err
  304. }
  305. if d.Jar != nil {
  306. if rc := resp.Cookies(); len(rc) > 0 {
  307. d.Jar.SetCookies(u, rc)
  308. }
  309. }
  310. if resp.StatusCode != 101 ||
  311. !strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
  312. !strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
  313. resp.Header.Get("Sec-Websocket-Accept") != computeAcceptKey(challengeKey) {
  314. // Before closing the network connection on return from this
  315. // function, slurp up some of the response to aid application
  316. // debugging.
  317. buf := make([]byte, 1024)
  318. n, _ := io.ReadFull(resp.Body, buf)
  319. resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n]))
  320. return nil, resp, ErrBadHandshake
  321. }
  322. for _, ext := range parseExtensions(req.Header) {
  323. if ext[""] != "permessage-deflate" {
  324. continue
  325. }
  326. _, snct := ext["server_no_context_takeover"]
  327. _, cnct := ext["client_no_context_takeover"]
  328. if !snct || !cnct {
  329. return nil, resp, errInvalidCompression
  330. }
  331. conn.newCompressionWriter = compressNoContextTakeover
  332. conn.newDecompressionReader = decompressNoContextTakeover
  333. break
  334. }
  335. resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
  336. conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")
  337. netConn.SetDeadline(time.Time{})
  338. netConn = nil // to avoid close in defer.
  339. return conn, resp, nil
  340. }