@marcel91200 / caddy Mirror · read-only / commits / 6db36155
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: 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

Matthew Holt committed Jun 16, 2020 at 10:41 UTC 6db3615547b8911ac81dc2b23c1ed8bedbef0fdb
2 files changed +27 -4
modules/caddyhttp/matchers.go
+9 -4
@@ -349,19 +349,21 @@ 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 -
352 for d.Next() {
353 var query string
354 if !d.Args(&query) {
355 return d.ArgErr()
356 }
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 + 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,6 +374,9 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
374
375 // Match returns true if r matches m.
376 func (m MatchQuery) Match(r *http.Request) bool {
377 + if m == nil {
378 + return len(r.URL.Query()) == 0
379 + }
380 for param, vals := range m {
381 paramVal, found := r.URL.Query()[param]
382 if found {
modules/caddyhttp/matchers_test.go
+18
@@ -528,6 +528,24 @@ func TestQueryMatcher(t *testing.T) {
528 input: "/?someparam",
529 expect: false,
530 },
531 + {
532 + scenario: "nil matcher value should match empty query",
533 + match: MatchQuery(nil),
534 + input: "/?",
535 + expect: true,
536 + },
537 + {
538 + scenario: "nil matcher value should NOT match a non-empty query",
539 + match: MatchQuery(nil),
540 + input: "/?foo=bar",
541 + expect: false,
542 + },
543 + {
544 + scenario: "non-nil matcher should NOT match an empty query",
545 + match: MatchQuery{"": []string{}},
546 + input: "/?",
547 + expect: false,
548 + },
549 } {
550
551 u, _ := url.Parse(tc.input)