Skip to content

Commit

Permalink
Remove unused types in actix-tls. Add ActixStream impl for Box<dyn Ac… (
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow committed Mar 26, 2021
1 parent a3c9ebc commit 12d3942
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 140 deletions.
12 changes: 11 additions & 1 deletion actix-rt/src/lib.rs
Expand Up @@ -87,7 +87,7 @@ pub mod net {
pub use tokio::net::{UnixDatagram, UnixListener, UnixStream};

/// Extension trait over async read+write types that can also signal readiness.
pub trait ActixStream: AsyncRead + AsyncWrite + Unpin + 'static {
pub trait ActixStream: AsyncRead + AsyncWrite + Unpin {
/// Poll stream and check read readiness of Self.
///
/// See [tokio::net::TcpStream::poll_read_ready] for detail on intended use.
Expand Down Expand Up @@ -127,6 +127,16 @@ pub mod net {
ready.poll(cx)
}
}

impl<Io: ActixStream + ?Sized> ActixStream for Box<Io> {
fn poll_read_ready(&self, cx: &mut Context<'_>) -> Poll<io::Result<Ready>> {
(**self).poll_read_ready(cx)
}

fn poll_write_ready(&self, cx: &mut Context<'_>) -> Poll<io::Result<Ready>> {
(**self).poll_write_ready(cx)
}
}
}

pub mod time {
Expand Down
4 changes: 3 additions & 1 deletion actix-tls/CHANGES.md
Expand Up @@ -3,9 +3,11 @@
## Unreleased - 2021-xx-xx
* Changed `connect::ssl::rustls::RustlsConnectorService` to return error when `DNSNameRef`
generation failed instead of panic. [#296]
* Remove `connect::ssl::openssl::OpensslConnectServiceFactory`. [#297]
* Remove `connect::ssl::openssl::OpensslConnectService`. [#297]

[#296]: https://github.com/actix/actix-net/pull/296

[#297]: https://github.com/actix/actix-net/pull/297

## 3.0.0-beta.4 - 2021-02-24
* Rename `accept::openssl::{SslStream => TlsStream}`.
Expand Down
17 changes: 4 additions & 13 deletions actix-tls/src/accept/nativetls.rs
Expand Up @@ -113,7 +113,7 @@ impl Clone for Acceptor {
}
}

impl<T: ActixStream> ServiceFactory<T> for Acceptor {
impl<T: ActixStream + 'static> ServiceFactory<T> for Acceptor {
type Response = TlsStream<T>;
type Error = Error;
type Config = ();
Expand All @@ -138,16 +138,7 @@ pub struct NativeTlsAcceptorService {
conns: Counter,
}

impl Clone for NativeTlsAcceptorService {
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
conns: self.conns.clone(),
}
}
}

impl<T: ActixStream> Service<T> for NativeTlsAcceptorService {
impl<T: ActixStream + 'static> Service<T> for NativeTlsAcceptorService {
type Response = TlsStream<T>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<TlsStream<T>, Error>>;
Expand All @@ -162,9 +153,9 @@ impl<T: ActixStream> Service<T> for NativeTlsAcceptorService {

fn call(&self, io: T) -> Self::Future {
let guard = self.conns.get();
let this = self.clone();
let acceptor = self.acceptor.clone();
Box::pin(async move {
let io = this.acceptor.accept(io).await;
let io = acceptor.accept(io).await;
drop(guard);
io.map(Into::into)
})
Expand Down
132 changes: 8 additions & 124 deletions actix-tls/src/connect/ssl/openssl.rs
@@ -1,24 +1,19 @@
use std::{
fmt,
future::Future,
io,
pin::Pin,
task::{Context, Poll},
};

use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::net::TcpStream;
use actix_rt::net::ActixStream;
use actix_service::{Service, ServiceFactory};
use futures_core::{future::LocalBoxFuture, ready};
use log::trace;

pub use openssl::ssl::{Error as SslError, HandshakeError, SslConnector, SslMethod};
pub use tokio_openssl::SslStream;

use crate::connect::resolve::Resolve;
use crate::connect::{
Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection, Resolver,
};
use crate::connect::{Address, Connection};

/// OpenSSL connector factory
pub struct OpensslConnector {
Expand All @@ -45,8 +40,8 @@ impl Clone for OpensslConnector {

impl<T, U> ServiceFactory<Connection<T, U>> for OpensslConnector
where
T: Address + 'static,
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
T: Address,
U: ActixStream + 'static,
{
type Response = Connection<T, SslStream<U>>;
type Error = io::Error;
Expand Down Expand Up @@ -75,8 +70,8 @@ impl Clone for OpensslConnectorService {

impl<T, U> Service<Connection<T, U>> for OpensslConnectorService
where
T: Address + 'static,
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
T: Address,
U: ActixStream,
{
type Response = Connection<T, SslStream<U>>;
type Error = io::Error;
Expand Down Expand Up @@ -112,7 +107,8 @@ pub struct ConnectAsyncExt<T, U> {

impl<T: Address, U> Future for ConnectAsyncExt<T, U>
where
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
T: Address,
U: ActixStream,
{
type Output = Result<Connection<T, SslStream<U>>, io::Error>;

Expand All @@ -132,115 +128,3 @@ where
}
}
}

pub struct OpensslConnectServiceFactory {
tcp: ConnectServiceFactory,
openssl: OpensslConnector,
}

impl OpensslConnectServiceFactory {
/// Construct new OpensslConnectService factory
pub fn new(connector: SslConnector) -> Self {
OpensslConnectServiceFactory {
tcp: ConnectServiceFactory::new(Resolver::Default),
openssl: OpensslConnector::new(connector),
}
}

/// Construct new connect service with custom DNS resolver
pub fn with_resolver(connector: SslConnector, resolver: impl Resolve + 'static) -> Self {
OpensslConnectServiceFactory {
tcp: ConnectServiceFactory::new(Resolver::new_custom(resolver)),
openssl: OpensslConnector::new(connector),
}
}

/// Construct OpenSSL connect service
pub fn service(&self) -> OpensslConnectService {
OpensslConnectService {
tcp: self.tcp.service(),
openssl: OpensslConnectorService {
connector: self.openssl.connector.clone(),
},
}
}
}

impl Clone for OpensslConnectServiceFactory {
fn clone(&self) -> Self {
OpensslConnectServiceFactory {
tcp: self.tcp.clone(),
openssl: self.openssl.clone(),
}
}
}

impl<T: Address + 'static> ServiceFactory<Connect<T>> for OpensslConnectServiceFactory {
type Response = SslStream<TcpStream>;
type Error = ConnectError;
type Config = ();
type Service = OpensslConnectService;
type InitError = ();
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;

fn new_service(&self, _: ()) -> Self::Future {
let service = self.service();
Box::pin(async { Ok(service) })
}
}

#[derive(Clone)]
pub struct OpensslConnectService {
tcp: ConnectService,
openssl: OpensslConnectorService,
}

impl<T: Address + 'static> Service<Connect<T>> for OpensslConnectService {
type Response = SslStream<TcpStream>;
type Error = ConnectError;
type Future = OpensslConnectServiceResponse<T>;

actix_service::always_ready!();

fn call(&self, req: Connect<T>) -> Self::Future {
OpensslConnectServiceResponse {
fut1: Some(self.tcp.call(req)),
fut2: None,
openssl: self.openssl.clone(),
}
}
}

pub struct OpensslConnectServiceResponse<T: Address + 'static> {
fut1: Option<<ConnectService as Service<Connect<T>>>::Future>,
fut2: Option<<OpensslConnectorService as Service<Connection<T, TcpStream>>>::Future>,
openssl: OpensslConnectorService,
}

impl<T: Address> Future for OpensslConnectServiceResponse<T> {
type Output = Result<SslStream<TcpStream>, ConnectError>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(ref mut fut) = self.fut1 {
match ready!(Pin::new(fut).poll(cx)) {
Ok(res) => {
let _ = self.fut1.take();
self.fut2 = Some(self.openssl.call(res));
}
Err(e) => return Poll::Ready(Err(e)),
}
}

if let Some(ref mut fut) = self.fut2 {
match ready!(Pin::new(fut).poll(cx)) {
Ok(connect) => Poll::Ready(Ok(connect.into_parts().0)),
Err(e) => Poll::Ready(Err(ConnectError::Io(io::Error::new(
io::ErrorKind::Other,
e,
)))),
}
} else {
Poll::Pending
}
}
}
2 changes: 1 addition & 1 deletion actix-tls/src/connect/ssl/rustls.rs
Expand Up @@ -47,7 +47,7 @@ impl Clone for RustlsConnector {
impl<T, U> ServiceFactory<Connection<T, U>> for RustlsConnector
where
T: Address,
U: ActixStream,
U: ActixStream + 'static,
{
type Response = Connection<T, TlsStream<U>>;
type Error = io::Error;
Expand Down

0 comments on commit 12d3942

Please sign in to comment.