From 1945fa06755555dfd96eb1de5b02d6ee40a81f22 Mon Sep 17 00:00:00 2001 From: morenol <22335041+morenol@users.noreply.github.com> Date: Tue, 5 Dec 2023 20:18:17 -0400 Subject: [PATCH] feat: added suport to `http` crate `1.0` (#508) * chore: use same feature * test: add test for new http version added * stylistic review --------- Co-authored-by: Luis Moreno Co-authored-by: Rob Ede --- actix-tls/CHANGES.md | 2 ++ actix-tls/Cargo.toml | 3 ++- actix-tls/src/connect/uri.rs | 17 +++++++++++++++-- actix-tls/tests/test_connect.rs | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/actix-tls/CHANGES.md b/actix-tls/CHANGES.md index 295d150b..25e8e56e 100644 --- a/actix-tls/CHANGES.md +++ b/actix-tls/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Added support to `http` crate version `1.0`. + ## 3.1.1 - Fix `rustls` v0.21 version requirement. diff --git a/actix-tls/Cargo.toml b/actix-tls/Cargo.toml index 7d0ebfa5..3b8d3ae8 100755 --- a/actix-tls/Cargo.toml +++ b/actix-tls/Cargo.toml @@ -56,7 +56,7 @@ rustls-0_21 = ["tokio-rustls-024", "webpki-roots-025"] native-tls = ["tokio-native-tls"] # support http::Uri as connect address -uri = ["http"] +uri = ["http", "http-1"] [dependencies] actix-rt = { version = "2.2", default-features = false } @@ -72,6 +72,7 @@ tracing = { version = "0.1.30", default-features = false, features = ["log"] } # uri http = { version = "0.2.3", optional = true } +http-1 = { package = "http", version = "1", optional = true } # openssl tls-openssl = { package = "openssl", version = "0.10.55", optional = true } diff --git a/actix-tls/src/connect/uri.rs b/actix-tls/src/connect/uri.rs index b1c7f0fe..d260bab9 100644 --- a/actix-tls/src/connect/uri.rs +++ b/actix-tls/src/connect/uri.rs @@ -1,8 +1,21 @@ -use http::Uri; +use http as http_02; use super::Host; -impl Host for Uri { +impl Host for http_02::Uri { + fn hostname(&self) -> &str { + self.host().unwrap_or("") + } + + fn port(&self) -> Option { + match self.port_u16() { + Some(port) => Some(port), + None => scheme_to_port(self.scheme_str()), + } + } +} + +impl Host for http_1::Uri { fn hostname(&self) -> &str { self.host().unwrap_or("") } diff --git a/actix-tls/tests/test_connect.rs b/actix-tls/tests/test_connect.rs index 1c969fec..8cf8d614 100644 --- a/actix-tls/tests/test_connect.rs +++ b/actix-tls/tests/test_connect.rs @@ -114,6 +114,23 @@ async fn test_openssl_uri() { assert_eq!(con.peer_addr().unwrap(), srv.addr()); } +#[cfg(all(feature = "rustls-0_21", feature = "uri"))] +#[actix_rt::test] +async fn test_rustls_uri_http1() { + let srv = TestServer::start(|| { + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) + }) + }); + + let conn = Connector::default().service(); + let addr = http_1::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap(); + let con = conn.call(addr.into()).await.unwrap(); + assert_eq!(con.peer_addr().unwrap(), srv.addr()); +} + #[cfg(all(feature = "rustls-0_21", feature = "uri"))] #[actix_rt::test] async fn test_rustls_uri() {