diff --git a/examples/hello/src/main.rs b/examples/hello/src/main.rs index 10c5b870..912e003f 100644 --- a/examples/hello/src/main.rs +++ b/examples/hello/src/main.rs @@ -3,15 +3,9 @@ extern crate rocket; use rocket::Rocket; -use rocket::response::{HypResponse, HypFresh, Responder}; use std::fs::File; -// #[route(GET, path = "/")] -// fn simple() -> &'static str { -// "Hello, simple example world! How is thou?" -// } - #[route(GET, path = "/")] fn simple() -> File { File::open("/tmp/index.html").unwrap() @@ -28,8 +22,8 @@ fn simple3() -> String { } #[route(GET, path = "//")] -fn simple4(name: &str, age: i8) -> &str { - name +fn simple4(name: &str, age: i8) -> String { + format!("Hello, {} year old named {}!", age, name) } #[route(GET, path = "/something")] diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 298bbd27..d11135df 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -7,8 +7,6 @@ pub mod request; pub mod param; pub mod router; -use std::io::Write; - pub use method::Method; pub use error::Error; pub use response::{Response, HypResponse, HypFresh, Responder}; @@ -41,7 +39,7 @@ pub struct Rocket { impl HypHandler for Rocket { fn handle<'a, 'k>(&'a self, req: HypRequest<'a, 'k>, - mut res: HypResponse<'a, HypFresh>) { + res: HypResponse<'a, HypFresh>) { println!("Request: {:?}", req.uri); if self.handler.is_some() { let handler = self.handler.as_ref(); diff --git a/lib/src/request.rs b/lib/src/request.rs index 97e122d2..56fce16b 100644 --- a/lib/src/request.rs +++ b/lib/src/request.rs @@ -8,7 +8,7 @@ impl Request { Request } - pub fn get_param_str<'a>(&self, name: &'a str) -> Result<&'a str, Error> { + pub fn get_param_str<'a>(&self, _name: &'a str) -> Result<&'a str, Error> { Err(Error::NoKey) } diff --git a/lib/src/response.rs b/lib/src/response.rs index 009e6bd4..c9be8a3a 100644 --- a/lib/src/response.rs +++ b/lib/src/response.rs @@ -2,8 +2,8 @@ pub use hyper::server::Response as HypResponse; pub use hyper::net::Fresh as HypFresh; use hyper::status::StatusCode; -use hyper::header::{self, Headers, Encoding}; -use std::io::{self, Read, Write}; +use hyper::header; +use std::io::{Read, Write}; use std::fs::File; pub struct Response<'a> { @@ -16,9 +16,31 @@ impl<'a> Response<'a> { body: Box::new(body) } } + + pub fn empty() -> Response<'a> { + Response { + body: Box::new(Empty::new(StatusCode::Ok)) + } + } + + pub fn not_found() -> Response<'a> { + Response { + body: Box::new(Empty::new(StatusCode::NotFound)) + } + } + + pub fn server_error() -> Response<'a> { + Response { + body: Box::new(Empty::new(StatusCode::InternalServerError)) + } + } } -struct Empty { +pub trait Responder { + fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>); +} + +pub struct Empty { status: StatusCode } @@ -30,24 +52,24 @@ impl Empty { } } -pub trait Responder { - fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>); -} - impl Responder for Empty { fn respond<'a>(&mut self, mut res: HypResponse<'a, HypFresh>) { - res.send(b"").unwrap(); + res.headers_mut().set(header::ContentLength(0)); + *(res.status_mut()) = self.status; + + let mut stream = res.start().unwrap(); + stream.write_all(b"").unwrap(); } } impl<'a> Responder for &'a str { - fn respond<'b>(&mut self, mut res: HypResponse<'b, HypFresh>) { + fn respond<'b>(&mut self, res: HypResponse<'b, HypFresh>) { res.send(self.as_bytes()).unwrap(); } } impl Responder for String { - fn respond<'b>(&mut self, mut res: HypResponse<'b, HypFresh>) { + fn respond<'b>(&mut self, res: HypResponse<'b, HypFresh>) { res.send(self.as_bytes()).unwrap(); } } @@ -86,27 +108,6 @@ impl Responder for File { // } // } -impl<'a> Response<'a> { - pub fn empty() -> Response<'a> { - Response { - body: Box::new(Empty::new(StatusCode::Ok)) - } - } - - pub fn not_found() -> Response<'a> { - Response { - body: Box::new(Empty::new(StatusCode::NotFound)) - } - } - - pub fn server_error() -> Response<'a> { - Response { - body: Box::new(Empty::new(StatusCode::InternalServerError)) - } - } -} - - // macro_rules! impl_from_lengthed { // ($name:ident, $T:ty) => ( // impl<'a> From<$T> for Response<'a> { diff --git a/lib/src/router.rs b/lib/src/router.rs index 33c03496..f017eb70 100644 --- a/lib/src/router.rs +++ b/lib/src/router.rs @@ -43,7 +43,8 @@ impl Router { } } - pub fn add_route(&mut self, method: Method, base: &str, route: &str) { + // TODO: Use `method` argument + pub fn add_route(&mut self, _method: Method, base: &str, route: &str) { // Allocate enough space for the worst case. let mut deduped = String::with_capacity(base.len() + route.len() + 1); @@ -56,7 +57,7 @@ impl Router { last = c; } - let mut path = PathBuf::from(deduped); + let path = PathBuf::from(deduped); println!("Mounted: {:?}", path); self.paths.push(path); }