Skip to content
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

Upgrade to Tokio 1.0 #79

Merged
merged 6 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ edition = "2018"
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"
bytes = "1.0.0"
native-tls = "0.2.6"
hyper = { version = "0.14.1", default-features = false, features = ["tcp", "client", "http1"] }
tokio = "1.0.0"
tokio-native-tls = "0.3.0"
seanmonstar marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
tokio = { version = "0.2", features = ["io-std", "macros"] }
tokio = { version = "1.0.0", features = ["io-std", "macros", "io-util"] }
9 changes: 3 additions & 6 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use hyper::{Client, body::HttpBody as _};
use hyper::{body::HttpBody as _, Client};
use hyper_tls::HttpsConnector;
use tokio::io::{self, AsyncWriteExt as _};

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
Expand All @@ -15,9 +14,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

while let Some(chunk) = res.body_mut().data().await {
let chunk = chunk?;
io::stdout()
.write_all(&chunk)
.await?
io::stdout().write_all(&chunk).await?
}
Ok(())
}
26 changes: 16 additions & 10 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,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 @@ -10,7 +10,7 @@
//! use hyper_tls::HttpsConnector;
//! use hyper::Client;
//!
//! #[tokio::main]
//! #[tokio::main(flavor = "current_thread")]
//! async fn main() -> Result<(), Box<dyn std::error::Error>>{
//! let https = HttpsConnector::new();
//! let client = Client::builder().build::<_, hyper::Body>(https);
Expand Down
48 changes: 17 additions & 31 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fmt;
use std::io;
use std::io::IoSlice;
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 +40,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>> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) -> Poll<Result<(), io::Error>> {
) -> Poll<io::Result<()>> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but I tried to keep these changes as minimal as possible for now. That kind of cleanup seems like a separate PR for me.

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,15 +66,21 @@ impl<T: AsyncWrite + AsyncRead + Unpin> AsyncWrite for MaybeHttpsStream<T> {
}
}

#[inline]
fn poll_write_buf<B: Buf>(
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut B,
bufs: &[IoSlice<'_>],
) -> 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),
MaybeHttpsStream::Http(s) => Pin::new(s).poll_write_vectored(cx, bufs),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_write_vectored(cx, bufs),
}
}

fn is_write_vectored(&self) -> bool {
match self {
MaybeHttpsStream::Http(s) => s.is_write_vectored(),
MaybeHttpsStream::Https(s) => s.is_write_vectored(),
}
}

Expand All @@ -119,7 +105,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(),
}
}
}