1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

rewrite: strip_prefix, strip_suffix, and uri_replace dirs (closes #2906)

This commit is contained in:
Matthew Holt 2019-12-12 15:46:13 -07:00
parent 5e9d81b507
commit b1a456cfe3
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 95 additions and 3 deletions

View file

@ -27,6 +27,9 @@ import (
// to apply directives in HTTP routes. // to apply directives in HTTP routes.
var defaultDirectiveOrder = []string{ var defaultDirectiveOrder = []string{
"rewrite", "rewrite",
"strip_prefix",
"strip_suffix",
"uri_replace",
"try_files", "try_files",
"basicauth", "basicauth",
"headers", "headers",

View file

@ -15,20 +15,25 @@
package rewrite package rewrite
import ( import (
"strconv"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/caddyserver/caddy/v2/modules/caddyhttp"
) )
func init() { 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> // rewrite [<matcher>] <to>
// //
// The <to> parameter becomes the new URI. // 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 var rewr Rewrite
for h.Next() { for h.Next() {
if !h.NextArg() { if !h.NextArg() {
@ -42,3 +47,87 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
rewr.Rehandle = true rewr.Rehandle = true
return rewr, nil 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
}