pub struct Negotiator { /* private fields */ }Expand description
The configurable form of the best_* functions.
§Examples
use parse_net::negotiate::{LanguageMatching, MalformedPolicy, Negotiator, Outcome};
let n = Negotiator::new().language_matching(LanguageMatching::BasicFilter);
assert_eq!(n.language(Some(b"en"), &["fr", "en-GB"]).index(), Some(1));
let strict = Negotiator::new().on_malformed(MalformedPolicy::Refuse);
assert!(matches!(strict.media(Some(b"text/html;q=2"), &["text/html"]), Outcome::Refused(_)));Implementations§
Source§impl Negotiator
impl Negotiator
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Default limits, RFC 4647 lookup, malformed headers treated as absent.
Sourcepub const fn limits(self, l: Limits) -> Self
pub const fn limits(self, l: Limits) -> Self
Sets the limits the request headers are parsed with.
Sourcepub const fn language_matching(self, m: LanguageMatching) -> Self
pub const fn language_matching(self, m: LanguageMatching) -> Self
Sets how language ranges are matched.
Sourcepub const fn on_malformed(self, p: MalformedPolicy) -> Self
pub const fn on_malformed(self, p: MalformedPolicy) -> Self
Sets what happens with a header that does not parse.
Sourcepub fn media(&self, accept: Option<&[u8]>, offers: &[&str]) -> Outcome
pub fn media(&self, accept: Option<&[u8]>, offers: &[&str]) -> Outcome
Selects the best of offers (media types such as "text/html") for
Accept.
An offer’s weight is the q of the most specific matching range
(RFC 9110 section 12.5.1); q=0 means not acceptable; ties go to the
earlier offer. An absent header makes every offer acceptable, so the
first wins. An offer that is not a valid media type is never chosen.
Sourcepub fn media_lines<'a, I>(&self, lines: I, offers: &[&str]) -> Outcome
pub fn media_lines<'a, I>(&self, lines: I, offers: &[&str]) -> Outcome
As media, for several Accept field lines read as
one list.
Sourcepub fn language(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome
pub fn language(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome
Selects the best of offers (language tags such as "fr-CA") for
Accept-Language.
With LanguageMatching::Lookup (the default), a range matches an
offer equal to it or reached by removing subtags from the range’s end,
and the exact match has precedence over a shortened one; with
LanguageMatching::BasicFilter, a range matches an offer equal to
it or starting with it and -, the longest range having precedence.
* matches every offer at the lowest precedence. The highest weight
wins; between equal weights the closer match (as lookup returns the
longest truncation), then the earlier offer. Tags compare ASCII
case-insensitively.
Sourcepub fn encoding(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome
pub fn encoding(&self, value: Option<&[u8]>, offers: &[&str]) -> Outcome
Selects the best of offers (content codings such as "br") for
Accept-Encoding (RFC 9110 section 12.5.3).
identity is acceptable unless identity;q=0, or *;q=0 without an
explicit identity; an empty field value means only identity is
acceptable; x-gzip equals gzip and x-compress equals compress.