|
1 |
| -//extern crate futures; |
2 |
| -//extern crate tokio_core; |
3 |
| -//extern crate hyper; |
4 |
| -//extern crate hyper_tls; |
5 |
| -//extern crate hyper_timeout; |
6 |
| -// |
7 |
| -//use std::env; |
8 |
| -//use std::time::Duration; |
9 |
| -// |
10 |
| -//use futures::Future; |
11 |
| -//use futures::stream::Stream; |
12 |
| -// |
13 |
| -//use hyper::Client; |
14 |
| -// |
15 |
| -////use hyper::client::HttpConnector; |
16 |
| -//use hyper_tls::HttpsConnector; |
17 |
| -// |
18 |
| -//use hyper_timeout::TimeoutConnector; |
19 |
| -// |
20 |
| -//fn main() { |
21 |
| -// |
22 |
| -// let url = match env::args().nth(1) { |
23 |
| -// Some(url) => url, |
24 |
| -// None => { |
25 |
| -// println!("Usage: client <url>"); |
26 |
| -// return; |
27 |
| -// } |
28 |
| -// }; |
29 |
| -// |
30 |
| -// let url = url.parse::<hyper::Uri>().unwrap(); |
31 |
| -// |
32 |
| -// let mut core = tokio_core::reactor::Core::new().unwrap(); |
33 |
| -// let handle = core.handle(); |
34 |
| -// |
35 |
| -// // This example uses `HttpsConnector`, but you can also use the default hyper `HttpConnector` |
36 |
| -// //let connector = HttpConnector::new(4, &handle); |
37 |
| -// let connector = HttpsConnector::new(4, &handle).unwrap(); |
38 |
| -// let mut tm = TimeoutConnector::new(connector, &handle); |
39 |
| -// tm.set_connect_timeout(Some(Duration::from_secs(5))); |
40 |
| -// tm.set_read_timeout(Some(Duration::from_secs(5))); |
41 |
| -// tm.set_write_timeout(Some(Duration::from_secs(5))); |
42 |
| -// let client = Client::configure().connector(tm).build(&handle); |
43 |
| -// |
44 |
| -// let get = client.get(url).and_then(|res| { |
45 |
| -// println!("Response: {}", res.status()); |
46 |
| -// println!("Headers: \n{}", res.headers()); |
47 |
| -// |
48 |
| -// res.body().concat2() |
49 |
| -// }); |
50 |
| -// |
51 |
| -// let got = core.run(get).unwrap(); |
52 |
| -// let output = String::from_utf8_lossy(&got); |
53 |
| -// println!("{}", output); |
54 |
| -//} |
55 |
| - |
56 |
| -fn main() {} |
| 1 | +extern crate futures; |
| 2 | +extern crate hyper; |
| 3 | +extern crate hyper_tls; |
| 4 | +extern crate hyper_timeout; |
| 5 | + |
| 6 | +use std::env; |
| 7 | +use std::io::{self, Write}; |
| 8 | +use std::time::Duration; |
| 9 | + |
| 10 | +use futures::Future; |
| 11 | +use futures::stream::Stream; |
| 12 | + |
| 13 | +use hyper::{rt, Client}; |
| 14 | + |
| 15 | +//use hyper::client::HttpConnector; |
| 16 | +use hyper_tls::HttpsConnector; |
| 17 | + |
| 18 | +use hyper_timeout::TimeoutConnector; |
| 19 | + |
| 20 | +fn main() { |
| 21 | + |
| 22 | + let url = match env::args().nth(1) { |
| 23 | + Some(url) => url, |
| 24 | + None => { |
| 25 | + println!("Usage: client <url>"); |
| 26 | + println!("Example: client https://example.com"); |
| 27 | + return; |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + let url = url.parse::<hyper::Uri>().unwrap(); |
| 32 | + |
| 33 | + rt::run(rt::lazy(|| { |
| 34 | + // This example uses `HttpsConnector`, but you can also use hyper `HttpConnector` |
| 35 | + //let connector = HttpConnector::new(1); |
| 36 | + let https = HttpsConnector::new(1).unwrap(); |
| 37 | + let mut connector = TimeoutConnector::new(https); |
| 38 | + connector.set_connect_timeout(Some(Duration::from_secs(5))); |
| 39 | + connector.set_read_timeout(Some(Duration::from_secs(5))); |
| 40 | + connector.set_write_timeout(Some(Duration::from_secs(5))); |
| 41 | + let client = Client::builder().build::<_, hyper::Body>(connector); |
| 42 | + |
| 43 | + client.get(url).and_then(|res| { |
| 44 | + println!("Response: {}", res.status()); |
| 45 | + |
| 46 | + res |
| 47 | + .into_body() |
| 48 | + // Body is a stream, so as each chunk arrives... |
| 49 | + .for_each(|chunk| { |
| 50 | + io::stdout() |
| 51 | + .write_all(&chunk) |
| 52 | + .map_err(|e| { |
| 53 | + panic!("example expects stdout is open, error={}", e) |
| 54 | + }) |
| 55 | + }) |
| 56 | + }) |
| 57 | + .map_err(|err| { |
| 58 | + println!("Error: {}", err); |
| 59 | + }) |
| 60 | + })); |
| 61 | +} |
0 commit comments