Struct rocket::http::Cookie
[−]
[src]
pub struct Cookie<'c> { /* fields omitted */ }
Representation of an HTTP cookie.
Constructing a 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();
Methods
impl Cookie<'static>
[src]
fn new<N, V>(name: N, value: V) -> Cookie<'static> where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
Creates a new Cookie
with the given name and value.
Example
use cookie::Cookie; let cookie = Cookie::new("name", "value"); assert_eq!(cookie.name_value(), ("name", "value"));
fn build<N, V>(name: N, value: V) -> CookieBuilder where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
Creates a new CookieBuilder
instance from the given key and value
strings.
Example
use cookie::Cookie; let c = Cookie::build("foo", "bar").finish(); assert_eq!(c.name_value(), ("foo", "bar"));
impl<'c> Cookie<'c>
[src]
fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError> where
S: Into<Cow<'c, str>>,
S: Into<Cow<'c, str>>,
Parses a Cookie
from the given HTTP cookie header value string. Does
not perform any percent-decoding.
Example
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(), true);
fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError> where
S: Into<Cow<'c, str>>,
S: Into<Cow<'c, str>>,
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.
Example
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(), true);
fn encoded(&'a self) -> EncodedCookie<'a, 'c>
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.
Example
use cookie::Cookie; let mut c = Cookie::new("my name", "this; value?"); assert_eq!(&c.encoded().to_string(), "my%20name=this%3B%20value%3F");
fn into_owned(self) -> Cookie<'static>
Converts self
into a Cookie
with a static lifetime. This method
results in at most one allocation.
Example
use cookie::Cookie; let c = Cookie::new("a", "b"); let owned_cookie = c.into_owned(); assert_eq!(owned_cookie.name_value(), ("a", "b"));
fn name(&self) -> &str
Returns the name of self
.
Example
use cookie::Cookie; let c = Cookie::new("name", "value"); assert_eq!(c.name(), "name");
fn value(&self) -> &str
Returns the value of self
.
Example
use cookie::Cookie; let c = Cookie::new("name", "value"); assert_eq!(c.value(), "value");
fn name_value(&self) -> (&str, &str)
Returns the name and value of self
as a tuple of (name, value)
.
Example
use cookie::Cookie; let c = Cookie::new("name", "value"); assert_eq!(c.name_value(), ("name", "value"));
fn http_only(&self) -> bool
Returns whether this cookie was marked HttpOnly
or not.
Example
use cookie::Cookie; let c = Cookie::parse("name=value; httponly").unwrap(); assert_eq!(c.http_only(), true);
fn secure(&self) -> bool
Returns whether this cookie was marked Secure
or not.
Example
use cookie::Cookie; let c = Cookie::parse("name=value; Secure").unwrap(); assert_eq!(c.secure(), true);
fn max_age(&self) -> Option<Duration>
Returns the specified max-age of the cookie if one was specified.
Example
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));
fn path(&self) -> Option<&str>
Returns the Path
of the cookie if one was specified.
Example
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"));
fn domain(&self) -> Option<&str>
Returns the Domain
of the cookie if one was specified.
Example
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"));
fn expires(&self) -> Option<Tm>
Returns the Expires
time of the cookie if one was specified.
Example
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));
fn set_name<N>(&mut self, name: N) where
N: Into<Cow<'static, str>>,
N: Into<Cow<'static, str>>,
Sets the name of self
to name
.
Example
use cookie::Cookie; let mut c = Cookie::new("name", "value"); assert_eq!(c.name(), "name"); c.set_name("foo"); assert_eq!(c.name(), "foo");
fn set_value<V>(&mut self, value: V) where
V: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
Sets the value of self
to value
.
Example
use cookie::Cookie; let mut c = Cookie::new("name", "value"); assert_eq!(c.value(), "value"); c.set_value("bar"); assert_eq!(c.value(), "bar");
fn set_http_only(&mut self, value: bool)
Sets the value of http_only
in self
to value
.
Example
use cookie::Cookie; let mut c = Cookie::new("name", "value"); assert_eq!(c.http_only(), false); c.set_http_only(true); assert_eq!(c.http_only(), true);
fn set_secure(&mut self, value: bool)
Sets the value of secure
in self
to value
.
Example
use cookie::Cookie; let mut c = Cookie::new("name", "value"); assert_eq!(c.secure(), false); c.set_secure(true); assert_eq!(c.secure(), true);
fn set_max_age(&mut self, value: Duration)
Sets the value of max_age
in self
to value
.
Example
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)));
fn set_path<P>(&mut self, path: P) where
P: Into<Cow<'static, str>>,
P: Into<Cow<'static, str>>,
Sets the path
of self
to path
.
Example
use cookie::Cookie; let mut c = Cookie::new("name", "value"); assert_eq!(c.path(), None); c.set_path("/"); assert_eq!(c.path(), Some("/"));
fn set_domain<D>(&mut self, domain: D) where
D: Into<Cow<'static, str>>,
D: Into<Cow<'static, str>>,
Sets the domain
of self
to domain
.
Example
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"));
fn set_expires(&mut self, time: Tm)
Sets the expires field of self
to time
.
Example
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())
Trait Implementations
impl<'a, 'b> PartialEq<Cookie<'b>> for Cookie<'a>
[src]
fn eq(&self, other: &Cookie<'b>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<'c> Clone for Cookie<'c>
[src]
fn clone(&self) -> Cookie<'c>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<'c> Debug for Cookie<'c>
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter.
impl<'c> Display for Cookie<'c>
[src]
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter. Read more
impl FromStr for Cookie<'static>
[src]
type Err = ParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<Cookie<'static>, ParseError>
Parses a string s
to return a value of this type. Read more