Skip to content

Commit

Permalink
caddyhttp: Support placeholders in query matcher (#3521)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbirtles authored Jun 26, 2020
1 parent 6004d3f commit ddd690d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/caddyhttp/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,13 @@ func (m *MatchQuery) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

// Match returns true if r matches m. An empty m matches an empty query string.
func (m MatchQuery) Match(r *http.Request) bool {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
for param, vals := range m {
param = repl.ReplaceAll(param, "")
paramVal, found := r.URL.Query()[param]
if found {
for _, v := range vals {
v = repl.ReplaceAll(v, "")
if paramVal[0] == v || v == "*" {
return true
}
Expand Down
17 changes: 17 additions & 0 deletions modules/caddyhttp/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,28 @@ func TestQueryMatcher(t *testing.T) {
input: "/?",
expect: false,
},
{
scenario: "match against a placeholder value",
match: MatchQuery{"debug": []string{"{http.vars.debug}"}},
input: "/?debug=1",
expect: true,
},
{
scenario: "match against a placeholder key",
match: MatchQuery{"{http.vars.key}": []string{"1"}},
input: "/?somekey=1",
expect: true,
},
} {

u, _ := url.Parse(tc.input)

req := &http.Request{URL: u}
repl := caddy.NewReplacer()
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
repl.Set("http.vars.debug", "1")
repl.Set("http.vars.key", "somekey")
req = req.WithContext(ctx)
actual := tc.match.Match(req)
if actual != tc.expect {
t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
Expand Down

0 comments on commit ddd690d

Please sign in to comment.