Skip to content

Commit 5413abc

Browse files
WeidiDenggopherbot
authored andcommitted
net/http: set Request.TLS when net.Conn implements ConnectionState
Fixes #56104 Change-Id: I8fbbb00379e51323e2782144070cbcad650eb6f1 GitHub-Last-Rev: 62d7a80 GitHub-Pull-Request: #56110 Reviewed-on: https://go-review.googlesource.com/c/go/+/440795 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 786e62b commit 5413abc

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/net/http/serve_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,53 @@ func testTLSServer(t *testing.T, mode testMode) {
16451645
}
16461646
}
16471647

1648+
type fakeConnectionStateConn struct {
1649+
net.Conn
1650+
}
1651+
1652+
func (fcsc *fakeConnectionStateConn) ConnectionState() tls.ConnectionState {
1653+
return tls.ConnectionState{
1654+
ServerName: "example.com",
1655+
}
1656+
}
1657+
1658+
func TestTLSServerWithoutTLSConn(t *testing.T) {
1659+
//set up
1660+
pr, pw := net.Pipe()
1661+
c := make(chan int)
1662+
listener := &oneConnListener{&fakeConnectionStateConn{pr}}
1663+
server := &Server{
1664+
Handler: HandlerFunc(func(writer ResponseWriter, request *Request) {
1665+
if request.TLS == nil {
1666+
t.Fatal("request.TLS is nil, expected not nil")
1667+
}
1668+
if request.TLS.ServerName != "example.com" {
1669+
t.Fatalf("request.TLS.ServerName is %s, expected %s", request.TLS.ServerName, "example.com")
1670+
}
1671+
writer.Header().Set("X-TLS-ServerName", "example.com")
1672+
}),
1673+
}
1674+
1675+
// write request and read response
1676+
go func() {
1677+
req, _ := NewRequest(MethodGet, "https://example.com", nil)
1678+
req.Write(pw)
1679+
1680+
resp, _ := ReadResponse(bufio.NewReader(pw), req)
1681+
if hdr := resp.Header.Get("X-TLS-ServerName"); hdr != "example.com" {
1682+
t.Errorf("response header X-TLS-ServerName is %s, expected %s", hdr, "example.com")
1683+
}
1684+
close(c)
1685+
pw.Close()
1686+
}()
1687+
1688+
server.Serve(listener)
1689+
1690+
// oneConnListener returns error after one accept, wait util response is read
1691+
<-c
1692+
pr.Close()
1693+
}
1694+
16481695
func TestServeTLS(t *testing.T) {
16491696
CondSkipHTTP2(t)
16501697
// Not parallel: uses global test hooks.

src/net/http/server.go

+12
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,10 @@ func isCommonNetReadError(err error) bool {
19241924
return false
19251925
}
19261926

1927+
type connectionStater interface {
1928+
ConnectionState() tls.ConnectionState
1929+
}
1930+
19271931
// Serve a new connection.
19281932
func (c *conn) serve(ctx context.Context) {
19291933
if ra := c.rwc.RemoteAddr(); ra != nil {
@@ -1996,6 +2000,14 @@ func (c *conn) serve(ctx context.Context) {
19962000

19972001
// HTTP/1.x from here on.
19982002

2003+
// Set Request.TLS if the conn is not a *tls.Conn, but implements ConnectionState.
2004+
if c.tlsState == nil {
2005+
if tc, ok := c.rwc.(connectionStater); ok {
2006+
c.tlsState = new(tls.ConnectionState)
2007+
*c.tlsState = tc.ConnectionState()
2008+
}
2009+
}
2010+
19992011
ctx, cancelCtx := context.WithCancel(ctx)
20002012
c.cancelCtx = cancelCtx
20012013
defer cancelCtx()

0 commit comments

Comments
 (0)