From: Jorropo Date: Tue, 2 Dec 2025 04:06:09 +0000 (+0100) Subject: net/http: remove hasPort and simplify logic X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=fc9f22134a870528a7a6d110fa6172431f73cccf;p=gostls13.git net/http: remove hasPort and simplify logic Fixes #76651 Change-Id: I306e127375095bc0caedb01ac458107cfec5f085 Reviewed-on: https://go-review.googlesource.com/c/go/+/725740 Auto-Submit: Sean Liao LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Knyszek Reviewed-by: Damien Neil Reviewed-by: Sean Liao --- diff --git a/src/net/http/http.go b/src/net/http/http.go index d3e9a2787a..100d4fe4a1 100644 --- a/src/net/http/http.go +++ b/src/net/http/http.go @@ -106,15 +106,15 @@ type contextKey struct { func (k *contextKey) String() string { return "net/http context value " + k.name } -// Given a string of the form "host", "host:port", or "[ipv6::address]:port", -// return true if the string includes a port. -func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } - -// removeEmptyPort strips the empty port in ":port" to "" -// as mandated by RFC 3986 Section 6.2.3. -func removeEmptyPort(host string) string { - if hasPort(host) { - return strings.TrimSuffix(host, ":") +// removePort strips the port while correclty handling IPv6. +func removePort(host string) string { + for i := len(host) - 1; i >= 0; i-- { + switch host[i] { + case ':': + return host[:i] + case ']': + return host + } } return host } diff --git a/src/net/http/http_test.go b/src/net/http/http_test.go index c12bbedac9..9ac6e97032 100644 --- a/src/net/http/http_test.go +++ b/src/net/http/http_test.go @@ -220,3 +220,22 @@ func BenchmarkHexEscapeNonASCII(b *testing.B) { hexEscapeNonASCII(redirectURL) } } + +func TestRemovePort(t *testing.T) { + tests := []struct { + in, want string + }{ + {"example.com:8080", "example.com"}, + {"example.com", "example.com"}, + {"[2001:db8::1]:443", "[2001:db8::1]"}, + {"[2001:db8::1]", "[2001:db8::1]"}, + {"192.0.2.1:8080", "192.0.2.1"}, + {"192.0.2.1", "192.0.2.1"}, + } + for _, tc := range tests { + got := removePort(tc.in) + if got != tc.want { + t.Errorf("removePort(%q) = %q; want %q", tc.in, got, tc.want) + } + } +} diff --git a/src/net/http/request.go b/src/net/http/request.go index 167cff585a..f49bbeee16 100644 --- a/src/net/http/request.go +++ b/src/net/http/request.go @@ -908,7 +908,7 @@ func NewRequestWithContext(ctx context.Context, method, url string, body io.Read rc = io.NopCloser(body) } // The host's colon:port should be normalized. See Issue 14836. - u.Host = removeEmptyPort(u.Host) + u.Host = strings.TrimSuffix(u.Host, ":") req := &Request{ ctx: ctx, Method: method, diff --git a/src/net/http/transport.go b/src/net/http/transport.go index 924e7cfcb0..49f9096e3f 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -2087,10 +2087,7 @@ func (cm *connectMethod) addr() string { // TLS certificate. func (cm *connectMethod) tlsHost() string { h := cm.targetAddr - if hasPort(h) { - h = h[:strings.LastIndex(h, ":")] - } - return h + return removePort(h) } // connectMethodKey is the map key version of connectMethod, with a