Skip to content

Commit

Permalink
hyper-boring: update to hyper 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Nov 15, 2023
1 parent cdb76dc commit 2579aaa
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 56 deletions.
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
resolver = "2"

[workspace.package]
version = "4.0.0"
version = "5.0.0"
repository = "https://github.com/cloudflare/boring"
edition = "2021"

Expand All @@ -19,9 +19,9 @@ tag-prefix = ""
publish = false

[workspace.dependencies]
boring-sys = { version = "4.0.0", path = "./boring-sys" }
boring = { version = "4.0.0", path = "./boring" }
tokio-boring = { version = "4.0.0", path = "./tokio-boring" }
boring-sys = { version = "5.0.0", path = "./boring-sys" }
boring = { version = "5.0.0", path = "./boring" }
tokio-boring = { version = "5.0.0", path = "./tokio-boring" }

bindgen = { version = "0.68.1", default-features = false, features = ["runtime"] }
cmake = "0.1.18"
Expand All @@ -36,8 +36,11 @@ futures = "0.3"
tokio = "1"
anyhow = "1"
antidote = "1.0.0"
http = "0.2"
hyper = { version = "0.14", default-features = false }
http = "1.0"
hyper = { version = "1.0", default-features = false }
hyper-util = { git = "https://github.com/hyperium/hyper-util" }
http-body-util = "0.1.0"
linked_hash_set = "0.1"
once_cell = "1.0"
tower-layer = "0.3"
tower-service = "0.3"
10 changes: 6 additions & 4 deletions hyper-boring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ features = ["rpk", "pq-experimental"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["runtime"]

runtime = ["hyper/runtime"]
default = []

# Use a FIPS-validated version of boringssl.
fips = ["tokio-boring/fips"]
Expand All @@ -31,15 +29,19 @@ pq-experimental = ["tokio-boring/pq-experimental"]
[dependencies]
antidote = { workspace = true }
http = { workspace = true }
hyper = { workspace = true, features = ["client"] }
hyper = { workspace = true, default-features = false }
hyper-util = { workspace = true, features = ["client"] }
linked_hash_set = { workspace = true }
once_cell = { workspace = true }
boring = { workspace = true }
tokio = { workspace = true }
tokio-boring = { workspace = true }
tower-layer = { workspace = true }
tower-service = { workspace = true }

[dev-dependencies]
hyper = { workspace = true, features = [ "full" ] }
tokio = { workspace = true, features = [ "full" ] }
futures = { workspace = true }
hyper-util = { workspace = true, features = ["full"] }
http-body-util = { workspace = true }
99 changes: 79 additions & 20 deletions hyper-boring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use boring::ssl::{
ConnectConfiguration, Ssl, SslConnector, SslConnectorBuilder, SslMethod, SslSessionCacheMode,
};
use http::uri::Scheme;
use hyper::client::connect::{Connected, Connection};
#[cfg(feature = "runtime")]
use hyper::client::HttpConnector;
use hyper::service::Service;
use hyper::rt::{Read, ReadBufCursor, Write};
use hyper::Uri;
use hyper_util::client::connect::HttpConnector;
use hyper_util::client::connect::{Connected, Connection};
use hyper_util::rt::TokioIo;
use once_cell::sync::OnceCell;
use std::fmt::Debug;
use std::future::Future;
Expand All @@ -27,6 +27,7 @@ use std::{error::Error, fmt};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio_boring::SslStream;
use tower_layer::Layer;
use tower_service::Service;

mod cache;
#[cfg(test)]
Expand Down Expand Up @@ -148,7 +149,6 @@ pub struct HttpsConnector<T> {
inner: Inner,
}

#[cfg(feature = "runtime")]
impl HttpsConnector<HttpConnector> {
/// Creates a a new `HttpsConnector` using default settings.
///
Expand All @@ -169,7 +169,7 @@ where
S: Service<Uri, Response = T> + Send,
S::Error: Into<Box<dyn Error + Send + Sync>>,
S::Future: Unpin + Send + 'static,
T: AsyncRead + AsyncWrite + Connection + Unpin + Debug + Sync + Send + 'static,
T: Read + Write + Connection + Unpin + Debug + Sync + Send + 'static,
{
/// Creates a new `HttpsConnector`.
///
Expand All @@ -195,9 +195,9 @@ where
S: Service<Uri> + Send,
S::Error: Into<Box<dyn Error + Send + Sync>>,
S::Future: Unpin + Send + 'static,
S::Response: AsyncRead + AsyncWrite + Connection + Unpin + Debug + Sync + Send + 'static,
S::Response: Read + Write + Connection + Unpin + Debug + Sync + Send + 'static,
{
type Response = MaybeHttpsStream<S::Response>;
type Response = MaybeHttpsStream<TokioIo<S::Response>>;
type Error = Box<dyn Error + Sync + Send>;
#[allow(clippy::type_complexity)]
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
Expand All @@ -221,8 +221,8 @@ where
let connect = self.http.call(uri);

let f = async {
let conn = connect.await.map_err(Into::into)?;

let conn = connect.await.map_err(Into::into)?; // This returns a hyper type
let conn = TokioIo::new(conn); // Turn it into a tokio one
let (inner, uri) = match tls_setup {
Some((inner, uri)) => (inner, uri),
None => return Ok(MaybeHttpsStream::Http(conn)),
Expand All @@ -246,7 +246,6 @@ where

let config = inner.setup_ssl(&uri, host)?;
let stream = tokio_boring::connect(config, host, conn).await?;

Ok(MaybeHttpsStream::Https(stream))
};

Expand All @@ -262,9 +261,67 @@ pub enum MaybeHttpsStream<T> {
Https(SslStream<T>),
}

impl<T> AsyncRead for MaybeHttpsStream<T>
impl<T> hyper::rt::Read for MaybeHttpsStream<T>
where
T: AsyncRead + AsyncWrite + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
ctx: &mut Context<'_>,
buf: ReadBufCursor,
) -> Poll<io::Result<()>> {
match &mut *self {
MaybeHttpsStream::Http(s) => {
hyper::rt::Read::poll_read(Pin::new(&mut TokioIo::new(s)), ctx, buf)
}
MaybeHttpsStream::Https(s) => {
hyper::rt::Read::poll_read(Pin::new(&mut TokioIo::new(s)), ctx, buf)
}
}
}
}

impl<T> hyper::rt::Write 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>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_write(ctx, buf),
MaybeHttpsStream::Https(s) => {
hyper::rt::Write::poll_write(Pin::new(&mut TokioIo::new(s)), ctx, buf)
}
}
}

fn poll_flush(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<io::Result<()>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_flush(ctx),
MaybeHttpsStream::Https(s) => {
hyper::rt::Write::poll_flush(Pin::new(&mut TokioIo::new(s)), ctx)
}
}
}

fn poll_shutdown(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<io::Result<()>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_shutdown(ctx),
MaybeHttpsStream::Https(s) => {
hyper::rt::Write::poll_shutdown(Pin::new(&mut TokioIo::new(s)), ctx)
}
}
}

// TODO: implement poll_write_vectored
}

impl<T> AsyncRead for MaybeHttpsStream<T>
where
T: AsyncRead + Unpin,
{
fn poll_read(
mut self: Pin<&mut Self>,
Expand All @@ -273,14 +330,14 @@ where
) -> Poll<io::Result<()>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_read(ctx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_read(ctx, buf),
MaybeHttpsStream::Https(s) => Pin::new(s.get_mut()).poll_read(ctx, buf),
}
}
}

impl<T> AsyncWrite for MaybeHttpsStream<T>
where
T: AsyncRead + AsyncWrite + Unpin,
T: AsyncWrite + Unpin,
{
fn poll_write(
mut self: Pin<&mut Self>,
Expand All @@ -289,34 +346,36 @@ where
) -> 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),
MaybeHttpsStream::Https(s) => Pin::new(s.get_mut()).poll_write(ctx, buf),
}
}

fn poll_flush(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<io::Result<()>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_flush(ctx),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_flush(ctx),
MaybeHttpsStream::Https(s) => Pin::new(s.get_mut()).poll_flush(ctx),
}
}

fn poll_shutdown(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<io::Result<()>> {
match &mut *self {
MaybeHttpsStream::Http(s) => Pin::new(s).poll_shutdown(ctx),
MaybeHttpsStream::Https(s) => Pin::new(s).poll_shutdown(ctx),
MaybeHttpsStream::Https(s) => Pin::new(s.get_mut()).poll_shutdown(ctx),
}
}

// TODO: implement poll_write_vectored
}

impl<T> Connection for MaybeHttpsStream<T>
impl<T> Connection for MaybeHttpsStream<TokioIo<T>>
where
T: Connection,
{
fn connected(&self) -> Connected {
match self {
MaybeHttpsStream::Http(s) => s.connected(),
MaybeHttpsStream::Http(s) => s.inner().connected(),
MaybeHttpsStream::Https(s) => {
let mut connected = s.get_ref().connected();
let mut connected = s.get_ref().inner().connected();

if s.ssl().selected_alpn_protocol() == Some(b"h2") {
connected = connected.negotiated_h2();
Expand Down
Loading

0 comments on commit 2579aaa

Please sign in to comment.