pub trait Policy<B, E> {
// Required method
fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>;
// Provided methods
fn on_request(&mut self, _request: &mut Request<B>) { ... }
fn clone_body(&self, _body: &B) -> Option<B> { ... }
}tower only.Expand description
Trait for the policy on handling redirection responses.
§Example
Detecting a cyclic redirection:
use http::{Method, Request, Uri};
use std::collections::HashSet;
use tower_http::follow_redirect::policy::{Action, Attempt, Policy};
#[derive(Clone)]
pub struct DetectCycle {
uris: HashSet<(Method, Uri)>,
}
impl<B, E> Policy<B, E> for DetectCycle {
fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E> {
if self.uris.contains(&(attempt.method().clone(), attempt.location().clone())) {
Ok(Action::Stop)
} else {
self.uris.insert((attempt.previous_method().clone(), attempt.previous().clone()));
Ok(Action::Follow)
}
}
}Required Methods§
Provided Methods§
Sourcefn on_request(&mut self, _request: &mut Request<B>)
fn on_request(&mut self, _request: &mut Request<B>)
Invoked right before the service makes a request, regardless of whether it is redirected or not.
This can for example be used to remove sensitive headers from the request or prepare the request in other ways.
On a redirected request, whatever this method leaves on the request becomes the baseline for the next hop, so a value removed here stays removed for the rest of the chain.
The default implementation does nothing.
Sourcefn clone_body(&self, _body: &B) -> Option<B>
fn clone_body(&self, _body: &B) -> Option<B>
Try to clone a request body before the service makes a redirected request.
If the request body cannot be cloned, return None.
This is not invoked when B::size_hint returns zero,
in which case B::default() will be used to create a new request body.
The default implementation returns None.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".