mirror of https://github.com/caddyserver/caddy.git
rewrite: Don't add / in Caddyfile, do it after replacer (#6662)
This commit is contained in:
parent
cc23ad6402
commit
5823eccf99
|
@ -110,9 +110,6 @@ func parseCaddyfileURI(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, err
|
||||||
return nil, h.ArgErr()
|
return nil, h.ArgErr()
|
||||||
}
|
}
|
||||||
rewr.StripPathPrefix = args[1]
|
rewr.StripPathPrefix = args[1]
|
||||||
if !strings.HasPrefix(rewr.StripPathPrefix, "/") {
|
|
||||||
rewr.StripPathPrefix = "/" + rewr.StripPathPrefix
|
|
||||||
}
|
|
||||||
|
|
||||||
case "strip_suffix":
|
case "strip_suffix":
|
||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
|
|
|
@ -259,6 +259,9 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
|
||||||
// strip path prefix or suffix
|
// strip path prefix or suffix
|
||||||
if rewr.StripPathPrefix != "" {
|
if rewr.StripPathPrefix != "" {
|
||||||
prefix := repl.ReplaceAll(rewr.StripPathPrefix, "")
|
prefix := repl.ReplaceAll(rewr.StripPathPrefix, "")
|
||||||
|
if !strings.HasPrefix(prefix, "/") {
|
||||||
|
prefix = "/" + prefix
|
||||||
|
}
|
||||||
mergeSlashes := !strings.Contains(prefix, "//")
|
mergeSlashes := !strings.Contains(prefix, "//")
|
||||||
changePath(r, func(escapedPath string) string {
|
changePath(r, func(escapedPath string) string {
|
||||||
escapedPath = caddyhttp.CleanPath(escapedPath, mergeSlashes)
|
escapedPath = caddyhttp.CleanPath(escapedPath, mergeSlashes)
|
||||||
|
|
|
@ -235,6 +235,11 @@ func TestRewrite(t *testing.T) {
|
||||||
input: newRequest(t, "GET", "/prefix/foo/bar"),
|
input: newRequest(t, "GET", "/prefix/foo/bar"),
|
||||||
expect: newRequest(t, "GET", "/foo/bar"),
|
expect: newRequest(t, "GET", "/foo/bar"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
rule: Rewrite{StripPathPrefix: "prefix"},
|
||||||
|
input: newRequest(t, "GET", "/prefix/foo/bar"),
|
||||||
|
expect: newRequest(t, "GET", "/foo/bar"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
rule: Rewrite{StripPathPrefix: "/prefix"},
|
rule: Rewrite{StripPathPrefix: "/prefix"},
|
||||||
input: newRequest(t, "GET", "/prefix"),
|
input: newRequest(t, "GET", "/prefix"),
|
||||||
|
|
Loading…
Reference in New Issue