@@ -117,20 +117,38 @@ func dialBackOffHelper(ctx context.Context, network, address string, bo wait.Bac
117117 return nil , fmt .Errorf ("%w %s after %.2fs" , ErrTimeoutDialing , address , elapsed .Seconds ())
118118}
119119
120- func newHTTPTransport (disableKeepAlives , disableCompression bool , maxIdle , maxIdlePerHost int ) http.RoundTripper {
120+ func newHTTPTransport (
121+ disableKeepAlives ,
122+ disableCompression bool ,
123+ maxIdle ,
124+ maxIdlePerHost int ,
125+ ) * http.Transport {
126+ var protocols http.Protocols
127+ protocols .SetHTTP1 (true )
128+
121129 transport := http .DefaultTransport .(* http.Transport ).Clone ()
122130 transport .DialContext = DialWithBackOff
123131 transport .DisableKeepAlives = disableKeepAlives
124132 transport .MaxIdleConns = maxIdle
125133 transport .MaxIdleConnsPerHost = maxIdlePerHost
126134 transport .ForceAttemptHTTP2 = false
127135 transport .DisableCompression = disableCompression
136+ transport .Protocols = & protocols
128137 return transport
129138}
130139
131140type DialTLSContextFunc func (ctx context.Context , network , addr string ) (net.Conn , error )
132141
133- func newHTTPSTransport (disableKeepAlives , disableCompression bool , maxIdle , maxIdlePerHost int , tlsContext DialTLSContextFunc ) http.RoundTripper {
142+ func newHTTPSTransport (
143+ disableKeepAlives ,
144+ disableCompression bool ,
145+ maxIdle ,
146+ maxIdlePerHost int ,
147+ tlsContext DialTLSContextFunc ,
148+ ) * http.Transport {
149+ var protocols http.Protocols
150+ protocols .SetHTTP1 (true )
151+
134152 transport := http .DefaultTransport .(* http.Transport ).Clone ()
135153 transport .DisableKeepAlives = disableKeepAlives
136154 transport .MaxIdleConns = maxIdle
@@ -145,33 +163,72 @@ func newHTTPSTransport(disableKeepAlives, disableCompression bool, maxIdle, maxI
145163// NewProberTransport creates a RoundTripper that is useful for probing,
146164// since it will not cache connections.
147165func NewProberTransport () http.RoundTripper {
148- return newAutoTransport (
149- newHTTPTransport (true /*disable keep-alives*/ , false /*disable auto-compression*/ , 0 , 0 /*no caching*/ ),
150- NewH2CTransport ())
166+ http := newHTTPTransport (
167+ true , /*disable keep-alives*/
168+ false , /*disable auto-compression*/
169+ 0 , /*max idle*/
170+ 0 , /*no caching*/
171+ )
172+
173+ // h2 prior knowledge
174+ h2 := http .Clone ()
175+ h2 .Protocols .SetHTTP1 (false )
176+ h2 .Protocols .SetUnencryptedHTTP2 (true )
177+
178+ return newAutoTransport (http , h2 )
151179}
152180
153181// NewProxyAutoTLSTransport is same with NewProxyAutoTransport but it has DialTLSContextFunc to create HTTPS request.
154182func NewProxyAutoTLSTransport (maxIdle , maxIdlePerHost int , tlsContext DialTLSContextFunc ) http.RoundTripper {
155- return newAutoTransport (
156- newHTTPSTransport (false /*disable keep-alives*/ , true /*disable auto-compression*/ , maxIdle , maxIdlePerHost , tlsContext ),
157- newH2Transport (true /*disable auto-compression*/ , tlsContext ))
183+ https := newHTTPSTransport (
184+ false , /*disable keep-alives*/
185+ true , /*disable auto-compression*/
186+ maxIdle ,
187+ maxIdlePerHost ,
188+ tlsContext ,
189+ )
190+
191+ h2 := https .Clone ()
192+ h2 .Protocols .SetHTTP1 (false )
193+ h2 .Protocols .SetHTTP2 (true )
194+ h2 .Protocols .SetUnencryptedHTTP2 (true )
195+
196+ return newAutoTransport (https , h2 )
158197}
159198
160199// NewAutoTransport creates a RoundTripper that can use appropriate transport
161200// based on the request's HTTP version.
162201func NewAutoTransport (maxIdle , maxIdlePerHost int ) http.RoundTripper {
163- return newAutoTransport (
164- newHTTPTransport (false /*disable keep-alives*/ , false /*disable auto-compression*/ , maxIdle , maxIdlePerHost ),
165- newH2CTransport (false /*disable auto-compression*/ ))
202+ http := newHTTPTransport (
203+ false , /*disable keep-alives*/
204+ false , /*disable auto-compression*/
205+ maxIdle ,
206+ maxIdlePerHost ,
207+ )
208+
209+ h2 := http .Clone ()
210+ h2 .Protocols .SetHTTP1 (false )
211+ h2 .Protocols .SetUnencryptedHTTP2 (true )
212+
213+ return newAutoTransport (http , h2 )
166214}
167215
168216// NewProxyAutoTransport creates a RoundTripper suitable for use by a reverse
169217// proxy. The returned transport uses HTTP or H2C based on the request's HTTP
170218// version. The transport has DisableCompression set to true.
171219func NewProxyAutoTransport (maxIdle , maxIdlePerHost int ) http.RoundTripper {
172- return newAutoTransport (
173- newHTTPTransport (false /*disable keep-alives*/ , true /*disable auto-compression*/ , maxIdle , maxIdlePerHost ),
174- newH2CTransport (true /*disable auto-compression*/ ))
220+ http := newHTTPTransport (
221+ false , /*disable keep-alives*/
222+ true , /*disable auto-compression*/
223+ maxIdle ,
224+ maxIdlePerHost ,
225+ )
226+
227+ h2 := http .Clone ()
228+ h2 .Protocols .SetHTTP1 (false )
229+ h2 .Protocols .SetUnencryptedHTTP2 (true )
230+
231+ return newAutoTransport (http , h2 )
175232}
176233
177234// AutoTransport uses h2c for HTTP2 requests and falls back to `http.DefaultTransport` for all others
0 commit comments