1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00

fix: IPv6 addr in square brackets

This commit is contained in:
MoonLightWatch 2022-09-09 16:44:07 +08:00 committed by GitHub
parent b33c8cec0b
commit 77c8d60092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -49,11 +49,16 @@ func (r *RequestDecorator) ServeHTTP(rw http.ResponseWriter, req *http.Request,
func parseHost(addr string) string {
if !strings.Contains(addr, ":") {
// IPv4 without port or empty address
return addr
}
// IPv4 with port or IPv6
host, _, err := net.SplitHostPort(addr)
if err != nil {
if addr[0] == '[' && addr[len(addr)-1] == ']' {
return addr[1 : len(addr)-1]
}
return addr
}
return host

View File

@ -104,7 +104,7 @@ func TestRequestFlattening(t *testing.T) {
}
}
func TestRequestHostParseHost(t *testing.T) {
func Test_parseHost(t *testing.T) {
testCases := []struct {
desc string
host string
@ -130,6 +130,46 @@ func TestRequestHostParseHost(t *testing.T) {
host: "127.0.0.1:",
expected: "127.0.0.1",
},
{
desc: "host with : and without port",
host: "fe80::215:5dff:fe20:cd6a",
expected: "fe80::215:5dff:fe20:cd6a",
},
{
desc: "IPv6 host with : and with port",
host: "[fe80::215:5dff:fe20:cd6a]:123",
expected: "fe80::215:5dff:fe20:cd6a",
},
{
desc: "IPv6 host with : and without port",
host: "[fe80::215:5dff:fe20:cd6a]:",
expected: "fe80::215:5dff:fe20:cd6a",
},
{
desc: "IPv6 host without : and without port",
host: "[fe80::215:5dff:fe20:cd6a]",
expected: "fe80::215:5dff:fe20:cd6a",
},
{
desc: "invalid IPv6: missing [",
host: "fe80::215:5dff:fe20:cd6a]",
expected: "fe80::215:5dff:fe20:cd6a]",
},
{
desc: "invalid IPv6: missing ]",
host: "[fe80::215:5dff:fe20:cd6a",
expected: "[fe80::215:5dff:fe20:cd6a",
},
{
desc: "empty address",
host: "",
expected: "",
},
{
desc: "only :",
host: ":",
expected: "",
},
}
for _, test := range testCases {