aboutsummaryrefslogtreecommitdiff
path: root/src/net/http
diff options
context:
space:
mode:
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