From 695b08885bdc63f574b0ec0c2279a381856ff8c9 Mon Sep 17 00:00:00 2001 From: Anthony Yeh Date: Mon, 2 Feb 2015 13:01:46 -0800 Subject: [PATCH] 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 --- pkg/apiserver/proxy.go | 26 ++++++++++++++++++-------- pkg/apiserver/proxy_test.go | 14 +++++++++++--- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/apiserver/proxy.go b/pkg/apiserver/proxy.go index 360ef417152..d42e9fc87c0 100644 --- a/pkg/apiserver/proxy.go +++ b/pkg/apiserver/proxy.go @@ -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() } } diff --git a/pkg/apiserver/proxy_test.go b/pkg/apiserver/proxy_test.go index 8eb51e4d09a..0113f86e8c5 100644 --- a/pkg/apiserver/proxy_test.go +++ b/pkg/apiserver/proxy_test.go @@ -76,7 +76,15 @@ func TestProxyTransport(t *testing.T) { input: `
kubelet.loggoogle.log
`, sourceURL: "http://myminion.com/logs/log.log", transport: testTransport, - output: `
kubelet.loggoogle.log
`, + output: `
kubelet.loggoogle.log
`, + contentType: "text/html", + forwardedURI: "/proxy/minion/minion1:10250/logs/log.log", + }, + "trailing slash": { + input: `
kubelet.loggoogle.log
`, + sourceURL: "http://myminion.com/logs/log.log", + transport: testTransport, + output: `
kubelet.loggoogle.log
`, contentType: "text/html", forwardedURI: "/proxy/minion/minion1:10250/logs/log.log", }, @@ -84,7 +92,7 @@ func TestProxyTransport(t *testing.T) { input: `
kubelet.loggoogle.log
`, sourceURL: "http://myminion.com/logs/log.log", transport: testTransport, - output: `
kubelet.loggoogle.log
`, + output: `
kubelet.loggoogle.log
`, contentType: "text/html; charset=utf-8", forwardedURI: "/proxy/minion/minion1:10250/logs/log.log", }, @@ -100,7 +108,7 @@ func TestProxyTransport(t *testing.T) { input: `kubelet.loggoogle.log`, sourceURL: "http://myminion.com/whatever/apt/somelog.log", transport: testTransport2, - output: `kubelet.loggoogle.log`, + output: `kubelet.loggoogle.log`, contentType: "text/html", forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log", },