mirror of https://github.com/rwf2/Rocket.git
Added Response::with_status. Using it for catchers.
This commit is contained in:
parent
0d3ef66774
commit
3f2954ab5c
|
@ -43,7 +43,7 @@ impl fmt::Display for Catcher {
|
|||
|
||||
pub mod defaults {
|
||||
use request::Request;
|
||||
use response::Response;
|
||||
use response::{StatusCode, Response};
|
||||
use super::Catcher;
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
@ -55,8 +55,7 @@ pub mod defaults {
|
|||
}
|
||||
|
||||
pub fn not_found(_request: Request) -> Response {
|
||||
// FIXME: Need a way to pass in the status code.
|
||||
Response::new("\
|
||||
Response::with_status(StatusCode::NotFound, "\
|
||||
<head>\
|
||||
<meta charset=\"utf-8\">\
|
||||
<title>404: Not Found</title>\
|
||||
|
@ -71,8 +70,7 @@ pub mod defaults {
|
|||
}
|
||||
|
||||
pub fn internal_error(_request: Request) -> Response {
|
||||
// FIXME: Need a way to pass in the status code.
|
||||
Response::new("\
|
||||
Response::with_status(StatusCode::InternalServerError, "\
|
||||
<head>\
|
||||
<meta charset=\"utf-8\">\
|
||||
<title>404: Not Found</title>\
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
mod empty;
|
||||
mod responder;
|
||||
mod redirect;
|
||||
mod with_status;
|
||||
|
||||
pub use hyper::server::Response as HyperResponse;
|
||||
pub use hyper::net::Fresh as HyperFresh;
|
||||
|
@ -10,6 +11,7 @@ pub use hyper::header;
|
|||
pub use self::responder::Responder;
|
||||
pub use self::empty::Empty;
|
||||
pub use self::redirect::Redirect;
|
||||
pub use self::with_status::StatusResponse;
|
||||
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
|
@ -20,6 +22,17 @@ impl<'a> Response<'a> {
|
|||
Response(Box::new(body))
|
||||
}
|
||||
|
||||
pub fn with_status<T: Responder + 'a>(status: StatusCode, body: T)
|
||||
-> Response<'a> {
|
||||
Response(Box::new(StatusResponse::new(status, body)))
|
||||
}
|
||||
|
||||
pub fn with_raw_status<T: Responder + 'a>(status: u16, body: T)
|
||||
-> Response<'a> {
|
||||
let status_code = StatusCode::from_u16(status);
|
||||
Response(Box::new(StatusResponse::new(status_code, body)))
|
||||
}
|
||||
|
||||
pub fn empty() -> Response<'a> {
|
||||
Response(Box::new(Empty::new(StatusCode::Ok)))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
use response::*;
|
||||
|
||||
pub struct StatusResponse<R: Responder> {
|
||||
status: StatusCode,
|
||||
responder: R
|
||||
}
|
||||
|
||||
impl<R: Responder> StatusResponse<R> {
|
||||
pub fn new(status: StatusCode, responder: R) -> StatusResponse<R> {
|
||||
StatusResponse {
|
||||
status: status,
|
||||
responder: responder
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Responder> Responder for StatusResponse<R> {
|
||||
fn respond<'b>(&mut self, mut res: HyperResponse<'b, HyperFresh>) {
|
||||
*(res.status_mut()) = self.status;
|
||||
self.responder.respond(res);
|
||||
}
|
||||
}
|
||||
|
|
@ -57,12 +57,13 @@ pub fn error_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem,
|
|||
|
||||
let fn_name = item.ident;
|
||||
let catch_fn_name = prepend_ident(CATCH_FN_PREFIX, &item.ident);
|
||||
let catch_code = error_params.code.node;
|
||||
let catch_fn_item = quote_item!(ecx,
|
||||
fn $catch_fn_name<'rocket>(_req: rocket::Request<'rocket>)
|
||||
-> rocket::Response<'rocket> {
|
||||
// TODO: Figure out what type signature of catcher should be.
|
||||
let result = $fn_name();
|
||||
rocket::Response::new(result)
|
||||
rocket::Response::with_raw_status($catch_code, result)
|
||||
}
|
||||
).unwrap();
|
||||
|
||||
|
@ -70,7 +71,6 @@ pub fn error_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem,
|
|||
push(Annotatable::Item(catch_fn_item));
|
||||
|
||||
let struct_name = prepend_ident(CATCH_STRUCT_PREFIX, &item.ident);
|
||||
let catch_code = error_params.code.node;
|
||||
push(Annotatable::Item(quote_item!(ecx,
|
||||
#[allow(non_upper_case_globals)]
|
||||
pub static $struct_name: rocket::StaticCatchInfo = rocket::StaticCatchInfo {
|
||||
|
|
Loading…
Reference in New Issue