Skip to content

Commit 83747a7

Browse files
authored
Upgrade to Tokio 1.0 (#79)
1 parent fa6d6a7 commit 83747a7

File tree

5 files changed

+44
-54
lines changed

5 files changed

+44
-54
lines changed

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ edition = "2018"
1414
vendored = ["native-tls/vendored"]
1515

1616
[dependencies]
17-
bytes = "0.5"
18-
native-tls = "0.2"
19-
hyper = { version = "0.13", default-features = false, features = ["tcp"] }
20-
tokio = { version = "0.2" }
21-
tokio-tls = "0.3"
17+
bytes = "1"
18+
native-tls = "0.2.1"
19+
hyper = { version = "0.14.2", default-features = false, features = ["tcp", "client"] }
20+
tokio = "1"
21+
tokio-native-tls = "0.3"
2222

2323
[dev-dependencies]
24-
tokio = { version = "0.2", features = ["io-std", "macros"] }
24+
tokio = { version = "1.0.0", features = ["io-std", "macros", "io-util"] }
25+
hyper = { version = "0.14.2", default-features = false, features = ["http1"] }

examples/client.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
2-
use hyper::{Client, body::HttpBody as _};
1+
use hyper::{body::HttpBody as _, Client};
32
use hyper_tls::HttpsConnector;
43
use tokio::io::{self, AsyncWriteExt as _};
54

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

1615
while let Some(chunk) = res.body_mut().data().await {
1716
let chunk = chunk?;
18-
io::stdout()
19-
.write_all(&chunk)
20-
.await?
17+
io::stdout().write_all(&chunk).await?
2118
}
2219
Ok(())
2320
}

src/client.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::task::{Context, Poll};
55

66
use hyper::{client::connect::HttpConnector, service::Service, Uri};
77
use tokio::io::{AsyncRead, AsyncWrite};
8-
use tokio_tls::TlsConnector;
8+
use tokio_native_tls::TlsConnector;
99

1010
use crate::stream::MaybeHttpsStream;
1111

@@ -65,13 +65,18 @@ impl<T> HttpsConnector<T> {
6565
pub fn https_only(&mut self, enable: bool) {
6666
self.force_https = enable;
6767
}
68-
68+
6969
/// With connector constructor
70-
///
70+
///
7171
pub fn new_with_connector(http: T) -> Self {
7272
native_tls::TlsConnector::new()
7373
.map(|tls| HttpsConnector::from((http, tls.into())))
74-
.unwrap_or_else(|e| panic!("HttpsConnector::new_with_connector(<connector>) failure: {}", e))
74+
.unwrap_or_else(|e| {
75+
panic!(
76+
"HttpsConnector::new_with_connector(<connector>) failure: {}",
77+
e
78+
)
79+
})
7580
}
7681
}
7782

@@ -120,15 +125,17 @@ where
120125
return err(ForceHttpsButUriNotHttps.into());
121126
}
122127

123-
let host = dst.host().unwrap_or("").trim_matches(|c| c == '[' || c == ']').to_owned();
128+
let host = dst
129+
.host()
130+
.unwrap_or("")
131+
.trim_matches(|c| c == '[' || c == ']')
132+
.to_owned();
124133
let connecting = self.http.call(dst);
125134
let tls = self.tls.clone();
126135
let fut = async move {
127136
let tcp = connecting.await.map_err(Into::into)?;
128137
let maybe = if is_https {
129-
let tls = tls
130-
.connect(&host, tcp)
131-
.await?;
138+
let tls = tls.connect(&host, tcp).await?;
132139
MaybeHttpsStream::Https(tls)
133140
} else {
134141
MaybeHttpsStream::Http(tcp)
@@ -143,8 +150,7 @@ fn err<T>(e: BoxError) -> HttpsConnecting<T> {
143150
HttpsConnecting(Box::pin(async { Err(e) }))
144151
}
145152

146-
type BoxedFut<T> =
147-
Pin<Box<dyn Future<Output = Result<MaybeHttpsStream<T>, BoxError>> + Send>>;
153+
type BoxedFut<T> = Pin<Box<dyn Future<Output = Result<MaybeHttpsStream<T>, BoxError>> + Send>>;
148154

149155
/// A Future representing work to connect to a URL, and a TLS handshake.
150156
pub struct HttpsConnecting<T>(BoxedFut<T>);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! use hyper_tls::HttpsConnector;
1111
//! use hyper::Client;
1212
//!
13-
//! #[tokio::main]
13+
//! #[tokio::main(flavor = "current_thread")]
1414
//! async fn main() -> Result<(), Box<dyn std::error::Error>>{
1515
//! let https = HttpsConnector::new();
1616
//! let client = Client::builder().build::<_, hyper::Body>(https);

src/stream.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::fmt;
22
use std::io;
3+
use std::io::IoSlice;
34
use std::pin::Pin;
45
use std::task::{Context, Poll};
56

6-
use bytes::{Buf, BufMut};
77
use hyper::client::connect::{Connected, Connection};
8-
use tokio::io::{AsyncRead, AsyncWrite};
9-
pub use tokio_tls::TlsStream;
8+
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
9+
pub use tokio_native_tls::TlsStream;
1010

1111
/// A stream that might be protected with TLS.
1212
pub enum MaybeHttpsStream<T> {
@@ -40,37 +40,17 @@ impl<T> From<TlsStream<T>> for MaybeHttpsStream<T> {
4040
}
4141

4242
impl<T: AsyncRead + AsyncWrite + Unpin> AsyncRead for MaybeHttpsStream<T> {
43-
#[inline]
44-
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
45-
match self {
46-
MaybeHttpsStream::Http(s) => s.prepare_uninitialized_buffer(buf),
47-
MaybeHttpsStream::Https(s) => s.prepare_uninitialized_buffer(buf),
48-
}
49-
}
50-
5143
#[inline]
5244
fn poll_read(
5345
self: Pin<&mut Self>,
5446
cx: &mut Context,
55-
buf: &mut [u8],
56-
) -> Poll<Result<usize, io::Error>> {
47+
buf: &mut ReadBuf,
48+
) -> Poll<Result<(), io::Error>> {
5749
match Pin::get_mut(self) {
5850
MaybeHttpsStream::Http(s) => Pin::new(s).poll_read(cx, buf),
5951
MaybeHttpsStream::Https(s) => Pin::new(s).poll_read(cx, buf),
6052
}
6153
}
62-
63-
#[inline]
64-
fn poll_read_buf<B: BufMut>(
65-
self: Pin<&mut Self>,
66-
cx: &mut Context<'_>,
67-
buf: &mut B,
68-
) -> Poll<Result<usize, io::Error>> {
69-
match Pin::get_mut(self) {
70-
MaybeHttpsStream::Http(s) => Pin::new(s).poll_read_buf(cx, buf),
71-
MaybeHttpsStream::Https(s) => Pin::new(s).poll_read_buf(cx, buf),
72-
}
73-
}
7454
}
7555

7656
impl<T: AsyncWrite + AsyncRead + Unpin> AsyncWrite for MaybeHttpsStream<T> {
@@ -86,15 +66,21 @@ impl<T: AsyncWrite + AsyncRead + Unpin> AsyncWrite for MaybeHttpsStream<T> {
8666
}
8767
}
8868

89-
#[inline]
90-
fn poll_write_buf<B: Buf>(
69+
fn poll_write_vectored(
9170
self: Pin<&mut Self>,
9271
cx: &mut Context<'_>,
93-
buf: &mut B,
72+
bufs: &[IoSlice<'_>],
9473
) -> Poll<Result<usize, io::Error>> {
9574
match Pin::get_mut(self) {
96-
MaybeHttpsStream::Http(s) => Pin::new(s).poll_write_buf(cx, buf),
97-
MaybeHttpsStream::Https(s) => Pin::new(s).poll_write_buf(cx, buf),
75+
MaybeHttpsStream::Http(s) => Pin::new(s).poll_write_vectored(cx, bufs),
76+
MaybeHttpsStream::Https(s) => Pin::new(s).poll_write_vectored(cx, bufs),
77+
}
78+
}
79+
80+
fn is_write_vectored(&self) -> bool {
81+
match self {
82+
MaybeHttpsStream::Http(s) => s.is_write_vectored(),
83+
MaybeHttpsStream::Https(s) => s.is_write_vectored(),
9884
}
9985
}
10086

@@ -119,7 +105,7 @@ impl<T: AsyncRead + AsyncWrite + Connection + Unpin> Connection for MaybeHttpsSt
119105
fn connected(&self) -> Connected {
120106
match self {
121107
MaybeHttpsStream::Http(s) => s.connected(),
122-
MaybeHttpsStream::Https(s) => s.get_ref().connected(),
108+
MaybeHttpsStream::Https(s) => s.get_ref().get_ref().get_ref().connected(),
123109
}
124110
}
125111
}

0 commit comments

Comments
 (0)