Commit 6433bff2 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: minor fixes and optimization for Response.TLS

Also add it to doc/go1.3.txt.

Update #7289

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/71740043
parent f40872d3
...@@ -8,6 +8,7 @@ crypto/x509: support CSRs (CL 49830048) ...@@ -8,6 +8,7 @@ crypto/x509: support CSRs (CL 49830048)
liblink: pull linker i/o into separate liblink C library (CL 35790044) liblink: pull linker i/o into separate liblink C library (CL 35790044)
misc/benchcmp: removed and replaced by go.tools/cmd/benchcmp (CL 47980043) misc/benchcmp: removed and replaced by go.tools/cmd/benchcmp (CL 47980043)
misc/dist: renamed misc/makerelease (CL 39920043) misc/dist: renamed misc/makerelease (CL 39920043)
net/http: add Request.TLS (CL 52660047)
net/http: add Server.ErrorLog; log and test TLS handshake errors (CL 70250044) net/http: add Server.ErrorLog; log and test TLS handshake errors (CL 70250044)
net/http: add Server.SetKeepAlivesEnabled (CL 69670043) net/http: add Server.SetKeepAlivesEnabled (CL 69670043)
net/http: add Transport.TLSHandshakeTimeout; set it by default (CL 68150045) net/http: add Transport.TLSHandshakeTimeout; set it by default (CL 68150045)
......
...@@ -727,14 +727,13 @@ func TestResponseSetsTLSConnectionState(t *testing.T) { ...@@ -727,14 +727,13 @@ func TestResponseSetsTLSConnectionState(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer res.Body.Close()
if res.TLS == nil { if res.TLS == nil {
t.Fatal("Response didn't set TLS Connection State.") t.Fatal("Response didn't set TLS Connection State.")
} }
if res.TLS.CipherSuite != tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA { if got, want := res.TLS.CipherSuite, tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA; got != want {
t.Errorf("Unexpected TLS Cipher Suite: %d != %d", t.Errorf("TLS Cipher Suite = %d; want %d", got, want)
res.TLS.CipherSuite, tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA)
} }
res.Body.Close()
} }
// Verify Response.ContentLength is populated. http://golang.org/issue/4126 // Verify Response.ContentLength is populated. http://golang.org/issue/4126
......
...@@ -76,10 +76,10 @@ type Response struct { ...@@ -76,10 +76,10 @@ type Response struct {
// This is only populated for Client requests. // This is only populated for Client requests.
Request *Request Request *Request
// TLS allows information about the TLS connection on which the // TLS contains information about the TLS connection on which the
// response was received. The Transport in this package sets the field // response was received. It is nil for unencrypted responses.
// for TLS-enabled connections before returning the Response otherwise // The pointer is shared between responses and should not be
// it leaves the field nil. // modified.
TLS *tls.ConnectionState TLS *tls.ConnectionState
} }
......
...@@ -583,6 +583,8 @@ func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) { ...@@ -583,6 +583,8 @@ func (t *Transport) dialConn(cm connectMethod) (*persistConn, error) {
return nil, err return nil, err
} }
} }
cs := tlsConn.ConnectionState()
pconn.tlsState = &cs
pconn.conn = tlsConn pconn.conn = tlsConn
} }
...@@ -718,6 +720,7 @@ type persistConn struct { ...@@ -718,6 +720,7 @@ type persistConn struct {
t *Transport t *Transport
cacheKey connectMethodKey cacheKey connectMethodKey
conn net.Conn conn net.Conn
tlsState *tls.ConnectionState
closed bool // whether conn has been closed closed bool // whether conn has been closed
br *bufio.Reader // from conn br *bufio.Reader // from conn
bw *bufio.Writer // to conn bw *bufio.Writer // to conn
...@@ -792,9 +795,8 @@ func (pc *persistConn) readLoop() { ...@@ -792,9 +795,8 @@ func (pc *persistConn) readLoop() {
} }
} }
if tlsConn, ok := pc.conn.(*tls.Conn); resp != nil && ok { if resp != nil {
resp.TLS = new(tls.ConnectionState) resp.TLS = pc.tlsState
*resp.TLS = tlsConn.ConnectionState()
} }
hasBody := resp != nil && rc.req.Method != "HEAD" && resp.ContentLength != 0 hasBody := resp != nil && rc.req.Method != "HEAD" && resp.ContentLength != 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment