rewrite: Prepend "/" if missing from strip path prefix

Paths always begin with a slash, and omitting the leading slash could be
convenient to avoid confusion with a path matcher in the Caddyfile. I do
not think there would be any harm to implicitly add the leading slash.
This commit is contained in:
Matthew Holt 2020-01-22 09:36:05 -07:00
parent 6b6cd934d0
commit 0742530d3d
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
1 changed files with 4 additions and 0 deletions

View File

@ -16,6 +16,7 @@ package rewrite
import ( import (
"strconv" "strconv"
"strings"
"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"
@ -60,6 +61,9 @@ func parseCaddyfileStripPrefix(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHand
return nil, h.ArgErr() return nil, h.ArgErr()
} }
rewr.StripPathPrefix = h.Val() rewr.StripPathPrefix = h.Val()
if !strings.HasPrefix(rewr.StripPathPrefix, "/") {
rewr.StripPathPrefix = "/" + rewr.StripPathPrefix
}
if h.NextArg() { if h.NextArg() {
return nil, h.ArgErr() return nil, h.ArgErr()
} }