mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 08:22:59 +01:00
add tls
This commit is contained in:
parent
57a1d68f89
commit
acd33cccbb
@ -19,7 +19,14 @@ use tokio_openssl::SslConnectorExt;
|
|||||||
#[cfg(feature="alpn")]
|
#[cfg(feature="alpn")]
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
use HAS_OPENSSL;
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
use native_tls::{TlsConnector, Error as TlsError};
|
||||||
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
use tokio_tls::TlsConnectorExt;
|
||||||
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
use futures::Future;
|
||||||
|
|
||||||
|
use {HAS_OPENSSL, HAS_TLS};
|
||||||
use server::IoStream;
|
use server::IoStream;
|
||||||
|
|
||||||
|
|
||||||
@ -61,6 +68,11 @@ pub enum ClientConnectorError {
|
|||||||
#[fail(display="{}", _0)]
|
#[fail(display="{}", _0)]
|
||||||
SslError(#[cause] OpensslError),
|
SslError(#[cause] OpensslError),
|
||||||
|
|
||||||
|
/// SSL error
|
||||||
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
#[fail(display="{}", _0)]
|
||||||
|
SslError(#[cause] TlsError),
|
||||||
|
|
||||||
/// Connection error
|
/// Connection error
|
||||||
#[fail(display = "{}", _0)]
|
#[fail(display = "{}", _0)]
|
||||||
Connector(#[cause] ConnectorError),
|
Connector(#[cause] ConnectorError),
|
||||||
@ -85,8 +97,10 @@ impl From<ConnectorError> for ClientConnectorError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ClientConnector {
|
pub struct ClientConnector {
|
||||||
#[cfg(feature="alpn")]
|
#[cfg(all(feature="alpn"))]
|
||||||
connector: SslConnector,
|
connector: SslConnector,
|
||||||
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
connector: TlsConnector,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Actor for ClientConnector {
|
impl Actor for ClientConnector {
|
||||||
@ -99,15 +113,22 @@ impl ArbiterService for ClientConnector {}
|
|||||||
|
|
||||||
impl Default for ClientConnector {
|
impl Default for ClientConnector {
|
||||||
fn default() -> ClientConnector {
|
fn default() -> ClientConnector {
|
||||||
#[cfg(feature="alpn")]
|
#[cfg(all(feature="alpn"))]
|
||||||
{
|
{
|
||||||
let builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
let builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||||
ClientConnector {
|
ClientConnector {
|
||||||
connector: builder.build()
|
connector: builder.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
{
|
||||||
|
let builder = TlsConnector::builder().unwrap();
|
||||||
|
ClientConnector {
|
||||||
|
connector: builder.build().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature="alpn"))]
|
#[cfg(not(any(feature="alpn", feature="tls")))]
|
||||||
ClientConnector {}
|
ClientConnector {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,7 +205,7 @@ impl Handler<Connect> for ClientConnector {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// check ssl availability
|
// check ssl availability
|
||||||
if proto.is_secure() && !HAS_OPENSSL { //&& !HAS_TLS {
|
if proto.is_secure() && !HAS_OPENSSL && !HAS_TLS {
|
||||||
return ActorResponse::reply(Err(ClientConnectorError::SslIsNotSupported))
|
return ActorResponse::reply(Err(ClientConnectorError::SslIsNotSupported))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,7 +235,23 @@ impl Handler<Connect> for ClientConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature="alpn"))]
|
#[cfg(all(feature="tls", not(feature="alpn")))]
|
||||||
|
match res {
|
||||||
|
Err(err) => fut::Either::B(fut::err(err.into())),
|
||||||
|
Ok(stream) => {
|
||||||
|
if proto.is_secure() {
|
||||||
|
fut::Either::A(
|
||||||
|
_act.connector.connect_async(&host, stream)
|
||||||
|
.map_err(ClientConnectorError::SslError)
|
||||||
|
.map(|stream| Connection{stream: Box::new(stream)})
|
||||||
|
.into_actor(_act))
|
||||||
|
} else {
|
||||||
|
fut::Either::B(fut::ok(Connection{stream: Box::new(stream)}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(feature="alpn", feature="tls")))]
|
||||||
match res {
|
match res {
|
||||||
Err(err) => fut::err(err.into()),
|
Err(err) => fut::err(err.into()),
|
||||||
Ok(stream) => {
|
Ok(stream) => {
|
||||||
|
@ -149,10 +149,10 @@ pub(crate) const HAS_OPENSSL: bool = true;
|
|||||||
#[cfg(not(feature="openssl"))]
|
#[cfg(not(feature="openssl"))]
|
||||||
pub(crate) const HAS_OPENSSL: bool = false;
|
pub(crate) const HAS_OPENSSL: bool = false;
|
||||||
|
|
||||||
// #[cfg(feature="tls")]
|
#[cfg(feature="tls")]
|
||||||
// pub(crate) const HAS_TLS: bool = true;
|
pub(crate) const HAS_TLS: bool = true;
|
||||||
// #[cfg(not(feature="tls"))]
|
#[cfg(not(feature="tls"))]
|
||||||
// pub(crate) const HAS_TLS: bool = false;
|
pub(crate) const HAS_TLS: bool = false;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[deprecated(since="0.4.4", note="please use `actix::header` module")]
|
#[deprecated(since="0.4.4", note="please use `actix::header` module")]
|
||||||
|
Loading…
Reference in New Issue
Block a user