From 3db0bfdf6569920137197d1497f3c61dec09a04e Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 2 Jul 2015 14:46:43 +0200 Subject: [PATCH] kubectl: Handle splitting host:port when no port is present In the proxy filter, if listening on port 80 or 443 there will be no port present in the Host: HTTP header. Just use the entire thing in these cases, and don't reject requests when no port is present in the Host: HTTP header. --- pkg/kubectl/proxy_server.go | 11 ++++++++++- pkg/kubectl/proxy_server_test.go | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/proxy_server.go b/pkg/kubectl/proxy_server.go index 70620f201af..f76f27cbc8e 100644 --- a/pkg/kubectl/proxy_server.go +++ b/pkg/kubectl/proxy_server.go @@ -106,8 +106,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) + } + } +}