diff --git a/proxy.go b/proxy.go index 28f1663..183bf66 100644 --- a/proxy.go +++ b/proxy.go @@ -529,26 +529,21 @@ func isIPv6(address string) bool { ip := net.ParseIP(address) return ip != nil && ip.To4() == nil } + func parseHost(request *http.Request) string { - //ignore conversion errors - if isIPv6(request.Host) { - return request.Host - } - al := strings.Split(request.Host, colon) - fmt.Println("Number of colons", len(al)) - if len(al) == 9 { - host := strings.Join(al[:8], ":") - fmt.Println("Host - ", host) - if isIPv6(host) { - return host - } - return request.Host + host := request.Host + hostElements := strings.Split(host, ":") + //trim port for ipv4 + if len(hostElements) == 2 { + host = hostElements[0] } - if len(al) > 2 { - return request.Host + + //trim port for ipv6 + if strings.Contains(host, "]") { + host = host[:strings.LastIndex(host, "]")+1] } - al2, _ := idna.ToASCII(al[0]) - return al2 + host, _ = idna.ToASCII(host) + return host } func parseMethod(request *http.Request) string { diff --git a/proxy_test.go b/proxy_test.go index 26cb67d..9f621a8 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -475,7 +475,15 @@ func TestParseHost(t *testing.T) { {name: "ipv4 with port", url: "http://127.0.0.1:8080/path", host: "127.0.0.1"}, {name: "simple ipv6", url: "http://::1/path", host: "::1"}, {name: "simple ipv6 in brackets", url: "http://[::1]/path", host: "[::1]"}, + {name: "simple ipv6 in brackets", url: "http://[::]/path", host: "[::]"}, + {name: "simple ipv6 in brackets", url: "http://[2001:db8::]/path", host: "[2001:db8::]"}, + {name: "simple ipv6 in brackets", url: "http://[::1234:5678]/path", host: "[::1234:5678]"}, + {name: "simple ipv6 in brackets", url: "http://[2001:db8::1234:5678]/path", host: "[2001:db8::1234:5678]"}, + {name: "full ipv6 in brackets", url: "http://[2001:db8:3333:4444:5555:6666:7777:8888]/path", host: "[2001:db8:3333:4444:5555:6666:7777:8888]"}, {name: "ipv6 with port", url: "http://[::1]:8080/path", host: "[::1]"}, + {name: "ipv6 with port", url: "http://[::1234:5678]:8080/path", host: "[::1234:5678]"}, + {name: "ipv6 with port", url: "http://[2001:db8::1234:5678]:8080/path", host: "[2001:db8::1234:5678]"}, + {name: "ipv6 with port", url: "http://[2001:db8:3333:4444:5555:6666:7777:8888]:8080/path", host: "[2001:db8:3333:4444:5555:6666:7777:8888]"}, {name: "simple with port", url: "http://host:8080/path", host: "host"}, {name: "fqdn with port", url: "http://sub.host.com:8080/path", host: "sub.host.com"}, {name: "idna simple", url: "http://aaa😀😀😀:8080/path", host: "xn--aaa-th33baa"},