Skip to content

Commit

Permalink
Upgrade hyper
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Oct 1, 2019
1 parent 876cd0e commit f988e93
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ runtime = ["hyper/runtime"]
[dependencies]
antidote = "1.0.0"
bytes = "0.4"
hyper = { version = "=0.13.0-alpha.1", default-features = false }
hyper = { version = "=0.13.0-alpha.4", default-features = false }
linked_hash_set = "0.1"
once_cell = "1.0"
openssl = "0.10.19"
openssl-sys = "0.9.26"
tokio = "=0.2.0-alpha.4"
tokio-openssl = "=0.4.0-alpha.4"
tokio = "=0.2.0-alpha.6"
tokio-openssl = "=0.4.0-alpha.6"

[dev-dependencies]
futures-preview = "=0.3.0-alpha.18"
hyper = "=0.13.0-alpha.1"
hyper = "=0.13.0-alpha.4"
36 changes: 27 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use hyper::client::connect::{Connect, Connected, Destination};
use hyper::client::HttpConnector;
use openssl::error::ErrorStack;
use openssl::ex_data::Index;
#[cfg(feature = "runtime")]
use openssl::ssl::SslMethod;
use openssl::ssl::{
ConnectConfiguration, Ssl, SslConnector, SslConnectorBuilder, SslSessionCacheMode,
};
#[cfg(feature = "runtime")]
use openssl::ssl::SslMethod;
use std::error::Error;
use std::fmt::Debug;
use std::io;
Expand All @@ -22,10 +22,10 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tokio_openssl::SslStream;

use cache::{SessionCache, SessionKey};
use once_cell::sync::OnceCell;
use std::future::Future;
use std::pin::Pin;
use std::task::{Poll, Context};
use once_cell::sync::OnceCell;
use std::task::{Context, Poll};

mod cache;
#[cfg(test)]
Expand All @@ -41,7 +41,9 @@ struct Inner {
ssl: SslConnector,
cache: Arc<Mutex<SessionCache>>,
callback: Option<
Arc<dyn Fn(&mut ConnectConfiguration, &Destination) -> Result<(), ErrorStack> + Sync + Send>,
Arc<
dyn Fn(&mut ConnectConfiguration, &Destination) -> Result<(), ErrorStack> + Sync + Send,
>,
>,
}

Expand Down Expand Up @@ -218,14 +220,22 @@ where
}
}

fn poll_read(mut self: Pin<&mut Self>, ctx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
fn poll_read(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_read(ctx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_read(ctx, buf),
}
}

fn poll_read_buf<B>(mut self: Pin<&mut Self>, ctx: &mut Context<'_>, buf: &mut B) -> Poll<io::Result<usize>>
fn poll_read_buf<B>(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<usize>>
where
B: BufMut,
{
Expand All @@ -240,7 +250,11 @@ impl<T> AsyncWrite for MaybeHttpsStream<T>
where
T: AsyncRead + AsyncWrite + Unpin,
{
fn poll_write(mut self: Pin<&mut Self>, ctx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
fn poll_write(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_write(ctx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_write(ctx, buf),
Expand All @@ -261,7 +275,11 @@ where
}
}

fn poll_write_buf<B>(mut self: Pin<&mut Self>, ctx: &mut Context<'_>, buf: &mut B) -> Poll<io::Result<usize>>
fn poll_write_buf<B>(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<usize>>
where
B: Buf,
{
Expand Down
51 changes: 35 additions & 16 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use hyper::client::HttpConnector;
use futures::future;
use futures::stream::TryStreamExt;
use hyper::server::conn::Http;
use hyper::{service, Response};
use tokio::net::TcpListener;
use hyper::{Body, Client};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use hyper::server::conn::Http;
use tokio::net::TcpListener;

use super::*;

Expand All @@ -16,9 +14,13 @@ async fn google() {
let client = Client::builder().keep_alive(false).build::<_, Body>(ssl);

for _ in 0..3 {
let resp = client.get("https://www.google.com".parse().unwrap()).await.unwrap();
let resp = client
.get("https://www.google.com".parse().unwrap())
.await
.unwrap();
assert!(resp.status().is_success(), "{}", resp.status());
resp.into_body().try_concat().await.unwrap();
let mut body = resp.into_body();
while let Some(_) = body.next().await.transpose().unwrap() {}
}
}

Expand All @@ -30,15 +32,20 @@ async fn localhost() {
let server = async move {
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
acceptor.set_session_id_context(b"test").unwrap();
acceptor.set_private_key_file("test/key.pem", SslFiletype::PEM).unwrap();
acceptor.set_certificate_chain_file("test/cert.pem").unwrap();
acceptor
.set_private_key_file("test/key.pem", SslFiletype::PEM)
.unwrap();
acceptor
.set_certificate_chain_file("test/cert.pem")
.unwrap();
let acceptor = acceptor.build();

for _ in 0..3 {
let stream = listener.accept().await.unwrap().0;
let stream = tokio_openssl::accept(&acceptor, stream).await.unwrap();

let service = service::service_fn(|_| future::ready(Ok::<_, io::Error>(Response::new(Body::empty()))));
let service =
service::service_fn(|_| async { Ok::<_, io::Error>(Response::new(Body::empty())) });

Http::new()
.keep_alive(false)
Expand Down Expand Up @@ -69,9 +76,13 @@ async fn localhost() {
let client = Client::builder().build::<_, Body>(ssl);

for _ in 0..3 {
let resp = client.get(format!("https://localhost:{}", port).parse().unwrap()).await.unwrap();
let resp = client
.get(format!("https://localhost:{}", port).parse().unwrap())
.await
.unwrap();
assert!(resp.status().is_success(), "{}", resp.status());
resp.into_body().try_concat().await.unwrap();
let mut body = resp.into_body();
while let Some(_) = body.next().await.transpose().unwrap() {}
}
}

Expand All @@ -85,8 +96,11 @@ async fn alpn_h2() {

let server = async move {
let mut acceptor = SslAcceptor::mozilla_modern(SslMethod::tls()).unwrap();
acceptor.set_certificate_chain_file("test/cert.pem").unwrap();
acceptor.set_private_key_file("test/key.pem", SslFiletype::PEM)
acceptor
.set_certificate_chain_file("test/cert.pem")
.unwrap();
acceptor
.set_private_key_file("test/key.pem", SslFiletype::PEM)
.unwrap();
acceptor.set_alpn_select_callback(|_, client| {
ssl::select_next_proto(b"\x02h2", client).ok_or(AlpnError::NOACK)
Expand All @@ -97,7 +111,8 @@ async fn alpn_h2() {
let stream = tokio_openssl::accept(&acceptor, stream).await.unwrap();
assert_eq!(stream.ssl().selected_alpn_protocol().unwrap(), b"h2");

let service = service::service_fn(|_| future::ready(Ok::<_, io::Error>(Response::new(Body::empty()))));
let service =
service::service_fn(|_| async { Ok::<_, io::Error>(Response::new(Body::empty())) });

Http::new()
.http2_only(true)
Expand All @@ -116,7 +131,11 @@ async fn alpn_h2() {
let ssl = HttpsConnector::with_connector(connector, ssl).unwrap();
let client = Client::builder().build::<_, Body>(ssl);

let resp = client.get(format!("https://localhost:{}", port).parse().unwrap()).await.unwrap();
let resp = client
.get(format!("https://localhost:{}", port).parse().unwrap())
.await
.unwrap();
assert!(resp.status().is_success(), "{}", resp.status());
resp.into_body().try_concat().await.unwrap();
let mut body = resp.into_body();
while let Some(_) = body.next().await.transpose().unwrap() {}
}

0 comments on commit f988e93

Please sign in to comment.