mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-22 16:46:53 +01:00
caddyhttp: Enable matching empty query string
Caddyfile syntax: query "" Or a nil matcher in the JSON should also match an empty query string. See https://caddy.community/t/v2-match-empty-query/8708?u=matt
This commit is contained in:
parent
32cafbb630
commit
6db3615547
2 changed files with 27 additions and 4 deletions
|
@ -349,19 +349,21 @@ func (MatchQuery) CaddyModule() caddy.ModuleInfo {
|
||||||
|
|
||||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||||
func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
if *m == nil {
|
|
||||||
*m = make(map[string][]string)
|
|
||||||
}
|
|
||||||
|
|
||||||
for d.Next() {
|
for d.Next() {
|
||||||
var query string
|
var query string
|
||||||
if !d.Args(&query) {
|
if !d.Args(&query) {
|
||||||
return d.ArgErr()
|
return d.ArgErr()
|
||||||
}
|
}
|
||||||
|
if query == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
parts := strings.SplitN(query, "=", 2)
|
parts := strings.SplitN(query, "=", 2)
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
|
return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
|
||||||
}
|
}
|
||||||
|
if *m == nil {
|
||||||
|
*m = make(map[string][]string)
|
||||||
|
}
|
||||||
url.Values(*m).Set(parts[0], parts[1])
|
url.Values(*m).Set(parts[0], parts[1])
|
||||||
if d.NextBlock(0) {
|
if d.NextBlock(0) {
|
||||||
return d.Err("malformed query matcher: blocks are not supported")
|
return d.Err("malformed query matcher: blocks are not supported")
|
||||||
|
@ -372,6 +374,9 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
|
|
||||||
// Match returns true if r matches m.
|
// Match returns true if r matches m.
|
||||||
func (m MatchQuery) Match(r *http.Request) bool {
|
func (m MatchQuery) Match(r *http.Request) bool {
|
||||||
|
if m == nil {
|
||||||
|
return len(r.URL.Query()) == 0
|
||||||
|
}
|
||||||
for param, vals := range m {
|
for param, vals := range m {
|
||||||
paramVal, found := r.URL.Query()[param]
|
paramVal, found := r.URL.Query()[param]
|
||||||
if found {
|
if found {
|
||||||
|
|
|
@ -528,6 +528,24 @@ func TestQueryMatcher(t *testing.T) {
|
||||||
input: "/?someparam",
|
input: "/?someparam",
|
||||||
expect: false,
|
expect: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "nil matcher value should match empty query",
|
||||||
|
match: MatchQuery(nil),
|
||||||
|
input: "/?",
|
||||||
|
expect: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
scenario: "nil matcher value should NOT match a non-empty query",
|
||||||
|
match: MatchQuery(nil),
|
||||||
|
input: "/?foo=bar",
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
scenario: "non-nil matcher should NOT match an empty query",
|
||||||
|
match: MatchQuery{"": []string{}},
|
||||||
|
input: "/?",
|
||||||
|
expect: false,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
|
|
||||||
u, _ := url.Parse(tc.input)
|
u, _ := url.Parse(tc.input)
|
||||||
|
|
Loading…
Reference in a new issue