Trait rama::http::layer::follow_redirect::policy::Policy

pub trait Policy<S, B, E>:
    Send
    + Sync
    + 'static {
    // Required method
    fn redirect(
        &mut self,
        ctx: &Context<S>,
        attempt: &Attempt<'_>,
    ) -> Result<Action, E>;

    // Provided methods
    fn on_request(&mut self, _ctx: &mut Context<S>, _request: &mut Request<B>) { ... }
    fn clone_body(&mut self, _ctx: &Context<S>, _body: &B) -> Option<B> { ... }
}
Expand description

Trait for the policy on handling redirection responses.

It is important for the policy to be cloneable, where the clone is a fresh instance of the policy, ready to be used in a new request.

§Example

Detecting a cyclic redirection:

use std::collections::HashSet;
use rama_core::Context;
use rama_http::{Request, Uri};
use rama_http::layer::follow_redirect::policy::{Action, Attempt, Policy};

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

impl<S, B, E> Policy<S, B, E> for DetectCycle {
    fn redirect(&mut self, _: &Context<S>, 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, ctx: &Context<S>, attempt: &Attempt<'_>, ) -> Result<Action, E>

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, _ctx: &mut Context<S>, _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.

The default implementation does nothing.

fn clone_body(&mut self, _ctx: &Context<S>, _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.

Implementations on Foreign Types§

§

impl<S, B, E> Policy<S, B, E> for Result<Action, E>
where E: Clone + Send + Sync + 'static,

§

fn redirect(&mut self, _: &Context<S>, _: &Attempt<'_>) -> Result<Action, E>

§

impl<S, B, E, P> Policy<S, B, E> for Box<P>
where P: Policy<S, B, E> + ?Sized,

§

fn redirect( &mut self, ctx: &Context<S>, attempt: &Attempt<'_>, ) -> Result<Action, E>

§

fn on_request(&mut self, ctx: &mut Context<S>, request: &mut Request<B>)

§

fn clone_body(&mut self, ctx: &Context<S>, body: &B) -> Option<B>

Implementors§

§

impl<F, S, B, E> Policy<S, B, E> for CloneBodyFn<F>
where F: FnMut(&B) -> Option<B> + Send + Sync + 'static,

§

impl<S, B, E> Policy<S, B, E> for Action

§

impl<S, B, E> Policy<S, B, E> for FilterCredentials

§

impl<S, B, E> Policy<S, B, E> for Limited

§

impl<S, B, E> Policy<S, B, E> for SameOrigin

§

impl<S, B, E, F> Policy<S, B, E> for RedirectFn<F>
where F: FnMut(&Attempt<'_>) -> Result<Action, E> + Send + Sync + 'static,

§

impl<S, Bd, E, A, B> Policy<S, Bd, E> for And<A, B>
where A: Policy<S, Bd, E>, B: Policy<S, Bd, E>,

§

impl<S, Bd, E, A, B> Policy<S, Bd, E> for Or<A, B>
where A: Policy<S, Bd, E>, B: Policy<S, Bd, E>,