Skip to content

Commit e518c24

Browse files
PaulDancekornelski
authored andcommitted
Refactor!: Remove strict TokioIo response requirement from hyper_boring::v1::HttpsConnector
Closes #295. Signed-off-by: Paul Mabileau <[email protected]>
1 parent 4685af0 commit e518c24

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ hyper_old = { package = "hyper", version = "0.14", default-features = false }
4646
linked_hash_set = "0.1"
4747
once_cell = "1.0"
4848
openssl-macros = "0.1.1"
49-
tower = "0.4"
49+
tower = { version = "0.4", default-features = false, features = ["util"] }
5050
tower-layer = "0.3"
5151
tower-service = "0.3"
5252
autocfg = "1.3.0"

hyper-boring/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1717
[features]
1818
default = ["runtime"]
1919

20-
runtime = ["hyper_old/runtime"]
20+
runtime = ["hyper_old/runtime", "dep:tower"]
2121

2222
# Use a FIPS-validated version of boringssl.
2323
fips = ["tokio-boring/fips"]
@@ -43,6 +43,7 @@ once_cell = { workspace = true }
4343
boring = { workspace = true }
4444
tokio = { workspace = true }
4545
tokio-boring = { workspace = true }
46+
tower = { workspace = true, optional = true }
4647
tower-layer = { workspace = true }
4748
tower-service = { workspace = true, optional = true }
4849

hyper-boring/src/v1.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ use std::sync::Arc;
1919
use std::task::{Context, Poll};
2020
use std::{io, net};
2121
use tokio::io::{AsyncRead, AsyncWrite};
22+
use tokio::net::TcpStream;
23+
#[cfg(feature = "runtime")]
24+
use tower::util::MapResponse;
25+
#[cfg(feature = "runtime")]
26+
use tower::ServiceExt;
2227
use tower_layer::Layer;
2328
use tower_service::Service;
2429

@@ -29,32 +34,41 @@ pub struct HttpsConnector<T> {
2934
inner: Inner,
3035
}
3136

37+
/// Specialized version of [`HttpConnector`] with responses wrapped with
38+
/// [`TokioIo::new`] in order to bring back compatibility with Tokio traits.
39+
pub type TokioHttpConnector =
40+
MapResponse<HttpConnector, fn(TokioIo<TcpStream>) -> TokioIo<TokioIo<TcpStream>>>;
41+
3242
#[cfg(feature = "runtime")]
33-
impl HttpsConnector<HttpConnector> {
43+
impl HttpsConnector<TokioHttpConnector> {
3444
/// Creates a a new `HttpsConnector` using default settings.
3545
///
3646
/// The Hyper `HttpConnector` is used to perform the TCP socket connection. ALPN is configured to support both
3747
/// HTTP/2 and HTTP/1.1.
3848
///
3949
/// Requires the `runtime` Cargo feature.
40-
pub fn new() -> Result<HttpsConnector<HttpConnector>, ErrorStack> {
50+
pub fn new() -> Result<Self, ErrorStack> {
4151
let mut http = HttpConnector::new();
4252
http.enforce_http(false);
4353

44-
HttpsLayer::new().map(|l| l.layer(http))
54+
HttpsLayer::new().map(|l| l.layer(http.map_response(TokioIo::new as _)))
4555
}
4656
}
4757

4858
impl<S, T> HttpsConnector<S>
4959
where
50-
S: Service<Uri, Response = TokioIo<T>> + Send,
60+
S: Service<Uri, Response = T> + Send,
5161
S::Error: Into<Box<dyn Error + Send + Sync>>,
5262
S::Future: Unpin + Send + 'static,
5363
T: AsyncRead + AsyncWrite + Connection + Unpin + fmt::Debug + Sync + Send + 'static,
5464
{
5565
/// Creates a new `HttpsConnector`.
5666
///
5767
/// The session cache configuration of `ssl` will be overwritten.
68+
///
69+
/// If the provided service's response type does not fit the trait
70+
/// requirements because it is closer to the Hyper ecosystem than the Tokio
71+
/// one, wrapping your responses with [`TokioIo`] should work.
5872
pub fn with_connector(
5973
http: S,
6074
ssl: SslConnectorBuilder,
@@ -215,7 +229,7 @@ impl Inner {
215229

216230
impl<T, S> Service<Uri> for HttpsConnector<S>
217231
where
218-
S: Service<Uri, Response = TokioIo<T>> + Send,
232+
S: Service<Uri, Response = T> + Send,
219233
S::Error: Into<Box<dyn Error + Send + Sync>>,
220234
S::Future: Unpin + Send + 'static,
221235
T: AsyncRead + AsyncWrite + Connection + Unpin + fmt::Debug + Sync + Send + 'static,
@@ -244,7 +258,7 @@ where
244258
let connect = self.http.call(uri);
245259

246260
let f = async {
247-
let conn = connect.await.map_err(Into::into)?.into_inner();
261+
let conn = connect.await.map_err(Into::into)?;
248262

249263
let (inner, uri) = match tls_setup {
250264
Some((inner, uri)) => (inner, uri),

hyper-boring/tests/v1.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use hyper_util::rt::{TokioExecutor, TokioIo};
1212
use std::convert::Infallible;
1313
use std::{io, iter};
1414
use tokio::net::TcpListener;
15+
use tower::ServiceExt;
1516

1617
#[tokio::test]
1718
async fn google() {
@@ -83,7 +84,7 @@ async fn localhost() {
8384
let _ = writeln!(&file, "{}", line);
8485
});
8586

86-
let ssl = HttpsConnector::with_connector(connector, ssl).unwrap();
87+
let ssl = HttpsConnector::with_connector(connector.map_response(TokioIo::new), ssl).unwrap();
8788
let client = Client::builder(TokioExecutor::new()).build::<_, Empty<Bytes>>(ssl);
8889

8990
for _ in 0..3 {
@@ -144,7 +145,8 @@ async fn alpn_h2() {
144145

145146
ssl.set_ca_file("tests/test/root-ca.pem").unwrap();
146147

147-
let mut ssl = HttpsConnector::with_connector(connector, ssl).unwrap();
148+
let mut ssl =
149+
HttpsConnector::with_connector(connector.map_response(TokioIo::new), ssl).unwrap();
148150

149151
ssl.set_ssl_callback(|ssl, _| ssl.set_alpn_protos(b"\x02h2\x08http/1.1"));
150152

0 commit comments

Comments
 (0)