summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/rewrite/caddyfile.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2021-03-01 18:27:59 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2021-03-01 18:27:59 -0700
commitad8d01cb66316cf04ea49ec277316d6f83b9abb6 (patch)
treec561189d8be27c5ab9145a86d8c9a6f26a339f1e /modules/caddyhttp/rewrite/caddyfile.go
parent5bf0a55df499e189424103d87d93960395d3172f (diff)
rewrite: Implement regex path replacements
https://caddy.community/t/collapsing-multiple-forward-slashes-in-path-only/11626
Diffstat (limited to 'modules/caddyhttp/rewrite/caddyfile.go')
-rw-r--r--modules/caddyhttp/rewrite/caddyfile.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/modules/caddyhttp/rewrite/caddyfile.go b/modules/caddyhttp/rewrite/caddyfile.go
index 950119d..9621af1 100644
--- a/modules/caddyhttp/rewrite/caddyfile.go
+++ b/modules/caddyhttp/rewrite/caddyfile.go
@@ -54,12 +54,14 @@ func parseCaddyfileRewrite(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler,
// parseCaddyfileURI sets up a handler for manipulating (but not "rewriting") the
// URI from Caddyfile tokens. Syntax:
//
-// uri [<matcher>] strip_prefix|strip_suffix|replace <target> [<replacement> [<limit>]]
+// uri [<matcher>] strip_prefix|strip_suffix|replace|path_regexp <target> [<replacement> [<limit>]]
//
// If strip_prefix or strip_suffix are used, then <target> will be stripped
// only if it is the beginning or the end, respectively, of the URI path. If
// replace is used, then <target> will be replaced with <replacement> across
-// the whole URI, up to <limit> times (or unlimited if unspecified).
+// the whole URI, up to <limit> times (or unlimited if unspecified). If
+// path_regexp is used, then regular expression replacements will be performed
+// on the path portion of the URI (and a limit cannot be set).
func parseCaddyfileURI(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var rewr Rewrite
for h.Next() {
@@ -103,11 +105,20 @@ func parseCaddyfileURI(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, err
}
}
- rewr.URISubstring = append(rewr.URISubstring, replacer{
+ rewr.URISubstring = append(rewr.URISubstring, substrReplacer{
Find: find,
Replace: replace,
Limit: limInt,
})
+ case "path_regexp":
+ if len(args) != 3 {
+ return nil, h.ArgErr()
+ }
+ find, replace := args[1], args[2]
+ rewr.PathRegexp = append(rewr.PathRegexp, &regexReplacer{
+ Find: find,
+ Replace: replace,
+ })
default:
return nil, h.Errf("unrecognized URI manipulation '%s'", args[0])
}