mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 12:25:55 +03:00
caddyhttp: Empty, not nil, query matcher matches empty query string
This commit is contained in:
parent
6db3615547
commit
3af15c0725
2 changed files with 9 additions and 12 deletions
|
@ -349,6 +349,9 @@ 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) {
|
||||||
|
@ -361,9 +364,6 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
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,11 +372,8 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match returns true if r matches m.
|
// Match returns true if r matches m. An empty m matches an empty query string.
|
||||||
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 {
|
||||||
|
@ -387,7 +384,7 @@ func (m MatchQuery) Match(r *http.Request) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return len(m) == 0 && len(r.URL.Query()) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaddyModule returns the Caddy module information.
|
// CaddyModule returns the Caddy module information.
|
||||||
|
|
|
@ -529,20 +529,20 @@ func TestQueryMatcher(t *testing.T) {
|
||||||
expect: false,
|
expect: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scenario: "nil matcher value should match empty query",
|
scenario: "empty matcher value should match empty query",
|
||||||
match: MatchQuery(nil),
|
match: MatchQuery{},
|
||||||
input: "/?",
|
input: "/?",
|
||||||
expect: true,
|
expect: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scenario: "nil matcher value should NOT match a non-empty query",
|
scenario: "nil matcher value should NOT match a non-empty query",
|
||||||
match: MatchQuery(nil),
|
match: MatchQuery{},
|
||||||
input: "/?foo=bar",
|
input: "/?foo=bar",
|
||||||
expect: false,
|
expect: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
scenario: "non-nil matcher should NOT match an empty query",
|
scenario: "non-nil matcher should NOT match an empty query",
|
||||||
match: MatchQuery{"": []string{}},
|
match: MatchQuery{"": nil},
|
||||||
input: "/?",
|
input: "/?",
|
||||||
expect: false,
|
expect: false,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue