mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	http: Fix subroutes, ensure that next handlers can still be called
This commit is contained in:
		
							parent
							
								
									fe5a531c58
								
							
						
					
					
						commit
						64f0173948
					
				@ -172,7 +172,7 @@ func (app *App) Provision(ctx caddy.Context) error {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			// pre-compile the handler chain, and be sure to wrap it in our
 | 
								// pre-compile the handler chain, and be sure to wrap it in our
 | 
				
			||||||
			// route handler so that important security checks are done, etc.
 | 
								// route handler so that important security checks are done, etc.
 | 
				
			||||||
			primaryRoute = srv.Routes.Compile()
 | 
								primaryRoute = srv.Routes.Compile(emptyHandler)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		srv.primaryHandlerChain = srv.wrapPrimaryRoute(primaryRoute)
 | 
							srv.primaryHandlerChain = srv.wrapPrimaryRoute(primaryRoute)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -181,7 +181,7 @@ func (app *App) Provision(ctx caddy.Context) error {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return fmt.Errorf("server %s: setting up server error handling routes: %v", srvName, err)
 | 
									return fmt.Errorf("server %s: setting up server error handling routes: %v", srvName, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			srv.errorHandlerChain = srv.Errors.Routes.Compile()
 | 
								srv.errorHandlerChain = srv.Errors.Routes.Compile(emptyHandler)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -546,7 +546,7 @@ func (app *App) automaticHTTPS() error {
 | 
				
			|||||||
				tlsApp:              tlsApp, // required to solve HTTP challenge
 | 
									tlsApp:              tlsApp, // required to solve HTTP challenge
 | 
				
			||||||
				logger:              app.logger.Named("log"),
 | 
									logger:              app.logger.Named("log"),
 | 
				
			||||||
				errorLogger:         app.logger.Named("log.error"),
 | 
									errorLogger:         app.logger.Named("log.error"),
 | 
				
			||||||
				primaryHandlerChain: redirRoutes.Compile(),
 | 
									primaryHandlerChain: redirRoutes.Compile(emptyHandler),
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -147,12 +147,12 @@ func (routes RouteList) Provision(ctx caddy.Context) error {
 | 
				
			|||||||
// Compile prepares a middleware chain from the route list.
 | 
					// Compile prepares a middleware chain from the route list.
 | 
				
			||||||
// This should only be done once: after all the routes have
 | 
					// This should only be done once: after all the routes have
 | 
				
			||||||
// been provisioned, and before serving requests.
 | 
					// been provisioned, and before serving requests.
 | 
				
			||||||
func (routes RouteList) Compile() Handler {
 | 
					func (routes RouteList) Compile(next Handler) Handler {
 | 
				
			||||||
	var mid []Middleware
 | 
						var mid []Middleware
 | 
				
			||||||
	for _, route := range routes {
 | 
						for _, route := range routes {
 | 
				
			||||||
		mid = append(mid, wrapRoute(route))
 | 
							mid = append(mid, wrapRoute(route))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	stack := emptyHandler
 | 
						stack := next
 | 
				
			||||||
	for i := len(mid) - 1; i >= 0; i-- {
 | 
						for i := len(mid) - 1; i >= 0; i-- {
 | 
				
			||||||
		stack = mid[i](stack)
 | 
							stack = mid[i](stack)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -167,6 +167,7 @@ func (routes RouteList) Compile() Handler {
 | 
				
			|||||||
func wrapRoute(route Route) Middleware {
 | 
					func wrapRoute(route Route) Middleware {
 | 
				
			||||||
	return func(next Handler) Handler {
 | 
						return func(next Handler) Handler {
 | 
				
			||||||
		return HandlerFunc(func(rw http.ResponseWriter, req *http.Request) error {
 | 
							return HandlerFunc(func(rw http.ResponseWriter, req *http.Request) error {
 | 
				
			||||||
 | 
								// TODO: Update this comment, it seems we've moved the copy into the handler?
 | 
				
			||||||
			// copy the next handler (it's an interface, so it's just
 | 
								// copy the next handler (it's an interface, so it's just
 | 
				
			||||||
			// a very lightweight copy of a pointer); this is important
 | 
								// a very lightweight copy of a pointer); this is important
 | 
				
			||||||
			// because this is a closure to the func below, which
 | 
								// because this is a closure to the func below, which
 | 
				
			||||||
 | 
				
			|||||||
@ -69,12 +69,12 @@ func (sr *Subroute) Provision(ctx caddy.Context) error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (sr *Subroute) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
 | 
					func (sr *Subroute) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
 | 
				
			||||||
	subroute := sr.Routes.Compile()
 | 
						subroute := sr.Routes.Compile(next)
 | 
				
			||||||
	err := subroute.ServeHTTP(w, r)
 | 
						err := subroute.ServeHTTP(w, r)
 | 
				
			||||||
	if err != nil && sr.Errors != nil {
 | 
						if err != nil && sr.Errors != nil {
 | 
				
			||||||
		r = sr.Errors.WithError(r, err)
 | 
							r = sr.Errors.WithError(r, err)
 | 
				
			||||||
		errRoute := sr.Errors.Routes.Compile()
 | 
							errRoute := sr.Errors.Routes.Compile(next)
 | 
				
			||||||
		return errRoute.ServeHTTP(w, r)
 | 
							return errRoute.ServeHTTP(w, r)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user