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
http: Shorten regexp matcher placeholders; allow "=/" for simple matcher
Matthew Holt committed
Nov 29, 2019 at 11:23 UTC
7c7ef8d40e3af35444d522debc95451c04666615
3 files changed
+10
-10
caddyconfig/httpcaddyfile/httptype.go
+1
-1
@@ -477,7 +477,7 @@ func matcherSetFromMatcherToken(
477
if tkn.Text == "*" {
478
// match all requests == no matchers, so nothing to do
479
return nil, true, nil
480
- } else if strings.HasPrefix(tkn.Text, "/") {
480
+ } else if strings.HasPrefix(tkn.Text, "/") || strings.HasPrefix(tkn.Text, "=/") {
481
// convenient way to specify a single path match
482
return map[string]json.RawMessage{
483
"path": caddyconfig.JSON(caddyhttp.MatchPath{tkn.Text}, warnings),
modules/caddyhttp/matchers.go
+5
-5
@@ -216,7 +216,7 @@ func (MatchPathRE) CaddyModule() caddy.ModuleInfo {
216
// Match returns true if r matches m.
217
func (m MatchPathRE) Match(r *http.Request) bool {
218
repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
219
- return m.MatchRegexp.Match(r.URL.Path, repl, "path_regexp")
219
+ return m.MatchRegexp.Match(r.URL.Path, repl)
220
}
221
222
// CaddyModule returns the Caddy module information.
@@ -363,7 +363,7 @@ func (m *MatchHeaderRE) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
363
func (m MatchHeaderRE) Match(r *http.Request) bool {
364
for field, rm := range m {
365
repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
366
- match := rm.Match(r.Header.Get(field), repl, "header_regexp")
366
+ match := rm.Match(r.Header.Get(field), repl)
367
if !match {
368
return false
369
}
@@ -638,7 +638,7 @@ func (mre *MatchRegexp) Validate() error {
638
// (namespace). Capture groups stored to repl will take on
639
// the name "http.matchers.<scope>.<mre.Name>.<N>" where
640
// <N> is the name or number of the capture group.
641
-func (mre *MatchRegexp) Match(input string, repl caddy.Replacer, scope string) bool {
641
+func (mre *MatchRegexp) Match(input string, repl caddy.Replacer) bool {
642
matches := mre.compiled.FindStringSubmatch(input)
643
if matches == nil {
644
return false
@@ -646,14 +646,14 @@ func (mre *MatchRegexp) Match(input string, repl caddy.Replacer, scope string) b
646
647
// save all capture groups, first by index
648
for i, match := range matches {
649
- key := fmt.Sprintf("http.matchers.%s.%s.%d", scope, mre.Name, i)
649
+ key := fmt.Sprintf("http.regexp.%s.%d", mre.Name, i)
650
repl.Set(key, match)
651
}
652
653
// then by name
654
for i, name := range mre.compiled.SubexpNames() {
655
if i != 0 && name != "" {
656
- key := fmt.Sprintf("http.matchers.%s.%s.%s", scope, mre.Name, name)
656
+ key := fmt.Sprintf("http.regexp.%s.%s", mre.Name, name)
657
repl.Set(key, matches[i])
658
}
659
}
modules/caddyhttp/matchers_test.go
+4
-4
@@ -324,10 +324,10 @@ func TestPathREMatcher(t *testing.T) {
324
}
325
326
for key, expectVal := range tc.expectRepl {
327
- placeholder := fmt.Sprintf("{http.matchers.path_regexp.%s}", key)
327
+ placeholder := fmt.Sprintf("{http.regexp.%s}", key)
328
actualVal := repl.ReplaceAll(placeholder, "<empty>")
329
if actualVal != expectVal {
330
- t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.path_regexp.%s} to be '%s' but got '%s'",
330
+ t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
331
i, tc.match.Pattern, key, expectVal, actualVal)
332
continue
333
}
@@ -442,10 +442,10 @@ func TestHeaderREMatcher(t *testing.T) {
442
}
443
444
for key, expectVal := range tc.expectRepl {
445
- placeholder := fmt.Sprintf("{http.matchers.header_regexp.%s}", key)
445
+ placeholder := fmt.Sprintf("{http.regexp.%s}", key)
446
actualVal := repl.ReplaceAll(placeholder, "<empty>")
447
if actualVal != expectVal {
448
- t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.header_regexp.%s} to be '%s' but got '%s'",
448
+ t.Errorf("Test %d [%v]: Expected placeholder {http.regexp.%s} to be '%s' but got '%s'",
449
i, tc.match, key, expectVal, actualVal)
450
continue
451
}