Skip to content

Expose tls connection state in http response #967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
}
}()

var tlsState *tls.ConnectionState
if u.Scheme == "https" && d.NetDialTLSContext == nil {
// If NetDialTLSContext is set, assume that the TLS handshake has already been done

Expand All @@ -330,13 +331,18 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
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 {
if err := doHandshake(ctx, tlsConn, cfg); err != nil {
if trace != nil && trace.TLSHandshakeDone != nil {
trace.TLSHandshakeDone(tls.ConnectionState{}, err)
}
return nil, nil, err
} else {
cs := tlsConn.ConnectionState()
if trace != nil && trace.TLSHandshakeDone != nil {
trace.TLSHandshakeDone(cs, nil)
}
tlsState = &cs
}
}

Expand Down Expand Up @@ -374,6 +380,10 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
}
}

if resp.TLS == nil && tlsState != nil {
resp.TLS = tlsState
}

if resp.StatusCode != 101 ||
!tokenListContainsValue(resp.Header, "Upgrade", "websocket") ||
!tokenListContainsValue(resp.Header, "Connection", "upgrade") ||
Expand Down
19 changes: 19 additions & 0 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ func TestDialTLS(t *testing.T) {
sendRecv(t, ws)
}

func TestDialTLSConnState(t *testing.T) {
s := newTLSServer(t)
defer s.Close()

d := cstDialer
d.TLSClientConfig = &tls.Config{RootCAs: rootCAs(t, s.Server)}
ws, resp, err := d.Dial(s.URL, nil)
if err != nil {
t.Fatalf("Dial: %v", err)
}
if resp.TLS == nil {
t.Errorf("http response tls is nil")
} else if len(resp.TLS.PeerCertificates) == 0 {
t.Errorf("http response PeerCertificates count is 0")
}
defer ws.Close()
sendRecv(t, ws)
}

func TestDialTimeout(t *testing.T) {
s := newServer(t)
defer s.Close()
Expand Down