tez 0.0.2

A lightweight, ergonomic, and fast high-level HTTP client for Rust
Documentation
//! Redirect behavior configured for a client.

use url::Url;

const SAME_ORIGIN_REDIRECT_LIMIT: usize = 10;

/// How a client handles HTTP redirect responses.
///
/// Redirects are disabled by default so a client never makes an unexpected
/// follow-up request. [`Policy::SameOrigin`] enables Tez to follow a bounded
/// set of same-origin redirects without exposing transport callbacks or
/// per-hop knobs.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Policy {
    /// Returns redirect responses without contacting their `Location`
    /// destination.
    #[default]
    NoFollow,
    /// Follows up to ten redirects that remain within the original origin.
    ///
    /// An origin is its scheme, host, and effective port. Cross-origin
    /// redirects are returned to the caller instead of being followed. The
    /// eleventh same-origin redirect is a [`crate::Error`] for which
    /// [`crate::Error::is_redirect`] is true.
    ///
    /// Tez preserves standard HTTP method rewriting for `301`, `302`, and
    /// `303`. It replays buffered bodies for `307` and `308`, but returns the
    /// response when replaying a one-shot streaming body would be required. An
    /// HTTPS-only client rejects an HTTPS-to-HTTP redirect as a redirect error.
    /// Tez never generates `Referer`.
    SameOrigin,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum Decision {
    Follow,
    Return,
    Error,
}

pub(crate) fn decide(
    policy: Policy,
    https_only: bool,
    previous: &Url,
    next: &Url,
    previous_requests: usize,
) -> Decision {
    match policy {
        Policy::NoFollow => Decision::Return,
        Policy::SameOrigin => {
            if https_only && next.scheme() != "https" {
                return Decision::Error;
            }
            if previous.origin() != next.origin() {
                return Decision::Return;
            }
            if previous_requests > SAME_ORIGIN_REDIRECT_LIMIT {
                return Decision::Error;
            }
            Decision::Follow
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn origins_include_scheme_host_and_effective_port() {
        let url = |value| Url::parse(value).unwrap();

        assert_eq!(
            url("http://example.com/path").origin(),
            url("http://example.com:80/elsewhere").origin(),
        );
        assert_eq!(
            url("https://example.com/path").origin(),
            url("https://example.com:443/elsewhere").origin(),
        );
        assert_ne!(
            url("http://example.com").origin(),
            url("https://example.com").origin(),
        );
        assert_ne!(
            url("https://example.com").origin(),
            url("https://api.example.com").origin(),
        );
        assert_ne!(
            url("https://example.com:443").origin(),
            url("https://example.com:8443").origin(),
        );
    }
}