feat(server): add Response.send to write a sized body

Closes #446
This commit is contained in:
Sean McArthur
2015-05-07 11:03:45 -07:00
parent d294ea501d
commit d5558b687d
3 changed files with 20 additions and 13 deletions

View File

@@ -2,15 +2,12 @@
extern crate hyper;
extern crate env_logger;
use std::io::Write;
use hyper::server::{Request, Response};
static PHRASE: &'static [u8] = b"Hello World!";
fn hello(_: Request, res: Response) {
let mut res = res.start().unwrap();
res.write_all(PHRASE).unwrap();
res.end().unwrap();
res.send(PHRASE).unwrap();
}
fn main() {

View File

@@ -2,10 +2,9 @@
extern crate hyper;
extern crate env_logger;
use std::io::{Write, copy};
use std::io::copy;
use hyper::{Get, Post};
use hyper::header::ContentLength;
use hyper::server::{Server, Request, Response};
use hyper::uri::RequestUri::AbsolutePath;
@@ -22,11 +21,7 @@ fn echo(mut req: Request, mut res: Response) {
match req.uri {
AbsolutePath(ref path) => match (&req.method, &path[..]) {
(&Get, "/") | (&Get, "/echo") => {
let out = b"Try POST /echo";
res.headers_mut().set(ContentLength(out.len() as u64));
let mut res = try_return!(res.start());
try_return!(res.write_all(out));
try_return!(res.send(b"Try POST /echo"));
return;
},
(&Post, "/echo") => (), // fall through, fighting mutable borrows