(u *url.URL)
| 596 | } |
| 597 | |
| 598 | func (nc *Conn) wsInitHandshake(u *url.URL) error { |
| 599 | compress := nc.Opts.Compression |
| 600 | tlsRequired := u.Scheme == wsSchemeTLS || nc.Opts.Secure || nc.Opts.TLSConfig != nil || nc.Opts.TLSCertCB != nil || nc.Opts.RootCAsCB != nil |
| 601 | // Do TLS here as needed. |
| 602 | if tlsRequired { |
| 603 | if err := nc.makeTLSConn(); err != nil { |
| 604 | return err |
| 605 | } |
| 606 | } else { |
| 607 | nc.bindToNewConn() |
| 608 | } |
| 609 | |
| 610 | var err error |
| 611 | |
| 612 | // For http request, we need the passed URL to contain either http or https scheme. |
| 613 | scheme := "http" |
| 614 | if tlsRequired { |
| 615 | scheme = "https" |
| 616 | } |
| 617 | ustr := fmt.Sprintf("%s://%s", scheme, u.Host) |
| 618 | |
| 619 | if nc.Opts.ProxyPath != "" { |
| 620 | proxyPath := nc.Opts.ProxyPath |
| 621 | if !strings.HasPrefix(proxyPath, "/") { |
| 622 | proxyPath = "/" + proxyPath |
| 623 | } |
| 624 | ustr += proxyPath |
| 625 | } |
| 626 | |
| 627 | u, err = url.Parse(ustr) |
| 628 | if err != nil { |
| 629 | return err |
| 630 | } |
| 631 | req := &http.Request{ |
| 632 | Method: "GET", |
| 633 | URL: u, |
| 634 | Proto: "HTTP/1.1", |
| 635 | ProtoMajor: 1, |
| 636 | ProtoMinor: 1, |
| 637 | Header: make(http.Header), |
| 638 | Host: u.Host, |
| 639 | } |
| 640 | wsKey, err := wsMakeChallengeKey() |
| 641 | if err != nil { |
| 642 | return err |
| 643 | } |
| 644 | |
| 645 | req.Header["Upgrade"] = []string{"websocket"} |
| 646 | req.Header["Connection"] = []string{"Upgrade"} |
| 647 | req.Header["Sec-WebSocket-Key"] = []string{wsKey} |
| 648 | req.Header["Sec-WebSocket-Version"] = []string{"13"} |
| 649 | if compress { |
| 650 | req.Header.Add("Sec-WebSocket-Extensions", wsPMCReqHeaderValue) |
| 651 | } |
| 652 | if err := nc.wsUpdateConnectionHeaders(req); err != nil { |
| 653 | return err |
| 654 | } |
| 655 | if err := req.Write(nc.conn); err != nil { |
no test coverage detected