Skip to content

Commit

Permalink
Use context.Context in TLS handshake (#751)
Browse files Browse the repository at this point in the history
Continued work on #730.
  • Loading branch information
garyburd committed Jan 1, 2022
1 parent 2c89656 commit bcef843
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -67,4 +67,4 @@ workflows:
- test:
matrix:
parameters:
version: ["latest", "1.15", "1.14", "1.13", "1.12", "1.11"]
version: ["latest", "1.17", "1.16", "1.15", "1.14", "1.13", "1.12", "1.11"]
23 changes: 6 additions & 17 deletions client.go
Expand Up @@ -314,11 +314,12 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
tlsConn := tls.Client(netConn, cfg)
netConn = tlsConn

var err error
if trace != nil {
err = doHandshakeWithTrace(trace, tlsConn, cfg)
} else {
err = doHandshake(tlsConn, cfg)
if trace != nil && trace.TLSHandshakeStart != nil {
trace.TLSHandshakeStart()
}
err := doHandshake(ctx, tlsConn, cfg)
if trace != nil && trace.TLSHandshakeDone != nil {
trace.TLSHandshakeDone(tlsConn.ConnectionState(), err)
}

if err != nil {
Expand Down Expand Up @@ -383,15 +384,3 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
netConn = nil // to avoid close in defer.
return conn, resp, nil
}

func doHandshake(tlsConn *tls.Conn, cfg *tls.Config) error {
if err := tlsConn.Handshake(); err != nil {
return err
}
if !cfg.InsecureSkipVerify {
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
return err
}
}
return nil
}
21 changes: 21 additions & 0 deletions tls_handshake.go
@@ -0,0 +1,21 @@
//go:build go1.17
// +build go1.17

package websocket

import (
"context"
"crypto/tls"
)

func doHandshake(ctx context.Context, tlsConn *tls.Conn, cfg *tls.Config) error {
if err := tlsConn.HandshakeContext(ctx); err != nil {
return err
}
if !cfg.InsecureSkipVerify {
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
return err
}
}
return nil
}
21 changes: 21 additions & 0 deletions tls_handshake_116.go
@@ -0,0 +1,21 @@
//go:build !go1.17
// +build !go1.17

package websocket

import (
"context"
"crypto/tls"
)

func doHandshake(ctx context.Context, tlsConn *tls.Conn, cfg *tls.Config) error {
if err := tlsConn.Handshake(); err != nil {
return err
}
if !cfg.InsecureSkipVerify {
if err := tlsConn.VerifyHostname(cfg.ServerName); err != nil {
return err
}
}
return nil
}
20 changes: 0 additions & 20 deletions trace.go

This file was deleted.

13 changes: 0 additions & 13 deletions trace_17.go

This file was deleted.

0 comments on commit bcef843

Please sign in to comment.