Struct rocket::http::Cookie [−][src]
pub struct Cookie<'c> { /* fields omitted */ }Expand description
Representation of an HTTP cookie.
Constructing a Cookie
To construct a cookie with only a name/value, use Cookie::new():
use cookie::Cookie; let cookie = Cookie::new("name", "value"); assert_eq!(&cookie.to_string(), "name=value");
To construct more elaborate cookies, use Cookie::build() and
CookieBuilder methods:
use cookie::Cookie; let cookie = Cookie::build("name", "value") .domain("www.rust-lang.org") .path("/") .secure(true) .http_only(true) .finish();
Implementations
impl Cookie<'static>[src]
impl Cookie<'static>[src]pub fn new<N, V>(name: N, value: V) -> Cookie<'static> where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>, [src]
pub fn new<N, V>(name: N, value: V) -> Cookie<'static> where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>, [src]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"));
pub fn named<N>(name: N) -> Cookie<'static> where
N: Into<Cow<'static, str>>, [src]
pub fn named<N>(name: N) -> Cookie<'static> where
N: Into<Cow<'static, str>>, [src]Creates a new Cookie with the given name and an empty value.
Example
use cookie::Cookie; let cookie = Cookie::named("name"); assert_eq!(cookie.name(), "name"); assert!(cookie.value().is_empty());
pub fn build<N, V>(name: N, value: V) -> CookieBuilder where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>, [src]
pub fn build<N, V>(name: N, value: V) -> CookieBuilder where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>, [src]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]
impl<'c> Cookie<'c>[src]pub fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError> where
S: Into<Cow<'c, str>>, [src]
pub fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError> where
S: Into<Cow<'c, str>>, [src]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(), Some(true));
pub fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError> where
S: Into<Cow<'c, str>>, [src]
pub fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError> where
S: Into<Cow<'c, str>>, [src]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.
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(), Some(true));
pub fn encoded(&'a self) -> EncodedCookie<'a, 'c>[src]
pub fn encoded(&'a self) -> EncodedCookie<'a, 'c>[src]Wraps self in an EncodedCookie: a cost-free wrapper around Cookie
whose Display implementation percent-encodes the name and value of the
wrapped Cookie.
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");
pub fn into_owned(self) -> Cookie<'static>[src]
pub fn into_owned(self) -> Cookie<'static>[src]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"));
pub fn name(&self) -> &str[src]
pub fn name(&self) -> &str[src]Returns the name of self.
Example
use cookie::Cookie; let c = Cookie::new("name", "value"); assert_eq!(c.name(), "name");
pub fn value(&self) -> &str[src]
pub fn value(&self) -> &str[src]Returns the value of self.
Example
use cookie::Cookie; let c = Cookie::new("name", "value"); assert_eq!(c.value(), "value");
pub fn name_value(&self) -> (&str, &str)[src]
pub fn name_value(&self) -> (&str, &str)[src]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"));
pub fn http_only(&self) -> Option<bool>[src]
pub fn http_only(&self) -> Option<bool>[src]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.
Example
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); // An explicitly set "false" value. c.set_http_only(false); assert_eq!(c.http_only(), Some(false)); // An explicitly set "true" value. c.set_http_only(true); assert_eq!(c.http_only(), Some(true));
pub fn secure(&self) -> Option<bool>[src]
pub fn secure(&self) -> Option<bool>[src]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.
Example
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); // An explicitly set "false" value. c.set_secure(false); assert_eq!(c.secure(), Some(false)); // An explicitly set "true" value. c.set_secure(true); assert_eq!(c.secure(), Some(true));
pub fn same_site(&self) -> Option<SameSite>[src]
pub fn same_site(&self) -> Option<SameSite>[src]Returns the SameSite attribute of this cookie if one was specified.
Example
use cookie::{Cookie, SameSite}; let c = Cookie::parse("name=value; SameSite=Lax").unwrap(); assert_eq!(c.same_site(), Some(SameSite::Lax));
pub fn max_age(&self) -> Option<Duration>[src]
pub fn max_age(&self) -> Option<Duration>[src]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));
pub fn path(&self) -> Option<&str>[src]
pub fn path(&self) -> Option<&str>[src]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"));
pub fn domain(&self) -> Option<&str>[src]
pub fn domain(&self) -> Option<&str>[src]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"));
pub fn expires(&self) -> Option<Tm>[src]
pub fn expires(&self) -> Option<Tm>[src]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));
pub fn set_name<N>(&mut self, name: N) where
N: Into<Cow<'static, str>>, [src]
pub fn set_name<N>(&mut self, name: N) where
N: Into<Cow<'static, str>>, [src]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");
pub fn set_value<V>(&mut self, value: V) where
V: Into<Cow<'static, str>>, [src]
pub fn set_value<V>(&mut self, value: V) where
V: Into<Cow<'static, str>>, [src]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");
pub fn set_http_only(&mut self, value: bool)[src]
pub fn set_http_only(&mut self, value: bool)[src]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(), None); c.set_http_only(true); assert_eq!(c.http_only(), Some(true));
pub fn set_secure(&mut self, value: bool)[src]
pub fn set_secure(&mut self, value: bool)[src]Sets the value of secure in self to value.
Example
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));
pub fn set_same_site(&mut self, value: SameSite)[src]
pub fn set_same_site(&mut self, value: SameSite)[src]Sets the value of same_site in self to value.
Example
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));
pub fn set_max_age(&mut self, value: Duration)[src]
pub fn set_max_age(&mut self, value: Duration)[src]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)));
pub fn set_path<P>(&mut self, path: P) where
P: Into<Cow<'static, str>>, [src]
pub fn set_path<P>(&mut self, path: P) where
P: Into<Cow<'static, str>>, [src]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("/"));
pub fn set_domain<D>(&mut self, domain: D) where
D: Into<Cow<'static, str>>, [src]
pub fn set_domain<D>(&mut self, domain: D) where
D: Into<Cow<'static, str>>, [src]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"));
pub fn set_expires(&mut self, time: Tm)[src]
pub fn set_expires(&mut self, time: Tm)[src]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())
pub fn make_permanent(&mut self)[src]
pub fn make_permanent(&mut self)[src]Makes self a “permanent” cookie by extending its expiration and max
age 20 years into the future.
Example
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)));
pub fn name_raw(&self) -> Option<&'c str>[src]
pub fn name_raw(&self) -> Option<&'c str>[src]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 Cookie::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 Cookie::name().
Example
use cookie::Cookie; let cookie_string = format!("{}={}", "foo", "bar"); // `c` will be dropped at the end of the scope, but `name` will live on let name = { let c = Cookie::parse(cookie_string.as_str()).unwrap(); c.name_raw() }; assert_eq!(name, Some("foo"));
pub fn value_raw(&self) -> Option<&'c str>[src]
pub fn value_raw(&self) -> Option<&'c str>[src]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 Cookie::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 Cookie::value().
Example
use cookie::Cookie; let cookie_string = format!("{}={}", "foo", "bar"); // `c` will be dropped at the end of the scope, but `value` will live on let value = { let c = Cookie::parse(cookie_string.as_str()).unwrap(); c.value_raw() }; assert_eq!(value, Some("bar"));
pub fn path_raw(&self) -> Option<&'c str>[src]
pub fn path_raw(&self) -> Option<&'c str>[src]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 Cookie::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 Cookie::path().
Example
use cookie::Cookie; let cookie_string = format!("{}={}; Path=/", "foo", "bar"); // `c` will be dropped at the end of the scope, but `path` will live on let path = { let c = Cookie::parse(cookie_string.as_str()).unwrap(); c.path_raw() }; assert_eq!(path, Some("/"));
pub fn domain_raw(&self) -> Option<&'c str>[src]
pub fn domain_raw(&self) -> Option<&'c str>[src]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 Cookie::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
Cookie::domain().
Example
use cookie::Cookie; let cookie_string = format!("{}={}; Domain=crates.io", "foo", "bar"); //`c` will be dropped at the end of the scope, but `domain` will live on let domain = { let c = Cookie::parse(cookie_string.as_str()).unwrap(); c.domain_raw() }; assert_eq!(domain, Some("crates.io"));
Trait Implementations
impl FromStr for Cookie<'static>[src]
impl FromStr for Cookie<'static>[src]type Err = ParseError
type Err = ParseErrorThe associated error which can be returned from parsing.
Auto Trait Implementations
impl<'c> RefUnwindSafe for Cookie<'c>
impl<'c> Send for Cookie<'c>
impl<'c> Sync for Cookie<'c>
impl<'c> Unpin for Cookie<'c>
impl<'c> UnwindSafe for Cookie<'c>
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]pub fn borrow_mut(&mut self) -> &mut T[src]
pub fn borrow_mut(&mut self) -> &mut T[src]Mutably borrows from an owned value. Read more
impl<T> IntoCollection<T> for T[src]
impl<T> IntoCollection<T> for T[src]pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>, [src]
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>, [src]Converts self into a collection.
pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>, [src]
F: FnMut(T) -> U,
A: Array<Item = U>,
impl<T> Same<T> for T
impl<T> Same<T> for Ttype Output = T
type Output = TShould always be Self
impl<T> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, [src]type Owned = T
type Owned = TThe resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn to_owned(&self) -> T[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)[src]
pub fn clone_into(&self, target: &mut T)[src]🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,