kvarn::vary

Type Alias Vary

source
pub type Vary = RuleSet<Settings>;
Expand description

A set of rules for the vary header.

See Settings::add_rule on adding rules and extensions::RuleSet::add for linking the Settings to paths.

§Examples

use kvarn::prelude::*;

fn test_lang (header: &str) -> &'static str {
    let mut langs = utils::list_header(header);
    langs.sort_by(|l1, l2| {
        l2.quality
            .partial_cmp(&l1.quality)
            .unwrap_or(cmp::Ordering::Equal)
    });

    for lang in &langs {
        // We take the first language; the values are sorted by quality, so the highest will be
        // chosen.
        match lang.value {
            "sv" => return "sv",
            "en-GB" | "en" => return "en-GB",
            _ => ()
        }
    }
    "en-GB"
}

let host = Host::non_secure("localhost", "web", Extensions::default(), host::Options::default());

host.vary.add_mut(
    "/test_lang",
    vary::Settings::empty().add_rule(
        "accept-language",
        |header| Cow::Borrowed(test_lang(header)),
        "en-GB",
    ),
);
host.extensions.add_prepare_single(
    "/test_lang",
    prepare!(req, _host, _path, _addr {
        let æ = req
            .headers()
            .get("accept-language")
            .map(HeaderValue::to_str)
            .and_then(Result::ok)
            .map_or(false, |header| test_lang(header) == "sv");

        let body = if æ {
            "Hej!"
        } else {
            "Hello."
        };

        FatResponse::cache(Response::new(Bytes::from_static(body.as_bytes())))
    }),
);

let data = Data::builder(host).build();
let port_descriptor = PortDescriptor::new(8080, data);

let shutdown_manager = run(run_config![port_descriptor]).await;

Aliased Type§

struct Vary { /* private fields */ }

Implementations§

source§

impl Vary

source

pub fn rules_from_request<'a, T>( &'a self, request: &Request<T>, ) -> Cow<'a, Settings>

Gets the Settings from the ruleset using the path of request.

Trait Implementations§

source§

impl Default for Vary

source§

fn default() -> Self

Returns the “default value” for a type. Read more