Skip to content

Add more functions for client to work with cookies. #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2014
Merged
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
21 changes: 19 additions & 2 deletions src/header/common/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ impl Cookies {
/// to manipulate cookies and create a corresponding `SetCookie` header afterwards.
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
let mut jar = CookieJar::new(key);
for cookie in self.0.iter() {
jar.add_original((*cookie).clone());
for cookie in self.iter() {
jar.add_original(cookie.clone());
}
jar
}

/// Extracts all cookies from `CookieJar` and creates Cookie header.
/// Useful for clients.
pub fn from_cookie_jar(jar: &CookieJar) -> Cookies {
Cookies(jar.iter().collect())
}
}


Expand All @@ -96,4 +102,15 @@ fn test_fmt() {
assert_eq!(headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
}

#[test]
fn cookie_jar() {
let cookie = Cookie::new("foo".to_string(), "bar".to_string());
let cookies = Cookies(vec![cookie]);
let jar = cookies.to_cookie_jar(&[]);
let new_cookies = Cookies::from_cookie_jar(&jar);

assert_eq!(cookies, new_cookies);
}


bench_header!(bench, Cookies, { vec![b"foo=bar; baz=quux".to_vec()] })
23 changes: 23 additions & 0 deletions src/header/common/set_cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ impl SetCookie {
pub fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
SetCookie(jar.delta())
}

/// Use this on client to apply changes from SetCookie to CookieJar.
/// Note that this will `panic!` if `CookieJar` is not root.
pub fn apply_to_cookie_jar(&self, jar: &mut CookieJar) {
for cookie in self.iter() {
jar.add_original(cookie.clone())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that I use add_original here. I do it because the header contains only raw data and:

  1. The data will be corrupted if we will use encrypted or signed jar here.
  2. I can't imagine a situation when we need to use delta to get this changes.

}
}
}


Expand All @@ -88,3 +96,18 @@ fn test_fmt() {

assert_eq!(headers.to_string()[], "Set-Cookie: foo=bar; HttpOnly; Path=/p\r\nSet-Cookie: baz=quux; Path=/\r\n");
}

#[test]
fn cookie_jar() {
let jar = CookieJar::new("secret".as_bytes());
let cookie = Cookie::new("foo".to_string(), "bar".to_string());
jar.encrypted().add(cookie);

let cookies = SetCookie::from_cookie_jar(&jar);

let mut new_jar = CookieJar::new("secret".as_bytes());
cookies.apply_to_cookie_jar(&mut new_jar);

assert_eq!(jar.encrypted().find("foo"), new_jar.encrypted().find("foo"));
assert_eq!(jar.iter().collect::<Vec<Cookie>>(), new_jar.iter().collect::<Vec<Cookie>>());
}