@marcel91200 / caddy Mirror · read-only / commits / 966d5e6b
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyhttp: Merge header matchers in Caddyfile (#3832)

Francis Lavoie committed Oct 31, 2020 at 12:27 UTC 966d5e6b42fc6da3da8bd39dd6ceceb8f1da3999
2 files changed +38 -1
caddytest/integration/caddyfile_adapt/matcher_syntax.txt
+28
@@ -18,6 +18,13 @@
18
19 @matcher6 vars_regexp "{http.request.uri}" `\.([a-f0-9]{6})\.(css|js)$`
20 respond @matcher6 "from vars_regexp matcher without name"
21 +
22 + @matcher7 {
23 + header Foo bar
24 + header Foo foobar
25 + header Bar foo
26 + }
27 + respond @matcher7 "header matcher merging values of the same field"
28 }
29 ----------
30 {
@@ -127,6 +134,27 @@
134 "handler": "static_response"
135 }
136 ]
137 + },
138 + {
139 + "match": [
140 + {
141 + "header": {
142 + "Bar": [
143 + "foo"
144 + ],
145 + "Foo": [
146 + "bar",
147 + "foobar"
148 + ]
149 + }
150 + }
151 + ],
152 + "handle": [
153 + {
154 + "body": "header matcher merging values of the same field",
155 + "handler": "static_response"
156 + }
157 + ]
158 }
159 ]
160 }
modules/caddyhttp/matchers.go
+10 -1
@@ -408,7 +408,16 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
408 if !d.Args(&field, &val) {
409 return d.Errf("malformed header matcher: expected both field and value")
410 }
411 - http.Header(*m).Set(field, val)
411 +
412 + // If multiple header matchers with the same header field are defined,
413 + // we want to add the existing to the list of headers (will be OR'ed)
414 + existing := http.Header(*m).Values(field)
415 + if len(existing) > 0 {
416 + http.Header(*m).Add(field, val)
417 + } else {
418 + http.Header(*m).Set(field, val)
419 + }
420 +
421 if d.NextBlock(0) {
422 return d.Err("malformed header matcher: blocks are not supported")
423 }