summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/matchers.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2021-02-11 16:27:09 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2021-02-11 16:27:09 -0700
commitcc63c5805e1587d7e5901deaad3f5bac944a2cf6 (patch)
tree174d047375d6c57e4e18f9a92570a6d1ad582b4e /modules/caddyhttp/matchers.go
parent51e3fdba7738e577b5ccfbad5bd7712fa594afc0 (diff)
caddyhttp: Support placeholders in header matcher values (close #3916)
Diffstat (limited to 'modules/caddyhttp/matchers.go')
-rw-r--r--modules/caddyhttp/matchers.go10
1 files changed, 7 insertions, 3 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go
index b77677b..a4f4502 100644
--- a/modules/caddyhttp/matchers.go
+++ b/modules/caddyhttp/matchers.go
@@ -513,7 +513,8 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Match returns true if r matches m.
func (m MatchHeader) Match(r *http.Request) bool {
- return matchHeaders(r.Header, http.Header(m), r.Host)
+ repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
+ return matchHeaders(r.Header, http.Header(m), r.Host, repl)
}
// getHeaderFieldVals returns the field values for the given fieldName from input.
@@ -530,7 +531,7 @@ func getHeaderFieldVals(input http.Header, fieldName, host string) []string {
// matchHeaders returns true if input matches the criteria in against without regex.
// The host parameter should be obtained from the http.Request.Host field since
// net/http removes it from the header map.
-func matchHeaders(input, against http.Header, host string) bool {
+func matchHeaders(input, against http.Header, host string, repl *caddy.Replacer) bool {
for field, allowedFieldVals := range against {
actualFieldVals := getHeaderFieldVals(input, field, host)
if allowedFieldVals != nil && len(allowedFieldVals) == 0 && actualFieldVals != nil {
@@ -546,6 +547,9 @@ func matchHeaders(input, against http.Header, host string) bool {
fieldVals:
for _, actualFieldVal := range actualFieldVals {
for _, allowedFieldVal := range allowedFieldVals {
+ if repl != nil {
+ allowedFieldVal = repl.ReplaceAll(allowedFieldVal, "")
+ }
switch {
case allowedFieldVal == "*":
match = true
@@ -985,7 +989,7 @@ func (rm ResponseMatcher) Match(statusCode int, hdr http.Header) bool {
if !rm.matchStatusCode(statusCode) {
return false
}
- return matchHeaders(hdr, rm.Headers, "")
+ return matchHeaders(hdr, rm.Headers, "", nil)
}
func (rm ResponseMatcher) matchStatusCode(statusCode int) bool {