From e09b8c99dc4d65592bb8990d2a1be7d14efd51b0 Mon Sep 17 00:00:00 2001 From: Cesar Wong Date: Thu, 16 Apr 2015 17:24:01 -0400 Subject: [PATCH] Add URL parameters to proxy redirect Location header When a URL that doesn't end in "/" is sent to the API proxy, the proxy responds with a redirect to the URL with a "/" at the end. The problem is that this redirect path does not include query parameters that were passed in the original request. This is a fix to that issue. --- pkg/apiserver/proxy.go | 6 +++++- pkg/apiserver/proxy_test.go | 15 ++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/apiserver/proxy.go b/pkg/apiserver/proxy.go index 83660205924..8b4336beed7 100644 --- a/pkg/apiserver/proxy.go +++ b/pkg/apiserver/proxy.go @@ -195,7 +195,11 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // This is essentially a hack for https://github.com/GoogleCloudPlatform/kubernetes/issues/4958. // Note: Keep this code after tryUpgrade to not break that flow. if len(parts) == 2 && !strings.HasSuffix(req.URL.Path, "/") { - w.Header().Set("Location", req.URL.Path+"/") + var queryPart string + if len(req.URL.RawQuery) > 0 { + queryPart = "?" + req.URL.RawQuery + } + w.Header().Set("Location", req.URL.Path+"/"+queryPart) w.WriteHeader(http.StatusMovedPermanently) return } diff --git a/pkg/apiserver/proxy_test.go b/pkg/apiserver/proxy_test.go index 15d66a65e41..9d2f25ecd09 100644 --- a/pkg/apiserver/proxy_test.go +++ b/pkg/apiserver/proxy_test.go @@ -374,13 +374,15 @@ func TestRedirectOnMissingTrailingSlash(t *testing.T) { path string // The path requested on the proxy server. proxyServerPath string + // query string + query string }{ - {"/trailing/slash/", "/trailing/slash/"}, - {"/", "/"}, + {"/trailing/slash/", "/trailing/slash/", ""}, + {"/", "/", "test1=value1&test2=value2"}, // "/" should be added at the end. - {"", "/"}, + {"", "/", "test1=value1&test2=value2"}, // "/" should not be added at a non-root path. - {"/some/path", "/some/path"}, + {"/some/path", "/some/path", ""}, } for _, item := range table { @@ -388,6 +390,9 @@ func TestRedirectOnMissingTrailingSlash(t *testing.T) { if req.URL.Path != item.proxyServerPath { t.Errorf("Unexpected request on path: %s, expected path: %s, item: %v", req.URL.Path, item.proxyServerPath, item) } + if req.URL.RawQuery != item.query { + t.Errorf("Unexpected query on url: %s, expected: %s", req.URL.RawQuery, item.query) + } })) defer proxyServer.Close() @@ -405,7 +410,7 @@ func TestRedirectOnMissingTrailingSlash(t *testing.T) { proxyTestPattern := "/api/version2/proxy/namespaces/ns/foo/id" + item.path req, err := http.NewRequest( "GET", - server.URL+proxyTestPattern, + server.URL+proxyTestPattern+"?"+item.query, strings.NewReader(""), ) if err != nil {