MCPcopy
hub / github.com/nats-io/nats.go / wsInitHandshake

Method wsInitHandshake

ws.go:598–704  ·  view source on GitHub ↗
(u *url.URL)

Source from the content-addressed store, hash-verified

596}
597
598func (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 {

Callers 1

createConnMethod · 0.95

Calls 13

makeTLSConnMethod · 0.95
bindToNewConnMethod · 0.95
wsMakeChallengeKeyFunction · 0.85
wsAcceptKeyFunction · 0.85
wsPMCExtensionSupportFunction · 0.85
wsNewReaderFunction · 0.85
SetReadDeadlineMethod · 0.80
BufferedMethod · 0.80
AddMethod · 0.65
GetMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected