Skip to content

Commit

Permalink
with_plaintext_http
Browse files Browse the repository at this point in the history
  • Loading branch information
goatgoose committed Dec 17, 2024
1 parent 9c998c6 commit 9f217b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
21 changes: 11 additions & 10 deletions bindings/rust/s2n-tls-hyper/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use tower_service::Service;
pub struct HttpsConnector<Http, ConnBuilder = Config> {
http: Http,
conn_builder: ConnBuilder,
insecure_http: bool,
plaintext_http: bool,
}

impl<ConnBuilder> HttpsConnector<HttpConnector, ConnBuilder>
Expand Down Expand Up @@ -105,7 +105,7 @@ where
Builder {
http,
conn_builder,
insecure_http: false,
plaintext_http: false,
}
}
}
Expand All @@ -115,13 +115,14 @@ where
pub struct Builder<Http, ConnBuilder> {
http: Http,
conn_builder: ConnBuilder,
insecure_http: bool,
plaintext_http: bool,
}

impl<Http, ConnBuilder> Builder<Http, ConnBuilder> {
/// If enabled, allows communication with insecure HTTP endpoints in addition to secure HTTPS endpoints (default: false).
pub fn with_insecure_http(&mut self, enabled: bool) -> &mut Self {
self.insecure_http = enabled;
/// If enabled, allows communication with plaintext HTTP endpoints in addition to secure HTTPS
/// endpoints (default: false).
pub fn with_plaintext_http(&mut self, enabled: bool) -> &mut Self {
self.plaintext_http = enabled;
self
}

Expand All @@ -130,7 +131,7 @@ impl<Http, ConnBuilder> Builder<Http, ConnBuilder> {
HttpsConnector {
http: self.http,
conn_builder: self.conn_builder,
insecure_http: self.insecure_http,
plaintext_http: self.plaintext_http,
}
}
}
Expand Down Expand Up @@ -170,7 +171,7 @@ where
fn call(&mut self, req: Uri) -> Self::Future {
match req.scheme() {
Some(scheme) if scheme == &http::uri::Scheme::HTTPS => (),
Some(scheme) if scheme == &http::uri::Scheme::HTTP && self.insecure_http => {
Some(scheme) if scheme == &http::uri::Scheme::HTTP && self.plaintext_http => {
let call = self.http.call(req);
return Box::pin(async move {
let tcp = call.await.map_err(|e| Error::HttpError(e.into()))?;
Expand Down Expand Up @@ -277,9 +278,9 @@ mod tests {

#[tokio::test]
async fn default_builder() -> Result<(), Box<dyn StdError>> {
// Ensure that insecure HTTP is disabled by default.
// Ensure that plaintext HTTP is disabled by default.
let connector = HttpsConnector::builder(Config::default()).build();
assert!(!connector.insecure_http);
assert!(!connector.plaintext_http);

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions bindings/rust/s2n-tls-hyper/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ async fn config_alpn_ignored() -> Result<(), Box<dyn Error + Send + Sync>> {
}

#[tokio::test]
async fn insecure_http() -> Result<(), Box<dyn Error + Send + Sync>> {
async fn plaintext_http() -> Result<(), Box<dyn Error + Send + Sync>> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;

Expand All @@ -356,11 +356,11 @@ async fn insecure_http() -> Result<(), Box<dyn Error + Send + Sync>> {
});

tasks.spawn(async move {
for enable_insecure_http in [false, true] {
for enable_plaintext_http in [false, true] {
let connector = {
let config = common::config()?.build()?;
let mut builder = HttpsConnector::builder(config);
builder.with_insecure_http(enable_insecure_http);
builder.with_plaintext_http(enable_plaintext_http);
builder.build()
};

Expand All @@ -369,12 +369,12 @@ async fn insecure_http() -> Result<(), Box<dyn Error + Send + Sync>> {
let uri = Uri::from_str(format!("http://127.0.0.1:{}", addr.port()).as_str())?;
let response = client.get(uri).await;

if enable_insecure_http {
// If insecure HTTP is enabled, the request should succeed.
if enable_plaintext_http {
// If plaintext HTTP is enabled, the request should succeed.
let response = response.unwrap();
assert_eq!(response.status(), 200);
} else {
// If insecure HTTP is disabled, the request should error.
// If plaintext HTTP is disabled, the request should error.
let error = response.unwrap_err();

// Ensure an InvalidScheme error is produced.
Expand Down

0 comments on commit 9f217b9

Please sign in to comment.