diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index 41a8e55b0..ebe911e81 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -935,10 +935,10 @@ func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter ctx = context.WithValue(ctx, ServerCtxKey, s) trusted, clientIP := determineTrustedProxy(r, s) - ctx = context.WithValue(ctx, VarsCtxKey, map[string]any{ - TrustedProxyVarKey: trusted, - ClientIPVarKey: clientIP, - }) + varsMap := &sync.Map{} + varsMap.Store(TrustedProxyVarKey, trusted) + varsMap.Store(ClientIPVarKey, clientIP) + ctx = context.WithValue(ctx, VarsCtxKey, varsMap) ctx = context.WithValue(ctx, routeGroupCtxKey, make(map[string]struct{})) diff --git a/modules/caddyhttp/vars.go b/modules/caddyhttp/vars.go index 68aaca331..6135f7d87 100644 --- a/modules/caddyhttp/vars.go +++ b/modules/caddyhttp/vars.go @@ -20,6 +20,7 @@ import ( "net/http" "reflect" "strings" + "sync" "github.com/google/cel-go/cel" "github.com/google/cel-go/common/types/ref" @@ -443,11 +444,12 @@ func (m MatchVarsRE) Validate() error { // GetVar gets a value out of the context's variable table by key. // If the key does not exist, the return value will be nil. func GetVar(ctx context.Context, key string) any { - varMap, ok := ctx.Value(VarsCtxKey).(map[string]any) + varMap, ok := ctx.Value(VarsCtxKey).(*sync.Map) if !ok { return nil } - return varMap[key] + val, _ := varMap.Load(key) + return val } // SetVar sets a value in the context's variable table with @@ -458,17 +460,15 @@ func GetVar(ctx context.Context, key string) any { // 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 any) { - varMap, ok := ctx.Value(VarsCtxKey).(map[string]any) + varMap, ok := ctx.Value(VarsCtxKey).(*sync.Map) if !ok { return } if value == nil { - if _, ok := varMap[key]; ok { - delete(varMap, key) - return - } + varMap.Delete(key) + return } - varMap[key] = value + varMap.Store(key, value) } // Interface guards