Module follow_redirect
http only.Expand description
Middleware for following redirections.
§Overview
The FollowRedirect middleware retries requests with the inner Service to follow HTTP
redirections.
The middleware tries to clone the original Request when making a redirected request.
Request Extensions are carried over to redirected
requests according to the configured RedirectExtensionsBehaviour (preserved — i.e. the
same store is shared — by default). The request body cannot always be cloned. When the original body is
known to be empty by StreamingBody::size_hint, the middleware uses the Default
implementation of the body type to create a new request body. If you know that the body can be
cloned in some way, you can tell the middleware to clone it by configuring a policy.
§Examples
§Basic usage
use rama_core::service::service_fn;
use rama_core::{extensions::ExtensionsRef, Service, Layer};
use rama_http::{Body, Request, Response, StatusCode, header};
use rama_http::layer::follow_redirect::{FollowRedirectLayer, RequestUri};
let mut client = FollowRedirectLayer::new().into_layer(http_client);
let request = Request::builder()
.uri("https://rust-lang.org/")
.body(Body::empty())
.unwrap();
let response = client.serve(request).await?;
// Get the final request URI.
assert_eq!(response.extensions().get_ref::<RequestUri>().unwrap().0, "https://www.rust-lang.org/");§Customizing the Policy
You can use a Policy value to customize how the middleware handles redirections.
use rama_core::service::service_fn;
use rama_core::layer::MapErrLayer;
use rama_core::{Service, Layer};
use rama_http::{Body, Request, Response};
use rama_http::layer::follow_redirect::{
policy::{self, PolicyExt},
FollowRedirectLayer,
};
use rama_core::error::BoxError;
#[derive(Debug)]
enum MyError {
TooManyRedirects,
Other(BoxError),
}
impl MyError {
fn from_std(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Other(BoxError::from(err))
}
}
let policy = policy::Limited::new(10) // Set the maximum number of redirections to 10.
// Return an error when the limit was reached.
.or::<_, (), _>(policy::redirect_fn(|_| Err(MyError::TooManyRedirects)))
// Do not follow cross-origin redirections, and return the redirection responses as-is.
.and::<_, (), _>(policy::SameOrigin::new());
let client = (
FollowRedirectLayer::with_policy(policy),
MapErrLayer::new(MyError::from_std),
).into_layer(http_client);
// ...
_ = client.serve(Request::default()).await?;Modules§
- policy
- Tools for customizing the behavior of a
FollowRedirectmiddleware.
Structs§
- Follow
Redirect - Middleware that retries requests with a
Serviceto follow redirection responses. - Follow
Redirect Layer Layerfor retrying requests with aServiceto follow redirection responses.- Request
Uri - Response [
Extensions][http::Extensions] value that represents the effective request URI of a response returned by aFollowRedirectmiddleware.
Enums§
- Redirect
Extensions Behaviour - Controls how request
Extensionsare carried over to redirected requests.