summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2020-10-31 12:27:01 -0400
committerGitHub <noreply@github.com>2020-10-31 10:27:01 -0600
commit966d5e6b42fc6da3da8bd39dd6ceceb8f1da3999 (patch)
tree2b763f98d8673c3d5c8795fbafb1313d091a06d8
parentb66099379d065ec3340cbe147f65dcf0a39e8e52 (diff)
caddyhttp: Merge header matchers in Caddyfile (#3832)
-rw-r--r--caddytest/integration/caddyfile_adapt/matcher_syntax.txt28
-rw-r--r--modules/caddyhttp/matchers.go11
2 files changed, 38 insertions, 1 deletions
diff --git a/caddytest/integration/caddyfile_adapt/matcher_syntax.txt b/caddytest/integration/caddyfile_adapt/matcher_syntax.txt
index 53ec023..55c06ea 100644
--- a/caddytest/integration/caddyfile_adapt/matcher_syntax.txt
+++ b/caddytest/integration/caddyfile_adapt/matcher_syntax.txt
@@ -18,6 +18,13 @@
@matcher6 vars_regexp "{http.request.uri}" `\.([a-f0-9]{6})\.(css|js)$`
respond @matcher6 "from vars_regexp matcher without name"
+
+ @matcher7 {
+ header Foo bar
+ header Foo foobar
+ header Bar foo
+ }
+ respond @matcher7 "header matcher merging values of the same field"
}
----------
{
@@ -127,6 +134,27 @@
"handler": "static_response"
}
]
+ },
+ {
+ "match": [
+ {
+ "header": {
+ "Bar": [
+ "foo"
+ ],
+ "Foo": [
+ "bar",
+ "foobar"
+ ]
+ }
+ }
+ ],
+ "handle": [
+ {
+ "body": "header matcher merging values of the same field",
+ "handler": "static_response"
+ }
+ ]
}
]
}
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go
index a832024..cbb6253 100644
--- a/modules/caddyhttp/matchers.go
+++ b/modules/caddyhttp/matchers.go
@@ -408,7 +408,16 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if !d.Args(&field, &val) {
return d.Errf("malformed header matcher: expected both field and value")
}
- http.Header(*m).Set(field, val)
+
+ // If multiple header matchers with the same header field are defined,
+ // we want to add the existing to the list of headers (will be OR'ed)
+ existing := http.Header(*m).Values(field)
+ if len(existing) > 0 {
+ http.Header(*m).Add(field, val)
+ } else {
+ http.Header(*m).Set(field, val)
+ }
+
if d.NextBlock(0) {
return d.Err("malformed header matcher: blocks are not supported")
}