diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2024-05-15 15:22:45 +0200 |
---|---|---|
committer | Joe Birr-Pixton <jpixton@gmail.com> | 2024-05-16 12:36:10 +0000 |
commit | ef024342d156ddbfd2769809fcf408b2f5a1d65c (patch) | |
tree | cdd6332b861e8e83c1375008d659b62cdd5700ce | |
parent | ccb352c075ae06ba83377615d5b05b953d2481c5 (diff) |
Deduplicate Reader state checks
-rw-r--r-- | rustls/src/conn.rs | 49 |
1 files changed, 21 insertions, 28 deletions
diff --git a/rustls/src/conn.rs b/rustls/src/conn.rs index bca9e27c..0f4b0ef4 100644 --- a/rustls/src/conn.rs +++ b/rustls/src/conn.rs @@ -161,6 +161,24 @@ mod connection { pub(super) has_seen_eof: bool, } + impl<'a> Reader<'a> { + /// Check the connection's state if no bytes are available for reading. + fn check_no_bytes_state(&self) -> io::Result<()> { + match (self.peer_cleanly_closed, self.has_seen_eof) { + // cleanly closed; don't care about TCP EOF: express this as Ok(0) + (true, _) => Ok(()), + // unclean closure + (false, true) => Err(io::Error::new( + io::ErrorKind::UnexpectedEof, + UNEXPECTED_EOF_MESSAGE, + )), + // connection still going, but need more data: signal `WouldBlock` so that + // the caller knows this + (false, false) => Err(io::ErrorKind::WouldBlock.into()), + } + } + } + impl<'a> io::Read for Reader<'a> { /// Obtain plaintext data received from the peer over this TLS connection. /// @@ -188,21 +206,8 @@ mod connection { return Ok(len); } - // No bytes available: - match (self.peer_cleanly_closed, self.has_seen_eof) { - // cleanly closed; don't care about TCP EOF: express this as Ok(0) - (true, _) => Ok(len), - // unclean closure - (false, true) => { - return Err(io::Error::new( - io::ErrorKind::UnexpectedEof, - UNEXPECTED_EOF_MESSAGE, - )) - } - // connection still going, but needs more data: signal `WouldBlock` so that - // the caller knows this - (false, false) => return Err(io::ErrorKind::WouldBlock.into()), - } + self.check_no_bytes_state() + .map(|()| len) } /// Obtain plaintext data received from the peer over this TLS connection. @@ -236,19 +241,7 @@ mod connection { return Ok(()); } - // No bytes available: - match (self.peer_cleanly_closed, self.has_seen_eof) { - // cleanly closed; don't care about TCP EOF: express this as Ok(0) - (true, _) => Ok(()), - // unclean closure - (false, true) => Err(io::Error::new( - io::ErrorKind::UnexpectedEof, - UNEXPECTED_EOF_MESSAGE, - )), - // connection still going, but need more data: signal `WouldBlock` so that - // the caller knows this - (false, false) => Err(io::ErrorKind::WouldBlock.into()), - } + self.check_no_bytes_state() } } |