Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caddyhttp: Merge query matchers in Caddyfile #3839

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions caddytest/integration/caddyfile_adapt/matcher_syntax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
header Bar foo
}
respond @matcher7 "header matcher merging values of the same field"

@matcher8 {
query foo=bar foo=baz bar=foo
query bar=baz
}
respond @matcher8 "query matcher merging pairs with the same keys"
}
----------
{
Expand Down Expand Up @@ -155,6 +161,28 @@
"handler": "static_response"
}
]
},
{
"match": [
{
"query": {
"bar": [
"foo",
"baz"
],
"foo": [
"bar",
"baz"
]
}
}
],
"handle": [
{
"body": "query matcher merging pairs with the same keys",
"handler": "static_response"
}
]
}
]
}
Expand Down
27 changes: 10 additions & 17 deletions modules/caddyhttp/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,16 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
*m = make(map[string][]string)
}
for d.Next() {
var query string
if !d.Args(&query) {
return d.ArgErr()
}
if query == "" {
continue
}
parts := strings.SplitN(query, "=", 2)
if len(parts) != 2 {
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
for _, query := range d.RemainingArgs() {
if query == "" {
continue
}
parts := strings.SplitN(query, "=", 2)
if len(parts) != 2 {
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
}
url.Values(*m).Add(parts[0], parts[1])
}
url.Values(*m).Set(parts[0], parts[1])
if d.NextBlock(0) {
return d.Err("malformed query matcher: blocks are not supported")
}
Expand Down Expand Up @@ -411,12 +409,7 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

// 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)
}
http.Header(*m).Add(field, val)

if d.NextBlock(0) {
return d.Err("malformed header matcher: blocks are not supported")
Expand Down