mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-19 15:53:17 +00:00
caddyhttp: remove redundant middleware next copy (#7217)
This commit is contained in:
parent
806fef85be
commit
3553cfb6ad
1 changed files with 2 additions and 14 deletions
|
@ -302,13 +302,7 @@ func wrapRoute(route Route) Middleware {
|
||||||
|
|
||||||
// wrapMiddleware wraps mh such that it can be correctly
|
// wrapMiddleware wraps mh such that it can be correctly
|
||||||
// appended to a list of middleware in preparation for
|
// appended to a list of middleware in preparation for
|
||||||
// compiling into a handler chain. We can't do this inline
|
// compiling into a handler chain.
|
||||||
// inside a loop, because it relies on a reference to mh
|
|
||||||
// not changing until the execution of its handler (which
|
|
||||||
// is deferred by multiple func closures). In other words,
|
|
||||||
// we need to pull this particular MiddlewareHandler
|
|
||||||
// pointer into its own stack frame to preserve it so it
|
|
||||||
// won't be overwritten in future loop iterations.
|
|
||||||
func wrapMiddleware(ctx caddy.Context, mh MiddlewareHandler, metrics *Metrics) Middleware {
|
func wrapMiddleware(ctx caddy.Context, mh MiddlewareHandler, metrics *Metrics) Middleware {
|
||||||
handlerToUse := mh
|
handlerToUse := mh
|
||||||
if metrics != nil {
|
if metrics != nil {
|
||||||
|
@ -317,18 +311,12 @@ func wrapMiddleware(ctx caddy.Context, mh MiddlewareHandler, metrics *Metrics) M
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next Handler) Handler {
|
return func(next Handler) Handler {
|
||||||
// copy the next handler (it's an interface, so it's
|
|
||||||
// just a very lightweight copy of a pointer); this
|
|
||||||
// is a safeguard against the handler changing the
|
|
||||||
// value, which could affect future requests (yikes)
|
|
||||||
nextCopy := next
|
|
||||||
|
|
||||||
return HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
return HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||||
// EXPERIMENTAL: Trace each module that gets invoked
|
// EXPERIMENTAL: Trace each module that gets invoked
|
||||||
if server, ok := r.Context().Value(ServerCtxKey).(*Server); ok && server != nil {
|
if server, ok := r.Context().Value(ServerCtxKey).(*Server); ok && server != nil {
|
||||||
server.logTrace(handlerToUse)
|
server.logTrace(handlerToUse)
|
||||||
}
|
}
|
||||||
return handlerToUse.ServeHTTP(w, r, nextCopy)
|
return handlerToUse.ServeHTTP(w, r, next)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue