Skip to content

Commit

Permalink
add builder
Browse files Browse the repository at this point in the history
  • Loading branch information
goatgoose committed Dec 11, 2024
1 parent aeb4710 commit 126cef1
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions bindings/rust/s2n-tls-hyper/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use hyper_util::{
};
use s2n_tls::{config::Config, connection};
use s2n_tls_tokio::TlsConnector;
use std::marker::PhantomData;
use std::{
future::Future,
pin::Pin,
Expand All @@ -34,10 +35,25 @@ where
ConnBuilder: connection::Builder,
<ConnBuilder as connection::Builder>::Output: Unpin,
{
/// Creates a new `HttpsConnector` with the default configuration. See `Builder::build` for
/// details.
/// Creates a new `HttpsConnector` with the default configuration. Use `HttpsConnector::builder`
/// instead to configure an `HttpsConnector`.
///
/// `conn_builder` will be used to produce the s2n-tls Connections used for negotiating HTTPS,
/// which can be an `s2n_tls::config::Config` or other `s2n_tls::connection::Builder`.
///
/// ```
/// use s2n_tls_hyper::connector::HttpsConnector;
/// use s2n_tls::config::Config;
///
/// // Create a new HttpsConnector.
/// let connector = HttpsConnector::new(Config::default());
/// ```
///
/// Note that s2n-tls-hyper will override the ALPN extension to negotiate HTTP. Any ALPN values
/// configured on `conn_builder` with APIs like
/// `s2n_tls::config::Builder::set_application_protocol_preference()` will be ignored.
pub fn new(conn_builder: ConnBuilder) -> HttpsConnector<HttpConnector, ConnBuilder> {
HttpsConnector::<HttpConnector, ConnBuilder>::builder().build(conn_builder)
HttpsConnector::builder().build(conn_builder)
}
}

Expand All @@ -58,16 +74,22 @@ where
/// // Build a new HttpsConnector.
/// let connector = builder.build(Config::default());
/// ```
pub fn builder() -> Builder {
Builder {}
pub fn builder() -> Builder<HttpConnector, ConnBuilder> {
Builder {
http: PhantomData,
conn_builder: PhantomData,
}
}
}

/// Builder used to configure an `HttpsConnector`. Create a new Builder with
/// `HttpsConnector::builder`.
pub struct Builder;
pub struct Builder<Http, ConnBuilder> {
http: PhantomData<Http>,
conn_builder: PhantomData<ConnBuilder>,
}

impl Builder {
impl<ConnBuilder> Builder<HttpConnector, ConnBuilder> {
/// Builds a new `HttpsConnector`.
///
/// `conn_builder` will be used to produce the s2n-tls Connections used for negotiating HTTPS,
Expand All @@ -79,13 +101,7 @@ impl Builder {
/// Note that s2n-tls-hyper will override the ALPN extension to negotiate HTTP. Any ALPN values
/// configured on `conn_builder` with APIs like
/// `s2n_tls::config::Builder::set_application_protocol_preference()` will be ignored.
pub fn build<ConnBuilder>(
self,
conn_builder: ConnBuilder,
) -> HttpsConnector<HttpConnector, ConnBuilder>
where
ConnBuilder: connection::Builder,
{
pub fn build(self, conn_builder: ConnBuilder) -> HttpsConnector<HttpConnector, ConnBuilder> {
let mut http = HttpConnector::new();

// By default, the `HttpConnector` only allows the HTTP URI scheme to be used. To negotiate
Expand All @@ -94,7 +110,9 @@ impl Builder {

self.build_with_http(http, conn_builder)
}
}

impl<Http, ConnBuilder> Builder<Http, ConnBuilder> {
/// Builds a new `HttpsConnector`.
///
/// `conn_builder` will be used to produce the s2n-tls Connections used for negotiating HTTPS,
Expand All @@ -116,19 +134,16 @@ impl Builder {
/// .build_with_http(http, Config::default());
/// ```
///
/// `HttpsConnector::new()` can be used to create the HTTP connector automatically.
/// `Builder::build()` can be used to create the HTTP connector automatically.
///
/// Note that s2n-tls-hyper will override the ALPN extension to negotiate HTTP. Any ALPN values
/// configured on `conn_builder` with APIs like
/// `s2n_tls::config::Builder::set_application_protocol_preference()` will be ignored.
pub fn build_with_http<Http, ConnBuilder>(
pub fn build_with_http(
self,
http: Http,
conn_builder: ConnBuilder,
) -> HttpsConnector<Http, ConnBuilder>
where
ConnBuilder: connection::Builder,
{
) -> HttpsConnector<Http, ConnBuilder> {
HttpsConnector { http, conn_builder }
}
}
Expand Down Expand Up @@ -226,6 +241,21 @@ mod tests {
use hyper_util::{client::legacy::Client, rt::TokioExecutor};
use std::{error::Error as StdError, str::FromStr};

#[tokio::test]
async fn connector_creation() {
let config = Config::default();
let connector_from_new = HttpsConnector::new(config.clone());
let _assert_type: HttpsConnector<HttpConnector, Config> = connector_from_new;

let connector_from_build = HttpsConnector::builder().build(config.clone());
let _assert_type: HttpsConnector<HttpConnector, Config> = connector_from_build;

let http: u32 = 10;
let connect_from_build_with_http =
HttpsConnector::builder().build_with_http(http, config.clone());
let _assert_type: HttpsConnector<u32, Config> = connect_from_build_with_http;
}

#[tokio::test]
async fn test_unsecure_http() -> Result<(), Box<dyn StdError>> {
let connector = HttpsConnector::new(Config::default());
Expand Down

0 comments on commit 126cef1

Please sign in to comment.