Skip to main content

Module client

Module client 

Source
Available on crate feature client only.
Expand description

Client-side follow-redirects middleware for trillium-client.

This module is gated behind the client feature flag. It provides FollowRedirects, a ClientHandler that automatically follows HTTP redirects (301, 302, 303, 307, 308) up to a configurable limit, with sensible defaults around security-sensitive cases.

§Behavior

On a redirect response, FollowRedirects resolves the Location header against the current request URL, applies the policy below, and re-issues the request through the same client, so the connector and connection pool are reused.

§Method handling

The redirect status determines whether the method changes and whether the request body is replayed:

StatusMethod changeBody
301 Moved PermanentlyPOST → GET, otherwise unchangeddropped if method changed
302 FoundPOST → GET, otherwise unchangeddropped if method changed
303 See Otheralways GETalways dropped
307 Temporary Redirectunchangedreplayed if static, dropped if streaming
308 Permanent Redirectunchangedreplayed if static, dropped if streaming

§Body replay

Static bodies (constructed via Body::new_static or any of the From conversions for Vec<u8>, &'static [u8], String, &'static str, etc.) are cloned and replayed on redirect.

Streaming bodies (constructed via Body::new_streaming) are one-shot. Once consumed by the original request they cannot be replayed, and the redirected request is sent without a body.

§Cross-origin header filtering

When the redirect target’s origin (scheme + host + port) differs from the original, the following headers are dropped from the redirected request to avoid credential leakage:

  • Authorization
  • Cookie
  • Proxy-Authorization

§Defaults

§Example

use trillium_client::Client;
use trillium_redirect::client::FollowRedirects;
use trillium_testing::client_config;

let client =
    Client::new(client_config()).with_handler(FollowRedirects::new().with_max_redirects(5));

Structs§

FollowRedirects
A ClientHandler that automatically follows HTTP redirects.

Enums§

RedirectError
Errors produced by FollowRedirects when a redirect cannot be followed.