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
}
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)
+ }
+ }
+}
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,
// 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