2016-09-12 01:57:04 +00:00
|
|
|
use std::convert::AsRef;
|
2016-10-04 00:09:13 +00:00
|
|
|
|
2016-10-25 09:17:49 +00:00
|
|
|
use outcome::IntoOutcome;
|
|
|
|
use response::{self, Responder};
|
|
|
|
use request::{self, Request, FromRequest};
|
2016-10-04 00:09:13 +00:00
|
|
|
use http::hyper::{HyperSetCookie, HyperCookiePair, FreshHyperResponse};
|
2016-09-12 01:57:04 +00:00
|
|
|
|
2016-09-22 11:12:07 +00:00
|
|
|
const FLASH_COOKIE_NAME: &'static str = "_flash";
|
|
|
|
|
2016-09-12 01:57:04 +00:00
|
|
|
pub struct Flash<R> {
|
|
|
|
name: String,
|
|
|
|
message: String,
|
2016-09-30 22:20:11 +00:00
|
|
|
responder: R,
|
2016-09-12 01:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Responder> Flash<R> {
|
|
|
|
pub fn new<N: AsRef<str>, M: AsRef<str>>(res: R, name: N, msg: M) -> Flash<R> {
|
|
|
|
Flash {
|
|
|
|
name: name.as_ref().to_string(),
|
|
|
|
message: msg.as_ref().to_string(),
|
|
|
|
responder: res,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn warning<S: AsRef<str>>(responder: R, msg: S) -> Flash<R> {
|
|
|
|
Flash::new(responder, "warning", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn success<S: AsRef<str>>(responder: R, msg: S) -> Flash<R> {
|
|
|
|
Flash::new(responder, "success", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error<S: AsRef<str>>(responder: R, msg: S) -> Flash<R> {
|
|
|
|
Flash::new(responder, "error", msg)
|
|
|
|
}
|
|
|
|
|
2016-10-04 00:09:13 +00:00
|
|
|
fn cookie_pair(&self) -> HyperCookiePair {
|
2016-09-12 01:57:04 +00:00
|
|
|
let content = format!("{}{}{}", self.name.len(), self.name, self.message);
|
2016-10-04 00:09:13 +00:00
|
|
|
let mut pair = HyperCookiePair::new(FLASH_COOKIE_NAME.to_string(), content);
|
2016-09-12 01:57:04 +00:00
|
|
|
pair.path = Some("/".to_string());
|
|
|
|
pair.max_age = Some(300);
|
|
|
|
pair
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Responder> Responder for Flash<R> {
|
2016-10-25 09:17:49 +00:00
|
|
|
fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> response::Outcome<'b> {
|
2016-09-12 01:57:04 +00:00
|
|
|
trace_!("Flash: setting message: {}:{}", self.name, self.message);
|
2016-10-04 00:09:13 +00:00
|
|
|
res.headers_mut().set(HyperSetCookie(vec![self.cookie_pair()]));
|
2016-09-12 01:57:04 +00:00
|
|
|
self.responder.respond(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Flash<()> {
|
|
|
|
fn named(name: &str, msg: &str) -> Flash<()> {
|
|
|
|
Flash {
|
|
|
|
name: name.to_string(),
|
|
|
|
message: msg.to_string(),
|
|
|
|
responder: (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
self.name.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn msg(&self) -> &str {
|
|
|
|
self.message.as_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Using Flash<()> is ugly. Either create a type FlashMessage = Flash<()>
|
|
|
|
// or create a Flash under request that does this.
|
2016-10-08 06:20:49 +00:00
|
|
|
impl<'r> FromRequest<'r> for Flash<()> {
|
2016-09-12 01:57:04 +00:00
|
|
|
type Error = ();
|
|
|
|
|
2016-10-25 09:17:49 +00:00
|
|
|
fn from_request(request: &'r Request) -> request::Outcome<Self, Self::Error> {
|
2016-09-12 01:57:04 +00:00
|
|
|
trace_!("Flash: attemping to retrieve message.");
|
2016-10-14 01:39:23 +00:00
|
|
|
let r = request.cookies().find(FLASH_COOKIE_NAME).ok_or(()).and_then(|cookie| {
|
2016-09-12 01:57:04 +00:00
|
|
|
// Clear the flash message.
|
|
|
|
trace_!("Flash: retrieving message: {:?}", cookie);
|
2016-09-22 11:12:07 +00:00
|
|
|
request.cookies().remove(FLASH_COOKIE_NAME);
|
2016-09-12 01:57:04 +00:00
|
|
|
|
|
|
|
// Parse the flash.
|
|
|
|
let content = cookie.pair().1;
|
|
|
|
let (len_str, rest) = match content.find(|c: char| !c.is_digit(10)) {
|
|
|
|
Some(i) => (&content[..i], &content[i..]),
|
2016-09-30 22:20:11 +00:00
|
|
|
None => (content, ""),
|
2016-09-12 01:57:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let name_len: usize = len_str.parse().map_err(|_| ())?;
|
|
|
|
let (name, msg) = (&rest[..name_len], &rest[name_len..]);
|
|
|
|
Ok(Flash::named(name, msg))
|
2016-10-14 01:39:23 +00:00
|
|
|
});
|
|
|
|
|
2016-10-25 09:17:49 +00:00
|
|
|
r.into_outcome()
|
2016-09-12 01:57:04 +00:00
|
|
|
}
|
|
|
|
}
|