Merge pull request #1605 from tw4452852/1604

proxy: take original URL path into account when remove prefix ("without")
This commit is contained in:
Matt Holt 2017-04-21 12:07:14 -06:00 committed by GitHub
commit 182e1b4fb2
2 changed files with 11 additions and 1 deletions

View file

@ -977,6 +977,13 @@ func TestProxyDirectorURL(t *testing.T) {
targetURL: `https://localhost:2021/`, targetURL: `https://localhost:2021/`,
expectURL: `https://localhost:2021/%2F/test`, 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) targetURL, err := url.Parse(c.targetURL)
if err != nil { if err != nil {

View file

@ -121,6 +121,7 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) *
} }
// We should remove the `without` prefix at first. // We should remove the `without` prefix at first.
untouchedPath, _ := req.Context().Value(staticfiles.URLPathCtxKey).(string)
if without != "" { if without != "" {
req.URL.Path = strings.TrimPrefix(req.URL.Path, without) req.URL.Path = strings.TrimPrefix(req.URL.Path, without)
if req.URL.Opaque != "" { if req.URL.Opaque != "" {
@ -129,6 +130,9 @@ func NewSingleHostReverseProxy(target *url.URL, without string, keepalive int) *
if req.URL.RawPath != "" { if req.URL.RawPath != "" {
req.URL.RawPath = strings.TrimPrefix(req.URL.RawPath, without) 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 // 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(target.RawPath, target.Path),
prefer(req.URL.RawPath, req.URL.Path)) prefer(req.URL.RawPath, req.URL.Path))
} }
untouchedPath, _ := req.Context().Value(staticfiles.URLPathCtxKey).(string)
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path = singleJoiningSlash(target.Path,
prefer(untouchedPath, req.URL.Path)) prefer(untouchedPath, req.URL.Path))
// req.URL.Path must be consistent with decoded form of req.URL.RawPath if any // req.URL.Path must be consistent with decoded form of req.URL.RawPath if any