Struct Policy

struct Policy { ... }

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: &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(())
# }

impl Debug for Policy

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

impl Default for Policy

fn default() -> Policy

impl Freeze for Policy

impl RefUnwindSafe for Policy

impl Send for Policy

impl Sync for Policy

impl Unpin for Policy

impl UnwindSafe for Policy

impl<T> Any for Policy

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Policy

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Policy

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

impl<T> ErasedDestructor for Policy

impl<T> From for Policy

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Policy

impl<T> PolicyExt for Policy

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

impl<T> WithSubscriber for Policy

impl<T, U> Into for Policy

fn into(self: 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 for Policy

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

impl<T, U> TryInto for Policy

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