implementation
This commit is contained in:
41
examples/client.rs
Normal file
41
examples/client.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
extern crate hyper;
|
||||
|
||||
use std::os;
|
||||
use std::io::stdout;
|
||||
use std::io::util::copy;
|
||||
|
||||
use hyper::Url;
|
||||
|
||||
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) => fail!("Invalid URL: {}", e)
|
||||
};
|
||||
|
||||
|
||||
let req = match hyper::get(url) {
|
||||
Ok(req) => req,
|
||||
Err(err) => fail!("Failed to connect: {}", err)
|
||||
};
|
||||
let mut res = req.send().unwrap();
|
||||
|
||||
println!("Response: {}", res.status);
|
||||
println!("{}", res.headers);
|
||||
match copy(&mut res, &mut stdout()) {
|
||||
Ok(..) => (),
|
||||
Err(e) => fail!("Stream failure: {}", e)
|
||||
};
|
||||
|
||||
}
|
||||
40
examples/server.rs
Normal file
40
examples/server.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
extern crate hyper;
|
||||
extern crate debug;
|
||||
|
||||
use std::io::{IoResult};
|
||||
use std::io::util::copy;
|
||||
use std::io::net::ip::Ipv4Addr;
|
||||
|
||||
use hyper::method::{Get, Post};
|
||||
use hyper::server::{Server, Handler, Request, Response};
|
||||
|
||||
struct Echo;
|
||||
|
||||
impl Handler for Echo {
|
||||
fn handle(&mut self, mut req: Request, mut res: Response) -> IoResult<()> {
|
||||
match &req.uri {
|
||||
&hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
|
||||
(&Get, "/") | (&Get, "/echo") => {
|
||||
try!(res.write_str("Try POST /echo"));
|
||||
return res.end();
|
||||
},
|
||||
(&Post, "/echo") => (), // fall through, fighting mutable borrows
|
||||
_ => {
|
||||
res.status = hyper::status::NotFound;
|
||||
return res.end();
|
||||
}
|
||||
},
|
||||
_ => return res.end()
|
||||
};
|
||||
|
||||
println!("copying...");
|
||||
try!(copy(&mut req, &mut res));
|
||||
println!("copied...");
|
||||
res.end()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 1337);
|
||||
server.listen(Echo);
|
||||
}
|
||||
Reference in New Issue
Block a user