Skip to content

Commit a1d6821

Browse files
committed
http2: export a field of an internal type for use by net/http
Updates golang/go#22891 Change-Id: Ibde5ce0867a78703a5a4f04fafc3d709ea4cbda3 Reviewed-on: https://go-review.googlesource.com/123656 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a45b4ab commit a1d6821

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

http2/configure_transport.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func configureTransport(t1 *http.Transport) (*Transport, error) {
5757

5858
// registerHTTPSProtocol calls Transport.RegisterProtocol but
5959
// converting panics into errors.
60-
func registerHTTPSProtocol(t *http.Transport, rt http.RoundTripper) (err error) {
60+
func registerHTTPSProtocol(t *http.Transport, rt noDialH2RoundTripper) (err error) {
6161
defer func() {
6262
if e := recover(); e != nil {
6363
err = fmt.Errorf("%v", e)
@@ -69,10 +69,12 @@ func registerHTTPSProtocol(t *http.Transport, rt http.RoundTripper) (err error)
6969

7070
// noDialH2RoundTripper is a RoundTripper which only tries to complete the request
7171
// if there's already has a cached connection to the host.
72-
type noDialH2RoundTripper struct{ t *Transport }
72+
// (The field is exported so it can be accessed via reflect from net/http; tested
73+
// by TestNoDialH2RoundTripperType)
74+
type noDialH2RoundTripper struct{ *Transport }
7375

7476
func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
75-
res, err := rt.t.RoundTrip(req)
77+
res, err := rt.Transport.RoundTrip(req)
7678
if isNoCachedConnError(err) {
7779
return nil, http.ErrSkipAltProtocol
7880
}

http2/transport_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -4161,3 +4161,25 @@ func TestTransportUsesGetBodyWhenPresent(t *testing.T) {
41614161
t.Error("req2.Body unchanged")
41624162
}
41634163
}
4164+
4165+
// Issue 22891: verify that the "https" altproto we register with net/http
4166+
// is a certain type: a struct with one field with our *http2.Transport in it.
4167+
func TestNoDialH2RoundTripperType(t *testing.T) {
4168+
t1 := new(http.Transport)
4169+
t2 := new(Transport)
4170+
rt := noDialH2RoundTripper{t2}
4171+
if err := registerHTTPSProtocol(t1, rt); err != nil {
4172+
t.Fatal(err)
4173+
}
4174+
rv := reflect.ValueOf(rt)
4175+
if rv.Type().Kind() != reflect.Struct {
4176+
t.Fatalf("kind = %v; net/http expects struct", rv.Type().Kind())
4177+
}
4178+
if n := rv.Type().NumField(); n != 1 {
4179+
t.Fatalf("fields = %d; net/http expects 1", n)
4180+
}
4181+
v := rv.Field(0)
4182+
if _, ok := v.Interface().(*Transport); !ok {
4183+
t.Fatalf("wrong kind %T; want *Transport", v.Interface())
4184+
}
4185+
}

0 commit comments

Comments
 (0)