CookieMiddleware

Struct CookieMiddleware 

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

§A middleware for sending received cookies in surf

§File system persistence

This middleware can optionally be constructed with a file or path to enable writing “persistent cookies” to disk after every received response.

§Cloning semantics

All clones of this middleware will refer to the same data and fd (if persistence is enabled).

§Usage example

use surf::Client;
use surf_cookie_middleware::CookieMiddleware;
let client = Client::new().with(CookieMiddleware::new());
// client.get(...).await?;
// client.get(...).await?; <- this request will send any appropriate
//                            cookies received from the first request,
//                            based on request url

Implementations§

Source§

impl CookieMiddleware

Source

pub fn new() -> Self

Builds a new CookieMiddleware

§Example
use surf_cookie_middleware::CookieMiddleware;

let client = surf::Client::new().with(CookieMiddleware::new());

// client.get(...).await?;
// client.get(...).await?; <- this request will send any appropriate
//                            cookies received from the first request,
//                            based on request url

Builds a CookieMiddleware with an existing cookie_store::CookieStore

§Example
use surf_cookie_middleware::{CookieStore, CookieMiddleware};

let cookie_store = CookieStore::default();
let client = surf::Client::new()
    .with(CookieMiddleware::with_cookie_store(cookie_store));
Source

pub async fn from_path(path: impl Into<PathBuf>) -> Result<Self>

Builds a CookieMiddleware from a path to a filesystem cookie jar. These jars are stored in ndjson format. If the file does not exist, it will be created. If the file does exist, the cookie jar will be initialized with those cookies.

Currently this only persists “persistent cookies” – cookies with an expiry. “Session cookies” (without an expiry) are not persisted to disk, nor are expired cookies.

§Example
use surf_cookie_middleware::{CookieStore, CookieMiddleware};

let cookie_store = CookieStore::default();
let client = surf::Client::new()
    .with(CookieMiddleware::from_path("./cookies.ndjson").await?);
Source

pub async fn from_file(file: impl Into<File>) -> Result<Self>

Builds a CookieMiddleware from a File (either async_std::fs::File or std::fs::File) that represents a filesystem cookie jar. These jars are stored in ndjson format. The cookie jar will be initialized with any cookies contained in this file, and persisted to the file after every request.

Currently this only persists “persistent cookies” – cookies with an expiry. “Session cookies” (without an expiry) are not persisted to disk, nor are expired cookies.

§Example
use surf::Client;
use surf_cookie_middleware::{CookieStore, CookieMiddleware};
let cookie_store = CookieStore::default();
let file = std::fs::File::create("./cookies.ndjson")?;
let client = Client::new()
    .with(CookieMiddleware::from_file(file).await?);

Trait Implementations§

Source§

impl Clone for CookieMiddleware

Source§

fn clone(&self) -> CookieMiddleware

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 Debug for CookieMiddleware

Source§

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

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

impl Default for CookieMiddleware

Source§

fn default() -> CookieMiddleware

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

impl Middleware for CookieMiddleware

Source§

fn handle<'life0, 'life1, 'async_trait>( &'life0 self, req: Request, client: Client, next: Next<'life1>, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Asynchronously handle the request, and return a response.

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> 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> 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<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> 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,