Fix apiserver proxy path rewriting.

1) Absolute paths without a hostname were being rewritten relative to
   the current page, rather than relative to the current host.
   e.g. When viewing /some/page.html, it would rewrite:
       /other/page.html => proxyPrepend/some/other/page.html
   Instead, it should rewrite:
       /other/page.html => proxyPrepend/other/page.html

2) Trailing slashes were being stripped from all rewritten URLs.
   This is because path.Join() always calls path.Clean() as well:
   http://golang.org/pkg/path/#Join
This commit is contained in:
Anthony Yeh
2015-02-02 13:01:46 -08:00
parent 6e415f760b
commit 695b08885b
2 changed files with 29 additions and 11 deletions

View File

@@ -216,16 +216,26 @@ func (t *proxyTransport) updateURLs(n *html.Node, sourceURL *url.URL) {
if err != nil {
continue
}
// Is this URL relative?
if url.Host == "" {
// Is this URL referring to the same host as sourceURL?
if url.Host == "" || url.Host == sourceURL.Host {
url.Scheme = t.proxyScheme
url.Host = t.proxyHost
url.Path = path.Join(t.proxyPathPrepend, path.Dir(sourceURL.Path), url.Path, "/")
n.Attr[i].Val = url.String()
} else if url.Host == sourceURL.Host {
url.Scheme = t.proxyScheme
url.Host = t.proxyHost
url.Path = path.Join(t.proxyPathPrepend, url.Path)
origPath := url.Path
if strings.HasPrefix(url.Path, "/") {
// The path is rooted at the host. Just add proxy prepend.
url.Path = path.Join(t.proxyPathPrepend, url.Path)
} else {
// The path is relative to sourceURL.
url.Path = path.Join(t.proxyPathPrepend, path.Dir(sourceURL.Path), url.Path)
}
if strings.HasSuffix(origPath, "/") {
// Add back the trailing slash, which was stripped by path.Join().
url.Path += "/"
}
n.Attr[i].Val = url.String()
}
}