@marcel91200 / caddy Mirror · read-only / commits / 64f01739
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

http: Fix subroutes, ensure that next handlers can still be called

Matthew Holt committed Jan 12, 2020 at 13:39 UTC 64f0173948e9b87a8f898d451ef35daefedf2c50
3 files changed +9 -8
modules/caddyhttp/caddyhttp.go
+3 -3
@@ -172,7 +172,7 @@ func (app *App) Provision(ctx caddy.Context) error {
172 }
173 // pre-compile the handler chain, and be sure to wrap it in our
174 // route handler so that important security checks are done, etc.
175 - primaryRoute = srv.Routes.Compile()
175 + primaryRoute = srv.Routes.Compile(emptyHandler)
176 }
177 srv.primaryHandlerChain = srv.wrapPrimaryRoute(primaryRoute)
178
@@ -181,7 +181,7 @@ func (app *App) Provision(ctx caddy.Context) error {
181 if err != nil {
182 return fmt.Errorf("server %s: setting up server error handling routes: %v", srvName, err)
183 }
184 - srv.errorHandlerChain = srv.Errors.Routes.Compile()
184 + srv.errorHandlerChain = srv.Errors.Routes.Compile(emptyHandler)
185 }
186 }
187
@@ -546,7 +546,7 @@ func (app *App) automaticHTTPS() error {
546 tlsApp: tlsApp, // required to solve HTTP challenge
547 logger: app.logger.Named("log"),
548 errorLogger: app.logger.Named("log.error"),
549 - primaryHandlerChain: redirRoutes.Compile(),
549 + primaryHandlerChain: redirRoutes.Compile(emptyHandler),
550 }
551 }
552 }
modules/caddyhttp/routes.go
+3 -2
@@ -147,12 +147,12 @@ func (routes RouteList) Provision(ctx caddy.Context) error {
147 // Compile prepares a middleware chain from the route list.
148 // This should only be done once: after all the routes have
149 // been provisioned, and before serving requests.
150 -func (routes RouteList) Compile() Handler {
150 +func (routes RouteList) Compile(next Handler) Handler {
151 var mid []Middleware
152 for _, route := range routes {
153 mid = append(mid, wrapRoute(route))
154 }
155 - stack := emptyHandler
155 + stack := next
156 for i := len(mid) - 1; i >= 0; i-- {
157 stack = mid[i](stack)
158 }
@@ -167,6 +167,7 @@ func (routes RouteList) Compile() Handler {
167 func wrapRoute(route Route) Middleware {
168 return func(next Handler) Handler {
169 return HandlerFunc(func(rw http.ResponseWriter, req *http.Request) error {
170 + // TODO: Update this comment, it seems we've moved the copy into the handler?
171 // copy the next handler (it's an interface, so it's just
172 // a very lightweight copy of a pointer); this is important
173 // because this is a closure to the func below, which
modules/caddyhttp/subroute.go
+3 -3
@@ -69,12 +69,12 @@ func (sr *Subroute) Provision(ctx caddy.Context) error {
69 return nil
70 }
71
72 -func (sr *Subroute) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
73 - subroute := sr.Routes.Compile()
72 +func (sr *Subroute) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
73 + subroute := sr.Routes.Compile(next)
74 err := subroute.ServeHTTP(w, r)
75 if err != nil && sr.Errors != nil {
76 r = sr.Errors.WithError(r, err)
77 - errRoute := sr.Errors.Routes.Compile()
77 + errRoute := sr.Errors.Routes.Compile(next)
78 return errRoute.ServeHTTP(w, r)
79 }
80 return err