浏览代码

http2: allow Transport to connect to https://[v6literal]/ without port

Fixes golang/go#18248

Change-Id: I271fbcc68a86f20c57b258c2e25788908dafdd94
Reviewed-on: https://go-review.googlesource.com/34143
Reviewed-by: Tom Bergan <tombergan@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Brad Fitzpatrick 9 年之前
父节点
当前提交
e31bd588d1
共有 2 个文件被更改,包括 25 次插入0 次删除
  1. 4 0
      http2/transport.go
  2. 21 0
      http2/transport_test.go

+ 4 - 0
http2/transport.go

@@ -315,6 +315,10 @@ func authorityAddr(scheme string, authority string) (addr string) {
 	if a, err := idna.ToASCII(host); err == nil {
 		host = a
 	}
+	// IPv6 address literal, without a port:
+	if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
+		return host + ":" + port
+	}
 	return net.JoinHostPort(host, port)
 }
 

+ 21 - 0
http2/transport_test.go

@@ -2892,3 +2892,24 @@ func TestTransportRetryAfterGOAWAY(t *testing.T) {
 		}
 	}
 }
+
+func TestAuthorityAddr(t *testing.T) {
+	tests := []struct {
+		scheme, authority string
+		want              string
+	}{
+		{"http", "foo.com", "foo.com:80"},
+		{"https", "foo.com", "foo.com:443"},
+		{"https", "foo.com:1234", "foo.com:1234"},
+		{"https", "1.2.3.4:1234", "1.2.3.4:1234"},
+		{"https", "1.2.3.4", "1.2.3.4:443"},
+		{"https", "[::1]:1234", "[::1]:1234"},
+		{"https", "[::1]", "[::1]:443"},
+	}
+	for _, tt := range tests {
+		got := authorityAddr(tt.scheme, tt.authority)
+		if got != tt.want {
+			t.Errorf("authorityAddr(%q, %q) = %q; want %q", tt.scheme, tt.authority, got, tt.want)
+		}
+	}
+}