@@ -119,7 +119,7 @@ where I: AsyncRead + AsyncWrite,
119
119
} else {
120
120
trace ! ( "poll when on keep-alive" ) ;
121
121
if !T :: should_read_first ( ) {
122
- self . try_empty_read ( ) ?;
122
+ self . require_empty_read ( ) ?;
123
123
if self . is_read_closed ( ) {
124
124
return Ok ( Async :: Ready ( None ) ) ;
125
125
}
@@ -281,17 +281,23 @@ where I: AsyncRead + AsyncWrite,
281
281
pub fn read_keep_alive ( & mut self ) -> Result < ( ) , :: Error > {
282
282
debug_assert ! ( !self . can_read_head( ) && !self . can_read_body( ) ) ;
283
283
284
- trace ! ( "Conn:: read_keep_alive" ) ;
284
+ trace ! ( "read_keep_alive; is_mid_message={}" , self . is_mid_message ( ) ) ;
285
285
286
- if T :: should_read_first ( ) || ! self . state . is_idle ( ) {
286
+ if self . is_mid_message ( ) {
287
287
self . maybe_park_read ( ) ;
288
288
} else {
289
- self . try_empty_read ( ) ?;
289
+ self . require_empty_read ( ) ?;
290
290
}
291
-
292
291
Ok ( ( ) )
293
292
}
294
293
294
+ fn is_mid_message ( & self ) -> bool {
295
+ match ( & self . state . reading , & self . state . writing ) {
296
+ ( & Reading :: Init , & Writing :: Init ) => false ,
297
+ _ => true ,
298
+ }
299
+ }
300
+
295
301
fn maybe_park_read ( & mut self ) {
296
302
if !self . io . is_read_blocked ( ) {
297
303
// the Io object is ready to read, which means it will never alert
@@ -312,40 +318,68 @@ where I: AsyncRead + AsyncWrite,
312
318
//
313
319
// This should only be called for Clients wanting to enter the idle
314
320
// state.
315
- fn try_empty_read ( & mut self ) -> io:: Result < ( ) > {
321
+ fn require_empty_read ( & mut self ) -> io:: Result < ( ) > {
316
322
assert ! ( !self . can_read_head( ) && !self . can_read_body( ) ) ;
317
323
318
324
if !self . io . read_buf ( ) . is_empty ( ) {
319
325
debug ! ( "received an unexpected {} bytes" , self . io. read_buf( ) . len( ) ) ;
320
326
Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "unexpected bytes after message ended" ) )
321
327
} else {
322
- match self . io . read_from_io ( ) {
323
- Ok ( Async :: Ready ( 0 ) ) => {
324
- trace ! ( "try_empty_read; found EOF on connection: {:?}" , self . state) ;
325
- let must_error = self . should_error_on_eof ( ) ;
326
- // order is important: must_error needs state BEFORE close_read
327
- self . state . close_read ( ) ;
328
- if must_error {
329
- Err ( io:: Error :: new ( io:: ErrorKind :: UnexpectedEof , "unexpected EOF waiting for response" ) )
330
- } else {
331
- Ok ( ( ) )
332
- }
328
+ match self . try_io_read ( ) ? {
329
+ Async :: Ready ( 0 ) => {
330
+ // case handled in try_io_read
331
+ Ok ( ( ) )
333
332
} ,
334
- Ok ( Async :: Ready ( n) ) => {
333
+ Async :: Ready ( n) => {
335
334
debug ! ( "received {} bytes on an idle connection" , n) ;
336
- Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "unexpected bytes after message ended" ) )
335
+ let desc = if self . state . is_idle ( ) {
336
+ "unexpected bytes after message ended"
337
+ } else {
338
+ "unexpected bytes before writing message"
339
+ } ;
340
+ Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , desc) )
337
341
} ,
338
- Ok ( Async :: NotReady ) => {
342
+ Async :: NotReady => {
339
343
Ok ( ( ) )
340
344
} ,
341
- Err ( e) => {
342
- self . state . close ( ) ;
343
- Err ( e)
344
- }
345
345
}
346
346
}
347
347
}
348
348
349
+ fn try_io_read ( & mut self ) -> Poll < usize , io:: Error > {
350
+ match self . io . read_from_io ( ) {
351
+ Ok ( Async :: Ready ( 0 ) ) => {
352
+ trace ! ( "try_io_read; found EOF on connection: {:?}" , self . state) ;
353
+ let must_error = self . should_error_on_eof ( ) ;
354
+ let ret = if must_error {
355
+ let desc = if self . is_mid_message ( ) {
356
+ "unexpected EOF waiting for response"
357
+ } else {
358
+ "unexpected EOF before writing message"
359
+ } ;
360
+ Err ( io:: Error :: new ( io:: ErrorKind :: UnexpectedEof , desc) )
361
+ } else {
362
+ Ok ( Async :: Ready ( 0 ) )
363
+ } ;
364
+
365
+ // order is important: must_error needs state BEFORE close_read
366
+ self . state . close_read ( ) ;
367
+ ret
368
+ } ,
369
+ Ok ( Async :: Ready ( n) ) => {
370
+ Ok ( Async :: Ready ( n) )
371
+ } ,
372
+ Ok ( Async :: NotReady ) => {
373
+ Ok ( Async :: NotReady )
374
+ } ,
375
+ Err ( e) => {
376
+ self . state . close ( ) ;
377
+ Err ( e)
378
+ }
379
+ }
380
+ }
381
+
382
+
349
383
fn maybe_notify ( & mut self ) {
350
384
// its possible that we returned NotReady from poll() without having
351
385
// exhausted the underlying Io. We would have done this when we
0 commit comments