Rustls buffered handshake eof failed (#98)

* rustls/tests: use BufWriter in handshake

* tokio-rustls: move test to stream_buffered_handshake

* Fix tokio-rustls bufwriter handshake fail #96

* Use need_flush

* More flush

* tokio-rustls: release 0.23.3

* Fix fmt

Co-authored-by: tharvik <tharvik@users.noreply.github.com>
This commit is contained in:
quininer 2022-03-19 13:09:28 +08:00 committed by GitHub
parent 47b2ef50c1
commit bcf4f8e3f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 5 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.23.2" version = "0.23.3"
authors = ["quininer kel <quininer@live.com>"] authors = ["quininer kel <quininer@live.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
repository = "https://github.com/tokio-rs/tls" repository = "https://github.com/tokio-rs/tls"

View File

@ -62,9 +62,7 @@ where
try_poll!(tls_stream.handshake(cx)); try_poll!(tls_stream.handshake(cx));
} }
while tls_stream.session.wants_write() { try_poll!(Pin::new(&mut tls_stream).poll_flush(cx));
try_poll!(tls_stream.write_io(cx));
}
} }
Poll::Ready(Ok(stream)) Poll::Ready(Ok(stream))

View File

@ -166,10 +166,14 @@ where
loop { loop {
let mut write_would_block = false; let mut write_would_block = false;
let mut read_would_block = false; let mut read_would_block = false;
let mut need_flush = false;
while self.session.wants_write() { while self.session.wants_write() {
match self.write_io(cx) { match self.write_io(cx) {
Poll::Ready(Ok(n)) => wrlen += n, Poll::Ready(Ok(n)) => {
wrlen += n;
need_flush = true;
}
Poll::Pending => { Poll::Pending => {
write_would_block = true; write_would_block = true;
break; break;
@ -178,6 +182,14 @@ where
} }
} }
if need_flush {
match Pin::new(&mut self.io).poll_flush(cx) {
Poll::Ready(Ok(())) => (),
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Pending => write_would_block = true,
}
}
while !self.eof && self.session.wants_read() { while !self.eof && self.session.wants_read() {
match self.read_io(cx) { match self.read_io(cx) {
Poll::Ready(Ok(0)) => self.eof = true, Poll::Ready(Ok(0)) => self.eof = true,

View File

@ -202,6 +202,30 @@ async fn stream_handshake() -> io::Result<()> {
Ok(()) as io::Result<()> Ok(()) as io::Result<()>
} }
#[tokio::test]
async fn stream_buffered_handshake() -> io::Result<()> {
use tokio::io::BufWriter;
let (server, mut client) = make_pair();
let mut server = Connection::from(server);
{
let mut good = BufWriter::new(Good(&mut server));
let mut stream = Stream::new(&mut good, &mut client);
let (r, w) = poll_fn(|cx| stream.handshake(cx)).await?;
assert!(r > 0);
assert!(w > 0);
poll_fn(|cx| stream.handshake(cx)).await?; // finish server handshake
}
assert!(!server.is_handshaking());
assert!(!client.is_handshaking());
Ok(()) as io::Result<()>
}
#[tokio::test] #[tokio::test]
async fn stream_handshake_eof() -> io::Result<()> { async fn stream_handshake_eof() -> io::Result<()> {
let (_, mut client) = make_pair(); let (_, mut client) = make_pair();