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 query matchers in Caddyfile (#3839)
Also, turns out that `Add` on headers will work even if there's nothing there yet, so we can remove the condition I introduced in #3832
Francis Lavoie committed
Nov 2, 2020 at 18:05 UTC
b4f49e2962201a87037f451a0b68323524828a58
2 files changed
+38
-17
caddytest/integration/caddyfile_adapt/matcher_syntax.txt
+28
@@ -25,6 +25,12 @@
25
header Bar foo
26
}
27
respond @matcher7 "header matcher merging values of the same field"
28
+
29
+ @matcher8 {
30
+ query foo=bar foo=baz bar=foo
31
+ query bar=baz
32
+ }
33
+ respond @matcher8 "query matcher merging pairs with the same keys"
34
}
35
----------
36
{
@@ -155,6 +161,28 @@
161
"handler": "static_response"
162
}
163
]
164
+ },
165
+ {
166
+ "match": [
167
+ {
168
+ "query": {
169
+ "bar": [
170
+ "foo",
171
+ "baz"
172
+ ],
173
+ "foo": [
174
+ "bar",
175
+ "baz"
176
+ ]
177
+ }
178
+ }
179
+ ],
180
+ "handle": [
181
+ {
182
+ "body": "query matcher merging pairs with the same keys",
183
+ "handler": "static_response"
184
+ }
185
+ ]
186
}
187
]
188
}
modules/caddyhttp/matchers.go
+10
-17
@@ -353,18 +353,16 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
353
*m = make(map[string][]string)
354
}
355
for d.Next() {
356
- var query string
357
- if !d.Args(&query) {
358
- return d.ArgErr()
359
- }
360
- if query == "" {
361
- continue
362
- }
363
- parts := strings.SplitN(query, "=", 2)
364
- if len(parts) != 2 {
365
- return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
356
+ for _, query := range d.RemainingArgs() {
357
+ if query == "" {
358
+ continue
359
+ }
360
+ parts := strings.SplitN(query, "=", 2)
361
+ if len(parts) != 2 {
362
+ return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
363
+ }
364
+ url.Values(*m).Add(parts[0], parts[1])
365
}
367
- url.Values(*m).Set(parts[0], parts[1])
366
if d.NextBlock(0) {
367
return d.Err("malformed query matcher: blocks are not supported")
368
}
@@ -411,12 +409,7 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
409
410
// If multiple header matchers with the same header field are defined,
411
// 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
- }
412
+ http.Header(*m).Add(field, val)
413
414
if d.NextBlock(0) {
415
return d.Err("malformed header matcher: blocks are not supported")