Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

64 lignes
2.4 KiB

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