From d8ab52db551e4d4080b73cf3e338e9f07d265fdd Mon Sep 17 00:00:00 2001 From: quininer Date: Tue, 26 Mar 2019 10:44:38 +0800 Subject: [PATCH] fix early-data read --- Cargo.toml | 2 +- src/client.rs | 64 ++++++++++++++++++++++++++++----------------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff95d5f..1082c02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tokio-rustls" -version = "0.10.0-alpha" +version = "0.10.0-alpha.1" authors = ["quininer kel "] license = "MIT/Apache-2.0" repository = "https://github.com/quininer/tokio-rustls" diff --git a/src/client.rs b/src/client.rs index 91a65aa..27ab944 100644 --- a/src/client.rs +++ b/src/client.rs @@ -79,45 +79,51 @@ impl io::Read for TlsStream where IO: AsyncRead + AsyncWrite { fn read(&mut self, buf: &mut [u8]) -> io::Result { - let mut stream = Stream::new(&mut self.io, &mut self.session); - match self.state { #[cfg(feature = "early-data")] TlsState::EarlyData => { use std::io::Write; - let (pos, data) = &mut self.early_data; + { + let mut stream = Stream::new(&mut self.io, &mut self.session); + let (pos, data) = &mut self.early_data; - // complete handshake - if stream.session.is_handshaking() { - stream.complete_io()?; - } - - // write early data (fallback) - if !stream.session.is_early_data_accepted() { - while *pos < data.len() { - let len = stream.write(&data[*pos..])?; - *pos += len; + // complete handshake + if stream.session.is_handshaking() { + stream.complete_io()?; } + + // write early data (fallback) + if !stream.session.is_early_data_accepted() { + while *pos < data.len() { + let len = stream.write(&data[*pos..])?; + *pos += len; + } + } + + // end + self.state = TlsState::Stream; + data.clear(); } - // end - self.state = TlsState::Stream; - data.clear(); - stream.read(buf) + self.read(buf) }, - TlsState::Stream => match stream.read(buf) { - Ok(0) => { - self.state = TlsState::Eof; - Ok(0) - }, - Ok(n) => Ok(n), - Err(ref e) if e.kind() == io::ErrorKind::ConnectionAborted => { - self.state = TlsState::Shutdown; - stream.session.send_close_notify(); - Ok(0) - }, - Err(e) => Err(e) + TlsState::Stream => { + let mut stream = Stream::new(&mut self.io, &mut self.session); + + match stream.read(buf) { + Ok(0) => { + self.state = TlsState::Eof; + Ok(0) + }, + Ok(n) => Ok(n), + Err(ref e) if e.kind() == io::ErrorKind::ConnectionAborted => { + self.state = TlsState::Shutdown; + stream.session.send_close_notify(); + Ok(0) + }, + Err(e) => Err(e) + } }, TlsState::Eof | TlsState::Shutdown => Ok(0), }