Skip to content

Commit

Permalink
http2: support getting the Server connection's base context from net/…
Browse files Browse the repository at this point in the history
…http

This is the x/net/http2 half of the fix. The net/http half is in CL 181260.

Updates golang/go#32476
Updates golang/go#30694

Change-Id: Ic25c678dad99acc4ae8d679384d9e9a38dc1291c
Reviewed-on: https://go-review.googlesource.com/c/net/+/181259
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
bradfitz committed Jun 7, 2019
1 parent 26fcbda commit 461777f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion http2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,20 @@ func ConfigureServer(s *http.Server, conf *Server) error {
if testHookOnConn != nil {
testHookOnConn()
}
// The TLSNextProto interface predates contexts, so
// the net/http package passes down its per-connection
// base context via an exported but unadvertised
// method on the Handler. This is for internal
// net/http<=>http2 use only.
var ctx context.Context
type baseContexter interface {
BaseContext() context.Context
}
if bc, ok := h.(baseContexter); ok {
ctx = bc.BaseContext()
}
conf.ServeConn(c, &ServeConnOpts{
Context: ctx,
Handler: h,
BaseConfig: hs,
})
Expand All @@ -284,6 +297,10 @@ func ConfigureServer(s *http.Server, conf *Server) error {

// ServeConnOpts are options for the Server.ServeConn method.
type ServeConnOpts struct {
// Context is the base context to use.
// If nil, context.Background is used.
Context context.Context

// BaseConfig optionally sets the base configuration
// for values. If nil, defaults are used.
BaseConfig *http.Server
Expand All @@ -294,6 +311,13 @@ type ServeConnOpts struct {
Handler http.Handler
}

func (o *ServeConnOpts) context() context.Context {
if o.Context != nil {
return o.Context
}
return context.Background()
}

func (o *ServeConnOpts) baseConfig() *http.Server {
if o != nil && o.BaseConfig != nil {
return o.BaseConfig
Expand Down Expand Up @@ -439,7 +463,7 @@ func (s *Server) ServeConn(c net.Conn, opts *ServeConnOpts) {
}

func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx context.Context, cancel func()) {
ctx, cancel = context.WithCancel(context.Background())
ctx, cancel = context.WithCancel(opts.context())
ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
if hs := opts.baseConfig(); hs != nil {
ctx = context.WithValue(ctx, http.ServerContextKey, hs)
Expand Down

0 comments on commit 461777f

Please sign in to comment.