From 8aef33d8ce74d16ebc2c5632e4c5fe68868488b0 Mon Sep 17 00:00:00 2001 From: Abhijit Gadgil Date: Mon, 28 Dec 2020 22:32:20 +0530 Subject: [PATCH] Support for 'hyper-0.14' and 'tokio-1' 1. Updated the dependencies in `Cargo.toml`. 2. Started using 'tokio-native-tls' as opposed to deprecated 'tokio-tls' 3. Updated implementation of `poll_read` to confirm to new signature as per new API. 4. Other Fixes to get build working. (Tested with hyper-0.14.1 and tokio-1.0.1) 6. Updated the version to v0.5.0 --- Cargo.toml | 10 +++++----- src/client.rs | 28 +++++++++++++++++----------- src/lib.rs | 2 +- src/stream.rs | 43 +++++-------------------------------------- 4 files changed, 28 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 94b54d9..05da62c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] license = "MIT/Apache-2.0" @@ -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"] } diff --git a/src/client.rs b/src/client.rs index a0cf624..780edd5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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; @@ -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..dd267f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] diff --git a/src/stream.rs b/src/stream.rs index a924c2c..38a88d6 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -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 { @@ -40,37 +39,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,18 +65,6 @@ impl AsyncWrite for MaybeHttpsStream { } } - #[inline] - fn poll_write_buf( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &mut B, - ) -> 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), - } - } - #[inline] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match Pin::get_mut(self) { @@ -119,7 +86,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(), } } }