MCPcopy
hub / github.com/caddyserver/caddy / MatchWithError

Method MatchWithError

modules/caddyhttp/vars.go:316–355  ·  view source on GitHub ↗

MatchWithError returns true if r matches m.

(r *http.Request)

Source from the content-addressed store, hash-verified

314
315// MatchWithError returns true if r matches m.
316func (m MatchVarsRE) MatchWithError(r *http.Request) (bool, error) {
317 vars := r.Context().Value(VarsCtxKey).(map[string]any)
318 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
319
320 var match bool
321 var varStr string
322 var varValue any
323 for key, val := range m {
324 if strings.HasPrefix(key, "{") &&
325 strings.HasSuffix(key, "}") &&
326 strings.Count(key, "{") == 1 {
327 varValue, _ = repl.Get(strings.Trim(key, "{}"))
328 } else {
329 varValue = vars[key]
330 }
331
332 switch vv := varValue.(type) {
333 case string:
334 varStr = vv
335 case fmt.Stringer:
336 varStr = vv.String()
337 case error:
338 varStr = vv.Error()
339 case nil:
340 varStr = ""
341 default:
342 varStr = fmt.Sprintf("%v", vv)
343 }
344
345 // Don't expand placeholders in values from literal variable names
346 // (e.g. map outputs) or other placeholders. These values are
347 // already final and must not be re-expanded, as that would allow
348 // user input like {env.SECRET} to be evaluated.
349
350 if match = val.Match(varStr, repl); match {
351 return match, nil
352 }
353 }
354 return false, nil
355}
356
357// CELLibrary produces options that expose this matcher for use in CEL
358// expression matchers.

Callers 1

MatchMethod · 0.95

Calls 5

GetMethod · 0.65
MatchMethod · 0.65
ValueMethod · 0.45
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected