Skip to content

Support for 'hyper-0.14' and 'tokio-1' (#76) #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyper-tls"
version = "0.4.3" # don't forget html_root_url in lib.rs
version = "0.5.0" # don't forget html_root_url in lib.rs
description = "Default TLS implementation for use with hyper"
authors = ["Sean McArthur <[email protected]>"]
license = "MIT/Apache-2.0"
Expand All @@ -16,9 +16,9 @@ vendored = ["native-tls/vendored"]
[dependencies]
bytes = "0.5"
native-tls = "0.2"
hyper = { version = "0.13", default-features = false, features = ["tcp"] }
tokio = { version = "0.2" }
tokio-tls = "0.3"
hyper = { version = "0.14", default-features = false, features = ["full"] }
tokio = { version = "1" }
tokio-native-tls = "0.3"

[dev-dependencies]
tokio = { version = "0.2", features = ["io-std", "macros"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread", "io-std"] }
28 changes: 17 additions & 11 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use hyper::{client::connect::HttpConnector, service::Service, Uri};
use hyper::{client::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,15 +125,17 @@ where
return err(ForceHttpsButUriNotHttps.into());
}

let host = dst.host().unwrap_or("").trim_matches(|c| c == '[' || c == ']').to_owned();
let host = dst
.host()
.unwrap_or("")
.trim_matches(|c| c == '[' || c == ']')
.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 @@ -143,8 +150,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
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! Ok(())
//! }
//! ```
#![doc(html_root_url = "https://docs.rs/hyper-tls/0.4.3")]
#![doc(html_root_url = "https://docs.rs/hyper-tls/0.5.0")]
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
Expand Down
43 changes: 5 additions & 38 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::io;
use std::pin::Pin;
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;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
pub use tokio_native_tls::TlsStream;

/// A stream that might be protected with TLS.
pub enum MaybeHttpsStream<T> {
Expand Down Expand Up @@ -40,37 +39,17 @@ impl<T> From<TlsStream<T>> for MaybeHttpsStream<T> {
}

impl<T: AsyncRead + AsyncWrite + Unpin> AsyncRead for MaybeHttpsStream<T> {
#[inline]
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
match self {
MaybeHttpsStream::Http(s) => s.prepare_uninitialized_buffer(buf),
MaybeHttpsStream::Https(s) => s.prepare_uninitialized_buffer(buf),
}
}

#[inline]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
buf: &mut ReadBuf,
) -> Poll<Result<(), io::Error>> {
match Pin::get_mut(self) {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_read(cx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_read(cx, buf),
}
}

#[inline]
fn poll_read_buf<B: BufMut>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<Result<usize, io::Error>> {
match Pin::get_mut(self) {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_read_buf(cx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_read_buf(cx, buf),
}
}
}

impl<T: AsyncWrite + AsyncRead + Unpin> AsyncWrite for MaybeHttpsStream<T> {
Expand All @@ -86,18 +65,6 @@ impl<T: AsyncWrite + AsyncRead + Unpin> AsyncWrite for MaybeHttpsStream<T> {
}
}

#[inline]
fn poll_write_buf<B: Buf>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<Result<usize, io::Error>> {
match Pin::get_mut(self) {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_write_buf(cx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_write_buf(cx, buf),
}
}

#[inline]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
match Pin::get_mut(self) {
Expand All @@ -119,7 +86,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(),
}
}
}