From 8d3a7bcdab06abcb240dedc5e77cbc78d757e600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Wed, 23 Dec 2020 23:13:07 +0100 Subject: [PATCH 1/6] Upgrade to Tokio 1.0 Currently pulling in tokio-native-tls via a git dependency, since https://github.com/tokio-rs/tls/pull/46 hasn't been released to crates.io yet. --- Cargo.toml | 15 +++++++++------ examples/client.rs | 9 +++------ src/client.rs | 26 +++++++++++++++---------- src/lib.rs | 2 +- src/stream.rs | 48 ++++++++++++++++------------------------------ 5 files changed, 46 insertions(+), 54 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 94b54d9..69bc1ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,14 @@ 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", "http2"] } +tokio = "1.0.0" +tokio-native-tls = "0.2.0" + +[patch.crates-io] +tokio-native-tls = { git = "https://github.com/tokio-rs/tls.git", rev = "44e978cfa6e46294c0e352fad820456dbe94bdaa" } [dev-dependencies] -tokio = { version = "0.2", features = ["io-std", "macros"] } +tokio = { version = "1.0.0", features = ["io-std", "macros"] } diff --git a/examples/client.rs b/examples/client.rs index 7855c58..005a333 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -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> { let https = HttpsConnector::new(); let client = Client::builder().build::<_, hyper::Body>(https); @@ -15,9 +14,7 @@ async fn main() -> Result<(), Box> { 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(()) } diff --git a/src/client.rs b/src/client.rs index a0cf624..3b8339b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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; @@ -65,13 +65,18 @@ impl HttpsConnector { 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() failure: {}", e)) + .unwrap_or_else(|e| { + panic!( + "HttpsConnector::new_with_connector() failure: {}", + e + ) + }) } } @@ -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) @@ -143,8 +150,7 @@ fn err(e: BoxError) -> HttpsConnecting { HttpsConnecting(Box::pin(async { Err(e) })) } -type BoxedFut = - Pin, BoxError>> + Send>>; +type BoxedFut = Pin, BoxError>> + Send>>; /// A Future representing work to connect to a URL, and a TLS handshake. pub struct HttpsConnecting(BoxedFut); diff --git a/src/lib.rs b/src/lib.rs index 92d0290..0b243df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ //! use hyper_tls::HttpsConnector; //! use hyper::Client; //! -//! #[tokio::main] +//! #[tokio::main(flavor = "current_thread")] //! async fn main() -> Result<(), Box>{ //! let https = HttpsConnector::new(); //! let client = Client::builder().build::<_, hyper::Body>(https); diff --git a/src/stream.rs b/src/stream.rs index a924c2c..4875410 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -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 { @@ -40,37 +40,17 @@ impl From> for MaybeHttpsStream { } impl AsyncRead for MaybeHttpsStream { - #[inline] - unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> 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> { + buf: &mut ReadBuf, + ) -> Poll> { 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( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> Poll> { - 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 AsyncWrite for MaybeHttpsStream { @@ -86,15 +66,21 @@ impl AsyncWrite for MaybeHttpsStream { } } - #[inline] - fn poll_write_buf( + fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, - buf: &mut B, + bufs: &[IoSlice<'_>], ) -> Poll> { 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(), } } @@ -119,7 +105,7 @@ impl 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(), } } } From 11998ee58a786d8d60a6271a465da79fe5881bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Thu, 24 Dec 2020 18:53:29 +0100 Subject: [PATCH 2/6] Use released tokio-native-tls --- Cargo.toml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 69bc1ad..5d6a9c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,10 +18,7 @@ bytes = "1.0.0" native-tls = "0.2.6" hyper = { version = "0.14.1", default-features = false, features = ["tcp", "client", "http1", "http2"] } tokio = "1.0.0" -tokio-native-tls = "0.2.0" - -[patch.crates-io] -tokio-native-tls = { git = "https://github.com/tokio-rs/tls.git", rev = "44e978cfa6e46294c0e352fad820456dbe94bdaa" } +tokio-native-tls = "0.3.0" [dev-dependencies] tokio = { version = "1.0.0", features = ["io-std", "macros"] } From 691c72738bd2a2bf81cbb49a0638295cc4f8a9e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Sat, 26 Dec 2020 17:59:28 +0100 Subject: [PATCH 3/6] Disable HTTP/2 feature, we don't support ALPN anyway See #61 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5d6a9c1..57689a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ vendored = ["native-tls/vendored"] [dependencies] bytes = "1.0.0" native-tls = "0.2.6" -hyper = { version = "0.14.1", default-features = false, features = ["tcp", "client", "http1", "http2"] } +hyper = { version = "0.14.1", default-features = false, features = ["tcp", "client", "http1"] } tokio = "1.0.0" tokio-native-tls = "0.3.0" From c451376ff212f94dbb8899ff6352335ff631e7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Sat, 26 Dec 2020 18:11:11 +0100 Subject: [PATCH 4/6] Examples depend on tokio/io-util --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 57689a6..69a07bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ tokio = "1.0.0" tokio-native-tls = "0.3.0" [dev-dependencies] -tokio = { version = "1.0.0", features = ["io-std", "macros"] } +tokio = { version = "1.0.0", features = ["io-std", "macros", "io-util"] } From ca26cf9a6350eeddfa6f027db82fae874454deaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Tue, 29 Dec 2020 20:17:53 +0100 Subject: [PATCH 5/6] Upgrade to hyper 0.14.2 and disable http1 feature See https://github.com/hyperium/hyper-tls/pull/79#discussion_r549812598 --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 69a07bc..5e4d2ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,9 +16,10 @@ vendored = ["native-tls/vendored"] [dependencies] bytes = "1.0.0" native-tls = "0.2.6" -hyper = { version = "0.14.1", default-features = false, features = ["tcp", "client", "http1"] } +hyper = { version = "0.14.2", default-features = false, features = ["tcp", "client"] } tokio = "1.0.0" tokio-native-tls = "0.3.0" [dev-dependencies] tokio = { version = "1.0.0", features = ["io-std", "macros", "io-util"] } +hyper = { version = "0.14.2", default-features = false, features = ["http1"] } From 8f4d8f9daa91dfa99e0742c3d940a1312666e9bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teo=20Klestrup=20R=C3=B6ijezon?= Date: Tue, 29 Dec 2020 20:22:01 +0100 Subject: [PATCH 6/6] Loosen dependency version ranges See https://github.com/hyperium/hyper-tls/pull/79#discussion_r549813037 --- Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e4d2ee..c09b7b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,11 @@ edition = "2018" vendored = ["native-tls/vendored"] [dependencies] -bytes = "1.0.0" -native-tls = "0.2.6" +bytes = "1" +native-tls = "0.2.1" hyper = { version = "0.14.2", default-features = false, features = ["tcp", "client"] } -tokio = "1.0.0" -tokio-native-tls = "0.3.0" +tokio = "1" +tokio-native-tls = "0.3" [dev-dependencies] tokio = { version = "1.0.0", features = ["io-std", "macros", "io-util"] }