Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "cookie"
version = "0.1.0"
authors = ["Zach Pomerantz <zmp@umich.edu>"]

[[lib]]
[lib]

name = "cookie"
path = "src/lib.rs"
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ extern crate url;
extern crate serialize;
extern crate iron;
extern crate http;
extern crate crypto = "rust-crypto";
extern crate "rust-crypto" as crypto;
#[cfg(test)]
extern crate test = "iron-test";
extern crate "iron-test" as test;

pub use cookie::Cookie;
pub use parser::CookieParser;
Expand Down
20 changes: 11 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use std::collections::treemap::TreeMap;
use url::lossy_utf8_percent_decode;
use serialize::json;
use serialize::json::{Json, Null};
use iron::{Request, Response, Middleware, Status, Continue};
use serialize::json::{mod, Json, Null};
use iron::{Request, Response, BeforeMiddleware, IronResult};
use super::Cookie;
use crypto::util::fixed_time_eq;
use iron::typemap::Assoc;

/// The cookie parsing `Middleware`.
///
Expand All @@ -20,6 +20,8 @@ pub struct CookieParser {
secret: Option<String>
}

impl Assoc<CookieParser> for Cookie {}

impl CookieParser {
/// Create a new instance of the cookie parsing `Middleware`.
///
Expand All @@ -37,11 +39,11 @@ impl CookieParser {
pub fn signed(secret: String) -> CookieParser { CookieParser{ secret: Some(secret) } }
}

impl Middleware for CookieParser {
impl BeforeMiddleware for CookieParser {
/// Parse the cookie received in the HTTP header.
///
/// This will parse the body of a cookie into the alloy, under type `Cookie`.
fn enter(&mut self, req: &mut Request, _res: &mut Response) -> Status {
fn before(&self, req: &mut Request) -> IronResult<()> {
// Initialize a cookie. This will store parsed cookies and generate signatures.
let mut new_cookie = Cookie::new(self.secret.clone());

Expand All @@ -55,7 +57,7 @@ impl Middleware for CookieParser {
.split(';')
// Decode from uri component encoding
.map(|substr| {
let vec: Vec<&str> = substr.splitn('=', 1).collect();
let vec: Vec<&str> = substr.splitn(1, '=').collect();
let key = from_rfc_compliant(vec[0]);
let val = from_rfc_compliant(vec[1]);
(key, val) })
Expand All @@ -71,8 +73,8 @@ impl Middleware for CookieParser {
},
None => ()
}
req.alloy.insert(new_cookie);
Continue
req.extensions.insert::<Cookie, CookieParser>(new_cookie);
Ok(())
}
}

Expand Down Expand Up @@ -140,7 +142,7 @@ fn parse_json(&(ref key, ref val): &(String, String), json: &mut Json) -> bool {
#[cfg(test)]
mod test {
use std::collections::{HashMap, TreeMap};
use iron::{Request, Middleware};
use iron::Request;
use test::mock::{request, response};
use super::*;
use super::super::cookie::*;
Expand Down
6 changes: 4 additions & 2 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Setting functionality - set cookie data

use url::{utf8_percent_encode, FORM_URLENCODED_ENCODE_SET};
use serialize::json::{Json, Number, String, Boolean, List, Object, Null};
use serialize::json::{mod, Json, String, Boolean, List, Object, Null};
use iron::Response;
use super::Cookie;
use time::Tm;
Expand Down Expand Up @@ -81,7 +81,9 @@ fn stringify_json(json: &Json) -> String {
let ary: Vec<String> = list.iter().map(stringify_json).collect();
"[".to_string().append(ary.connect(",").as_slice()).append("]")
},
Number(number) => number.to_string(),
json::I64(number) => number.to_string(),
json::U64(number) => number.to_string(),
json::F64(number) => number.to_string(),
String(ref string) => "\"".to_string().append(string.as_slice()).append("\""),
Boolean(true) => "true".to_string(),
Boolean(false) => "false".to_string(),
Expand Down