pub struct Policy { /* private fields */ }Expand description
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.
limitedcan be used have the same as the default behavior, but adjust the allowed maximum redirect hops in a chain.nonecan be used to disable all redirect behavior.customcan be used to create a customized policy.
Implementations§
Source§impl Policy
impl Policy
Sourcepub fn limited(max: usize) -> Policy
pub fn limited(max: usize) -> Policy
Create a Policy with a maximum number of redirects.
An Error will be returned if the max is reached.
Sourcepub fn custom<T>(policy: T) -> Policy
pub fn custom<T>(policy: T) -> Policy
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
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()?;Sourcepub fn redirect(&self, attempt: Attempt<'_>) -> Action
pub 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
let custom = redirect::Policy::custom(|attempt| {
eprintln!("{}, Location: {:?}", attempt.status(), attempt.url());
redirect::Policy::default().redirect(attempt)
});Trait Implementations§
Auto Trait Implementations§
impl Freeze for Policy
impl !RefUnwindSafe for Policy
impl Send for Policy
impl Sync for Policy
impl Unpin for Policy
impl !UnwindSafe for Policy
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more