1 use std::env;
2 use std::time::Duration;
3 
4 use hyper::{body::HttpBody as _, Client};
5 use tokio::io::{self, AsyncWriteExt as _};
6 
7 use hyper_tls::HttpsConnector;
8 
9 use hyper_timeout::TimeoutConnector;
10 
11 #[tokio::main(flavor = "current_thread")]
main() -> Result<(), Box<dyn std::error::Error>>12 async fn main() -> Result<(), Box<dyn std::error::Error>> {
13     let url = match env::args().nth(1) {
14         Some(url) => url,
15         None => {
16             println!("Usage: client <url>");
17             println!("Example: client https://example.com");
18             return Ok(());
19         }
20     };
21 
22     let url = url.parse::<hyper::Uri>().unwrap();
23 
24     // This example uses `HttpsConnector`, but you can also use hyper `HttpConnector`
25     //let h = hyper::client::HttpConnector::new();
26     let h = HttpsConnector::new();
27     let mut connector = TimeoutConnector::new(h);
28     connector.set_connect_timeout(Some(Duration::from_secs(5)));
29     connector.set_read_timeout(Some(Duration::from_secs(5)));
30     connector.set_write_timeout(Some(Duration::from_secs(5)));
31     let client = Client::builder().build::<_, hyper::Body>(connector);
32 
33     let mut res = client.get(url).await?;
34 
35     println!("Status: {}", res.status());
36     println!("Headers:\n{:#?}", res.headers());
37 
38     while let Some(chunk) = res.body_mut().data().await {
39         let chunk = chunk?;
40         io::stdout().write_all(&chunk).await?
41     }
42     Ok(())
43 }
44