Skip to content

Commit

Permalink
Merge pull request #76 from SergioBenitez/master
Browse files Browse the repository at this point in the history
Overhaul `CookieJar`: more efficient, more secure, more docs.
  • Loading branch information
alexcrichton authored Mar 1, 2017
2 parents 904b337 + 4ba4735 commit 62ae077
Show file tree
Hide file tree
Showing 12 changed files with 1,219 additions and 541 deletions.
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

name = "cookie"
authors = ["Alex Crichton <alex@alexcrichton.com>", "Sergio Benitez <sb@sergio.bz>"]
version = "0.6.1"
version = "0.7.0"
license = "MIT/Apache-2.0"
repository = "https://github.com/alexcrichton/cookie-rs"
documentation = "https://docs.rs/cookie"
description = """
Crate for parsing HTTP cookie headers and managing a cookie jar. Supports
encrypted, signed, and permanent cookie chars composed together in a manner
similar to Rails' cookie jar.
Crate for parsing HTTP cookie headers and managing a cookie jar. Supports signed
and private (encrypted + signed) jars.
"""

[features]
secure = ["openssl", "rustc-serialize"]
secure = ["ring", "rustc-serialize"]
percent-encode = ["url"]

[dependencies]
time = "0.1"
url = { version = "1.0", optional = true }
openssl = { version = "0.9.0", optional = true }
ring = { version = "0.7", optional = true }
rustc-serialize = { version = "0.3", optional = true }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A library for parsing HTTP cookies and managing cookie jars
```toml
# Cargo.toml
[dependencies]
cookie = "0.6"
cookie = "0.7"
```

# License
Expand Down
27 changes: 27 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,33 @@ impl CookieBuilder {
self
}

/// Makes the cookie being built 'permanent' by extending its expiration and
/// max age 20 years into the future.
///
/// # Example
///
/// ```rust
/// # extern crate cookie;
/// extern crate time;
///
/// use cookie::Cookie;
/// use time::Duration;
///
/// # fn main() {
/// let c = Cookie::build("foo", "bar")
/// .permanent()
/// .finish();
///
/// assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
/// # assert!(c.expires().is_some());
/// # }
/// ```
#[inline]
pub fn permanent(mut self) -> CookieBuilder {
self.cookie.make_permanent();
self
}

/// Finishes building and returns the built `Cookie`.
///
/// # Example
Expand Down
71 changes: 71 additions & 0 deletions src/delta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::ops::{Deref, DerefMut};
use std::hash::{Hash, Hasher};
use std::borrow::Borrow;

use Cookie;

/// A `DeltaCookie` is a helper structure used in a cookie jar. It wraps a
/// `Cookie` so that it can be hashed and compared purely by name. It further
/// records whether the wrapped cookie is a "removal" cookie, that is, a cookie
/// that when sent to the client removes the named cookie on the client's
/// machine.
#[derive(Clone, Debug)]
pub struct DeltaCookie {
pub cookie: Cookie<'static>,
pub removed: bool,
}

impl DeltaCookie {
/// Create a new `DeltaCookie` that is being added to a jar.
#[inline]
pub fn added(cookie: Cookie<'static>) -> DeltaCookie {
DeltaCookie {
cookie: cookie,
removed: false,
}
}

/// Create a new `DeltaCookie` that is being removed from a jar. The
/// `cookie` should be a "removal" cookie.
#[inline]
pub fn removed(cookie: Cookie<'static>) -> DeltaCookie {
DeltaCookie {
cookie: cookie,
removed: true,
}
}
}

impl Deref for DeltaCookie {
type Target = Cookie<'static>;

fn deref(&self) -> &Cookie<'static> {
&self.cookie
}
}

impl DerefMut for DeltaCookie {
fn deref_mut(&mut self) -> &mut Cookie<'static> {
&mut self.cookie
}
}

impl PartialEq for DeltaCookie {
fn eq(&self, other: &DeltaCookie) -> bool {
self.name() == other.name()
}
}

impl Eq for DeltaCookie {}

impl Hash for DeltaCookie {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name().hash(state);
}
}

impl Borrow<str> for DeltaCookie {
fn borrow(&self) -> &str {
self.name()
}
}
Loading

0 comments on commit 62ae077

Please sign in to comment.