Skip to content
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
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
default = ["h1_client"]
docs = ["h1_client"]
h1_client = ["async-h1", "async-std", "async-native-tls"]
h1_client_rustls = ["async-h1", "async-std", "async-tls"]
native_client = ["curl_client", "wasm_client"]
curl_client = ["isahc", "async-std"]
wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "futures"]
Expand All @@ -38,6 +39,9 @@ async-h1 = { version = "2.0.0", optional = true }
async-std = { version = "1.6.0", default-features = false, optional = true }
async-native-tls = { version = "0.3.1", optional = true }

# h1_client_rustls
async-tls = { version = "0.10.0", optional = true }

# hyper_client
hyper = { version = "0.13.6", features = ["tcp"], optional = true }
hyper-tls = { version = "0.4.3", optional = true }
Expand Down Expand Up @@ -76,5 +80,7 @@ features = [
[dev-dependencies]
async-std = { version = "1.6.0", features = ["unstable", "attributes"] }
portpicker = "0.1.0"
tide = { version = "0.13.0" }
tide = { version = "0.15.0" }
tide-rustls = { version = "0.1.4" }
tokio = { version = "0.2.21", features = ["macros"] }
serde_json = "1.0"
37 changes: 33 additions & 4 deletions src/h1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,31 @@ impl HttpClient for H1Client {
let raw_stream = async_std::net::TcpStream::connect(addr).await?;
req.set_peer_addr(raw_stream.peer_addr().ok());
req.set_local_addr(raw_stream.local_addr().ok());

let stream = async_native_tls::connect(host, raw_stream).await?;

client::connect(stream, req).await
let tls_stream = add_tls(host, raw_stream).await?;
client::connect(tls_stream, req).await
}
_ => unreachable!(),
}
}
}

#[cfg(not(feature = "h1_client_rustls"))]
async fn add_tls(
host: String,
stream: async_std::net::TcpStream,
) -> Result<async_native_tls::TlsStream<async_std::net::TcpStream>, async_native_tls::Error> {
async_native_tls::connect(host, stream).await
}

#[cfg(feature = "h1_client_rustls")]
async fn add_tls(
host: String,
stream: async_std::net::TcpStream,
) -> std::io::Result<async_tls::client::TlsStream<async_std::net::TcpStream>> {
let connector = async_tls::TlsConnector::default();
connector.connect(host, stream).await
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -120,4 +135,18 @@ mod tests {

Ok(())
}

#[async_std::test]
async fn https_functionality() -> Result<()> {
task::sleep(Duration::from_millis(100)).await;
// Send a POST request to https://httpbin.org/post
// The result should be a JSon string similar to what you get with:
// curl -X POST "https://httpbin.org/post" -H "accept: application/json" -H "Content-Type: text/plain;charset=utf-8" -d "hello"
let request = build_test_request(Url::parse("https://httpbin.org/post").unwrap());
let mut response: Response = H1Client::new().send(request).await?;
let json_val: serde_json::value::Value =
serde_json::from_str(&response.body_string().await.unwrap())?;
assert_eq!(*json_val.get("data").unwrap(), serde_json::json!("hello"));
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod wasm;
pub mod native;

#[cfg_attr(feature = "docs", doc(cfg(h1_client)))]
#[cfg(feature = "h1_client")]
#[cfg(any(feature = "h1_client", feature = "h1_client_rustls"))]
pub mod h1;

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