Skip to content

Commit a4020ef

Browse files
CryZectz
authored andcommitted
Update Dependencies to support Rust Beta
Since async await is now stable on Rust Beta 1.39, using the futures ecosystem should work just fine, except there's still many crates that have #![feature(async_await)] somewhere in their dependency graph. Most crates have been updated by now. This updates hyper-rustls as well.
1 parent abd92a5 commit a4020ef

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ repository = "https://github.com/ctz/hyper-rustls"
1212
[dependencies]
1313
bytes = "0.4"
1414
ct-logs = { version = "^0.6.0", optional = true }
15-
futures-preview = { version = "0.3.0-alpha.18", features = ["async-await", "nightly"] }
16-
hyper = { version = "0.13.0-alpha.1", default-features = false }
15+
futures-util-preview = { version = "0.3.0-alpha.19" }
16+
hyper = { version = "0.13.0-alpha.4", default-features = false, features = ["unstable-stream"] }
1717
rustls = "0.16"
18-
tokio-io = { version="0.2.0-alpha.2" }
19-
tokio-rustls = "0.12.0-alpha.1"
18+
tokio-io = { version="0.2.0-alpha.6" }
19+
tokio-rustls = "0.12.0-alpha.4"
2020
webpki = "^0.21.0"
2121
rustls-native-certs = { version = "^0.1.0", optional = true }
2222

2323
[dev-dependencies]
24-
tokio = "0.2.0-alpha.2"
24+
tokio = "0.2.0-alpha.6"
2525

2626
[features]
2727
default = ["tokio-runtime"]

examples/client.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
//!
33
//! First parameter is the mandatory URL to GET.
44
//! Second parameter is an optional path to CA store.
5-
#![feature(async_await)]
6-
use futures::TryStreamExt;
7-
use hyper::{client, Uri, Body, Chunk};
5+
use futures_util::TryStreamExt;
6+
use hyper::{client, Body, Chunk, Uri};
87
use std::str::FromStr;
98
use std::{env, fs, io};
109

@@ -67,16 +66,18 @@ async fn run_client() -> io::Result<()> {
6766
// the returned headers, collects the whole body and prints it to
6867
// stdout.
6968
let fut = async move {
70-
let res = client.get(url).await.map_err(|e| {
71-
error(format!("Could not get: {:?}", e))
72-
})?;
69+
let res = client
70+
.get(url)
71+
.await
72+
.map_err(|e| error(format!("Could not get: {:?}", e)))?;
7373
println!("Status:\n{}", res.status());
7474
println!("Headers:\n{:#?}", res.headers());
7575

7676
let body: Body = res.into_body();
77-
let body: Chunk = body.try_concat().await.map_err(|e| {
78-
error(format!("Could not get body: {:?}", e))
79-
})?;
77+
let body: Chunk = body
78+
.try_concat()
79+
.await
80+
.map_err(|e| error(format!("Could not get body: {:?}", e)))?;
8081
println!("Body:\n{}", String::from_utf8_lossy(&body));
8182

8283
Ok(())

examples/server.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
//!
33
//! First parameter is the mandatory port to use.
44
//! Certificate and private key are hardcoded to sample files.
5-
#![feature(async_await)]
6-
use futures::{Stream, StreamExt, TryFutureExt, TryStreamExt};
5+
use core::task::{Context, Poll};
6+
use futures_util::{
7+
stream::{Stream, StreamExt},
8+
try_future::TryFutureExt,
9+
try_stream::TryStreamExt,
10+
};
711
use hyper::service::{make_service_fn, service_fn};
812
use hyper::{Body, Method, Request, Response, Server, StatusCode};
913
use rustls::internal::pemfile;
@@ -66,14 +70,33 @@ async fn run_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
6670
.boxed();
6771

6872
let service = make_service_fn(|_| async { Ok::<_, io::Error>(service_fn(echo)) });
69-
let server = Server::builder(incoming_tls_stream).serve(service);
73+
let server = Server::builder(HyperAcceptor {
74+
acceptor: incoming_tls_stream,
75+
})
76+
.serve(service);
7077

7178
// Run the future, keep going until an error occurs.
7279
println!("Starting to serve on https://{}.", addr);
7380
server.await?;
7481
Ok(())
7582
}
7683

84+
struct HyperAcceptor {
85+
acceptor: Pin<Box<dyn Stream<Item = Result<TlsStream<TcpStream>, io::Error>>>>,
86+
}
87+
88+
impl hyper::server::accept::Accept for HyperAcceptor {
89+
type Conn = TlsStream<TcpStream>;
90+
type Error = io::Error;
91+
92+
fn poll_accept(
93+
mut self: Pin<&mut Self>,
94+
cx: &mut Context,
95+
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
96+
Pin::new(&mut self.acceptor).poll_next(cx)
97+
}
98+
}
99+
77100
// Custom echo service, handling two different routes and a
78101
// catch-all 404 responder.
79102
async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {

src/connector.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use futures::FutureExt;
1+
use futures_util::FutureExt;
22
use hyper::client::connect::{self, Connect};
33
#[cfg(feature = "tokio-runtime")]
44
use hyper::client::HttpConnector;
@@ -70,7 +70,16 @@ where
7070
{
7171
type Transport = MaybeHttpsStream<T::Transport>;
7272
type Error = io::Error;
73-
type Future = Pin<Box<dyn Future<Output = Result<(MaybeHttpsStream<T::Transport>, connect::Connected), io::Error>> + Send>>;
73+
type Future = Pin<
74+
Box<
75+
dyn Future<
76+
Output = Result<
77+
(MaybeHttpsStream<T::Transport>, connect::Connected),
78+
io::Error,
79+
>,
80+
> + Send,
81+
>,
82+
>;
7483

7584
fn connect(&self, dst: connect::Destination) -> Self::Future {
7685
let is_https = dst.scheme() == "https";
@@ -92,12 +101,12 @@ where
92101
let f = async move {
93102
let (tcp, conn) = connecting_future.await?;
94103
let connector = TlsConnector::from(cfg);
95-
let dnsname = DNSNameRef::try_from_ascii_str(&hostname).map_err(|_| {
96-
io::Error::new(io::ErrorKind::Other, "invalid dnsname")
97-
})?;
98-
let tls = connector.connect(dnsname, tcp).await.map_err(|e| {
99-
io::Error::new(io::ErrorKind::Other, e)
100-
})?;
104+
let dnsname = DNSNameRef::try_from_ascii_str(&hostname)
105+
.map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid dnsname"))?;
106+
let tls = connector
107+
.connect(dnsname, tcp)
108+
.await
109+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
101110
let connected = if tls.get_ref().1.get_alpn_protocol() == Some(b"h2") {
102111
conn.negotiated_h2()
103112
} else {

0 commit comments

Comments
 (0)