@@ -10,13 +10,14 @@ use governor::{
1010 clock:: MonotonicClock , middleware:: NoOpMiddleware , state:: InMemoryState , Quota , RateLimiter ,
1111} ;
1212use http:: { header:: HeaderValue , Uri } ;
13- use hyper:: {
14- client:: { HttpConnector , ResponseFuture } ,
15- header:: USER_AGENT ,
16- Body , Client , HeaderMap , Request , Response , StatusCode ,
17- } ;
18- use hyper_proxy:: { Intercept , Proxy , ProxyConnector } ;
13+ use http_body_util:: { BodyExt , Full } ;
14+ use hyper:: { body:: Incoming , header:: USER_AGENT , HeaderMap , Request , Response , StatusCode } ;
15+ use hyper_proxy2:: { Intercept , Proxy , ProxyConnector } ;
1916use hyper_rustls:: { HttpsConnector , HttpsConnectorBuilder } ;
17+ use hyper_util:: {
18+ client:: legacy:: { connect:: HttpConnector , Client , ResponseFuture } ,
19+ rt:: TokioExecutor ,
20+ } ;
2021use nonzero_ext:: nonzero;
2122use once_cell:: sync:: OnceCell ;
2223use parking_lot:: Mutex ;
@@ -89,7 +90,7 @@ impl From<HttpClientError> for Error {
8990 }
9091}
9192
92- type HyperClient = Client < ProxyConnector < HttpsConnector < HttpConnector > > , Body > ;
93+ type HyperClient = Client < ProxyConnector < HttpsConnector < HttpConnector > > , Full < bytes :: Bytes > > ;
9394
9495pub struct HttpClient {
9596 user_agent : HeaderValue ,
@@ -146,7 +147,7 @@ impl HttpClient {
146147 fn try_create_hyper_client ( proxy_url : Option < & Url > ) -> Result < HyperClient , Error > {
147148 // configuring TLS is expensive and should be done once per process
148149 let https_connector = HttpsConnectorBuilder :: new ( )
149- . with_native_roots ( )
150+ . with_native_roots ( ) ?
150151 . https_or_http ( )
151152 . enable_http1 ( )
152153 . enable_http2 ( )
@@ -160,7 +161,7 @@ impl HttpClient {
160161 } ;
161162 let proxy_connector = ProxyConnector :: from_proxy ( https_connector, proxy) ?;
162163
163- let client = Client :: builder ( )
164+ let client = Client :: builder ( TokioExecutor :: new ( ) )
164165 . http2_adaptive_window ( true )
165166 . build ( proxy_connector) ;
166167 Ok ( client)
@@ -171,23 +172,20 @@ impl HttpClient {
171172 . get_or_try_init ( || Self :: try_create_hyper_client ( self . proxy_url . as_ref ( ) ) )
172173 }
173174
174- pub async fn request ( & self , req : Request < Body > ) -> Result < Response < Body > , Error > {
175+ pub async fn request ( & self , req : Request < Bytes > ) -> Result < Response < Incoming > , Error > {
175176 debug ! ( "Requesting {}" , req. uri( ) . to_string( ) ) ;
176177
177178 // `Request` does not implement `Clone` because its `Body` may be a single-shot stream.
178179 // As correct as that may be technically, we now need all this boilerplate to clone it
179180 // ourselves, as any `Request` is moved in the loop.
180- let ( parts, body) = req. into_parts ( ) ;
181- let body_as_bytes = hyper:: body:: to_bytes ( body)
182- . await
183- . unwrap_or_else ( |_| Bytes :: new ( ) ) ;
181+ let ( parts, body_as_bytes) = req. into_parts ( ) ;
184182
185183 loop {
186184 let mut req = Request :: builder ( )
187185 . method ( parts. method . clone ( ) )
188186 . uri ( parts. uri . clone ( ) )
189187 . version ( parts. version )
190- . body ( Body :: from ( body_as_bytes. clone ( ) ) ) ?;
188+ . body ( body_as_bytes. clone ( ) ) ?;
191189 * req. headers_mut ( ) = parts. headers . clone ( ) ;
192190
193191 let request = self . request_fut ( req) ?;
@@ -212,20 +210,21 @@ impl HttpClient {
212210 }
213211 }
214212
215- return Ok ( response?) ;
213+ let response = response?;
214+ return Ok ( response) ;
216215 }
217216 }
218217
219- pub async fn request_body ( & self , req : Request < Body > ) -> Result < Bytes , Error > {
218+ pub async fn request_body ( & self , req : Request < Bytes > ) -> Result < Bytes , Error > {
220219 let response = self . request ( req) . await ?;
221- Ok ( hyper :: body :: to_bytes ( response. into_body ( ) ) . await ?)
220+ Ok ( response. into_body ( ) . collect ( ) . await ?. to_bytes ( ) )
222221 }
223222
224- pub fn request_stream ( & self , req : Request < Body > ) -> Result < IntoStream < ResponseFuture > , Error > {
223+ pub fn request_stream ( & self , req : Request < Bytes > ) -> Result < IntoStream < ResponseFuture > , Error > {
225224 Ok ( self . request_fut ( req) ?. into_stream ( ) )
226225 }
227226
228- pub fn request_fut ( & self , mut req : Request < Body > ) -> Result < ResponseFuture , Error > {
227+ pub fn request_fut ( & self , mut req : Request < Bytes > ) -> Result < ResponseFuture , Error > {
229228 let headers_mut = req. headers_mut ( ) ;
230229 headers_mut. insert ( USER_AGENT , self . user_agent . clone ( ) ) ;
231230
@@ -251,7 +250,7 @@ impl HttpClient {
251250 ) )
252251 } ) ?;
253252
254- Ok ( self . hyper_client ( ) ?. request ( req) )
253+ Ok ( self . hyper_client ( ) ?. request ( req. map ( Full :: new ) ) )
255254 }
256255
257256 pub fn get_retry_after ( headers : & HeaderMap < HeaderValue > ) -> Option < Duration > {
0 commit comments