summaryrefslogtreecommitdiff
path: root/modules/caddyhttp
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-12-12 15:46:13 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2019-12-12 15:46:13 -0700
commitb1a456cfe38e83eedb778032ed6739e7e3cbe8a4 (patch)
tree7ec9343779274979a50dbb80c10283eee9cc5365 /modules/caddyhttp
parent5e9d81b507e0beb46b3812e21566bfef79c87af4 (diff)
rewrite: strip_prefix, strip_suffix, and uri_replace dirs (closes #2906)
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r--modules/caddyhttp/rewrite/caddyfile.go95
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
+}