aboutsummaryrefslogtreecommitdiff
path: root/src/net/http
diff options
context:
space:
mode:
authorJorropo <jorropo.pgm@gmail.com>2025-12-02 05:06:09 +0100
committerGopher Robot <gobot@golang.org>2026-02-06 09:37:27 -0800
commitfc9f22134a870528a7a6d110fa6172431f73cccf (patch)
treedbd929a6cd9f5bfb7df43c6922ceee07fe5840f8 /src/net/http
parenta72a4295edf694395ba9d3b5e7c5393ebf6a415d (diff)
downloadgo-fc9f22134a870528a7a6d110fa6172431f73cccf.tar.xz
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 <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>
Diffstat (limited to 'src/net/http')
-rw-r--r--src/net/http/http.go18
-rw-r--r--src/net/http/http_test.go19
-rw-r--r--src/net/http/request.go2
-rw-r--r--src/net/http/transport.go5
4 files changed, 30 insertions, 14 deletions
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