Trait tower_http::follow_redirect::policy::Policy[][src]

pub trait Policy<B, E> {
    fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>;

    fn on_request(&mut self, _request: &mut Request<B>) { ... }
fn clone_body(&self, _body: &B) -> Option<B> { ... } }
This is supported on crate feature follow-redirect only.
Expand description

Trait for the policy on handling redirection responses.

Example

Detecting a cyclic redirection:

use http::{Request, Uri};
use std::collections::HashSet;
use tower_http::follow_redirect::policy::{Action, Attempt, Policy};

#[derive(Clone)]
pub struct DetectCycle {
    uris: HashSet<Uri>,
}

impl<B, E> Policy<B, E> for DetectCycle {
    fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E> {
        if self.uris.contains(attempt.location()) {
            Ok(Action::Stop)
        } else {
            self.uris.insert(attempt.previous().clone());
            Ok(Action::Follow)
        }
    }
}

Required methods

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

Invoked when the service received a response with a redirection status code (3xx).

This method returns an Action which indicates whether the service should follow the redirection.

Provided methods

fn on_request(&mut self, _request: &mut Request<B>)[src]

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.

The default implementation does nothing.

fn clone_body(&self, _body: &B) -> Option<B>[src]

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.

Implementations on Foreign Types

impl<B, E, P: ?Sized> Policy<B, E> for &mut P where
    P: Policy<B, E>, 
[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

fn on_request(&mut self, request: &mut Request<B>)[src]

fn clone_body(&self, body: &B) -> Option<B>[src]

impl<B, E, P: ?Sized> Policy<B, E> for Box<P> where
    P: Policy<B, E>, 
[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

fn on_request(&mut self, request: &mut Request<B>)[src]

fn clone_body(&self, body: &B) -> Option<B>[src]

impl<B, E> Policy<B, E> for Result<Action, E> where
    E: Clone
[src]

fn redirect(&mut self, _: &Attempt<'_>) -> Result<Action, E>[src]

Implementors

impl<B, E> Policy<B, E> for Action[src]

fn redirect(&mut self, _: &Attempt<'_>) -> Result<Action, E>[src]

impl<B, E> Policy<B, E> for FilterCredentials[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

fn on_request(&mut self, request: &mut Request<B>)[src]

impl<B, E> Policy<B, E> for Limited[src]

fn redirect(&mut self, _: &Attempt<'_>) -> Result<Action, E>[src]

impl<B, E> Policy<B, E> for SameOrigin[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

impl<B, E, F> Policy<B, E> for RedirectFn<F> where
    F: FnMut(&Attempt<'_>) -> Result<Action, E>, 
[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

impl<Bd, E, A, B> Policy<Bd, E> for And<A, B> where
    A: Policy<Bd, E>,
    B: Policy<Bd, E>, 
[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

fn on_request(&mut self, request: &mut Request<Bd>)[src]

fn clone_body(&self, body: &Bd) -> Option<Bd>[src]

impl<Bd, E, A, B> Policy<Bd, E> for Or<A, B> where
    A: Policy<Bd, E>,
    B: Policy<Bd, E>, 
[src]

fn redirect(&mut self, attempt: &Attempt<'_>) -> Result<Action, E>[src]

fn on_request(&mut self, request: &mut Request<Bd>)[src]

fn clone_body(&self, body: &Bd) -> Option<Bd>[src]

impl<F, B, E> Policy<B, E> for CloneBodyFn<F> where
    F: Fn(&B) -> Option<B>, 
[src]

fn redirect(&mut self, _: &Attempt<'_>) -> Result<Action, E>[src]

fn clone_body(&self, body: &B) -> Option<B>[src]