github.com/Psiphon-Labs/tls-tris@v0.0.0-20230824155421-58bf6d336a9a/_dev/patches/88253a956a753213617d95af3f42a23a78798473.patch (about) 1 From 88253a956a753213617d95af3f42a23a78798473 Mon Sep 17 00:00:00 2001 2 From: Filippo Valsorda <filippo@cloudflare.com> 3 Date: Mon, 28 Nov 2016 05:24:21 +0000 4 Subject: [PATCH] net/http: attach TLSConnContextKey to the request Context 5 6 Change-Id: Ic59c84f992c829dc7da741b128dd6899366fa1d2 7 --- 8 src/net/http/request.go | 4 +++- 9 src/net/http/server.go | 12 ++++++++++++ 10 2 files changed, 15 insertions(+), 1 deletion(-) 11 12 diff --git a/src/net/http/request.go b/src/net/http/request.go 13 index 13f367c1a8..b2827ff123 100644 14 --- a/src/net/http/request.go 15 +++ b/src/net/http/request.go 16 @@ -275,7 +275,9 @@ type Request struct { 17 // was received. This field is not filled in by ReadRequest. 18 // The HTTP server in this package sets the field for 19 // TLS-enabled connections before invoking a handler; 20 - // otherwise it leaves the field nil. 21 + // otherwise it leaves the field nil. The value is fixed 22 + // at the state of the connection immediately after Handshake, 23 + // for an immediate value use TLSConnContextKey. 24 // This field is ignored by the HTTP client. 25 TLS *tls.ConnectionState 26 27 diff --git a/src/net/http/server.go b/src/net/http/server.go 28 index 2fa8ab23d8..b0542cdbc3 100644 29 --- a/src/net/http/server.go 30 +++ b/src/net/http/server.go 31 @@ -223,6 +223,12 @@ var ( 32 // the local address the connection arrived on. 33 // The associated value will be of type net.Addr. 34 LocalAddrContextKey = &contextKey{"local-addr"} 35 + 36 + // TLSConnContextKey is a context key. It can be used in 37 + // HTTP handlers with context.WithValue to access the 38 + // underlying *tls.Conn being served. If the connection 39 + // is not TLS, the key is not set. 40 + TLSConnContextKey = &contextKey{"tls-conn"} 41 ) 42 43 // A conn represents the server side of an HTTP connection. 44 @@ -969,6 +975,9 @@ func (c *conn) readRequest(ctx context.Context) (w *response, err error) { 45 delete(req.Header, "Host") 46 47 ctx, cancelCtx := context.WithCancel(ctx) 48 + if tlsConn, ok := c.rwc.(*tls.Conn); ok { 49 + ctx = context.WithValue(ctx, TLSConnContextKey, tlsConn) 50 + } 51 req.ctx = ctx 52 req.RemoteAddr = c.remoteAddr 53 req.TLS = c.tlsState 54 @@ -3161,6 +3170,9 @@ func (h initNPNRequest) ServeHTTP(rw ResponseWriter, req *Request) { 55 if req.RemoteAddr == "" { 56 req.RemoteAddr = h.c.RemoteAddr().String() 57 } 58 + if req.ctx != nil && req.ctx.Value(TLSConnContextKey) == nil { 59 + req.ctx = context.WithValue(req.ctx, TLSConnContextKey, h.c) 60 + } 61 h.h.ServeHTTP(rw, req) 62 } 63