MatchWithError returns true if r matches m.
(r *http.Request)
| 314 | |
| 315 | // MatchWithError returns true if r matches m. |
| 316 | func (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. |