Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compio-io/src/compat/sync_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ impl<S> SyncStream<S> {
Ok(to_read)
}

pub(crate) fn has_pending_write(&self) -> bool {
/// Returns `true` if there is pending data in the write buffer that needs
/// to be flushed.
pub fn has_pending_write(&self) -> bool {
!self.write_buf.is_empty()
}
}
Expand Down
12 changes: 9 additions & 3 deletions compio-tls/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,20 @@ async fn handshake_native_tls<S: AsyncRead + AsyncWrite>(
loop {
match res {
Ok(mut s) => {
s.get_mut().flush_write_buf().await?;
let inner = s.get_mut();
if inner.has_pending_write() {
inner.flush_write_buf().await?;
}
return Ok(TlsStream::from(s));
}
Err(e) => match e {
HandshakeError::Failure(e) => return Err(io::Error::other(e)),
HandshakeError::WouldBlock(mut mid_stream) => {
if mid_stream.get_mut().flush_write_buf().await? == 0 {
mid_stream.get_mut().fill_read_buf().await?;
let s = mid_stream.get_mut();
if s.has_pending_write() {
s.flush_write_buf().await?;
} else {
s.fill_read_buf().await?;
}
res = mid_stream.handshake();
}
Expand Down
73 changes: 49 additions & 24 deletions compio-tls/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ impl<S> From<futures_rustls::server::TlsStream<AsyncStream<S>>> for TlsStream<S>
}
}

#[cfg(feature = "native-tls")]
#[inline]
async fn drive<S, F, T>(s: &mut native_tls::TlsStream<SyncStream<S>>, mut f: F) -> io::Result<T>
where
S: AsyncRead + AsyncWrite,
F: FnMut(&mut native_tls::TlsStream<SyncStream<S>>) -> io::Result<T>,
{
loop {
match f(s) {
Ok(res) => {
let s = s.get_mut();
if s.has_pending_write() {
s.flush_write_buf().await?;
}
break Ok(res);
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
let s = s.get_mut();
if s.has_pending_write() {
s.flush_write_buf().await?;
} else {
s.fill_read_buf().await?;
}
}
Err(e) => break Err(e),
}
}
}

impl<S: AsyncRead + AsyncWrite + 'static> AsyncRead for TlsStream<S> {
async fn read<B: IoBufMut>(&mut self, mut buf: B) -> BufResult<usize, B> {
let slice = buf.as_uninit();
Expand All @@ -84,20 +113,12 @@ impl<S: AsyncRead + AsyncWrite + 'static> AsyncRead for TlsStream<S> {
unsafe { std::slice::from_raw_parts_mut::<u8>(slice.as_mut_ptr().cast(), slice.len()) };
match &mut self.0 {
#[cfg(feature = "native-tls")]
TlsStreamInner::NativeTls(s) => loop {
match io::Read::read(s, slice) {
Ok(res) => {
unsafe { buf.advance_to(res) };
return BufResult(Ok(res), buf);
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
match s.get_mut().fill_read_buf().await {
Ok(_) => continue,
Err(e) => return BufResult(Err(e), buf),
}
}
res => return BufResult(res, buf),
TlsStreamInner::NativeTls(s) => match drive(s, |s| io::Read::read(s, slice)).await {
Ok(res) => {
unsafe { buf.advance_to(res) };
BufResult(Ok(res), buf)
}
res => BufResult(res, buf),
},
#[cfg(feature = "rustls")]
TlsStreamInner::Rustls(s) => {
Expand Down Expand Up @@ -140,16 +161,10 @@ impl<S: AsyncRead + AsyncWrite + 'static> AsyncWrite for TlsStream<S> {
let slice = buf.as_init();
match &mut self.0 {
#[cfg(feature = "native-tls")]
TlsStreamInner::NativeTls(s) => loop {
let res = io::Write::write(s, slice);
match res {
Err(e) if e.kind() == io::ErrorKind::WouldBlock => match flush_impl(s).await {
Ok(_) => continue,
Err(e) => return BufResult(Err(e), buf),
},
_ => return BufResult(res, buf),
}
},
TlsStreamInner::NativeTls(s) => {
let res = drive(s, |s| io::Write::write(s, slice)).await;
BufResult(res, buf)
}
#[cfg(feature = "rustls")]
TlsStreamInner::Rustls(s) => {
let res = futures_util::AsyncWriteExt::write(s, slice).await;
Expand All @@ -175,7 +190,17 @@ impl<S: AsyncRead + AsyncWrite + 'static> AsyncWrite for TlsStream<S> {
self.flush().await?;
match &mut self.0 {
#[cfg(feature = "native-tls")]
TlsStreamInner::NativeTls(s) => s.get_mut().get_mut().shutdown().await,
TlsStreamInner::NativeTls(s) => {
// Send close_notify alert, then shutdown the underlying stream.
// Note, this implementation is platform-specific relying on how
// native-tls handles shutdown. In general, it's consistent on
// first call (sending close_notify); but it may or may not block
// and wait for the peer to respond with close_notify on any
// subsequent calls. Here we just let such behavior propagate,
// and suggest the users to call shutdown() at most once.
drive(s, |s| s.shutdown()).await?;
s.get_mut().get_mut().shutdown().await
}
#[cfg(feature = "rustls")]
TlsStreamInner::Rustls(s) => futures_util::AsyncWriteExt::close(s).await,
#[cfg(not(any(feature = "native-tls", feature = "rustls")))]
Expand Down
1 change: 1 addition & 0 deletions compio-tls/tests/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async fn connect(connector: TlsConnector) {
stream.flush().await.unwrap();
let (_, res) = stream.read_to_end(vec![]).await.unwrap();
println!("{}", String::from_utf8_lossy(&res));
stream.shutdown().await.unwrap();
}

#[cfg(feature = "native-tls")]
Expand Down
Loading