diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
| commit | 8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch) | |
| tree | 22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/hyper-timeout/examples | |
| parent | 4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff) | |
chore: add vendor directory
Diffstat (limited to 'vendor/hyper-timeout/examples')
| -rw-r--r-- | vendor/hyper-timeout/examples/client.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/vendor/hyper-timeout/examples/client.rs b/vendor/hyper-timeout/examples/client.rs new file mode 100644 index 00000000..0e75c9c5 --- /dev/null +++ b/vendor/hyper-timeout/examples/client.rs @@ -0,0 +1,48 @@ +use std::env; +use std::time::Duration; + +use http_body_util::{BodyExt, Empty}; +use hyper::body::Bytes; +use hyper_util::{client::legacy::Client, rt::TokioExecutor}; +use tokio::io::{self, AsyncWriteExt}; + +use hyper_tls::HttpsConnector; + +use hyper_timeout::TimeoutConnector; + +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + let url = match env::args().nth(1) { + Some(url) => url, + None => { + println!("Usage: client <url>"); + println!("Example: client https://example.com"); + return Ok(()); + } + }; + + let url = url.parse::<hyper::Uri>().unwrap(); + + // This example uses `HttpsConnector`, but you can also use hyper `HttpConnector` + //let h = hyper_util::client::legacy::connect::HttpConnector::new(); + let h = HttpsConnector::new(); + let mut connector = TimeoutConnector::new(h); + connector.set_connect_timeout(Some(Duration::from_secs(5))); + connector.set_read_timeout(Some(Duration::from_secs(5))); + connector.set_write_timeout(Some(Duration::from_secs(5))); + let client = Client::builder(TokioExecutor::new()).build::<_, Empty<Bytes>>(connector); + + let mut res = client.get(url).await?; + + println!("Status: {}", res.status()); + println!("Headers:\n{:#?}", res.headers()); + + while let Some(frame) = res.body_mut().frame().await { + let bytes = frame? + .into_data() + .map_err(|_| io::Error::new(io::ErrorKind::Other, "Error when consuming frame"))?; + io::stdout().write_all(&bytes).await?; + } + + Ok(()) +} |
