Skip to main content

Cookies

Struct Cookies 

Source
pub struct Cookies(/* private fields */);
Expand description

The request’s cookies; what handlers set goes out with the response.

Signed and encrypted cookies use keys derived from SECRET_KEY_BASE; values made with a key of SECRET_KEY_BASE_PREVIOUS still read. Cookies Ocre sets are HttpOnly, SameSite=Lax, Path=/, and Secure over HTTPS. For per-visitor state prefer the Session, which is encrypted too; use cookies for values that outlive it or that another part of the site reads (a theme, a “remember me” token).

§Free plan

Nothing billed: cookies travel with the requests.

§Examples

use std::time::Duration;
use ocre::{Cookies, Result};

async fn theme(cookies: Cookies) -> Result<String> {
    let theme = cookies.get("theme").unwrap_or_else(|| "light".to_owned());
    cookies.set_signed("seen_banner", "1", Some(Duration::from_secs(30 * 86_400)))?;
    Ok(theme)
}

Implementations§

Source§

impl Cookies

Source

pub fn get(&self, name: &str) -> Option<String>

The value of the plain cookie name.

§Examples
async fn theme(cookies: ocre::Cookies) -> String {
    cookies.get("theme").unwrap_or_default()
}
Source

pub fn signed(&self, name: &str) -> Result<Option<String>>

The value of the signed cookie name; None when absent or changed by the client.

§Errors

Error::Internal when SECRET_KEY_BASE is missing or shorter than 64 characters.

Source

pub fn encrypted(&self, name: &str) -> Result<Option<String>>

The value of the encrypted cookie name; None when absent or changed by the client.

§Errors

Error::Internal when SECRET_KEY_BASE is missing or shorter than 64 characters.

Source

pub fn set( &self, name: &str, value: &str, max_age: Option<Duration>, ) -> Result<()>

Sets a plain cookie; max_age None makes it last until the browser closes.

§Errors

Error::Internal for the session cookie’s name, which Ocre manages.

Source

pub fn set_signed( &self, name: &str, value: &str, max_age: Option<Duration>, ) -> Result<()>

Sets a signed cookie: the browser sees the value, and a changed one reads as None.

§Errors

Error::Internal for the session cookie’s name, or when SECRET_KEY_BASE is missing or shorter than 64 characters.

Source

pub fn set_encrypted( &self, name: &str, value: &str, max_age: Option<Duration>, ) -> Result<()>

Sets an encrypted cookie: the browser can neither read nor change the value.

§Errors

Error::Internal for the session cookie’s name, or when SECRET_KEY_BASE is missing or shorter than 64 characters.

Source

pub fn remove(&self, name: &str)

Deletes the cookie name from the browser.

Trait Implementations§

Source§

impl Clone for Cookies

Source§

fn clone(&self) -> Cookies

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Sync> FromRequestParts<S> for Cookies

Source§

type Rejection = Error

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Error>

Perform the extraction.

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

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

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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, 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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

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

§

fn vzip(self) -> V