mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Handles redirection when service returns absolute path with request's
host.
This commit is contained in:
parent
6567bbee7b
commit
3102f37e8a
@ -109,7 +109,7 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
}
|
||||
|
||||
if redirect := resp.Header.Get("Location"); redirect != "" {
|
||||
resp.Header.Set("Location", t.rewriteURL(redirect, req.URL))
|
||||
resp.Header.Set("Location", t.rewriteURL(redirect, req.URL, req.Host))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@ -131,21 +131,27 @@ func (rt *Transport) WrappedRoundTripper() http.RoundTripper {
|
||||
|
||||
// rewriteURL rewrites a single URL to go through the proxy, if the URL refers
|
||||
// to the same host as sourceURL, which is the page on which the target URL
|
||||
// occurred. If any error occurs (e.g. parsing), it returns targetURL.
|
||||
func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string {
|
||||
// occurred, or if the URL matches the sourceHost. If any error occurs (e.g.
|
||||
// parsing), it returns targetURL.
|
||||
func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL, sourceHost string) string {
|
||||
url, err := url.Parse(targetURL)
|
||||
if err != nil {
|
||||
return targetURL
|
||||
}
|
||||
|
||||
isDifferentHost := url.Host != "" && url.Host != sourceURL.Host
|
||||
isDifferentHost := url.Host != "" && url.Host != sourceURL.Host && url.Host != sourceHost
|
||||
isRelative := !strings.HasPrefix(url.Path, "/")
|
||||
if isDifferentHost || isRelative {
|
||||
return targetURL
|
||||
}
|
||||
|
||||
url.Scheme = t.Scheme
|
||||
url.Host = t.Host
|
||||
// Do not rewrite scheme and host if the Transport has empty scheme and host
|
||||
// when targetURL already contains the sourceHost
|
||||
if !(url.Host == sourceHost && t.Scheme == "" && t.Host == "") {
|
||||
url.Scheme = t.Scheme
|
||||
url.Host = t.Host
|
||||
}
|
||||
|
||||
origPath := url.Path
|
||||
// Do not rewrite URL if the sourceURL already contains the necessary prefix.
|
||||
if strings.HasPrefix(url.Path, t.PathPrepend) {
|
||||
@ -223,7 +229,7 @@ func (t *Transport) rewriteResponse(req *http.Request, resp *http.Response) (*ht
|
||||
}
|
||||
|
||||
urlRewriter := func(targetUrl string) string {
|
||||
return t.rewriteURL(targetUrl, req.URL)
|
||||
return t.rewriteURL(targetUrl, req.URL, req.Host)
|
||||
}
|
||||
err := rewriteHTML(reader, writer, urlRewriter)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user