diff options
author | Matthew Holt <[email protected]> | 2019-11-29 11:23:49 -0700 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-11-29 11:23:49 -0700 |
commit | 7c7ef8d40e3af35444d522debc95451c04666615 (patch) | |
tree | 30cf3cb2b10e0e19e308eef8031f64bf7739b6bb | |
parent | 14d3fd7d0307e69c3c8995c42052b9103d1e2299 (diff) | |
download | caddy-7c7ef8d40e3af35444d522debc95451c04666615.tar.gz caddy-7c7ef8d40e3af35444d522debc95451c04666615.zip |
http: Shorten regexp matcher placeholders; allow "=/" for simple matcher
-rw-r--r-- | caddyconfig/httpcaddyfile/httptype.go | 2 | ||||
-rw-r--r-- | modules/caddyhttp/matchers.go | 10 | ||||
-rw-r--r-- | modules/caddyhttp/matchers_test.go | 8 |
3 files changed, 10 insertions, 10 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index 05022d12c..6ea32a28f 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -477,7 +477,7 @@ func matcherSetFromMatcherToken( if tkn.Text == "*" { // match all requests == no matchers, so nothing to do return nil, true, nil - } else if strings.HasPrefix(tkn.Text, "/") { + } else if strings.HasPrefix(tkn.Text, "/") || strings.HasPrefix(tkn.Text, "=/") { // convenient way to specify a single path match return map[string]json.RawMessage{ "path": caddyconfig.JSON(caddyhttp.MatchPath{tkn.Text}, warnings), diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index 5ea606d3d..c0e38726f 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -216,7 +216,7 @@ func (MatchPathRE) CaddyModule() caddy.ModuleInfo { // Match returns true if r matches m. func (m MatchPathRE) Match(r *http.Request) bool { repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer) - return m.MatchRegexp.Match(r.URL.Path, repl, "path_regexp") + return m.MatchRegexp.Match(r.URL.Path, repl) } // CaddyModule returns the Caddy module information. @@ -363,7 +363,7 @@ func (m *MatchHeaderRE) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { func (m MatchHeaderRE) Match(r *http.Request) bool { for field, rm := range m { repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer) - match := rm.Match(r.Header.Get(field), repl, "header_regexp") + match := rm.Match(r.Header.Get(field), repl) if !match { return false } @@ -638,7 +638,7 @@ func (mre *MatchRegexp) Validate() error { // (namespace). Capture groups stored to repl will take on // the name "http.matchers.<scope>.<mre.Name>.<N>" where // <N> is the name or number of the capture group. -func (mre *MatchRegexp) Match(input string, repl caddy.Replacer, scope string) bool { +func (mre *MatchRegexp) Match(input string, repl caddy.Replacer) bool { matches := mre.compiled.FindStringSubmatch(input) if matches == nil { return false @@ -646,14 +646,14 @@ func (mre *MatchRegexp) Match(input string, repl caddy.Replacer, scope string) b // save all capture groups, first by index for i, match := range matches { - key := fmt.Sprintf("http.matchers.%s.%s.%d", scope, mre.Name, i) + key := fmt.Sprintf("http.regexp.%s.%d", mre.Name, i) repl.Set(key, match) } // then by name for i, name := range mre.compiled.SubexpNames() { if i != 0 && name != "" { - key := fmt.Sprintf("http.matchers.%s.%s.%s", scope, mre.Name, name) + key := fmt.Sprintf("http.regexp.%s.%s", mre.Name, name) repl.Set(key, matches[i]) } } diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go index 321d3ce7e..9fa7d8e8a 100644 --- a/modules/caddyhttp/matchers_test.go +++ b/modules/caddyhttp/matchers_test.go @@ -324,10 +324,10 @@ func TestPathREMatcher(t *testing.T) { } for key, expectVal := range tc.expectRepl { - placeholder := fmt.Sprintf("{http.matchers.path_regexp.%s}", key) + placeholder := fmt.Sprintf("{http.regexp.%s}", key) actualVal := repl.ReplaceAll(placeholder, "<empty>") if actualVal != expectVal { - t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.path_regexp.%s} to be '%s' but got '%s'", + t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'", i, tc.match.Pattern, key, expectVal, actualVal) continue } @@ -442,10 +442,10 @@ func TestHeaderREMatcher(t *testing.T) { } for key, expectVal := range tc.expectRepl { - placeholder := fmt.Sprintf("{http.matchers.header_regexp.%s}", key) + placeholder := fmt.Sprintf("{http.regexp.%s}", key) actualVal := repl.ReplaceAll(placeholder, "<empty>") if actualVal != expectVal { - t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.header_regexp.%s} to be '%s' but got '%s'", + t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'", i, tc.match, key, expectVal, actualVal) continue } |