mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 10:43:56 +00:00
Merge pull request #9768 from timstclair/proxy-rel
Don't rewrite relative URLs when proxying HTTP content.
This commit is contained in:
commit
bcf5cffa1a
@ -119,21 +119,17 @@ func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return targetURL
|
return targetURL
|
||||||
}
|
}
|
||||||
if url.Host != "" && url.Host != sourceURL.Host {
|
|
||||||
|
isDifferentHost := url.Host != "" && url.Host != sourceURL.Host
|
||||||
|
isRelative := !strings.HasPrefix(url.Path, "/")
|
||||||
|
if isDifferentHost || isRelative {
|
||||||
return targetURL
|
return targetURL
|
||||||
}
|
}
|
||||||
|
|
||||||
url.Scheme = t.Scheme
|
url.Scheme = t.Scheme
|
||||||
url.Host = t.Host
|
url.Host = t.Host
|
||||||
origPath := 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.PathPrepend, url.Path)
|
url.Path = path.Join(t.PathPrepend, url.Path)
|
||||||
} else {
|
|
||||||
// The path is relative to sourceURL.
|
|
||||||
url.Path = path.Join(t.PathPrepend, path.Dir(sourceURL.Path), url.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasSuffix(origPath, "/") {
|
if strings.HasSuffix(origPath, "/") {
|
||||||
// Add back the trailing slash, which was stripped by path.Join().
|
// Add back the trailing slash, which was stripped by path.Join().
|
||||||
@ -144,10 +140,9 @@ func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// updateURLs checks and updates any of n's attributes that are listed in tagsToAttrs.
|
// updateURLs checks and updates any of n's attributes that are listed in tagsToAttrs.
|
||||||
// Any URLs found are, if they're relative, updated with the necessary changes to make
|
// Any URLs found are, if they share the source host, updated with the necessary changes
|
||||||
// a visit to that URL also go through the proxy.
|
// to make a visit to that URL also go through the proxy.
|
||||||
// sourceURL is the URL of the page which we're currently on; it's required to make
|
// sourceURL is the URL of the page which we're currently on.
|
||||||
// relative links work.
|
|
||||||
func (t *Transport) updateURLs(n *html.Node, sourceURL *url.URL) {
|
func (t *Transport) updateURLs(n *html.Node, sourceURL *url.URL) {
|
||||||
if n.Type != html.ElementNode {
|
if n.Type != html.ElementNode {
|
||||||
return
|
return
|
||||||
|
@ -77,7 +77,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
sourceURL: "http://myminion.com/logs/log.log",
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
transport: testTransport,
|
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>`,
|
output: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
@ -85,7 +85,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log/">google.log</a></pre>`,
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log/">google.log</a></pre>`,
|
||||||
sourceURL: "http://myminion.com/logs/log.log",
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
transport: testTransport,
|
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>`,
|
output: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log/">google.log</a></pre>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
@ -93,7 +93,7 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
|
||||||
sourceURL: "http://myminion.com/logs/log.log",
|
sourceURL: "http://myminion.com/logs/log.log",
|
||||||
transport: testTransport,
|
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>`,
|
output: `<pre><a href="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",
|
contentType: "text/html; charset=utf-8",
|
||||||
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
|
||||||
},
|
},
|
||||||
@ -109,15 +109,15 @@ func TestProxyTransport(t *testing.T) {
|
|||||||
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
|
input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
|
||||||
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
|
sourceURL: "http://myminion.com/whatever/apt/somelog.log",
|
||||||
transport: testTransport2,
|
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/google.log">google.log</a>`,
|
output: `<a href="kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/google.log">google.log</a>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
|
forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
|
||||||
},
|
},
|
||||||
"image": {
|
"image": {
|
||||||
input: `<pre><img src="kubernetes.jpg"/></pre>`,
|
input: `<pre><img src="kubernetes.jpg"/><img src="/kubernetes_abs.jpg"/></pre>`,
|
||||||
sourceURL: "http://myminion.com/",
|
sourceURL: "http://myminion.com/",
|
||||||
transport: testTransport,
|
transport: testTransport,
|
||||||
output: `<pre><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes.jpg"/></pre>`,
|
output: `<pre><img src="kubernetes.jpg"/><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes_abs.jpg"/></pre>`,
|
||||||
contentType: "text/html",
|
contentType: "text/html",
|
||||||
forwardedURI: "/proxy/minion/minion1:10250/",
|
forwardedURI: "/proxy/minion/minion1:10250/",
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user