diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-12-12 15:46:13 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-12-12 15:46:13 -0700 |
commit | b1a456cfe38e83eedb778032ed6739e7e3cbe8a4 (patch) | |
tree | 7ec9343779274979a50dbb80c10283eee9cc5365 /modules/caddyhttp/rewrite | |
parent | 5e9d81b507e0beb46b3812e21566bfef79c87af4 (diff) |
rewrite: strip_prefix, strip_suffix, and uri_replace dirs (closes #2906)
Diffstat (limited to 'modules/caddyhttp/rewrite')
-rw-r--r-- | modules/caddyhttp/rewrite/caddyfile.go | 95 |
1 files changed, 92 insertions, 3 deletions
diff --git a/modules/caddyhttp/rewrite/caddyfile.go b/modules/caddyhttp/rewrite/caddyfile.go index a1fc874..6674313 100644 --- a/modules/caddyhttp/rewrite/caddyfile.go +++ b/modules/caddyhttp/rewrite/caddyfile.go @@ -15,20 +15,25 @@ package rewrite import ( + "strconv" + "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" "github.com/caddyserver/caddy/v2/modules/caddyhttp" ) func init() { - httpcaddyfile.RegisterHandlerDirective("rewrite", parseCaddyfile) + httpcaddyfile.RegisterHandlerDirective("rewrite", parseCaddyfileRewrite) + httpcaddyfile.RegisterHandlerDirective("strip_prefix", parseCaddyfileStripPrefix) + httpcaddyfile.RegisterHandlerDirective("strip_suffix", parseCaddyfileStripSuffix) + httpcaddyfile.RegisterHandlerDirective("uri_replace", parseCaddyfileURIReplace) } -// parseCaddyfile sets up the handler from Caddyfile tokens. Syntax: +// parseCaddyfileRewrite sets up a basic rewrite handler from Caddyfile tokens. Syntax: // // rewrite [<matcher>] <to> // // The <to> parameter becomes the new URI. -func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { +func parseCaddyfileRewrite(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { var rewr Rewrite for h.Next() { if !h.NextArg() { @@ -42,3 +47,87 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) rewr.Rehandle = true return rewr, nil } + +// parseCaddyfileStripPrefix sets up a handler from Caddyfile tokens. Syntax: +// +// strip_prefix [<matcher>] <prefix> +// +// The request path will be stripped its prefix if it matches <prefix>. +func parseCaddyfileStripPrefix(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { + var rewr Rewrite + for h.Next() { + if !h.NextArg() { + return nil, h.ArgErr() + } + rewr.StripPathPrefix = h.Val() + if h.NextArg() { + return nil, h.ArgErr() + } + } + return rewr, nil +} + +// parseCaddyfileStripSuffix sets up a handler from Caddyfile tokens. Syntax: +// +// strip_suffix [<matcher>] <suffix> +// +// The request path will be stripped its suffix if it matches <suffix>. +func parseCaddyfileStripSuffix(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { + var rewr Rewrite + for h.Next() { + if !h.NextArg() { + return nil, h.ArgErr() + } + rewr.StripPathSuffix = h.Val() + if h.NextArg() { + return nil, h.ArgErr() + } + } + return rewr, nil +} + +// parseCaddyfileURIReplace sets up a handler from Caddyfile tokens. Syntax: +// +// uri_replace [<matcher>] <find> <replace> [<limit>] +// +// Substring replacements will be performed on the request URI up to the +// number specified by limit, if any (default = 0, or no limit). +func parseCaddyfileURIReplace(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { + var rewr Rewrite + + var repls []replacer + + for h.Next() { + args := h.RemainingArgs() + var find, replace, lim string + switch len(args) { + case 3: + lim = args[2] + fallthrough + case 2: + find = args[0] + replace = args[1] + default: + return nil, h.ArgErr() + } + + var limInt int + if lim != "" { + var err error + limInt, err = strconv.Atoi(lim) + if err != nil { + return nil, h.Errf("limit must be an integer; invalid: %v", err) + } + } + + repls = append(repls, replacer{ + Find: find, + Replace: replace, + Limit: limInt, + }) + } + + rewr.URISubstring = repls + + return rewr, nil +} |