mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
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:
commit
eb3d6822d0
@ -117,8 +117,17 @@ func (f *FilterServer) HandlerFor(delegate http.Handler) *FilterServer {
|
|||||||
return &f2
|
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) {
|
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) {
|
if f.accept(req.Method, req.URL.Path, host) {
|
||||||
f.delegate.ServeHTTP(rw, req)
|
f.delegate.ServeHTTP(rw, req)
|
||||||
return
|
return
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user