forked from hyperium/hyper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for local bind to HttpConnector.
Resolves hyperium#1498.
- Loading branch information
Showing
3 changed files
with
112 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![deny(warnings)] | ||
extern crate hyper; | ||
extern crate pretty_env_logger; | ||
|
||
use std::env; | ||
use std::net::IpAddr; | ||
use std::io::{self, Write}; | ||
|
||
use hyper::{Body, Client, Request}; | ||
use hyper::rt::{self, Future, Stream}; | ||
use hyper::client::connect::HttpConnector; | ||
|
||
fn main() { | ||
pretty_env_logger::init(); | ||
|
||
let url = match env::args().nth(1) { | ||
Some(url) => url, | ||
None => { | ||
println!("Usage: client <url> [<bind_addr>]"); | ||
return; | ||
} | ||
}; | ||
|
||
let url = url.parse::<hyper::Uri>().unwrap(); | ||
if url.scheme_part().map(|s| s.as_ref()) != Some("http") { | ||
println!("This example only works with 'http' URLs."); | ||
return; | ||
} | ||
|
||
let bind_addr = env::args().nth(2); | ||
|
||
let bind_addr: Option<IpAddr> = bind_addr.map(|s| s.parse::<IpAddr>().unwrap()); | ||
|
||
rt::run(rt::lazy(move || { | ||
let mut connector = HttpConnector::new(4); | ||
connector.set_local_address(bind_addr); | ||
let client = Client::builder().build(connector); | ||
|
||
let mut req = Request::new(Body::empty()); | ||
*req.uri_mut() = url; | ||
|
||
client.request(req).and_then(|res| { | ||
println!("Response: {}", res.status()); | ||
println!("Headers: {:#?}", res.headers()); | ||
|
||
res.into_body().for_each(|chunk| { | ||
io::stdout().write_all(&chunk) | ||
.map_err(|e| panic!("example expects stdout is open, error={}", e)) | ||
}) | ||
}).map(|_| { | ||
println!("\n\nDone."); | ||
}).map_err(|err| { | ||
eprintln!("Error {}", err); | ||
}) | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters