Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 11:45 UTC
caddyhttp: Support configuring Server from handler provisioning (#4933)
* configuring http.Server from handlers. * Minor tweaks * Run gofmt Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
WeidiDeng committed
Sep 17, 2022 at 04:48 UTC
bffc2587329ca7e1f8a28edae9b5a9bf11e2fc7c
2 files changed
+54
-1
modules/caddyhttp/app.go
+4
-1
@@ -178,7 +178,9 @@ func (app *App) Provision(ctx caddy.Context) error {
178
}
179
180
// prepare each server
181
+ oldContext := ctx.Context
182
for srvName, srv := range app.Servers {
183
+ ctx.Context = context.WithValue(oldContext, ServerCtxKey, srv)
184
srv.name = srvName
185
srv.tlsApp = app.tlsApp
186
srv.events = eventsAppIface.(*caddyevents.App)
@@ -293,7 +295,7 @@ func (app *App) Provision(ctx caddy.Context) error {
295
srv.IdleTimeout = defaultIdleTimeout
296
}
297
}
296
-
298
+ ctx.Context = oldContext
299
return nil
300
}
301
@@ -365,6 +367,7 @@ func (app *App) Start() error {
367
// this TLS config is used by the std lib to choose the actual TLS config for connections
368
// by looking through the connection policies to find the first one that matches
369
tlsCfg := srv.TLSConnPolicies.TLSConfig(app.ctx)
370
+ srv.configureServer(srv.server)
371
372
// enable H2C if configured
373
if srv.protocol("h2c") {
modules/caddyhttp/server.go
+50
@@ -176,6 +176,11 @@ type Server struct {
176
177
shutdownAt time.Time
178
shutdownAtMu *sync.RWMutex
179
+
180
+ // registered callback functions
181
+ connStateFuncs []func(net.Conn, http.ConnState)
182
+ connContextFuncs []func(ctx context.Context, c net.Conn) context.Context
183
+ onShutdownFuncs []func()
184
}
185
186
// ServeHTTP is the entry point for all HTTP requests.
@@ -513,6 +518,51 @@ func (s *Server) serveHTTP3(hostport string, tlsCfg *tls.Config) error {
518
return nil
519
}
520
521
+// configureServer applies/binds the registered callback functions to the server.
522
+func (s *Server) configureServer(server *http.Server) {
523
+ for _, f := range s.connStateFuncs {
524
+ if server.ConnState != nil {
525
+ baseConnStateFunc := server.ConnState
526
+ server.ConnState = func(conn net.Conn, state http.ConnState) {
527
+ baseConnStateFunc(conn, state)
528
+ f(conn, state)
529
+ }
530
+ } else {
531
+ server.ConnState = f
532
+ }
533
+ }
534
+
535
+ for _, f := range s.connContextFuncs {
536
+ if server.ConnContext != nil {
537
+ baseConnContextFunc := server.ConnContext
538
+ server.ConnContext = func(ctx context.Context, c net.Conn) context.Context {
539
+ return f(baseConnContextFunc(ctx, c), c)
540
+ }
541
+ } else {
542
+ server.ConnContext = f
543
+ }
544
+ }
545
+
546
+ for _, f := range s.onShutdownFuncs {
547
+ server.RegisterOnShutdown(f)
548
+ }
549
+}
550
+
551
+// RegisterConnState registers f to be invoked on s.ConnState.
552
+func (s *Server) RegisterConnState(f func(net.Conn, http.ConnState)) {
553
+ s.connStateFuncs = append(s.connStateFuncs, f)
554
+}
555
+
556
+// RegisterConnContext registers f to be invoked as part of s.ConnContext.
557
+func (s *Server) RegisterConnContext(f func(ctx context.Context, c net.Conn) context.Context) {
558
+ s.connContextFuncs = append(s.connContextFuncs, f)
559
+}
560
+
561
+// RegisterOnShutdown registers f to be invoked on server shutdown.
562
+func (s *Server) RegisterOnShutdown(f func()) {
563
+ s.onShutdownFuncs = append(s.onShutdownFuncs, f)
564
+}
565
+
566
// HTTPErrorConfig determines how to handle errors
567
// from the HTTP handlers.
568
type HTTPErrorConfig struct {