1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-23 22:51:07 +01:00

use RequestHost trait for OpensslConnector service

This commit is contained in:
Nikolay Kim 2018-11-11 23:04:39 -08:00
parent 9b9599500a
commit 8f20f69559

View File

@ -6,8 +6,8 @@ use tokio_io::{AsyncRead, AsyncWrite};
use tokio_openssl::{AcceptAsync, ConnectAsync, SslAcceptorExt, SslConnectorExt, SslStream};
use super::MAX_CONN_COUNTER;
use connector::Connect;
use counter::{Counter, CounterGuard};
use resolver::RequestHost;
use service::{NewService, Service};
/// Support `SSL` connections via openssl package
@ -102,12 +102,12 @@ impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {
}
/// Openssl connector factory
pub struct OpensslConnector<T, E> {
pub struct OpensslConnector<R, T, E> {
connector: SslConnector,
_t: PhantomData<(T, E)>,
_t: PhantomData<(R, T, E)>,
}
impl<T, E> OpensslConnector<T, E> {
impl<R, T, E> OpensslConnector<R, T, E> {
pub fn new(connector: SslConnector) -> Self {
OpensslConnector {
connector,
@ -116,11 +116,10 @@ impl<T, E> OpensslConnector<T, E> {
}
}
impl<T: AsyncRead + AsyncWrite> OpensslConnector<T, ()> {
impl<R: RequestHost, T: AsyncRead + AsyncWrite> OpensslConnector<R, T, ()> {
pub fn service(
connector: SslConnector,
) -> impl Service<Request = (Connect, T), Response = (Connect, SslStream<T>), Error = Error>
{
) -> impl Service<Request = (R, T), Response = (R, SslStream<T>), Error = Error> {
OpensslConnectorService {
connector: connector,
_t: PhantomData,
@ -128,7 +127,7 @@ impl<T: AsyncRead + AsyncWrite> OpensslConnector<T, ()> {
}
}
impl<T, E> Clone for OpensslConnector<T, E> {
impl<R, T, E> Clone for OpensslConnector<R, T, E> {
fn clone(&self) -> Self {
Self {
connector: self.connector.clone(),
@ -137,11 +136,11 @@ impl<T, E> Clone for OpensslConnector<T, E> {
}
}
impl<T: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<T, E> {
type Request = (Connect, T);
type Response = (Connect, SslStream<T>);
impl<R: RequestHost, T: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<R, T, E> {
type Request = (R, T);
type Response = (R, SslStream<T>);
type Error = Error;
type Service = OpensslConnectorService<T>;
type Service = OpensslConnectorService<R, T>;
type InitError = E;
type Future = FutureResult<Self::Service, Self::InitError>;
@ -153,16 +152,16 @@ impl<T: AsyncRead + AsyncWrite, E> NewService for OpensslConnector<T, E> {
}
}
pub struct OpensslConnectorService<T> {
pub struct OpensslConnectorService<R, T> {
connector: SslConnector,
_t: PhantomData<T>,
_t: PhantomData<(R, T)>,
}
impl<T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T> {
type Request = (Connect, T);
type Response = (Connect, SslStream<T>);
impl<R: RequestHost, T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<R, T> {
type Request = (R, T);
type Response = (R, SslStream<T>);
type Error = Error;
type Future = ConnectAsyncExt<T>;
type Future = ConnectAsyncExt<R, T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
@ -170,22 +169,23 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslConnectorService<T> {
fn call(&mut self, (req, stream): Self::Request) -> Self::Future {
ConnectAsyncExt {
fut: SslConnectorExt::connect_async(&self.connector, &req.host, stream),
fut: SslConnectorExt::connect_async(&self.connector, req.host(), stream),
req: Some(req),
}
}
}
pub struct ConnectAsyncExt<T> {
pub struct ConnectAsyncExt<R, T> {
req: Option<R>,
fut: ConnectAsync<T>,
req: Option<Connect>,
}
impl<T> Future for ConnectAsyncExt<T>
impl<R, T> Future for ConnectAsyncExt<R, T>
where
R: RequestHost,
T: AsyncRead + AsyncWrite,
{
type Item = (Connect, SslStream<T>);
type Item = (R, SslStream<T>);
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {