diff options
author | Francis Lavoie <[email protected]> | 2024-04-17 12:16:51 -0400 |
---|---|---|
committer | Francis Lavoie <[email protected]> | 2024-04-17 12:16:51 -0400 |
commit | 358e09d97bab4e4c7f494e4e17ba76b6eac194f6 (patch) | |
tree | 2393ff4a9a2280e35acfacf4109ca99075c26a43 /modules | |
parent | 9b757ce862fcaf5535c07ef67c233dfc358316ef (diff) | |
download | caddy-358e09d97bab4e4c7f494e4e17ba76b6eac194f6.tar.gz caddy-358e09d97bab4e4c7f494e4e17ba76b6eac194f6.zip |
Compat with #6113: fix adapt test, set both styles in replacermatcher-regexp-names
Diffstat (limited to 'modules')
-rw-r--r-- | modules/caddyhttp/matchers.go | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index 23dc6e8a8..b1da14686 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -1309,7 +1309,6 @@ type MatchRegexp struct { Pattern string `json:"pattern"` compiled *regexp.Regexp - phPrefix string } // Provision compiles the regular expression. @@ -1319,10 +1318,6 @@ func (mre *MatchRegexp) Provision(caddy.Context) error { return fmt.Errorf("compiling matcher regexp %s: %v", mre.Pattern, err) } mre.compiled = re - mre.phPrefix = regexpPlaceholderPrefix - if mre.Name != "" { - mre.phPrefix += "." + mre.Name - } return nil } @@ -1346,16 +1341,25 @@ func (mre *MatchRegexp) Match(input string, repl *caddy.Replacer) bool { // save all capture groups, first by index for i, match := range matches { - key := mre.phPrefix + "." + strconv.Itoa(i) - repl.Set(key, match) + keySuffix := "." + strconv.Itoa(i) + if mre.Name != "" { + repl.Set(regexpPlaceholderPrefix+"."+mre.Name+keySuffix, match) + } + repl.Set(regexpPlaceholderPrefix+keySuffix, match) } // then by name for i, name := range mre.compiled.SubexpNames() { - if i != 0 && name != "" { - key := mre.phPrefix + "." + name - repl.Set(key, matches[i]) + // skip the first element (the full match), and empty names + if i == 0 || name == "" { + continue + } + + keySuffix := "." + name + if mre.Name != "" { + repl.Set(regexpPlaceholderPrefix+"."+mre.Name+keySuffix, matches[i]) } + repl.Set(regexpPlaceholderPrefix+keySuffix, matches[i]) } return true |