mirror of https://github.com/rwf2/Rocket.git
Can now retrieve cookies from a handler. SWEET! Bumped version to 0.0.5.
This commit is contained in:
parent
95a8a51b76
commit
3a89cb8e2b
|
@ -8,14 +8,14 @@ extern crate tera;
|
|||
|
||||
use rocket::Rocket;
|
||||
use rocket::response::{Cookied, Redirect};
|
||||
use rocket::Method;
|
||||
use rocket::request::Cookies;
|
||||
|
||||
lazy_static!(static ref TERA: tera::Tera = tera::Tera::new("templates/**/*"););
|
||||
|
||||
fn ctxt(message: Option<&str>) -> tera::Context {
|
||||
fn ctxt(message: Option<String>) -> tera::Context {
|
||||
let mut context = tera::Context::new();
|
||||
context.add("have_message", &message.is_some());
|
||||
context.add("message", &message.unwrap_or("").to_string());
|
||||
context.add("message", &message.unwrap_or("".to_string()));
|
||||
context
|
||||
}
|
||||
|
||||
|
@ -30,9 +30,9 @@ fn submit(message: Message) -> Cookied<Redirect> {
|
|||
}
|
||||
|
||||
#[route(GET, path = "/")]
|
||||
fn index(method: Method) -> tera::TeraResult<String> {
|
||||
println!("Method is: {}", method);
|
||||
TERA.render("index.html", ctxt(None))
|
||||
fn index(cookies: Cookies) -> tera::TeraResult<String> {
|
||||
let message = cookies.find("message").map(|msg| msg.value);
|
||||
TERA.render("index.html", ctxt(message))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rocket"
|
||||
version = "0.0.1"
|
||||
version = "0.0.5"
|
||||
authors = ["Sergio Benitez <sb@sergio.bz>"]
|
||||
|
||||
[dependencies]
|
||||
|
|
|
@ -24,6 +24,18 @@ impl<'r, 'c> FromRequest<'r, 'c> for Method {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'r, 'c> FromRequest<'r, 'c> for Cookies {
|
||||
type Error = &'static str;
|
||||
|
||||
fn from_request(request: &'r Request<'c>) -> Result<Self, Self::Error> {
|
||||
match request.headers.get::<HyperCookie>() {
|
||||
// TODO: What to do about key?
|
||||
Some(cookie) => Ok(cookie.to_cookie_jar(&[])),
|
||||
None => Ok(Cookies::new(&[]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r, 'c, T: FromRequest<'r, 'c>> FromRequest<'r, 'c> for Option<T> {
|
||||
type Error = ();
|
||||
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
mod request;
|
||||
mod from_request;
|
||||
|
||||
pub use hyper::server::Request as HyperRequest;
|
||||
pub use self::request::Request;
|
||||
pub use self::from_request::FromRequest;
|
||||
|
||||
pub use hyper::server::Request as HyperRequest;
|
||||
pub use hyper::header::Headers as HyperHeaders;
|
||||
pub use hyper::header::Cookie as HyperCookie;
|
||||
use hyper::header::CookieJar;
|
||||
|
||||
pub type Cookies = CookieJar<'static>;
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
use error::Error;
|
||||
use param::FromParam;
|
||||
use method::Method;
|
||||
use request::HyperHeaders;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Request<'a> {
|
||||
params: Option<Vec<&'a str>>,
|
||||
pub headers: &'a HyperHeaders, // TODO: Don't make pub?....
|
||||
pub method: Method,
|
||||
pub uri: &'a str,
|
||||
pub data: &'a [u8]
|
||||
}
|
||||
|
||||
impl<'a> Request<'a> {
|
||||
pub fn new(method: Method, uri: &'a str, params: Option<Vec<&'a str>>,
|
||||
data: &'a [u8]) -> Request<'a> {
|
||||
pub fn new(headers: &'a HyperHeaders, method: Method, uri: &'a str,
|
||||
params: Option<Vec<&'a str>>, data: &'a [u8]) -> Request<'a> {
|
||||
Request {
|
||||
headers: headers,
|
||||
method: method,
|
||||
params: params,
|
||||
uri: uri,
|
||||
|
|
|
@ -15,7 +15,7 @@ impl<R: Responder> Cookied<R> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with(responder: R, pairs: &[(&ToString, &ToString)]) -> Cookied<R> {
|
||||
pub fn pairs(responder: R, pairs: &[(&ToString, &ToString)]) -> Cookied<R> {
|
||||
Cookied {
|
||||
cookies: Some(
|
||||
pairs.iter()
|
||||
|
|
|
@ -84,7 +84,7 @@ impl Rocket {
|
|||
let handle_not_found = |response: FreshHyperResponse| {
|
||||
println!("{}", Red.paint("\t<= Dispatch failed. Returning 404."));
|
||||
|
||||
let request = Request::new(method, uri, None, &buf);
|
||||
let request = Request::new(&req.headers, method, uri, None, &buf);
|
||||
let catcher = self.catchers.get(&404).unwrap();
|
||||
catcher.handle(RoutingError::unchained(request)).respond(response);
|
||||
};
|
||||
|
@ -98,7 +98,7 @@ impl Rocket {
|
|||
// Okay, we've got a route. Unwrap it, generate a request, and dispatch.
|
||||
let route = route.unwrap();
|
||||
let params = route.get_params(uri);
|
||||
let request = Request::new(method, uri, Some(params), &buf);
|
||||
let request = Request::new(&req.headers, method, uri, Some(params), &buf);
|
||||
|
||||
println!("\t=> {}", Magenta.paint("Dispatching request."));
|
||||
let outcome = (route.handler)(request).respond(res);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rocket_macros"
|
||||
version = "0.0.1"
|
||||
version = "0.0.5"
|
||||
authors = ["Sergio Benitez <sb@sergio.bz>"]
|
||||
|
||||
[lib]
|
||||
|
|
Loading…
Reference in New Issue