Skip to content

Commit 749e374

Browse files
authored
Merge pull request #53 from JEnoch/main
Add 'h1_client_rustls' feature using 'async-tls'
2 parents 352ad27 + 3c4183e commit 749e374

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
2323
default = ["h1_client"]
2424
docs = ["h1_client"]
2525
h1_client = ["async-h1", "async-std", "async-native-tls"]
26+
h1_client_rustls = ["async-h1", "async-std", "async-tls"]
2627
native_client = ["curl_client", "wasm_client"]
2728
curl_client = ["isahc", "async-std"]
2829
wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "futures"]
@@ -38,6 +39,9 @@ async-h1 = { version = "2.0.0", optional = true }
3839
async-std = { version = "1.6.0", default-features = false, optional = true }
3940
async-native-tls = { version = "0.3.1", optional = true }
4041

42+
# h1_client_rustls
43+
async-tls = { version = "0.10.0", optional = true }
44+
4145
# hyper_client
4246
hyper = { version = "0.13.6", features = ["tcp"], optional = true }
4347
hyper-tls = { version = "0.4.3", optional = true }
@@ -76,5 +80,7 @@ features = [
7680
[dev-dependencies]
7781
async-std = { version = "1.6.0", features = ["unstable", "attributes"] }
7882
portpicker = "0.1.0"
79-
tide = { version = "0.13.0" }
83+
tide = { version = "0.15.0" }
84+
tide-rustls = { version = "0.1.4" }
8085
tokio = { version = "0.2.21", features = ["macros"] }
86+
serde_json = "1.0"

src/h1.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,31 @@ impl HttpClient for H1Client {
6666
let raw_stream = async_std::net::TcpStream::connect(addr).await?;
6767
req.set_peer_addr(raw_stream.peer_addr().ok());
6868
req.set_local_addr(raw_stream.local_addr().ok());
69-
70-
let stream = async_native_tls::connect(host, raw_stream).await?;
71-
72-
client::connect(stream, req).await
69+
let tls_stream = add_tls(host, raw_stream).await?;
70+
client::connect(tls_stream, req).await
7371
}
7472
_ => unreachable!(),
7573
}
7674
}
7775
}
7876

77+
#[cfg(not(feature = "h1_client_rustls"))]
78+
async fn add_tls(
79+
host: String,
80+
stream: async_std::net::TcpStream,
81+
) -> Result<async_native_tls::TlsStream<async_std::net::TcpStream>, async_native_tls::Error> {
82+
async_native_tls::connect(host, stream).await
83+
}
84+
85+
#[cfg(feature = "h1_client_rustls")]
86+
async fn add_tls(
87+
host: String,
88+
stream: async_std::net::TcpStream,
89+
) -> std::io::Result<async_tls::client::TlsStream<async_std::net::TcpStream>> {
90+
let connector = async_tls::TlsConnector::default();
91+
connector.connect(host, stream).await
92+
}
93+
7994
#[cfg(test)]
8095
mod tests {
8196
use super::*;
@@ -120,4 +135,18 @@ mod tests {
120135

121136
Ok(())
122137
}
138+
139+
#[async_std::test]
140+
async fn https_functionality() -> Result<()> {
141+
task::sleep(Duration::from_millis(100)).await;
142+
// Send a POST request to https://httpbin.org/post
143+
// The result should be a JSon string similar to what you get with:
144+
// curl -X POST "https://httpbin.org/post" -H "accept: application/json" -H "Content-Type: text/plain;charset=utf-8" -d "hello"
145+
let request = build_test_request(Url::parse("https://httpbin.org/post").unwrap());
146+
let mut response: Response = H1Client::new().send(request).await?;
147+
let json_val: serde_json::value::Value =
148+
serde_json::from_str(&response.body_string().await.unwrap())?;
149+
assert_eq!(*json_val.get("data").unwrap(), serde_json::json!("hello"));
150+
Ok(())
151+
}
123152
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub mod wasm;
2727
pub mod native;
2828

2929
#[cfg_attr(feature = "docs", doc(cfg(h1_client)))]
30-
#[cfg(feature = "h1_client")]
30+
#[cfg(any(feature = "h1_client", feature = "h1_client_rustls"))]
3131
pub mod h1;
3232

3333
#[cfg_attr(feature = "docs", doc(cfg(hyper_client)))]

0 commit comments

Comments
 (0)