|
| 1 | +use std::{borrow::Cow, io}; |
| 2 | + |
| 3 | +use compio_io::{AsyncRead, AsyncWrite, compat::SyncStream}; |
| 4 | +use compio_py_dynamic_openssl::ssl::{Error, ErrorCode, HandshakeError, ShutdownResult, SslStream}; |
| 5 | + |
| 6 | +use crate::TlsStream; |
| 7 | + |
| 8 | +pub(crate) async fn handshake<S: AsyncRead + AsyncWrite>( |
| 9 | + mut res: Result<SslStream<SyncStream<S>>, HandshakeError<SyncStream<S>>>, |
| 10 | +) -> io::Result<TlsStream<S>> { |
| 11 | + loop { |
| 12 | + match res { |
| 13 | + Ok(mut s) => { |
| 14 | + let inner = s.get_mut(); |
| 15 | + if inner.has_pending_write() { |
| 16 | + inner.flush_write_buf().await?; |
| 17 | + } |
| 18 | + return Ok(TlsStream::from(s)); |
| 19 | + } |
| 20 | + Err(e) => match e { |
| 21 | + HandshakeError::SetupFailure(e) => return Err(io::Error::other(e)), |
| 22 | + HandshakeError::Failure(mid_stream) => { |
| 23 | + return Err(io::Error::other(mid_stream.into_error())); |
| 24 | + } |
| 25 | + HandshakeError::WouldBlock(mut mid_stream) => { |
| 26 | + let s = mid_stream.get_mut(); |
| 27 | + if s.has_pending_write() { |
| 28 | + s.flush_write_buf().await?; |
| 29 | + } else { |
| 30 | + s.fill_read_buf().await?; |
| 31 | + } |
| 32 | + res = mid_stream.handshake(); |
| 33 | + } |
| 34 | + }, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +enum DriveResult<T> { |
| 40 | + WantRead, |
| 41 | + WantWrite, |
| 42 | + Ready(io::Result<T>), |
| 43 | +} |
| 44 | + |
| 45 | +impl<T> From<Error> for DriveResult<T> { |
| 46 | + fn from(e: Error) -> Self { |
| 47 | + match e.code() { |
| 48 | + ErrorCode::WANT_READ => DriveResult::WantRead, |
| 49 | + ErrorCode::WANT_WRITE => DriveResult::WantWrite, |
| 50 | + _ => DriveResult::Ready(Err(e.into_io_error().unwrap_or_else(io::Error::other))), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<T> From<Result<T, Error>> for DriveResult<T> { |
| 56 | + fn from(res: Result<T, Error>) -> Self { |
| 57 | + match res { |
| 58 | + Ok(t) => DriveResult::Ready(Ok(t)), |
| 59 | + Err(e) => e.into(), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[inline] |
| 65 | +async fn drive<S, F, T>(s: &mut SslStream<SyncStream<S>>, mut f: F) -> io::Result<T> |
| 66 | +where |
| 67 | + S: AsyncRead + AsyncWrite, |
| 68 | + F: FnMut(&mut SslStream<SyncStream<S>>) -> DriveResult<T>, |
| 69 | +{ |
| 70 | + loop { |
| 71 | + let res = f(s); |
| 72 | + let s = s.get_mut(); |
| 73 | + if s.has_pending_write() { |
| 74 | + s.flush_write_buf().await?; |
| 75 | + } |
| 76 | + match res { |
| 77 | + DriveResult::Ready(res) => break res, |
| 78 | + DriveResult::WantRead => _ = s.fill_read_buf().await?, |
| 79 | + DriveResult::WantWrite => {} |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub(crate) fn negotiated_alpn<S>(s: &SslStream<SyncStream<S>>) -> Option<Cow<'_, [u8]>> { |
| 85 | + s.ssl() |
| 86 | + .selected_alpn_protocol() |
| 87 | + .map(|alpn| alpn.to_vec()) |
| 88 | + .map(Cow::from) |
| 89 | +} |
| 90 | + |
| 91 | +pub(crate) async fn read<S>(s: &mut SslStream<SyncStream<S>>, slice: &mut [u8]) -> io::Result<usize> |
| 92 | +where |
| 93 | + S: AsyncRead + AsyncWrite, |
| 94 | +{ |
| 95 | + drive(s, |s| match s.ssl_read(slice) { |
| 96 | + Ok(n) => DriveResult::Ready(Ok(n)), |
| 97 | + Err(e) => match e.code() { |
| 98 | + ErrorCode::ZERO_RETURN => DriveResult::Ready(Ok(0)), |
| 99 | + ErrorCode::SYSCALL if e.io_error().is_none() => DriveResult::Ready(Ok(0)), |
| 100 | + _ => e.into(), |
| 101 | + }, |
| 102 | + }) |
| 103 | + .await |
| 104 | +} |
| 105 | + |
| 106 | +pub(crate) async fn write<S>(s: &mut SslStream<SyncStream<S>>, slice: &[u8]) -> io::Result<usize> |
| 107 | +where |
| 108 | + S: AsyncRead + AsyncWrite, |
| 109 | +{ |
| 110 | + drive(s, |s| s.ssl_write(slice).into()).await |
| 111 | +} |
| 112 | + |
| 113 | +pub(crate) async fn shutdown<S>(s: &mut SslStream<SyncStream<S>>) -> io::Result<()> |
| 114 | +where |
| 115 | + S: AsyncRead + AsyncWrite, |
| 116 | +{ |
| 117 | + let res = drive(s, |s| match s.shutdown() { |
| 118 | + Ok(res) => DriveResult::Ready(Ok(res)), |
| 119 | + Err(e) => { |
| 120 | + if e.code() == ErrorCode::ZERO_RETURN { |
| 121 | + DriveResult::Ready(Ok(ShutdownResult::Received)) |
| 122 | + } else { |
| 123 | + e.into() |
| 124 | + } |
| 125 | + } |
| 126 | + }) |
| 127 | + .await?; |
| 128 | + if let Err(e) = s.get_mut().get_mut().shutdown().await |
| 129 | + && e.kind() != io::ErrorKind::NotConnected |
| 130 | + { |
| 131 | + return Err(e); |
| 132 | + } |
| 133 | + match res { |
| 134 | + // If close_notify has been sent but the peer has not responded with |
| 135 | + // close_notify, we let the caller know by returning Err(WouldBlock). |
| 136 | + // This behavior is different from the others as a Python-only hack. |
| 137 | + ShutdownResult::Sent => Err(io::Error::new( |
| 138 | + io::ErrorKind::WouldBlock, |
| 139 | + "close_notify sent", |
| 140 | + )), |
| 141 | + ShutdownResult::Received => Ok(()), |
| 142 | + } |
| 143 | +} |
0 commit comments