diff --git a/pkg/kubectl/proxy_server.go b/pkg/kubectl/proxy_server.go index 4c6196a0daf..0d039325eed 100644 --- a/pkg/kubectl/proxy_server.go +++ b/pkg/kubectl/proxy_server.go @@ -117,8 +117,17 @@ func (f *FilterServer) HandlerFor(delegate http.Handler) *FilterServer { return &f2 } +// Get host from a host header value like "localhost" or "localhost:8080" +func extractHost(header string) (host string) { + host, _, err := net.SplitHostPort(header) + if err != nil { + host = header + } + return host +} + func (f *FilterServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { - host, _, _ := net.SplitHostPort(req.Host) + host := extractHost(req.Host) if f.accept(req.Method, req.URL.Path, host) { f.delegate.ServeHTTP(rw, req) return diff --git a/pkg/kubectl/proxy_server_test.go b/pkg/kubectl/proxy_server_test.go index 10eba1133bb..883c083562a 100644 --- a/pkg/kubectl/proxy_server_test.go +++ b/pkg/kubectl/proxy_server_test.go @@ -309,3 +309,16 @@ func TestPathHandling(t *testing.T) { }() } } + +func TestExtractHost(t *testing.T) { + fixtures := map[string]string{ + "localhost:8085": "localhost", + "marmalade": "marmalade", + } + for header, expected := range fixtures { + host := extractHost(header) + if host != expected { + t.Fatalf("%s != %s", host, expected) + } + } +}