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()
}
}

View File

@ -76,7 +76,15 @@ func TestProxyTransport(t *testing.T) {
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
sourceURL: "http://myminion.com/logs/log.log",
transport: testTransport,
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
contentType: "text/html",
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
},
"trailing slash": {
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log/">google.log</a></pre>`,
sourceURL: "http://myminion.com/logs/log.log",
transport: testTransport,
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log/">google.log</a></pre>`,
contentType: "text/html",
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
},
@ -84,7 +92,7 @@ func TestProxyTransport(t *testing.T) {
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
sourceURL: "http://myminion.com/logs/log.log",
transport: testTransport,
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/logs/google.log">google.log</a></pre>`,
output: `<pre><a href="http://foo.com/proxy/minion/minion1:10250/logs/kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
contentType: "text/html; charset=utf-8",
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
},
@ -100,7 +108,7 @@ func TestProxyTransport(t *testing.T) {
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
transport: testTransport2,
output: `<a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/google.log">google.log</a>`,
output: `<a href="https://foo.com/proxy/minion/minion1:8080/whatever/apt/kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/google.log">google.log</a>`,
contentType: "text/html",
forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
},