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

@@ -109,8 +109,6 @@ impl<'a, W: Any> Response<'a, W> {
Ok(body_type)
}
}
impl<'a> Response<'a, Fresh> {
@@ -126,6 +124,23 @@ impl<'a> Response<'a, Fresh> {
}
}
/// Writes the body and ends the response.
///
/// # Example
///
/// ```
/// # use hyper::server::Response;
/// fn handler(res: Response) {
/// res.send(b"Hello World!").unwrap();
/// }
/// ```
pub fn send(mut self, body: &[u8]) -> io::Result<()> {
self.headers.set(header::ContentLength(body.len() as u64));
let mut stream = try!(self.start());
try!(stream.write_all(body));
stream.end()
}
/// Consume this Response<Fresh>, writing the Headers and Status and creating a Response<Streaming>
pub fn start(mut self) -> io::Result<Response<'a, Streaming>> {
let body_type = try!(self.write_head());