Representation of an HTTP cookie.
To construct a cookie with only a name/value, use the new
method:
use cookie::Cookie;
let cookie = Cookie::new("name", "value");
assert_eq!(&cookie.to_string(), "name=value");
To construct more elaborate cookies, use the build method
and CookieBuilder methods:
use cookie::Cookie;
let cookie = Cookie::build("name", "value")
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true)
.finish();
Parses a Cookie from the given HTTP cookie header value string. Does
not perform any percent-decoding.
use cookie::Cookie;
let c = Cookie::parse("foo=bar%20baz; HttpOnly").unwrap();
assert_eq!(c.name_value(), ("foo", "bar%20baz"));
assert_eq!(c.http_only(), Some(true));
Parses a Cookie from the given HTTP cookie header value string where
the name and value fields are percent-encoded. Percent-decodes the
name/value fields.
This API requires the percent-encode feature to be enabled on this
crate.
use cookie::Cookie;
let c = Cookie::parse_encoded("foo=bar%20baz; HttpOnly").unwrap();
assert_eq!(c.name_value(), ("foo", "bar baz"));
assert_eq!(c.http_only(), Some(true));
Wraps self in an EncodedCookie: a cost-free wrapper around Cookie
whose Display implementation percent-encodes the name and value of the
wrapped Cookie.
This method is only available when the percent-encode feature is
enabled.
use cookie::Cookie;
let mut c = Cookie::new("my name", "this; value?");
assert_eq!(&c.encoded().to_string(), "my%20name=this%3B%20value%3F");
Converts self into a Cookie with a static lifetime. This method
results in at most one allocation.
use cookie::Cookie;
let c = Cookie::new("a", "b");
let owned_cookie = c.into_owned();
assert_eq!(owned_cookie.name_value(), ("a", "b"));
Returns the name of self.
use cookie::Cookie;
let c = Cookie::new("name", "value");
assert_eq!(c.name(), "name");
Returns the value of self.
use cookie::Cookie;
let c = Cookie::new("name", "value");
assert_eq!(c.value(), "value");
Returns the name and value of self as a tuple of (name, value).
use cookie::Cookie;
let c = Cookie::new("name", "value");
assert_eq!(c.name_value(), ("name", "value"));
Returns whether this cookie was marked HttpOnly or not. Returns
Some(true) when the cookie was explicitly set (manually or parsed) as
HttpOnly, Some(false) when http_only was manually set to false,
and None otherwise.
use cookie::Cookie;
let c = Cookie::parse("name=value; httponly").unwrap();
assert_eq!(c.http_only(), Some(true));
let mut c = Cookie::new("name", "value");
assert_eq!(c.http_only(), None);
let mut c = Cookie::new("name", "value");
assert_eq!(c.http_only(), None);
c.set_http_only(false);
assert_eq!(c.http_only(), Some(false));
c.set_http_only(true);
assert_eq!(c.http_only(), Some(true));
Returns whether this cookie was marked Secure or not. Returns
Some(true) when the cookie was explicitly set (manually or parsed) as
Secure, Some(false) when secure was manually set to false, and
None otherwise.
use cookie::Cookie;
let c = Cookie::parse("name=value; Secure").unwrap();
assert_eq!(c.secure(), Some(true));
let mut c = Cookie::parse("name=value").unwrap();
assert_eq!(c.secure(), None);
let mut c = Cookie::new("name", "value");
assert_eq!(c.secure(), None);
c.set_secure(false);
assert_eq!(c.secure(), Some(false));
c.set_secure(true);
assert_eq!(c.secure(), Some(true));
Returns the SameSite attribute of this cookie if one was specified.
use cookie::{Cookie, SameSite};
let c = Cookie::parse("name=value; SameSite=Lax").unwrap();
assert_eq!(c.same_site(), Some(SameSite::Lax));
Returns the specified max-age of the cookie if one was specified.
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.max_age(), None);
let c = Cookie::parse("name=value; Max-Age=3600").unwrap();
assert_eq!(c.max_age().map(|age| age.num_hours()), Some(1));
Returns the Path of the cookie if one was specified.
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.path(), None);
let c = Cookie::parse("name=value; Path=/").unwrap();
assert_eq!(c.path(), Some("/"));
let c = Cookie::parse("name=value; path=/sub").unwrap();
assert_eq!(c.path(), Some("/sub"));
Returns the Domain of the cookie if one was specified.
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.domain(), None);
let c = Cookie::parse("name=value; Domain=crates.io").unwrap();
assert_eq!(c.domain(), Some("crates.io"));
Returns the Expires time of the cookie if one was specified.
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.expires(), None);
let expire_time = "Wed, 21 Oct 2017 07:28:00 GMT";
let cookie_str = format!("name=value; Expires={}", expire_time);
let c = Cookie::parse(cookie_str).unwrap();
assert_eq!(c.expires().map(|t| t.tm_year), Some(117));
Sets the name of self to name.
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.name(), "name");
c.set_name("foo");
assert_eq!(c.name(), "foo");
Sets the value of self to value.
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.value(), "value");
c.set_value("bar");
assert_eq!(c.value(), "bar");
Sets the value of http_only in self to value.
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.http_only(), None);
c.set_http_only(true);
assert_eq!(c.http_only(), Some(true));
Sets the value of secure in self to value.
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.secure(), None);
c.set_secure(true);
assert_eq!(c.secure(), Some(true));
Sets the value of same_site in self to value.
use cookie::{Cookie, SameSite};
let mut c = Cookie::new("name", "value");
assert!(c.same_site().is_none());
c.set_same_site(SameSite::Strict);
assert_eq!(c.same_site(), Some(SameSite::Strict));
Sets the value of max_age in self to value.
extern crate time;
use cookie::Cookie;
use time::Duration;
let mut c = Cookie::new("name", "value");
assert_eq!(c.max_age(), None);
c.set_max_age(Duration::hours(10));
assert_eq!(c.max_age(), Some(Duration::hours(10)));
Sets the path of self to path.
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.path(), None);
c.set_path("/");
assert_eq!(c.path(), Some("/"));
Sets the domain of self to domain.
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.domain(), None);
c.set_domain("rust-lang.org");
assert_eq!(c.domain(), Some("rust-lang.org"));
Sets the expires field of self to time.
extern crate time;
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.expires(), None);
let mut now = time::now();
now.tm_year += 1;
c.set_expires(now);
assert!(c.expires().is_some())
Makes self a "permanent" cookie by extending its expiration and max
age 20 years into the future.
extern crate time;
use cookie::Cookie;
use time::Duration;
let mut c = Cookie::new("foo", "bar");
assert!(c.expires().is_none());
assert!(c.max_age().is_none());
c.make_permanent();
assert!(c.expires().is_some());
assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
Returns the name of self as a string slice of the raw string self
was originally parsed from. If self was not originally parsed from a
raw string, returns None.
This method differs from name in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self. If a longer lifetime is not required, or
you're unsure if you need a longer lifetime, use name.
use cookie::Cookie;
let cookie_string = format!("{}={}", "foo", "bar");
let name = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.name_raw()
};
assert_eq!(name, Some("foo"));
Returns the value of self as a string slice of the raw string self
was originally parsed from. If self was not originally parsed from a
raw string, returns None.
This method differs from value in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self. If a longer lifetime is not required, or
you're unsure if you need a longer lifetime, use value.
use cookie::Cookie;
let cookie_string = format!("{}={}", "foo", "bar");
let value = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.value_raw()
};
assert_eq!(value, Some("bar"));
Returns the Path of self as a string slice of the raw string self
was originally parsed from. If self was not originally parsed from a
raw string, or if self doesn't contain a Path, or if the Path has
changed since parsing, returns None.
This method differs from path in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self. If a longer lifetime is not required, or
you're unsure if you need a longer lifetime, use path.
use cookie::Cookie;
let cookie_string = format!("{}={}; Path=/", "foo", "bar");
let path = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.path_raw()
};
assert_eq!(path, Some("/"));
Returns the Domain of self as a string slice of the raw string
self was originally parsed from. If self was not originally parsed
from a raw string, or if self doesn't contain a Domain, or if the
Domain has changed since parsing, returns None.
This method differs from domain in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self struct. If a longer lifetime is not
required, or you're unsure if you need a longer lifetime, use
domain.
use cookie::Cookie;
let cookie_string = format!("{}={}; Domain=crates.io", "foo", "bar");
let domain = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.domain_raw()
};
assert_eq!(domain, Some("crates.io"));