Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))

Expand Down
16 changes: 8 additions & 8 deletions modules/caddyhttp/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"reflect"
"strings"
"sync"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types/ref"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading