1
1
use std:: error:: Error as StdError ;
2
+ use std:: marker:: Unpin ;
2
3
4
+ use futures_core:: Stream ;
3
5
use h2:: Reason ;
4
6
use h2:: server:: { Builder , Connection , Handshake , SendResponse } ;
5
7
use tokio_io:: { AsyncRead , AsyncWrite } ;
@@ -49,27 +51,23 @@ where
49
51
50
52
impl < T , S , B , E > Server < T , S , B , E >
51
53
where
52
- T : AsyncRead + AsyncWrite ,
54
+ T : AsyncRead + AsyncWrite + Unpin ,
53
55
S : Service < ReqBody =Body , ResBody =B > ,
54
56
S :: Error : Into < Box < dyn StdError + Send + Sync > > ,
55
57
B : Payload ,
58
+ B :: Data : Unpin ,
56
59
E : H2Exec < S :: Future , B > ,
57
60
{
58
61
pub ( crate ) fn new ( io : T , service : S , builder : & Builder , exec : E ) -> Server < T , S , B , E > {
59
- unimplemented ! ( "proto::h2::Server::new" )
60
- /*
61
62
let handshake = builder. handshake ( io) ;
62
63
Server {
63
64
exec,
64
65
state : State :: Handshaking ( handshake) ,
65
66
service,
66
67
}
67
- */
68
68
}
69
69
70
70
pub fn graceful_shutdown ( & mut self ) {
71
- unimplemented ! ( "proto::h2::Server::graceful_shutdown" )
72
- /*
73
71
trace ! ( "graceful_shutdown" ) ;
74
72
match self . state {
75
73
State :: Handshaking ( ..) => {
@@ -86,54 +84,53 @@ where
86
84
}
87
85
}
88
86
self . state = State :: Closed ;
89
- */
90
87
}
91
88
}
92
89
93
90
impl < T , S , B , E > Future for Server < T , S , B , E >
94
91
where
95
- T : AsyncRead + AsyncWrite ,
92
+ T : AsyncRead + AsyncWrite + Unpin ,
96
93
S : Service < ReqBody =Body , ResBody =B > ,
97
94
S :: Error : Into < Box < dyn StdError + Send + Sync > > ,
98
95
B : Payload ,
96
+ B :: Data : Unpin ,
99
97
E : H2Exec < S :: Future , B > ,
100
98
{
101
99
type Output = crate :: Result < Dispatched > ;
102
100
103
101
fn poll ( mut self : Pin < & mut Self > , cx : & mut task:: Context < ' _ > ) -> Poll < Self :: Output > {
104
- unimplemented ! ( "h2 server future" )
105
- /*
102
+ let me = & mut * self ;
106
103
loop {
107
- let next = match self .state {
104
+ let next = match me . state {
108
105
State :: Handshaking ( ref mut h) => {
109
- let conn = try_ready!(h .poll().map_err(crate::Error::new_h2));
106
+ let conn = ready ! ( Pin :: new ( h ) . poll( cx ) . map_err( crate :: Error :: new_h2) ) ? ;
110
107
State :: Serving ( Serving {
111
108
conn,
112
109
closing : None ,
113
110
} )
114
111
} ,
115
112
State :: Serving ( ref mut srv) => {
116
- try_ready !(srv.poll_server(&mut self .service, &self .exec));
117
- return Ok(Async ::Ready(Dispatched::Shutdown));
113
+ ready ! ( srv. poll_server( cx , & mut me . service, & mut me . exec) ) ? ;
114
+ return Poll :: Ready ( Ok ( Dispatched :: Shutdown ) ) ;
118
115
}
119
116
State :: Closed => {
120
117
// graceful_shutdown was called before handshaking finished,
121
118
// nothing to do here...
122
- return Ok(Async ::Ready(Dispatched::Shutdown));
119
+ return Poll :: Ready ( Ok ( Dispatched :: Shutdown ) ) ;
123
120
}
124
121
} ;
125
- self .state = next;
122
+ me . state = next;
126
123
}
127
- */
128
124
}
129
125
}
130
126
131
127
impl < T , B > Serving < T , B >
132
128
where
133
- T : AsyncRead + AsyncWrite ,
129
+ T : AsyncRead + AsyncWrite + Unpin ,
134
130
B : Payload ,
131
+ B :: Data : Unpin ,
135
132
{
136
- fn poll_server < S , E > ( & mut self , service : & mut S , exec : & E ) -> Poll < crate :: Result < ( ) > >
133
+ fn poll_server < S , E > ( & mut self , cx : & mut task :: Context < ' _ > , service : & mut S , exec : & mut E ) -> Poll < crate :: Result < ( ) > >
137
134
where
138
135
S : Service <
139
136
ReqBody =Body ,
@@ -142,19 +139,18 @@ where
142
139
S :: Error : Into < Box < dyn StdError + Send + Sync > > ,
143
140
E : H2Exec < S :: Future , B > ,
144
141
{
145
- /*
146
142
if self . closing . is_none ( ) {
147
143
loop {
148
144
// At first, polls the readiness of supplied service.
149
- match service.poll_ready() {
150
- Ok(Async ::Ready(())) => (),
151
- Ok(Async::NotReady) => {
145
+ match service. poll_ready ( cx ) {
146
+ Poll :: Ready ( Ok ( ( ) ) ) => ( ) ,
147
+ Poll :: Pending => {
152
148
// use `poll_close` instead of `poll`, in order to avoid accepting a request.
153
- try_ready !(self.conn.poll_close().map_err(crate::Error::new_h2));
149
+ ready ! ( self . conn. poll_close( cx ) . map_err( crate :: Error :: new_h2) ) ? ;
154
150
trace ! ( "incoming connection complete" ) ;
155
- return Ok(Async ::Ready(()));
151
+ return Poll :: Ready ( Ok ( ( ) ) ) ;
156
152
}
157
- Err(err) => {
153
+ Poll :: Ready ( Err ( err) ) => {
158
154
let err = crate :: Error :: new_user_service ( err) ;
159
155
debug ! ( "service closed: {}" , err) ;
160
156
@@ -173,29 +169,33 @@ where
173
169
}
174
170
175
171
// When the service is ready, accepts an incoming request.
176
- if let Some((req, respond)) = try_ready!(self.conn.poll().map_err(crate::Error::new_h2)) {
177
- trace!("incoming request");
178
- let content_length = content_length_parse_all(req.headers());
179
- let req = req.map(|stream| {
180
- crate::Body::h2(stream, content_length)
181
- });
182
- let fut = H2Stream::new(service.call(req), respond);
183
- exec.execute_h2stream(fut)?;
184
- } else {
185
- // no more incoming streams...
186
- trace!("incoming connection complete");
187
- return Ok(Async::Ready(()))
172
+ match ready ! ( Pin :: new( & mut self . conn) . poll_next( cx) ) {
173
+ Some ( Ok ( ( req, respond) ) ) => {
174
+ trace ! ( "incoming request" ) ;
175
+ let content_length = content_length_parse_all ( req. headers ( ) ) ;
176
+ let req = req. map ( |stream| {
177
+ crate :: Body :: h2 ( stream, content_length)
178
+ } ) ;
179
+ let fut = H2Stream :: new ( service. call ( req) , respond) ;
180
+ exec. execute_h2stream ( fut) ?;
181
+ } ,
182
+ Some ( Err ( e) ) => {
183
+ return Poll :: Ready ( Err ( crate :: Error :: new_h2 ( e) ) ) ;
184
+ } ,
185
+ None => {
186
+ // no more incoming streams...
187
+ trace ! ( "incoming connection complete" ) ;
188
+ return Poll :: Ready ( Ok ( ( ) ) ) ;
189
+ } ,
188
190
}
189
191
}
190
192
}
191
193
192
194
debug_assert ! ( self . closing. is_some( ) , "poll_server broke loop without closing" ) ;
193
195
194
- try_ready !(self.conn.poll_close().map_err(crate::Error::new_h2));
196
+ ready ! ( self . conn. poll_close( cx ) . map_err( crate :: Error :: new_h2) ) ? ;
195
197
196
- Err(self.closing.take().expect("polled after error"))
197
- */
198
- unimplemented ! ( "h2 server poll_server" )
198
+ Poll :: Ready ( Err ( self . closing . take ( ) . expect ( "polled after error" ) ) )
199
199
}
200
200
}
201
201
@@ -230,38 +230,37 @@ where
230
230
}
231
231
}
232
232
233
- impl < F , B > Future for H2Stream < F , B >
233
+ impl < F , B , E > H2Stream < F , B >
234
234
where
235
- //F: Future<Item=Response<B>>,
236
- //F::Error: Into<Box<dyn StdError + Send + Sync>>,
237
- B : Payload ,
235
+ F : Future < Output = Result < Response < B > , E > > ,
236
+ B : Payload + Unpin ,
237
+ B :: Data : Unpin ,
238
+ E : Into < Box < dyn StdError + Send + Sync > > ,
238
239
{
239
- type Output = ( ) ;
240
-
241
- fn poll ( mut self : Pin < & mut Self > , cx : & mut task:: Context < ' _ > ) -> Poll < Self :: Output > {
242
- unimplemented ! ( "impl Future for H2Stream" ) ;
243
- /*
240
+ fn poll2 ( mut self : Pin < & mut Self > , cx : & mut task:: Context < ' _ > ) -> Poll < crate :: Result < ( ) > > {
241
+ // Safety: State::{Service, Body} futures are never moved
242
+ let me = unsafe { self . get_unchecked_mut ( ) } ;
244
243
loop {
245
- let next = match self .state {
244
+ let next = match me . state {
246
245
H2StreamState :: Service ( ref mut h) => {
247
- let res = match h .poll() {
248
- Ok(Async ::Ready(r)) => r,
249
- Ok(Async::NotReady) => {
250
- // Body is not yet ready, so we want to check if the client has sent a
246
+ let res = match unsafe { Pin :: new_unchecked ( h ) } . poll ( cx ) {
247
+ Poll :: Ready ( Ok ( r) ) => r,
248
+ Poll :: Pending => {
249
+ // Response is not yet ready, so we want to check if the client has sent a
251
250
// RST_STREAM frame which would cancel the current request.
252
- if let Async ::Ready(reason) =
253
- self .reply.poll_reset().map_err(|e| crate::Error::new_h2(e))?
251
+ if let Poll :: Ready ( reason) =
252
+ me . reply . poll_reset ( cx ) . map_err ( |e| crate :: Error :: new_h2 ( e) ) ?
254
253
{
255
254
debug ! ( "stream received RST_STREAM: {:?}" , reason) ;
256
- return Err(crate::Error::new_h2(reason.into()));
255
+ return Poll :: Ready ( Err ( crate :: Error :: new_h2 ( reason. into ( ) ) ) ) ;
257
256
}
258
- return Ok(Async::NotReady) ;
257
+ return Poll :: Pending ;
259
258
}
260
- Err(e) => {
259
+ Poll :: Ready ( Err ( e) ) => {
261
260
let err = crate :: Error :: new_user_service ( e) ;
262
261
warn ! ( "http2 service errored: {}" , err) ;
263
- self .reply.send_reset(err.h2_reason());
264
- return Err(err);
262
+ me . reply . send_reset ( err. h2_reason ( ) ) ;
263
+ return Poll :: Ready ( Err ( err) ) ;
265
264
} ,
266
265
} ;
267
266
@@ -278,12 +277,12 @@ where
278
277
279
278
macro_rules! reply {
280
279
( $eos: expr) => ( {
281
- match self .reply.send_response(res, $eos) {
280
+ match me . reply. send_response( res, $eos) {
282
281
Ok ( tx) => tx,
283
282
Err ( e) => {
284
283
debug!( "send response error: {}" , e) ;
285
- self .reply.send_reset(Reason::INTERNAL_ERROR);
286
- return Err(crate::Error::new_h2(e));
284
+ me . reply. send_reset( Reason :: INTERNAL_ERROR ) ;
285
+ return Poll :: Ready ( Err ( crate :: Error :: new_h2( e) ) ) ;
287
286
}
288
287
}
289
288
} )
@@ -300,33 +299,35 @@ where
300
299
body_tx
301
300
. send_data ( buf, true )
302
301
. map_err ( crate :: Error :: new_body_write) ?;
303
- return Ok(Async ::Ready(()));
302
+ return Poll :: Ready ( Ok ( ( ) ) ) ;
304
303
}
305
304
306
305
if !body. is_end_stream ( ) {
307
306
let body_tx = reply ! ( false ) ;
308
307
H2StreamState :: Body ( PipeToSendStream :: new ( body, body_tx) )
309
308
} else {
310
309
reply ! ( true ) ;
311
- return Ok(Async ::Ready(()));
310
+ return Poll :: Ready ( Ok ( ( ) ) ) ;
312
311
}
313
312
} ,
314
313
H2StreamState :: Body ( ref mut pipe) => {
315
- return pipe.poll();
314
+ return Pin :: new ( pipe) . poll ( cx ) ;
316
315
}
317
316
} ;
318
- self .state = next;
317
+ me . state = next;
319
318
}
320
- */
321
319
}
322
320
}
323
321
/*
324
- impl<F, B> Future for H2Stream<F, B>
322
+ impl<F, B, E > Future for H2Stream<F, B>
325
323
where
326
- F: Future<Item=Response<B>>,
327
- F::Error: Into<Box<dyn StdError + Send + Sync>>,
324
+ F: Future<Output = Result<Response<B>, E>>,
328
325
B: Payload,
326
+ E: Into<Box<dyn StdError + Send + Sync>>,
329
327
{
328
+ type Output = ();
329
+
330
+ fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
330
331
type Item = ();
331
332
type Error = ();
332
333
0 commit comments