Skip to content

Commit

Permalink
Upgrade to tokio-native-tls (#65)
Browse files Browse the repository at this point in the history
Signed-off-by: Lucio Franco <luciofranco14@gmail.com>
  • Loading branch information
LucioFranco committed May 8, 2020
1 parent b17bc23 commit 7c9adcd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bytes = "0.5"
native-tls = "0.2"
hyper = { version = "0.13", default-features = false, features = ["tcp"] }
tokio = { version = "0.2" }
tokio-tls = "0.3"
tokio-native-tls = "0.1"

[dev-dependencies]
tokio = { version = "0.2", features = ["io-std", "macros"] }
24 changes: 12 additions & 12 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::task::{Context, Poll};

use hyper::{client::connect::HttpConnector, service::Service, Uri};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_tls::TlsConnector;
use tokio_native_tls::TlsConnector;

use crate::stream::MaybeHttpsStream;

Expand Down Expand Up @@ -65,13 +65,18 @@ impl<T> HttpsConnector<T> {
pub fn https_only(&mut self, enable: bool) {
self.force_https = enable;
}

/// With connector constructor
///
///
pub fn new_with_connector(http: T) -> Self {
native_tls::TlsConnector::new()
.map(|tls| HttpsConnector::from((http, tls.into())))
.unwrap_or_else(|e| panic!("HttpsConnector::new_with_connector(<connector>) failure: {}", e))
.unwrap_or_else(|e| {
panic!(
"HttpsConnector::new_with_connector(<connector>) failure: {}",
e
)
})
}
}

Expand Down Expand Up @@ -120,17 +125,13 @@ where
return err(ForceHttpsButUriNotHttps.into());
}

let host = dst.host()
.unwrap_or("")
.to_owned();
let host = dst.host().unwrap_or("").to_owned();
let connecting = self.http.call(dst);
let tls = self.tls.clone();
let fut = async move {
let tcp = connecting.await.map_err(Into::into)?;
let maybe = if is_https {
let tls = tls
.connect(&host, tcp)
.await?;
let tls = tls.connect(&host, tcp).await?;
MaybeHttpsStream::Https(tls)
} else {
MaybeHttpsStream::Http(tcp)
Expand All @@ -145,8 +146,7 @@ fn err<T>(e: BoxError) -> HttpsConnecting<T> {
HttpsConnecting(Box::pin(async { Err(e) }))
}

type BoxedFut<T> =
Pin<Box<dyn Future<Output = Result<MaybeHttpsStream<T>, BoxError>> + Send>>;
type BoxedFut<T> = Pin<Box<dyn Future<Output = Result<MaybeHttpsStream<T>, BoxError>> + Send>>;

/// A Future representing work to connect to a URL, and a TLS handshake.
pub struct HttpsConnecting<T>(BoxedFut<T>);
Expand Down
4 changes: 2 additions & 2 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::task::{Context, Poll};
use bytes::{Buf, BufMut};
use hyper::client::connect::{Connected, Connection};
use tokio::io::{AsyncRead, AsyncWrite};
pub use tokio_tls::TlsStream;
pub use tokio_native_tls::TlsStream;

/// A stream that might be protected with TLS.
pub enum MaybeHttpsStream<T> {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<T: AsyncRead + AsyncWrite + Connection + Unpin> Connection for MaybeHttpsSt
fn connected(&self) -> Connected {
match self {
MaybeHttpsStream::Http(s) => s.connected(),
MaybeHttpsStream::Https(s) => s.get_ref().connected(),
MaybeHttpsStream::Https(s) => s.get_ref().get_ref().get_ref().connected(),
}
}
}

0 comments on commit 7c9adcd

Please sign in to comment.