caddyhttp: Clear out matcher error immediately after grabbing it (#4916)

Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
This commit is contained in:
Francis Lavoie 2022-07-28 17:19:48 -04:00 committed by GitHub
parent 4fced0b6e1
commit ff2ba6de8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View file

@ -204,6 +204,10 @@ func wrapRoute(route Route) Middleware {
// the request and trigger the error handling chain // the request and trigger the error handling chain
err, ok := GetVar(req.Context(), MatcherErrorVarKey).(error) err, ok := GetVar(req.Context(), MatcherErrorVarKey).(error)
if ok { if ok {
// clear out the error from context, otherwise
// it will cascade to the error routes (#4916)
SetVar(req.Context(), MatcherErrorVarKey, nil)
// return the matcher's error
return err return err
} }

View file

@ -301,11 +301,21 @@ func GetVar(ctx context.Context, key string) interface{} {
// SetVar sets a value in the context's variable table with // SetVar sets a value in the context's variable table with
// the given key. It overwrites any previous value with the // the given key. It overwrites any previous value with the
// same key. // same key.
//
// If the value is nil (note: non-nil interface with nil
// underlying value does not count) and the key exists in
// the table, the key+value will be deleted from the table.
func SetVar(ctx context.Context, key string, value interface{}) { func SetVar(ctx context.Context, key string, value interface{}) {
varMap, ok := ctx.Value(VarsCtxKey).(map[string]interface{}) varMap, ok := ctx.Value(VarsCtxKey).(map[string]interface{})
if !ok { if !ok {
return return
} }
if value == nil {
if _, ok := varMap[key]; ok {
delete(varMap, key)
return
}
}
varMap[key] = value varMap[key] = value
} }