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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ compio-net = { path = "./compio-net", version = "0.9.0" }
compio-signal = { path = "./compio-signal", version = "0.7.0" }
compio-dispatcher = { path = "./compio-dispatcher", version = "0.8.1" }
compio-log = { path = "./compio-log", version = "0.1.0" }
compio-tls = { path = "./compio-tls", version = "0.7.1", default-features = false }
compio-tls = { path = "./compio-tls", version = "0.7.2", default-features = false }
compio-process = { path = "./compio-process", version = "0.6.0" }
compio-quic = { path = "./compio-quic", version = "0.5.1", default-features = false }
compio-ws = { path = "./compio-ws", version = "0.1.0", default-features = false }
Comment thread
Berrysoft marked this conversation as resolved.
Expand Down
2 changes: 1 addition & 1 deletion compio-tls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compio-tls"
version = "0.7.1"
version = "0.7.2"
description = "TLS adaptor with compio"
categories = ["asynchronous", "network-programming"]
keywords = ["async", "net", "tls"]
Expand Down
2 changes: 2 additions & 0 deletions compio-tls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub use native_tls;
pub use rustls;

mod adapter;
mod maybe;
mod stream;

pub use adapter::*;
pub use maybe::*;
pub use stream::*;
71 changes: 71 additions & 0 deletions compio-tls/src/maybe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::io;

use compio_buf::{BufResult, IoBuf, IoBufMut};
use compio_io::{AsyncRead, AsyncWrite};

use crate::TlsStream;

/// Stream that can be either plain TCP or TLS-encrypted
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum MaybeTlsStream<S> {
/// Plain, unencrypted stream
Plain(S),
/// TLS-encrypted stream
Tls(TlsStream<S>),
}

impl<S> MaybeTlsStream<S> {
/// Create an unencrypted stream.
pub fn plain(stream: S) -> Self {
MaybeTlsStream::Plain(stream)
}

/// Create a TLS-encrypted stream.
pub fn tls(stream: TlsStream<S>) -> Self {
MaybeTlsStream::Tls(stream)
}

/// Whether the stream is TLS-encrypted.
pub fn is_tls(&self) -> bool {
matches!(self, MaybeTlsStream::Tls(_))
}
}

impl<S> AsyncRead for MaybeTlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin + 'static,
{
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
match self {
MaybeTlsStream::Plain(stream) => stream.read(buf).await,
MaybeTlsStream::Tls(stream) => stream.read(buf).await,
}
}
}

impl<S> AsyncWrite for MaybeTlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin + 'static,
{
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
match self {
MaybeTlsStream::Plain(stream) => stream.write(buf).await,
MaybeTlsStream::Tls(stream) => stream.write(buf).await,
}
}

async fn flush(&mut self) -> io::Result<()> {
match self {
MaybeTlsStream::Plain(stream) => stream.flush().await,
MaybeTlsStream::Tls(stream) => stream.flush().await,
}
}

async fn shutdown(&mut self) -> io::Result<()> {
match self {
MaybeTlsStream::Plain(stream) => stream.shutdown().await,
MaybeTlsStream::Tls(stream) => stream.shutdown().await,
}
}
}
2 changes: 1 addition & 1 deletion compio-ws/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compio-ws"
version = "0.1.0"
version = "0.1.1"
description = "WebSocket library for the compio runtime"
edition = { workspace = true }
authors = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions compio-ws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]

#[deprecated = "Use `compio-tls` crate instead."]
pub mod stream;

#[cfg(feature = "rustls")]
Expand Down
6 changes: 2 additions & 4 deletions compio-ws/src/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use compio_io::{AsyncRead, AsyncWrite};
use compio_net::TcpStream;
use compio_tls::TlsConnector;
use compio_tls::{MaybeTlsStream, TlsConnector};
#[cfg(any(feature = "rustls-platform-verifier", feature = "webpki-roots"))]
use rustls::{ClientConfig, RootCertStore};
use tungstenite::{
Expand All @@ -15,9 +15,7 @@ use tungstenite::{
stream::Mode,
};

use crate::{
WebSocketConfig, WebSocketStream, client_async_with_config, domain, stream::MaybeTlsStream,
};
use crate::{WebSocketConfig, WebSocketStream, client_async_with_config, domain};

/// Type alias for a stream that can be either plain TCP or TLS-encrypted.
pub type AutoStream<S> = MaybeTlsStream<S>;
Expand Down
91 changes: 1 addition & 90 deletions compio-ws/src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,4 @@
//! Provides [`MaybeTlsStream`].

#[cfg(feature = "rustls")]
use std::io::Result as IoResult;

#[cfg(feature = "rustls")]
use compio_buf::{BufResult, IoBuf, IoBufMut};
#[cfg(feature = "rustls")]
use compio_io::{AsyncRead, AsyncWrite};
#[cfg(feature = "rustls")]
use compio_tls::TlsStream;

/// Stream that can be either plain TCP or TLS-encrypted
#[cfg(feature = "rustls")]
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum MaybeTlsStream<S> {
/// Plain, unencrypted stream
Plain(S),
/// TLS-encrypted stream
#[cfg(feature = "rustls")]
Tls(TlsStream<S>),
}

#[cfg(feature = "rustls")]
impl<S> MaybeTlsStream<S> {
/// Create an unencrypted stream.
pub fn plain(stream: S) -> Self {
MaybeTlsStream::Plain(stream)
}

/// Create a TLS-encrypted stream.
#[cfg(feature = "rustls")]
pub fn tls(stream: TlsStream<S>) -> Self {
MaybeTlsStream::Tls(stream)
}

/// Whether the stream is TLS-encrypted.
pub fn is_tls(&self) -> bool {
#[cfg(feature = "rustls")]
{
matches!(self, MaybeTlsStream::Tls(_))
}
#[cfg(not(feature = "rustls"))]
{
false
}
}
}

#[cfg(feature = "rustls")]
impl<S> AsyncRead for MaybeTlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin + 'static,
{
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
match self {
MaybeTlsStream::Plain(stream) => stream.read(buf).await,
#[cfg(feature = "rustls")]
MaybeTlsStream::Tls(stream) => stream.read(buf).await,
}
}
}

#[cfg(feature = "rustls")]
impl<S> AsyncWrite for MaybeTlsStream<S>
where
S: AsyncRead + AsyncWrite + Unpin + 'static,
{
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
match self {
MaybeTlsStream::Plain(stream) => stream.write(buf).await,
#[cfg(feature = "rustls")]
MaybeTlsStream::Tls(stream) => stream.write(buf).await,
}
}

async fn flush(&mut self) -> IoResult<()> {
match self {
MaybeTlsStream::Plain(stream) => stream.flush().await,
#[cfg(feature = "rustls")]
MaybeTlsStream::Tls(stream) => stream.flush().await,
}
}

async fn shutdown(&mut self) -> IoResult<()> {
match self {
MaybeTlsStream::Plain(stream) => stream.shutdown().await,
#[cfg(feature = "rustls")]
MaybeTlsStream::Tls(stream) => stream.shutdown().await,
}
}
}
pub use compio_tls::MaybeTlsStream;
Loading