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: Support placeholders in query matcher (#3521)
James Birtles committed
Jun 26, 2020 at 22:14 UTC
ddd690de4cc133bc728f765b2492ccb54f9d84ca
2 files changed
+20
modules/caddyhttp/matchers.go
+3
@@ -374,10 +374,13 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
374
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
+ repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
378
for param, vals := range m {
379
+ param = repl.ReplaceAll(param, "")
380
paramVal, found := r.URL.Query()[param]
381
if found {
382
for _, v := range vals {
383
+ v = repl.ReplaceAll(v, "")
384
if paramVal[0] == v || v == "*" {
385
return true
386
}
modules/caddyhttp/matchers_test.go
+17
@@ -546,11 +546,28 @@ func TestQueryMatcher(t *testing.T) {
546
input: "/?",
547
expect: false,
548
},
549
+ {
550
+ scenario: "match against a placeholder value",
551
+ match: MatchQuery{"debug": []string{"{http.vars.debug}"}},
552
+ input: "/?debug=1",
553
+ expect: true,
554
+ },
555
+ {
556
+ scenario: "match against a placeholder key",
557
+ match: MatchQuery{"{http.vars.key}": []string{"1"}},
558
+ input: "/?somekey=1",
559
+ expect: true,
560
+ },
561
} {
562
563
u, _ := url.Parse(tc.input)
564
565
req := &http.Request{URL: u}
566
+ repl := caddy.NewReplacer()
567
+ ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
568
+ repl.Set("http.vars.debug", "1")
569
+ repl.Set("http.vars.key", "somekey")
570
+ req = req.WithContext(ctx)
571
actual := tc.match.Match(req)
572
if actual != tc.expect {
573
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)