@@ -22,14 +22,17 @@ use std::{
22
22
23
23
use event_listener:: { Event , EventListener } ;
24
24
use futures_util:: { stream:: SelectAll , Stream , StreamExt } ;
25
- use http_body:: Body ;
26
- use hyper:: { body:: HttpBody , server:: conn:: Connection , Request , Response } ;
25
+ use hyper:: { Request , Response } ;
26
+ use hyper_util:: {
27
+ rt:: { TokioExecutor , TokioIo } ,
28
+ server:: conn:: auto:: Connection ,
29
+ service:: TowerToHyperService ,
30
+ } ;
27
31
use pin_project_lite:: pin_project;
28
32
use thiserror:: Error ;
29
- use tokio:: io:: { AsyncRead , AsyncWrite } ;
30
33
use tokio_rustls:: rustls:: ServerConfig ;
34
+ use tower:: Service ;
31
35
use tower_http:: add_extension:: AddExtension ;
32
- use tower_service:: Service ;
33
36
use tracing:: Instrument ;
34
37
35
38
use crate :: {
@@ -91,10 +94,10 @@ impl<S> Server<S> {
91
94
/// Run a single server
92
95
pub async fn run < B , SD > ( self , shutdown : SD )
93
96
where
94
- S : Service < Request < hyper:: Body > , Response = Response < B > > + Clone + Send + ' static ,
97
+ S : Service < Request < hyper:: body :: Incoming > , Response = Response < B > > + Clone + Send + ' static ,
95
98
S :: Future : Send + ' static ,
96
99
S :: Error : std:: error:: Error + Send + Sync + ' static ,
97
- B : Body + Send + ' static ,
100
+ B : http_body :: Body + Send + ' static ,
98
101
B :: Data : Send ,
99
102
B :: Error : std:: error:: Error + Send + Sync + ' static ,
100
103
SD : Stream + Unpin ,
@@ -173,15 +176,20 @@ async fn accept<S, B>(
173
176
stream : UnixOrTcpConnection ,
174
177
service : S ,
175
178
) -> Result <
176
- Connection < MaybeTlsStream < Rewind < UnixOrTcpConnection > > , AddExtension < S , ConnectionInfo > > ,
179
+ Connection <
180
+ ' static ,
181
+ TokioIo < MaybeTlsStream < Rewind < UnixOrTcpConnection > > > ,
182
+ TowerToHyperService < AddExtension < S , ConnectionInfo > > ,
183
+ TokioExecutor ,
184
+ > ,
177
185
AcceptError ,
178
186
>
179
187
where
180
- S : Service < Request < hyper:: Body > , Response = Response < B > > ,
188
+ S : Service < Request < hyper:: body :: Incoming > , Response = Response < B > > + Send + Clone + ' static ,
181
189
S :: Error : std:: error:: Error + Send + Sync + ' static ,
182
190
S :: Future : Send + ' static ,
183
- B : HttpBody + Send + ' static ,
184
- B :: Data : Send + ' static ,
191
+ B : http_body :: Body + Send + ' static ,
192
+ B :: Data : Send ,
185
193
B :: Error : std:: error:: Error + Send + Sync + ' static ,
186
194
{
187
195
let span = tracing:: Span :: current ( ) ;
@@ -219,18 +227,17 @@ where
219
227
net_peer_addr : peer_addr. into_net ( ) ,
220
228
} ;
221
229
222
- let service = AddExtension :: new ( service, info) ;
223
-
224
- let conn = if is_h2 {
225
- hyper:: server:: conn:: Http :: new ( )
226
- . http2_only ( true )
227
- . serve_connection ( stream, service)
228
- } else {
229
- hyper:: server:: conn:: Http :: new ( )
230
- . http1_only ( true )
231
- . http1_keep_alive ( true )
232
- . serve_connection ( stream, service)
233
- } ;
230
+ let mut builder = hyper_util:: server:: conn:: auto:: Builder :: new ( TokioExecutor :: new ( ) ) ;
231
+ if is_h2 {
232
+ builder = builder. http2_only ( ) ;
233
+ }
234
+ builder. http1 ( ) . keep_alive ( true ) ;
235
+
236
+ let service = TowerToHyperService :: new ( AddExtension :: new ( service, info) ) ;
237
+
238
+ let conn = builder
239
+ . serve_connection ( TokioIo :: new ( stream) , service)
240
+ . into_owned ( ) ;
234
241
235
242
Ok ( conn)
236
243
} )
@@ -270,18 +277,19 @@ impl<C> AbortableConnection<C> {
270
277
}
271
278
}
272
279
273
- impl < T , S , B > Future for AbortableConnection < Connection < T , S > >
280
+ impl < T , S , B > Future
281
+ for AbortableConnection < Connection < ' static , T , TowerToHyperService < S > , TokioExecutor > >
274
282
where
275
- Connection < T , S > : Future ,
276
- S : Service < Request < hyper:: Body > , Response = Response < B > > + Send + ' static ,
283
+ Connection < ' static , T , TowerToHyperService < S > , TokioExecutor > : Future ,
284
+ S : Service < Request < hyper:: body :: Incoming > , Response = Response < B > > + Send + Clone + ' static ,
277
285
S :: Future : Send + ' static ,
278
286
S :: Error : std:: error:: Error + Send + Sync ,
279
- B : HttpBody + Send + ' static ,
287
+ T : hyper:: rt:: Read + hyper:: rt:: Write + Unpin ,
288
+ B : http_body:: Body + Send + ' static ,
280
289
B :: Data : Send ,
281
- B :: Error : std:: error:: Error + Send + Sync ,
282
- T : AsyncRead + AsyncWrite + Unpin ,
290
+ B :: Error : std:: error:: Error + Send + Sync + ' static ,
283
291
{
284
- type Output = <Connection < T , S > as Future >:: Output ;
292
+ type Output = <Connection < ' static , T , TowerToHyperService < S > , TokioExecutor > as Future >:: Output ;
285
293
286
294
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
287
295
let mut this = self . project ( ) ;
@@ -308,10 +316,10 @@ where
308
316
#[ allow( clippy:: too_many_lines) ]
309
317
pub async fn run_servers < S , B , SD > ( listeners : impl IntoIterator < Item = Server < S > > , mut shutdown : SD )
310
318
where
311
- S : Service < Request < hyper:: Body > , Response = Response < B > > + Clone + Send + ' static ,
319
+ S : Service < Request < hyper:: body :: Incoming > , Response = Response < B > > + Clone + Send + ' static ,
312
320
S :: Future : Send + ' static ,
313
321
S :: Error : std:: error:: Error + Send + Sync + ' static ,
314
- B : Body + Send + ' static ,
322
+ B : http_body :: Body + Send + ' static ,
315
323
B :: Data : Send ,
316
324
B :: Error : std:: error:: Error + Send + Sync + ' static ,
317
325
SD : Stream + Unpin ,
0 commit comments