]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: remove hasPort and simplify logic
authorJorropo <jorropo.pgm@gmail.com>
Tue, 2 Dec 2025 04:06:09 +0000 (05:06 +0100)
committerGopher Robot <gobot@golang.org>
Fri, 6 Feb 2026 17:37:27 +0000 (09:37 -0800)
Fixes #76651

Change-Id: I306e127375095bc0caedb01ac458107cfec5f085
Reviewed-on: https://go-review.googlesource.com/c/go/+/725740
Auto-Submit: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
src/net/http/http.go
src/net/http/http_test.go
src/net/http/request.go
src/net/http/transport.go

index d3e9a2787ad430f1b14784cc99cf5fb5f37536d5..100d4fe4a1ab34a52c21950c38151e26a2892a45 100644 (file)
@@ -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
 }
index c12bbedac986db5995c2b6a2b5ce9406a3de5b56..9ac6e970324f25b88f430caae2175ea5231ccb79 100644 (file)
@@ -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)
+               }
+       }
+}
index 167cff585af34a660aed36a395e8693b27b78086..f49bbeee161e703e9401d576030ebbfdc5110835 100644 (file)
@@ -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,
index 924e7cfcb0913a140a0c444a391ba59b4610fe38..49f9096e3fd3397dceb3454f9219b56709a41cf4 100644 (file)
@@ -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