hyperlane-plugin-canary 23.3.1

A deterministic feature-flag, A/B testing, and progressive canary rollout plugin for the Hyperlane framework with XxHash3_64 basis-point sticky bucketing, header overrides, Context CanaryDecision injection, and X-Variant/Vary headers.
Documentation
use super::*;

/// Computes a deterministic basis-point bucket in `0..10_000` from `experiment` and `subject`.
///
/// # Arguments
///
/// - `&str` - Experiment or feature-flag name.
/// - `&str` - Sticky bucketing subject key (for example user ID or session ID).
///
/// # Returns
///
/// - `u16` - Bucket value in `0..10_000`.
#[inline]
pub fn bucket_basis_points(experiment: &str, subject: &str) -> u16 {
    let mut hasher: XxHash3_64 = XxHash3_64::default();
    hasher.write(experiment.as_bytes());
    hasher.write(&[0xffu8]);
    hasher.write(subject.as_bytes());
    let digest: u64 = hasher.finish();
    (digest % u64::from(BASIS_POINTS_MAX)) as u16
}

/// Extracts a cookie value by `cookie_name` from a raw `Cookie` header without allocating.
///
/// # Arguments
///
/// - `&'a str` - Raw `Cookie` header string.
/// - `&str` - Cookie name to look up.
///
/// # Returns
///
/// - `Option<&'a str>` - Cookie value slice when present.
#[inline]
pub(crate) fn find_cookie_slice<'a>(raw_cookie: &'a str, cookie_name: &str) -> Option<&'a str> {
    for pair in raw_cookie.split(';') {
        let trimmed: &'a str = pair.trim();
        if let Some((k, v)) = trimmed.split_once('=')
            && k.trim() == cookie_name
        {
            return Some(v.trim());
        }
    }
    None
}

/// Normalizes a path prefix by ensuring a leading `/` and trimming trailing `/`.
///
/// # Arguments
///
/// - `&str` - Raw path prefix.
///
/// # Returns
///
/// - `String` - Normalized prefix.
pub(crate) fn normalize_prefix(raw: &str) -> String {
    let trimmed: &str = raw.trim().trim_end_matches('/');
    if trimmed.is_empty() || trimmed == ROOT_SLASH {
        return ROOT_SLASH.to_owned();
    }
    if trimmed.starts_with('/') {
        trimmed.to_owned()
    } else {
        format!("/{trimmed}")
    }
}

/// Reports whether `path` matches `prefix` on a segment boundary.
///
/// # Arguments
///
/// - `&str` - Incoming request path.
/// - `&str` - Normalized path prefix.
///
/// # Returns
///
/// - `bool` - `true` when matched.
#[inline]
pub(crate) fn path_matches_prefix(path: &str, prefix: &str) -> bool {
    if prefix == ROOT_SLASH {
        return true;
    }
    if let Some(rest) = path.strip_prefix(prefix) {
        rest.is_empty() || rest.starts_with('/')
    } else {
        false
    }
}

/// Ensures `header_name` is listed in the `Vary` response header using the pooled header path.
///
/// # Arguments
///
/// - `&mut Response` - Outgoing HTTP response.
/// - `&str` - Header token to add to `Vary`.
#[inline]
pub(crate) fn merge_vary_header(response: &mut Response, header_name: &str) {
    if header_name.is_empty() {
        return;
    }
    if let Some(slot) = response.get_headers().get(VARY) {
        let present: bool = slot.iter().any(|v: &String| {
            v.split(',').any(|tok: &str| {
                let t: &str = tok.trim();
                t == "*" || t.eq_ignore_ascii_case(header_name)
            })
        });
        if !present {
            response.add_header(VARY, header_name);
        }
    } else {
        response.set_header(VARY, header_name);
    }
}