@@ -117,61 +117,120 @@ 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
137+
128138 return transport
129139}
130140
131141type DialTLSContextFunc func (ctx context.Context , network , addr string ) (net.Conn , error )
132142
133- func newHTTPSTransport (disableKeepAlives , disableCompression bool , maxIdle , maxIdlePerHost int , tlsContext DialTLSContextFunc ) http.RoundTripper {
143+ func newHTTPSTransport (
144+ disableKeepAlives ,
145+ disableCompression bool ,
146+ maxIdle ,
147+ maxIdlePerHost int ,
148+ tlsContext DialTLSContextFunc ,
149+ ) * http.Transport {
150+ var protocols http.Protocols
151+ protocols .SetHTTP1 (true )
152+
134153 transport := http .DefaultTransport .(* http.Transport ).Clone ()
135154 transport .DisableKeepAlives = disableKeepAlives
136155 transport .MaxIdleConns = maxIdle
137156 transport .MaxIdleConnsPerHost = maxIdlePerHost
138157 transport .ForceAttemptHTTP2 = false
139158 transport .DisableCompression = disableCompression
140159 transport .DialTLSContext = tlsContext
160+ transport .Protocols = & protocols
141161
142162 return transport
143163}
144164
145165// NewProberTransport creates a RoundTripper that is useful for probing,
146166// since it will not cache connections.
147167func NewProberTransport () http.RoundTripper {
148- return newAutoTransport (
149- newHTTPTransport (true /*disable keep-alives*/ , false /*disable auto-compression*/ , 0 , 0 /*no caching*/ ),
150- NewH2CTransport ())
168+ http := newHTTPTransport (
169+ true , /*disable keep-alives*/
170+ false , /*disable auto-compression*/
171+ 0 , /*max idle*/
172+ 0 , /*no caching*/
173+ )
174+
175+ // h2 prior knowledge
176+ h2 := http .Clone ()
177+ h2 .Protocols .SetHTTP1 (false )
178+ h2 .Protocols .SetUnencryptedHTTP2 (true )
179+
180+ return newAutoTransport (http , h2 )
151181}
152182
153183// NewProxyAutoTLSTransport is same with NewProxyAutoTransport but it has DialTLSContextFunc to create HTTPS request.
154184func 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 ))
185+ https := newHTTPSTransport (
186+ false , /*disable keep-alives*/
187+ true , /*disable auto-compression*/
188+ maxIdle ,
189+ maxIdlePerHost ,
190+ tlsContext ,
191+ )
192+
193+ h2 := https .Clone ()
194+ h2 .Protocols .SetHTTP1 (false )
195+ h2 .Protocols .SetHTTP2 (true )
196+ h2 .Protocols .SetUnencryptedHTTP2 (true )
197+
198+ return newAutoTransport (https , h2 )
158199}
159200
160201// NewAutoTransport creates a RoundTripper that can use appropriate transport
161202// based on the request's HTTP version.
162203func 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*/ ))
204+ http := newHTTPTransport (
205+ false , /*disable keep-alives*/
206+ false , /*disable auto-compression*/
207+ maxIdle ,
208+ maxIdlePerHost ,
209+ )
210+
211+ h2 := http .Clone ()
212+ h2 .Protocols .SetHTTP1 (false )
213+ h2 .Protocols .SetUnencryptedHTTP2 (true )
214+
215+ return newAutoTransport (http , h2 )
166216}
167217
168218// NewProxyAutoTransport creates a RoundTripper suitable for use by a reverse
169219// proxy. The returned transport uses HTTP or H2C based on the request's HTTP
170220// version. The transport has DisableCompression set to true.
171221func 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*/ ))
222+ http := newHTTPTransport (
223+ false , /*disable keep-alives*/
224+ true , /*disable auto-compression*/
225+ maxIdle ,
226+ maxIdlePerHost ,
227+ )
228+
229+ h2 := http .Clone ()
230+ h2 .Protocols .SetHTTP1 (false )
231+ h2 .Protocols .SetUnencryptedHTTP2 (true )
232+
233+ return newAutoTransport (http , h2 )
175234}
176235
177236// AutoTransport uses h2c for HTTP2 requests and falls back to `http.DefaultTransport` for all others
0 commit comments