@@ -2,15 +2,12 @@
|
|||||||
extern crate hyper;
|
extern crate hyper;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
use std::io::Write;
|
|
||||||
use hyper::server::{Request, Response};
|
use hyper::server::{Request, Response};
|
||||||
|
|
||||||
static PHRASE: &'static [u8] = b"Hello World!";
|
static PHRASE: &'static [u8] = b"Hello World!";
|
||||||
|
|
||||||
fn hello(_: Request, res: Response) {
|
fn hello(_: Request, res: Response) {
|
||||||
let mut res = res.start().unwrap();
|
res.send(PHRASE).unwrap();
|
||||||
res.write_all(PHRASE).unwrap();
|
|
||||||
res.end().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -2,10 +2,9 @@
|
|||||||
extern crate hyper;
|
extern crate hyper;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
use std::io::{Write, copy};
|
use std::io::copy;
|
||||||
|
|
||||||
use hyper::{Get, Post};
|
use hyper::{Get, Post};
|
||||||
use hyper::header::ContentLength;
|
|
||||||
use hyper::server::{Server, Request, Response};
|
use hyper::server::{Server, Request, Response};
|
||||||
use hyper::uri::RequestUri::AbsolutePath;
|
use hyper::uri::RequestUri::AbsolutePath;
|
||||||
|
|
||||||
@@ -22,11 +21,7 @@ fn echo(mut req: Request, mut res: Response) {
|
|||||||
match req.uri {
|
match req.uri {
|
||||||
AbsolutePath(ref path) => match (&req.method, &path[..]) {
|
AbsolutePath(ref path) => match (&req.method, &path[..]) {
|
||||||
(&Get, "/") | (&Get, "/echo") => {
|
(&Get, "/") | (&Get, "/echo") => {
|
||||||
let out = b"Try POST /echo";
|
try_return!(res.send(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));
|
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
(&Post, "/echo") => (), // fall through, fighting mutable borrows
|
(&Post, "/echo") => (), // fall through, fighting mutable borrows
|
||||||
|
|||||||
@@ -109,8 +109,6 @@ impl<'a, W: Any> Response<'a, W> {
|
|||||||
|
|
||||||
Ok(body_type)
|
Ok(body_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Response<'a, Fresh> {
|
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>
|
/// Consume this Response<Fresh>, writing the Headers and Status and creating a Response<Streaming>
|
||||||
pub fn start(mut self) -> io::Result<Response<'a, Streaming>> {
|
pub fn start(mut self) -> io::Result<Response<'a, Streaming>> {
|
||||||
let body_type = try!(self.write_head());
|
let body_type = try!(self.write_head());
|
||||||
|
|||||||
Reference in New Issue
Block a user