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

Method MatchWithError

modules/caddyhttp/vars.go:176–222  ·  view source on GitHub ↗

MatchWithError returns true if r matches m.

(r *http.Request)

Source from the content-addressed store, hash-verified

174
175// MatchWithError returns true if r matches m.
176func (m VarsMatcher) MatchWithError(r *http.Request) (bool, error) {
177 if len(m) == 0 {
178 return true, nil
179 }
180
181 vars := r.Context().Value(VarsCtxKey).(map[string]any)
182 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
183
184 var matcherValExpanded, varStr, v string
185 var varValue any
186 for key, vals := range m {
187 if strings.HasPrefix(key, "{") &&
188 strings.HasSuffix(key, "}") &&
189 strings.Count(key, "{") == 1 {
190 varValue, _ = repl.Get(strings.Trim(key, "{}"))
191 } else {
192 varValue = vars[key]
193 }
194
195 switch vv := varValue.(type) {
196 case string:
197 varStr = vv
198 case fmt.Stringer:
199 varStr = vv.String()
200 case error:
201 varStr = vv.Error()
202 case nil:
203 varStr = ""
204 default:
205 varStr = fmt.Sprintf("%v", vv)
206 }
207
208 // Don't expand placeholders in values from literal variable names
209 // (e.g. map outputs) or other placeholders. These values are
210 // already final and must not be re-expanded, as that would allow
211 // user input like {env.SECRET} to be evaluated.
212
213 // see if any of the values given in the matcher match the actual value
214 for _, v = range vals {
215 matcherValExpanded = repl.ReplaceAll(v, "")
216 if varStr == matcherValExpanded {
217 return true, nil
218 }
219 }
220 }
221 return false, nil
222}
223
224// CELLibrary produces options that expose this matcher for use in CEL
225// expression matchers.

Callers 1

MatchMethod · 0.95

Calls 5

ReplaceAllMethod · 0.80
GetMethod · 0.65
ValueMethod · 0.45
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected