net/http: remove hasPort and simplify logic
Fixes #76651
diff --git a/src/net/http/http.go b/src/net/http/http.go
index d346e60..fe0f2b3 100644
--- a/src/net/http/http.go
+++ b/src/net/http/http.go
@@ -106,17 +106,22 @@
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, ":")
+ 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
}
// isToken reports whether v is a valid token (https://www.rfc-editor.org/rfc/rfc2616#section-2.2).
diff --git a/src/net/http/transport.go b/src/net/http/transport.go
index 26a25d2..5179239 100644
--- a/src/net/http/transport.go
+++ b/src/net/http/transport.go
@@ -2087,10 +2087,7 @@
// 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
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Probably need a test for `removePort`
todo
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
return strings.TrimSuffix(host, ":")How about we drop `removeEmptyPort` and replace the one usage with TrimSuffix?
return removePort(h)Use `net.SplitHostPort` instead? Or is there a reason that doesn't work?
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |