Cookie

Struct Cookie 

Source
pub struct Cookie { /* private fields */ }
Expand description

An HTTP Cookie.

Implementations§

Source

pub fn expires_time(&self) -> Option<OffsetDateTime>

Returns the Expires attribute using a OffsetDateTime.

Source

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

Returns the Max-Age attribute using a Duration.

Source

pub fn expires_chrono(&self) -> Option<DateTime<Utc>>

Returns the Expires attribute using chrono::DateTime.

Source

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

Returns the Max-Age attribute using chrono::Duration.

Source

pub fn expires_jiff(&self) -> Option<&Zoned>

Returns the Expires attribute using a Zoned.

Source

pub fn max_age_jiff(&self) -> Option<SignedDuration>

Returns the Max-Age attribute using a SignedDuration.

Source

pub fn expires_is_set(&self) -> bool

If the Expires attribute is not set, the expiration of the cookie is tied to the session with the user-agent.

Parses the given cookie header value. Errors when:

  • No ‘=’ is found.
  • The name is empty.
  • The name contains an invalid character.
  • The cookie value contains an invalid character.

Since this only parses a cookie header value, it does not parse any cookie attributes.

Parses a percent encoded cookie value. Errors when:

  • No ‘=’ is found.
  • The name is empty.
  • The name contains an invalid character.
  • The cookie value contains an invalid character.

Since this only parses a cookie header value, it does not parse any cookie attributes.

Source

pub fn serialize(&self) -> Result<String, Error>

Serializes the cookie. Errors when:

  • The name is empty.
  • Name or value contain invalid cookie character.
  • Path attribute is empty.
  • Path attribute does not start with a leading ‘/’.
  • Path attribute contains an invalid cookie character.

Ignores domains with invalid cookie characters.

Source

pub fn serialize_encoded(&self) -> Result<String, Error>

Serializes and percent encodes the cookie. Errors when:

  • The name is empty.
  • Path attribute is empty.
  • Path attribute does not start with a leading ‘/’.
  • Path attribute contains an invalid cookie character.

Ignores domains with invalid cookie characters.

Source

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

Creates a new cookie with the given name and value.

§Example
use cookie_monster::Cookie;

let cookie = Cookie::new("hello", "world");

assert_eq!(cookie.name(), "hello");
assert_eq!(cookie.value(), "world");

For more options, see Cookie::build.

Source

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

Creates a cookie that can be used to remove the cookie from the user-agent. This sets the Expires attribute in the past and Max-Age to 0 seconds.

If one of the time, chrono or jiff features are enabled, the Expires tag is set to the current time minus one year. If none of the those features are enabled, the Expires attribute is set to 1 Jan 1970 00:00.

To ensure a cookie is removed from the user-agent, set the Path and Domain attributes with the same values that were used to create the cookie.

§Note

You don’t have to use this method in combination with CookieJar::remove, the jar automatically set’s the Expires and Max-Age attributes.

§Example
use cookie_monster::Cookie;

let cookie = Cookie::remove("session");

assert_eq!(cookie.max_age_secs(), Some(0));
assert!(cookie.expires_is_set());
Source

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

Build a new cookie. This returns a CookieBuilder that can be used to set other attribute values.

§Example
use cookie_monster::Cookie;

let cookie = Cookie::build("foo", "bar")
    .secure()
    .http_only()
    .build();

assert!(cookie.secure());
assert!(cookie.http_only());
Source

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

Creates a CookieBuilder with the given name and an empty value. This can be used when removing a cookie from a CookieJar.

§Example
use cookie_monster::{Cookie, CookieJar};

let mut jar = CookieJar::empty();
jar.remove(Cookie::named("session").path("/login"));

assert!(jar.get("session").is_none());
Source

pub fn name(&self) -> &str

Returns the cookie name.

Source

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

Set the cookie name.

Source

pub fn value(&self) -> &str

Get the cookie value. This does not trim " characters.

Source

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

Set the cookie value.

Source

pub fn set_expires<E: Into<Expires>>(&mut self, expires: E)

Set the Expired attribute.

Source

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

Get the Max-Age duration. This returns a std::time::Duration.

If you’d like a time, chrono or jiff specific duration use the max_age_{time,chrono,jiff} methods.

Source

pub fn max_age_secs(&self) -> Option<u64>

Get the Max-Age as seconds.

Source

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

Set the Max-Age attribute.

Source

pub fn set_max_age_secs(&mut self, max_age_secs: u64)

Set the Max-Age value in seconds.

Source

pub fn unset_max_age(&mut self)

Removes the Max-Age attribute.

Source

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

Returns the Domain attribute if it’s set.

Source

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

Set the Domain attribute.

Source

pub fn unset_domain(&mut self)

Removes the Domain attribute.

Source

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

Returns the Path attribute if it’s set.

Source

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

Set the Path attribute.

Source

pub fn unset_path(&mut self)

Removes the path attribute.

Source

pub fn secure(&self) -> bool

Returns if the Secure attribute is set.

Source

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

Sets the Secure attribute of the cookie.

Source

pub fn http_only(&self) -> bool

Returns if the HttpOnly attribute is set.

Source

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

Sets the HttpOnly attribute of the cookie.

Source

pub fn partitioned(&self) -> bool

Returns if the Partitioned attribute is set.

Source

pub fn set_partitioned(&mut self, partitioned: bool)

Set the Partitioned flag, enabling the Partitioned attribute also enables the Secure Attribute.

Source

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

Returns the SameSite attribute if it is set.

Source

pub fn set_same_site<S: Into<Option<SameSite>>>(&mut self, same_site: S)

Set the SameSite attribute.

Trait Implementations§

Source§

impl Borrow<Cookie> for CookieBuilder

Source§

fn borrow(&self) -> &Cookie

Immutably borrows from an owned value. Read more
Source§

fn clone(&self) -> Cookie

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§

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

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

fn default() -> Cookie

Returns the “default value” for a type. Read more
Source§

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

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

fn from(value: CookieBuilder) -> Self

Converts to this type from the input type.
Source§

fn into_response(self) -> Response

Create a response.
Source§

type Error = Infallible

The type returned in the event of an error. Read more
Source§

fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::Error>

Set parts of the response
Source§

fn eq(&self, other: &Cookie) -> 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§

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> 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.