@marcel91200 / caddy Mirror · read-only / commits / 3af15c07
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: Empty, not nil, query matcher matches empty query string

Matthew Holt committed Jun 16, 2020 at 12:02 UTC 3af15c0725605162dc8dad0c6ab241e2dc4553c6
2 files changed +9 -12
modules/caddyhttp/matchers.go
+5 -8
@@ -349,6 +349,9 @@ func (MatchQuery) CaddyModule() caddy.ModuleInfo {
349
350 // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
351 func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
352 + if *m == nil {
353 + *m = make(map[string][]string)
354 + }
355 for d.Next() {
356 var query string
357 if !d.Args(&query) {
@@ -361,9 +364,6 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
364 if len(parts) != 2 {
365 return d.Errf("malformed query matcher token: %s; must be in param=val format", d.Val())
366 }
364 - if *m == nil {
365 - *m = make(map[string][]string)
366 - }
367 url.Values(*m).Set(parts[0], parts[1])
368 if d.NextBlock(0) {
369 return d.Err("malformed query matcher: blocks are not supported")
@@ -372,11 +372,8 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
372 return nil
373 }
374
375 -// Match returns true if r matches m.
375 +// Match returns true if r matches m. An empty m matches an empty query string.
376 func (m MatchQuery) Match(r *http.Request) bool {
377 - if m == nil {
378 - return len(r.URL.Query()) == 0
379 - }
377 for param, vals := range m {
378 paramVal, found := r.URL.Query()[param]
379 if found {
@@ -387,7 +384,7 @@ func (m MatchQuery) Match(r *http.Request) bool {
384 }
385 }
386 }
390 - return false
387 + return len(m) == 0 && len(r.URL.Query()) == 0
388 }
389
390 // CaddyModule returns the Caddy module information.
modules/caddyhttp/matchers_test.go
+4 -4
@@ -529,20 +529,20 @@ func TestQueryMatcher(t *testing.T) {
529 expect: false,
530 },
531 {
532 - scenario: "nil matcher value should match empty query",
533 - match: MatchQuery(nil),
532 + scenario: "empty matcher value should match empty query",
533 + match: MatchQuery{},
534 input: "/?",
535 expect: true,
536 },
537 {
538 scenario: "nil matcher value should NOT match a non-empty query",
539 - match: MatchQuery(nil),
539 + match: MatchQuery{},
540 input: "/?foo=bar",
541 expect: false,
542 },
543 {
544 scenario: "non-nil matcher should NOT match an empty query",
545 - match: MatchQuery{"": []string{}},
545 + match: MatchQuery{"": nil},
546 input: "/?",
547 expect: false,
548 },