From c684de9a887570c21959e0ba1d59fe6efbf4592c Mon Sep 17 00:00:00 2001 From: Tw Date: Fri, 21 Apr 2017 19:35:32 +0800 Subject: [PATCH] proxy: take original URL path into account when remove prefix fix issue #1604 Signed-off-by: Tw --- caddyhttp/proxy/proxy_test.go | 7 +++++++ caddyhttp/proxy/reverseproxy.go | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/caddyhttp/proxy/proxy_test.go b/caddyhttp/proxy/proxy_test.go index 80ef06d9..9ccda845 100644 --- a/caddyhttp/proxy/proxy_test.go +++ b/caddyhttp/proxy/proxy_test.go @@ -977,6 +977,13 @@ func TestProxyDirectorURL(t *testing.T) { targetURL: `https://localhost:2021/`, expectURL: `https://localhost:2021/%2F/test`, }, + { + originalPath: `/test///mypath`, + requestURL: `http://localhost:2020/test/%2F/mypath`, + targetURL: `https://localhost:2021/t/`, + expectURL: `https://localhost:2021/t/%2F/mypath`, + without: "/test", + }, } { targetURL, err := url.Parse(c.targetURL) if err != nil { diff --git a/caddyhttp/proxy/reverseproxy.go b/caddyhttp/proxy/reverseproxy.go index ad0381ff..56d3de47 100644 --- a/caddyhttp/proxy/reverseproxy.go +++ b/caddyhttp/proxy/reverseproxy.go @@ -121,6 +121,7 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) * } // We should remove the `without` prefix at first. + untouchedPath, _ := req.Context().Value(staticfiles.URLPathCtxKey).(string) if without != "" { req.URL.Path = strings.TrimPrefix(req.URL.Path, without) if req.URL.Opaque != "" { @@ -129,6 +130,9 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) * if req.URL.RawPath != "" { req.URL.RawPath = strings.TrimPrefix(req.URL.RawPath, without) } + if untouchedPath != "" { + untouchedPath = strings.TrimPrefix(untouchedPath, without) + } } // prefer returns val if it isn't empty, otherwise def @@ -155,7 +159,6 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) * prefer(target.RawPath, target.Path), prefer(req.URL.RawPath, req.URL.Path)) } - untouchedPath, _ := req.Context().Value(staticfiles.URLPathCtxKey).(string) req.URL.Path = singleJoiningSlash(target.Path, prefer(untouchedPath, req.URL.Path)) // req.URL.Path must be consistent with decoded form of req.URL.RawPath if any