Struct Cookie

Source
pub struct Cookie<'c> { /* private fields */ }
Expand description

Representation of an HTTP cookie.

To construct a cookie with only a name/value, use the new method:

use requiem_http::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 requiem_http::cookie::Cookie;

let cookie = Cookie::build("name", "value")
    .domain("www.rust-lang.org")
    .path("/")
    .secure(true)
    .http_only(true)
    .finish();

Implementations§

Source§

impl Cookie<'static>

Source

pub fn new<N, V>(name: N, value: V) -> Cookie<'static>
where N: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,

Creates a new Cookie with the given name and value.

§Example
use requiem_http::cookie::Cookie;

let cookie = Cookie::new("name", "value");
assert_eq!(cookie.name_value(), ("name", "value"));
Source

pub fn named<N>(name: N) -> Cookie<'static>
where N: Into<Cow<'static, str>>,

Creates a new Cookie with the given name and an empty value.

§Example
use requiem_http::cookie::Cookie;

let cookie = Cookie::named("name");
assert_eq!(cookie.name(), "name");
assert!(cookie.value().is_empty());
Source

pub fn build<N, V>(name: N, value: V) -> CookieBuilder
where N: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,

Creates a new CookieBuilder instance from the given key and value strings.

§Example
use requiem_http::cookie::Cookie;

let c = Cookie::build("foo", "bar").finish();
assert_eq!(c.name_value(), ("foo", "bar"));
Source§

impl<'c> Cookie<'c>

Source

pub fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError>
where S: Into<Cow<'c, str>>,

Parses a Cookie from the given HTTP cookie header value string. Does not perform any percent-decoding.

§Example
use requiem_http::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));
Source

pub fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError>
where 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 requiem_http::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));
Source

pub fn encoded<'a>(&'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 requiem_http::cookie::Cookie;

let mut c = Cookie::new("my name", "this; value?");
assert_eq!(&c.encoded().to_string(), "my%20name=this%3B%20value%3F");
Source

pub 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 requiem_http::cookie::Cookie;

let c = Cookie::new("a", "b");
let owned_cookie = c.into_owned();
assert_eq!(owned_cookie.name_value(), ("a", "b"));
Source

pub fn name(&self) -> &str

Returns the name of self.

§Example
use requiem_http::cookie::Cookie;

let c = Cookie::new("name", "value");
assert_eq!(c.name(), "name");
Source

pub fn value(&self) -> &str

Returns the value of self.

§Example
use requiem_http::cookie::Cookie;

let c = Cookie::new("name", "value");
assert_eq!(c.value(), "value");
Source

pub fn name_value(&self) -> (&str, &str)

Returns the name and value of self as a tuple of (name, value).

§Example
use requiem_http::cookie::Cookie;

let c = Cookie::new("name", "value");
assert_eq!(c.name_value(), ("name", "value"));
Source

pub fn http_only(&self) -> Option<bool>

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 requiem_http::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));
Source

pub fn secure(&self) -> Option<bool>

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 requiem_http::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));
Source

pub fn same_site(&self) -> Option<SameSite>

Returns the SameSite attribute of this cookie if one was specified.

§Example
use requiem_http::cookie::{Cookie, SameSite};

let c = Cookie::parse("name=value; SameSite=Lax").unwrap();
assert_eq!(c.same_site(), Some(SameSite::Lax));
Source

pub fn max_age(&self) -> Option<Duration>

Returns the specified max-age of the cookie if one was specified.

§Example
use requiem_http::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));
Source

pub fn path(&self) -> Option<&str>

Returns the Path of the cookie if one was specified.

§Example
use requiem_http::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"));
Source

pub fn domain(&self) -> Option<&str>

Returns the Domain of the cookie if one was specified.

§Example
use requiem_http::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"));
Source

pub fn expires(&self) -> Option<Tm>

Returns the Expires time of the cookie if one was specified.

§Example
use requiem_http::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));
Source

pub fn set_name<N: Into<Cow<'static, str>>>(&mut self, name: N)

Sets the name of self to name.

§Example
use requiem_http::cookie::Cookie;

let mut c = Cookie::new("name", "value");
assert_eq!(c.name(), "name");

c.set_name("foo");
assert_eq!(c.name(), "foo");
Source

pub fn set_value<V: Into<Cow<'static, str>>>(&mut self, value: V)

Sets the value of self to value.

§Example
use requiem_http::cookie::Cookie;

let mut c = Cookie::new("name", "value");
assert_eq!(c.value(), "value");

c.set_value("bar");
assert_eq!(c.value(), "bar");
Source

pub fn set_http_only(&mut self, value: bool)

Sets the value of http_only in self to value.

§Example
use requiem_http::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));
Source

pub fn set_secure(&mut self, value: bool)

Sets the value of secure in self to value.

§Example
use requiem_http::cookie::Cookie;

let mut c = Cookie::new("name", "value");
assert_eq!(c.secure(), None);

c.set_secure(true);
assert_eq!(c.secure(), Some(true));
Source

pub fn set_same_site(&mut self, value: SameSite)

Sets the value of same_site in self to value.

§Example
use requiem_http::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));
Source

pub fn set_max_age(&mut self, value: Duration)

Sets the value of max_age in self to value.

§Example
use requiem_http::cookie::Cookie;
use chrono::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)));
Source

pub fn set_path<P: Into<Cow<'static, str>>>(&mut self, path: P)

Sets the path of self to path.

§Example
use requiem_http::cookie::Cookie;

let mut c = Cookie::new("name", "value");
assert_eq!(c.path(), None);

c.set_path("/");
assert_eq!(c.path(), Some("/"));
Source

pub fn set_domain<D: Into<Cow<'static, str>>>(&mut self, domain: D)

Sets the domain of self to domain.

§Example
use requiem_http::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"));
Source

pub fn set_expires(&mut self, time: Tm)

Sets the expires field of self to time.

§Example
use requiem_http::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())
Source

pub fn make_permanent(&mut self)

Makes self a “permanent” cookie by extending its expiration and max age 20 years into the future.

§Example
use requiem_http::cookie::Cookie;
use chrono::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)));
Source

pub fn name_raw(&self) -> Option<&'c str>

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.

§Example
use requiem_http::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"));
Source

pub fn value_raw(&self) -> Option<&'c str>

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.

§Example
use requiem_http::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"));
Source

pub fn path_raw(&self) -> Option<&'c str>

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.

§Example
use requiem_http::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("/"));
Source

pub fn domain_raw(&self) -> Option<&'c str>

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.

§Example
use requiem_http::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§

Source§

impl<'c> Clone for Cookie<'c>

Source§

fn clone(&self) -> Cookie<'c>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'c> Debug for Cookie<'c>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'c> Display for Cookie<'c>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the cookie self as a Set-Cookie header value.

§Example
use requiem_http::cookie::Cookie;

let mut cookie = Cookie::build("foo", "bar")
    .path("/")
    .finish();

assert_eq!(&cookie.to_string(), "foo=bar; Path=/");
Source§

impl FromStr for Cookie<'static>

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Cookie<'static>, ParseError>

Parses a string s to return a value of this type. Read more
Source§

impl<'a, 'b> PartialEq<Cookie<'b>> for Cookie<'a>

Source§

fn eq(&self, other: &Cookie<'b>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'c> Freeze for Cookie<'c>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,