Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare 0.27.5 release #296

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyper-rustls"
version = "0.27.4"
version = "0.27.5"
edition = "2021"
rust-version = "1.71"
license = "Apache-2.0 OR ISC OR MIT"
Expand Down Expand Up @@ -38,6 +38,7 @@ webpki-roots = { version = "0.26", optional = true }
futures-util = { version = "0.3", default-features = false }

[dev-dependencies]
cfg-if = "1"
http-body-util = "0.1"
hyper-util = { version = "0.1", default-features = false, features = ["server-auto"] }
rustls = { version = "0.23", default-features = false, features = ["tls12"] }
Expand Down
108 changes: 51 additions & 57 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,90 +213,84 @@ mod tests {
use std::future::poll_fn;

use http::Uri;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::rt::TokioIo;
use tokio::net::TcpStream;
use tower_service::Service;

use super::HttpsConnector;
use crate::{ConfigBuilderExt, HttpsConnectorBuilder};

fn tls_config() -> rustls::ClientConfig {
#[cfg(feature = "rustls-native-certs")]
return rustls::ClientConfig::builder()
.with_native_roots()
.unwrap()
.with_no_client_auth();

#[cfg(feature = "webpki-roots")]
return rustls::ClientConfig::builder()
.with_webpki_roots()
.with_no_client_auth();

#[cfg(feature = "rustls-platform-verifier")]
return rustls::ClientConfig::builder()
.with_platform_verifier()
.with_no_client_auth();
}

fn https_or_http_connector() -> HttpsConnector<HttpConnector> {
HttpsConnectorBuilder::new()
.with_tls_config(tls_config())
.https_or_http()
.enable_http1()
.build()
}

fn https_only_connector() -> HttpsConnector<HttpConnector> {
HttpsConnectorBuilder::new()
.with_tls_config(tls_config())
.https_only()
.enable_http1()
.build()
}

async fn oneshot<S, Req>(mut service: S, req: Req) -> Result<S::Response, S::Error>
where
S: Service<Req>,
{
poll_fn(|cx| service.poll_ready(cx)).await?;
service.call(req).await
}

fn https_uri() -> Uri {
Uri::from_static("https://google.com")
}

fn http_uri() -> Uri {
Uri::from_static("http://google.com")
}
use super::*;
use crate::{ConfigBuilderExt, HttpsConnectorBuilder, MaybeHttpsStream};

#[tokio::test]
async fn connects_https() {
oneshot(https_or_http_connector(), https_uri())
connect(Allow::Any, Scheme::Https)
.await
.unwrap();
}

#[tokio::test]
async fn connects_http() {
oneshot(https_or_http_connector(), http_uri())
connect(Allow::Any, Scheme::Http)
.await
.unwrap();
}

#[tokio::test]
async fn connects_https_only() {
oneshot(https_only_connector(), https_uri())
connect(Allow::Https, Scheme::Https)
.await
.unwrap();
}

#[tokio::test]
async fn enforces_https_only() {
let message = oneshot(https_only_connector(), http_uri())
let message = connect(Allow::Https, Scheme::Http)
.await
.unwrap_err()
.to_string();

assert_eq!(message, "unsupported scheme http");
}

async fn connect(
allow: Allow,
scheme: Scheme,
) -> Result<MaybeHttpsStream<TokioIo<TcpStream>>, BoxError> {
let config_builder = rustls::ClientConfig::builder();
ctz marked this conversation as resolved.
Show resolved Hide resolved
cfg_if::cfg_if! {
if #[cfg(feature = "rustls-platform-verifier")] {
let config_builder = config_builder.with_platform_verifier();
} else if #[cfg(feature = "rustls-native-certs")] {
let config_builder = config_builder.with_native_roots().unwrap();
} else if #[cfg(feature = "webpki-roots")] {
let config_builder = config_builder.with_webpki_roots();
}
}
let config = config_builder.with_no_client_auth();

let builder = HttpsConnectorBuilder::new().with_tls_config(config);
let mut service = match allow {
Allow::Https => builder.https_only(),
Allow::Any => builder.https_or_http(),
}
.enable_http1()
.build();

poll_fn(|cx| service.poll_ready(cx)).await?;
service
.call(Uri::from_static(match scheme {
Scheme::Https => "https://google.com",
Scheme::Http => "http://google.com",
}))
.await
}

enum Allow {
Https,
Any,
}

enum Scheme {
Https,
Http,
}
}
Loading