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 | |
parent | 9b757ce862fcaf5535c07ef67c233dfc358316ef (diff) | |
download | caddy-matcher-regexp-names.tar.gz caddy-matcher-regexp-names.zip |
Compat with #6113: fix adapt test, set both styles in replacermatcher-regexp-names
-rw-r--r-- | caddytest/integration/caddyfile_adapt/shorthand_parameterized_placeholders.caddyfiletest | 1 | ||||
-rw-r--r-- | modules/caddyhttp/matchers.go | 24 |
2 files changed, 15 insertions, 10 deletions
diff --git a/caddytest/integration/caddyfile_adapt/shorthand_parameterized_placeholders.caddyfiletest b/caddytest/integration/caddyfile_adapt/shorthand_parameterized_placeholders.caddyfiletest index 30bc2c128..ef8d2330b 100644 --- a/caddytest/integration/caddyfile_adapt/shorthand_parameterized_placeholders.caddyfiletest +++ b/caddytest/integration/caddyfile_adapt/shorthand_parameterized_placeholders.caddyfiletest @@ -36,6 +36,7 @@ respond @match "{re.1}" "match": [ { "path_regexp": { + "name": "match", "pattern": "^/foo(.*)$" } } 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 |