Skip to content

Commit

Permalink
fixed naive impl, added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmittag committed Dec 2, 2023
1 parent c7dbad6 commit fab215d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
29 changes: 12 additions & 17 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit fab215d

Please sign in to comment.