|
@@ -273,7 +273,20 @@ func ConfigureServer(s *http.Server, conf *Server) error {
|
|
|
if testHookOnConn != nil {
|
|
if testHookOnConn != nil {
|
|
|
testHookOnConn()
|
|
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{
|
|
conf.ServeConn(c, &ServeConnOpts{
|
|
|
|
|
+ Context: ctx,
|
|
|
Handler: h,
|
|
Handler: h,
|
|
|
BaseConfig: hs,
|
|
BaseConfig: hs,
|
|
|
})
|
|
})
|
|
@@ -284,6 +297,10 @@ func ConfigureServer(s *http.Server, conf *Server) error {
|
|
|
|
|
|
|
|
// ServeConnOpts are options for the Server.ServeConn method.
|
|
// ServeConnOpts are options for the Server.ServeConn method.
|
|
|
type ServeConnOpts struct {
|
|
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
|
|
// BaseConfig optionally sets the base configuration
|
|
|
// for values. If nil, defaults are used.
|
|
// for values. If nil, defaults are used.
|
|
|
BaseConfig *http.Server
|
|
BaseConfig *http.Server
|
|
@@ -294,6 +311,13 @@ type ServeConnOpts struct {
|
|
|
Handler http.Handler
|
|
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 {
|
|
func (o *ServeConnOpts) baseConfig() *http.Server {
|
|
|
if o != nil && o.BaseConfig != nil {
|
|
if o != nil && o.BaseConfig != nil {
|
|
|
return o.BaseConfig
|
|
return o.BaseConfig
|
|
@@ -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()) {
|
|
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())
|
|
ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
|
|
|
if hs := opts.baseConfig(); hs != nil {
|
|
if hs := opts.baseConfig(); hs != nil {
|
|
|
ctx = context.WithValue(ctx, http.ServerContextKey, hs)
|
|
ctx = context.WithValue(ctx, http.ServerContextKey, hs)
|