46 lines
977 B
Rust
46 lines
977 B
Rust
extern crate hyper;
|
|
|
|
use std::os;
|
|
use std::io::stdout;
|
|
use std::io::util::copy;
|
|
|
|
use hyper::Url;
|
|
use hyper::client::Request;
|
|
|
|
fn main() {
|
|
let args = os::args();
|
|
match args.len() {
|
|
2 => (),
|
|
_ => {
|
|
println!("Usage: client <url>");
|
|
return;
|
|
}
|
|
};
|
|
|
|
let url = match Url::parse(args[1].as_slice()) {
|
|
Ok(url) => {
|
|
println!("GET {}...", url)
|
|
url
|
|
},
|
|
Err(e) => panic!("Invalid URL: {}", e)
|
|
};
|
|
|
|
|
|
let req = match Request::get(url) {
|
|
Ok(req) => req,
|
|
Err(err) => panic!("Failed to connect: {}", err)
|
|
};
|
|
|
|
let mut res = req
|
|
.start().unwrap() // failure: Error writing Headers
|
|
.send().unwrap(); // failure: Error reading Response head.
|
|
|
|
println!("Response: {}", res.status);
|
|
println!("{}", res.headers);
|
|
match copy(&mut res, &mut stdout()) {
|
|
Ok(..) => (),
|
|
Err(e) => panic!("Stream failure: {}", e)
|
|
};
|
|
|
|
}
|