Struct Policy

pub struct Policy { /* private fields */ }

A type that controls the policy on how to handle the following of redirects.

The default value will catch redirect loops, and has a maximum of 10 redirects it will follow in a chain before returning an error.

Implementations

impl Policy

fn limited(max: usize) -> Self

Create a Policy with a maximum number of redirects.

An Error will be returned if the max is reached.

fn none() -> Self

Create a Policy that does not follow any redirect.

fn custom<T>(policy: T) -> Self
where
    T: Fn(Attempt<'_>) -> Action + Send + Sync + 'static,

Create a custom Policy using the passed function.

Note

The default Policy handles a maximum loop chain, but the custom variant does not do that for you automatically. The custom policy should have some way of handling those.

Information on the next request and previous requests can be found on the Attempt argument passed to the closure.

Actions can be conveniently created from methods on the Attempt.

Example

# use reqwest::{Error, redirect};
#
# fn run() -> Result<(), Error> {
let custom = redirect::Policy::custom(|attempt| {
    if attempt.previous().len() > 5 {
        attempt.error("too many redirects")
    } else if attempt.url().host_str() == Some("example.domain") {
        // prevent redirects to 'example.domain'
        attempt.stop()
    } else {
        attempt.follow()
    }
});
let client = reqwest::Client::builder()
    .redirect(custom)
    .build()?;
# Ok(())
# }
fn redirect(&self, attempt: Attempt<'_>) -> Action

Apply this policy to a given Attempt to produce a Action.

Note

This method can be used together with Policy::custom() to construct one Policy that wraps another.

Example

# use reqwest::{Error, redirect};
#
# fn run() -> Result<(), Error> {
let custom = redirect::Policy::custom(|attempt| {
    eprintln!("{}, Location: {:?}", attempt.status(), attempt.url());
    redirect::Policy::default().redirect(attempt)
});
# Ok(())
# }

Trait Implementations

impl Debug for Policy

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

impl Default for Policy

fn default() -> Policy

Auto Trait Implementations

impl !RefUnwindSafe for Policy

impl !UnwindSafe for Policy

impl Freeze for Policy

impl Send for Policy

impl Sync for Policy

impl Unpin for Policy

impl UnsafeUnpin for Policy

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> From<T> for Policy

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Policy

impl<T> PolicyExt for Policy where T: ?Sized,

fn and<P, B, E>(self, other: P) -> And<T, P>
where
    T: Sized + Policy<B, E>,
    P: Policy<B, E>,
fn or<P, B, E>(self, other: P) -> Or<T, P>
where
    T: Sized + Policy<B, E>,
    P: Policy<B, E>,

impl<T> WithSubscriber for Policy

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

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, U> TryFrom<U> for Policy where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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