[go] net/http: remove hasPort and simplify logic

3 views
Skip to first unread message

Jorropo (Gerrit)

unread,
Dec 1, 2025, 11:06:54 PM (2 days ago) Dec 1
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Jorropo has uploaded the change for review

Commit message

net/http: remove hasPort and simplify logic

Fixes #76651
Change-Id: I306e127375095bc0caedb01ac458107cfec5f085

Change diff

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

Change information

Files:
  • M src/net/http/http.go
  • M src/net/http/transport.go
Change size: S
Delta: 2 files changed, 13 insertions(+), 11 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I306e127375095bc0caedb01ac458107cfec5f085
Gerrit-Change-Number: 725740
Gerrit-PatchSet: 1
Gerrit-Owner: Jorropo <jorro...@gmail.com>
Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Jorropo (Gerrit)

unread,
Dec 1, 2025, 11:07:26 PM (2 days ago) Dec 1
to goph...@pubsubhelper.golang.org, Go LUCI, golang-co...@googlegroups.com

Jorropo added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Jorropo . resolved

Probably need a test for `removePort`

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedNo-Wait-Release
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I306e127375095bc0caedb01ac458107cfec5f085
    Gerrit-Change-Number: 725740
    Gerrit-PatchSet: 1
    Gerrit-Owner: Jorropo <jorro...@gmail.com>
    Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
    Gerrit-Comment-Date: Tue, 02 Dec 2025 04:07:18 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Jorropo (Gerrit)

    unread,
    Dec 1, 2025, 11:07:43 PM (2 days ago) Dec 1
    to goph...@pubsubhelper.golang.org, Go LUCI, golang-co...@googlegroups.com

    Jorropo added 1 comment

    Patchset-level comments
    Jorropo . unresolved

    Probably need a test for `removePort`

    Jorropo

    todo

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I306e127375095bc0caedb01ac458107cfec5f085
      Gerrit-Change-Number: 725740
      Gerrit-PatchSet: 1
      Gerrit-Owner: Jorropo <jorro...@gmail.com>
      Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
      Gerrit-Comment-Date: Tue, 02 Dec 2025 04:07:36 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Jorropo <jorro...@gmail.com>
      unsatisfied_requirement
      open
      diffy

      Damien Neil (Gerrit)

      unread,
      Dec 2, 2025, 6:21:23 PM (9 hours ago) Dec 2
      to Jorropo, goph...@pubsubhelper.golang.org, Russ Cox, Gopher Robot, Go LUCI, golang-co...@googlegroups.com
      Attention needed from Jorropo

      Damien Neil added 2 comments

      File src/net/http/http.go
      Line 112, Patchset 1 (Latest): return strings.TrimSuffix(host, ":")
      Damien Neil . unresolved

      How about we drop `removeEmptyPort` and replace the one usage with TrimSuffix?

      File src/net/http/transport.go
      Line 2090, Patchset 1 (Latest): return removePort(h)
      Damien Neil . unresolved

      Use `net.SplitHostPort` instead? Or is there a reason that doesn't work?

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Jorropo
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedNo-Wait-Release
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I306e127375095bc0caedb01ac458107cfec5f085
      Gerrit-Change-Number: 725740
      Gerrit-PatchSet: 1
      Gerrit-Owner: Jorropo <jorro...@gmail.com>
      Gerrit-Reviewer: Damien Neil <dn...@google.com>
      Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Russ Cox <r...@golang.org>
      Gerrit-Attention: Jorropo <jorro...@gmail.com>
      Gerrit-Comment-Date: Tue, 02 Dec 2025 23:21:17 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages