Merge pull request #10673 from stefwalter/proxy-split-hostport-without-port

kubectl: Handle splitting host:port when no port is present
This commit is contained in:
Mike Danese 2015-07-24 17:23:05 -07:00
commit eb3d6822d0
2 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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)
}
}
}