1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
//! Provides a struct [`Accept`] which implements [`Header`] and owns a list of
//! [`MediaTypeBuf`] in precedence order.
//!
//! See [RFC 9110, 12.5.1 Accept](https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.1).
//!
//! # Examples
//!
//! ```rust
//! use std::str::FromStr;
//!
//! use headers_accept::Accept;
//! use mediatype::MediaTypeBuf;
//!
//! let accept = Accept::from_str("audio/*; q=0.2, audio/basic").unwrap();
//! let mut media_types = accept.media_types();
//! assert_eq!(
//! media_types.next(),
//! Some(&MediaTypeBuf::from_str("audio/basic").unwrap())
//! );
//! assert_eq!(
//! media_types.next(),
//! Some(&MediaTypeBuf::from_str("audio/*; q=0.2").unwrap())
//! );
//! assert_eq!(media_types.next(), None);
//! ```
//!
//! Content type negotiation is also facilitated through a method,
//! [`negotiate`](Accept::negotiate), which allows a user agent and server to
//! determine the best shared format.
//!
//! ```rust
//! # use std::str::FromStr;
//! # use headers_accept::Accept;
//! # use mediatype::{names::*, values::*, MediaType, MediaTypeBuf};
//! const TEXT_HTML: MediaType = MediaType::new(TEXT, HTML);
//! const APPLICATION_JSON: MediaType = MediaType::new(APPLICATION, JSON);
//!
//! const AVAILABLE: &[MediaType] = &[TEXT_HTML, APPLICATION_JSON];
//!
//! let accept = Accept::from_str(
//! "text/html, application/xhtml+xml, application/xml;q=0.9, text/*;q=0.7, text/csv;q=0",
//! )
//! .unwrap();
//!
//! assert_eq!(accept.negotiate(AVAILABLE), Some(&TEXT_HTML));
//! ```
#![warn(
clippy::all,
nonstandard_style,
future_incompatible,
missing_debug_implementations
)]
#![deny(missing_docs)]
#![forbid(unsafe_code)]
use std::{
cmp::Ordering,
collections::BTreeMap,
fmt::{self, Display},
str::FromStr,
};
use headers_core::{Error as HeaderError, Header, HeaderName, HeaderValue};
use mediatype::{names, MediaType, MediaTypeBuf, ReadParams};
/// Represents a parsed `Accept` HTTP header.
///
/// This struct holds a list of `MediaTypeBuf` which are sorted based on
/// their specificity and the value of the `q` (quality) parameter. In the
/// absence of a `q` parameter, media types are assumed to have the highest
/// priority. When media types have equal quality parameters, they maintain the
/// order in which they were originally specified.
#[derive(Debug)]
pub struct Accept(Vec<MediaTypeBuf>);
impl Accept {
/// Creates an iterator over the `MediaTypeBuf` entries in the `Accept`
/// header.
///
/// The media types are returned in the order determined by their
/// specificity and the value of their `q` parameter. Media types with
/// the same `q` value retain their initial relative ordering from the
/// original header.
pub fn media_types(&self) -> impl Iterator<Item = &MediaTypeBuf> {
self.0.iter()
}
/// Determine the most acceptable media type from a list of media types
/// available from the server.
///
/// The intent here is that the server knows what formats it is capable of
/// delivering, and passes that list to this method. The `Accept`
/// instance knows what types the client is willing to accept, and works
/// through that list in order of quality until a match is found.
///
/// If no agreement on a media type can be reached, then this method returns
/// `None`.
///
/// # Tiebreaking
///
/// Firstly, this method obeys RFC9110 s12.5.1's rules around media range
/// specificity:
///
/// > Media ranges can be overridden by more specific media ranges or
/// > specific media types. If
/// > more than one media range applies to a given type, the most specific
/// > reference has
/// > precedence.
///
/// Next, if two types in the list of acceptable types have the same quality
/// score, and both are in the `available` list, then the type that is
/// listed first in the list of acceptable types will be chosen. For
/// example, if the client provides `Accept: text/html, text/plain`, and
/// the `available` list is `application/json, text/plain, text/html`,
/// then `text/html` will be chosen, as it is deemed to be the client's
/// preferred option, based on the order in the `Accept` header.
///
/// Finally, the order of the types in the `available` parameter should
/// match the server's preference for delivery. In the event that two
/// `available` types match the *same* entry in the list of acceptable
/// types, then the first entry in the `available` list will be chosen.
/// For example, if the client provides `Accept: text/html, image/*;q=0.8`,
/// and the `available` list is `image/png, image/gif`, then `image/png`
/// will be returned, because it is the first entry in the `available`
/// list.
///
/// # Caveats
///
/// Don't put wildcard types or the `q` parameter in the `available` list;
/// if you do, all bets are off as to what might happen.
pub fn negotiate<'a, Available>(&self, available: Available) -> Option<&MediaType<'a>>
where
Available: IntoIterator<Item = &'a MediaType<'a>>,
{
let mut best = BestMediaType::default();
available
.into_iter()
.enumerate()
.map(|(given_priority, available_type)| {
if let Some(matched_range) = self
.0
.iter()
.enumerate()
.find(|(_, available_range)| MediaRange(available_range) == *available_type)
{
let quality = Self::parse_q_value(matched_range.1);
BestMediaType {
quality,
parsed_priority: matched_range.0,
given_priority,
ty: Some(available_type),
}
} else {
BestMediaType::default()
}
})
.for_each(|new_best| {
if new_best.quality > best.quality
|| new_best.quality == best.quality
&& (new_best.parsed_priority, new_best.given_priority)
< (best.parsed_priority, best.given_priority)
{
best = new_best
}
});
best.ty
}
fn parse(mut s: &str) -> Result<Self, HeaderError> {
let mut media_types = Vec::new();
// Parsing adapted from `mediatype::MediaTypeList`.
//
// See: https://github.com/picoHz/mediatype/blob/29921e91f7176784d4ed1fe42ca40f8a8f225941/src/media_type_list.rs#L34-L63
while !s.is_empty() {
// Skip initial whitespace.
if let Some(index) = s.find(|c: char| !is_ows(c)) {
s = &s[index..];
} else {
break;
}
let mut end = 0;
let mut quoted = false;
let mut escaped = false;
for c in s.chars() {
if escaped {
escaped = false;
} else {
match c {
'"' => quoted = !quoted,
'\\' if quoted => escaped = true,
',' if !quoted => break,
_ => (),
}
}
end += c.len_utf8();
}
// Parse the media type from the current segment.
match MediaTypeBuf::from_str(s[..end].trim()) {
Ok(mt) => media_types.push(mt),
Err(_) => return Err(HeaderError::invalid()),
}
// Move past the current segment.
s = s[end..].trim_start_matches(',');
}
// Sort media types relative to their `q` parameter.
media_types.sort_by(|a, b| {
let spec_a = Self::parse_specificity(a);
let spec_b = Self::parse_specificity(b);
let q_a = Self::parse_q_value(a);
let q_b = Self::parse_q_value(b);
spec_b
.cmp(&spec_a)
.then_with(|| q_b.partial_cmp(&q_a).unwrap_or(Ordering::Equal))
});
Ok(Self(media_types))
}
fn parse_q_value(media_type: &MediaTypeBuf) -> f32 {
media_type
.get_param(names::Q)
.and_then(|v| v.as_str().parse().ok())
.unwrap_or(1.0)
}
fn parse_specificity(media_type: &MediaTypeBuf) -> usize {
let type_specificity = if media_type.ty() != "*" { 1 } else { 0 };
let subtype_specificity = if media_type.subty() != "*" { 1 } else { 0 };
let parameter_count = media_type
.params()
.filter(|&(name, _)| name != names::Q)
.count();
type_specificity + subtype_specificity + parameter_count
}
}
// See: https://docs.rs/headers/0.4.0/headers/#implementing-the-header-trait
impl Header for Accept {
fn name() -> &'static HeaderName {
&http::header::ACCEPT
}
fn decode<'i, I>(values: &mut I) -> Result<Self, HeaderError>
where
I: Iterator<Item = &'i HeaderValue>,
{
let value = values.next().ok_or_else(HeaderError::invalid)?;
let value_str = value.to_str().map_err(|_| HeaderError::invalid())?;
Self::parse(value_str)
}
fn encode<E>(&self, values: &mut E)
where
E: Extend<HeaderValue>,
{
let value = HeaderValue::from_str(&self.to_string())
.expect("Header value should only contain visible ASCII characters (32-127)");
values.extend(std::iter::once(value));
}
}
impl FromStr for Accept {
type Err = HeaderError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s).map_err(|_| HeaderError::invalid())
}
}
impl Display for Accept {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let media_types = self
.0
.iter()
.map(|mt| mt.to_string())
.collect::<Vec<_>>()
.join(", ");
write!(f, "{}", media_types)
}
}
// Copied directly from `mediatype::parse` as the module is private.
//
// See: https://github.com/picoHz/mediatype/blob/29921e91f7176784d4ed1fe42ca40f8a8f225941/src/parse.rs#L136-L138
const fn is_ows(c: char) -> bool {
c == ' ' || c == '\t'
}
struct MediaRange<'a>(&'a MediaTypeBuf);
impl PartialEq<MediaType<'_>> for MediaRange<'_> {
fn eq(&self, other: &MediaType<'_>) -> bool {
let (type_match, subtype_match, suffix_match) = (
self.0.ty() == other.ty,
self.0.subty() == other.subty,
self.0.suffix() == other.suffix,
);
let wildcard_type = self.0.ty() == names::_STAR;
let wildcard_subtype = self.0.subty() == names::_STAR && type_match;
let exact_match =
type_match && subtype_match && suffix_match && self.0.params().count() == 0;
let params_match = type_match && subtype_match && suffix_match && {
let self_params = self
.0
.params()
.filter(|&(name, _)| name != names::Q)
.collect::<BTreeMap<_, _>>();
let other_params = other
.params()
.filter(|&(name, _)| name != names::Q)
.collect::<BTreeMap<_, _>>();
self_params == other_params
};
wildcard_type || wildcard_subtype || exact_match || params_match
}
}
#[derive(Default)]
struct BestMediaType<'ty> {
quality: f32,
parsed_priority: usize,
given_priority: usize,
ty: Option<&'ty MediaType<'ty>>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reordering() {
let accept = Accept::from_str("audio/*; q=0.2, audio/basic").unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("audio/basic").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("audio/*; q=0.2").unwrap())
);
assert_eq!(media_types.next(), None);
}
#[test]
fn reordering_elaborate() {
let accept =
Accept::from_str("text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c").unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/html").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/x-c").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/x-dvi; q=0.8").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/plain; q=0.5").unwrap())
);
assert_eq!(media_types.next(), None);
}
#[test]
fn preserve_ordering() {
let accept = Accept::from_str("x/y, a/b").unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("x/y").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("a/b").unwrap())
);
assert_eq!(media_types.next(), None);
}
#[test]
fn params() {
let accept =
Accept::from_str("text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8")
.unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/html").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("application/xhtml+xml").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("application/xml;q=0.9").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("*/*;q=0.8").unwrap())
);
assert_eq!(media_types.next(), None);
}
#[test]
fn quoted_params() {
let accept = Accept::from_str(
"text/html; message=\"Hello, world!\", application/xhtml+xml; message=\"Hello, \
world?\"",
)
.unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/html; message=\"Hello, world!\"").unwrap())
);
assert_eq!(
media_types.next(),
Some(
&MediaTypeBuf::from_str("application/xhtml+xml; message=\"Hello, world?\"")
.unwrap()
)
);
assert_eq!(media_types.next(), None);
}
#[test]
fn more_specifics() {
let accept = Accept::from_str("text/*, text/plain, text/plain;format=flowed, */*").unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/plain;format=flowed").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/plain").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/*").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("*/*").unwrap())
);
assert_eq!(media_types.next(), None);
}
#[test]
fn variable_quality_more_specifics() {
let accept = Accept::from_str(
"text/*;q=0.3, text/plain;q=0.7, text/csv;q=0, text/plain;format=flowed, \
text/plain;format=fixed;q=0.4, */*;q=0.5",
)
.unwrap();
let mut media_types = accept.media_types();
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/plain;format=flowed").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/plain;format=fixed;q=0.4").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/plain;q=0.7").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/csv;q=0").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("text/*;q=0.3").unwrap())
);
assert_eq!(
media_types.next(),
Some(&MediaTypeBuf::from_str("*/*;q=0.5").unwrap())
);
assert_eq!(media_types.next(), None);
}
#[test]
fn negotiate() {
let accept = Accept::from_str(
"text/html, application/xhtml+xml, application/xml;q=0.9, text/*;q=0.7, text/csv;q=0",
)
.unwrap();
// Pick the only available type that's acceptable
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/html").unwrap(),
MediaType::parse("application/json").unwrap()
])
.unwrap(),
&MediaType::parse("text/html").unwrap()
);
// Pick the type that's first in the acceptable list
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("application/xhtml+xml").unwrap(),
MediaType::parse("text/html").unwrap()
])
.unwrap(),
&MediaType::parse("text/html").unwrap()
);
// Pick the only available type that's acceptable by wildcard subtype
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/plain").unwrap(),
MediaType::parse("image/gif").unwrap()
])
.unwrap(),
&MediaType::parse("text/plain").unwrap()
);
// Pick the first available type that matches the wildcard
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("image/gif").unwrap(),
MediaType::parse("text/plain").unwrap(),
MediaType::parse("text/troff").unwrap(),
])
.unwrap(),
&MediaType::parse("text/plain").unwrap()
);
// No acceptable type
assert_eq!(
accept.negotiate(&vec![
MediaType::parse("image/gif").unwrap(),
MediaType::parse("image/png").unwrap()
]),
None
);
// Type excluded by q=0
assert_eq!(
accept.negotiate(&vec![
MediaType::parse("image/gif").unwrap(),
MediaType::parse("text/csv").unwrap()
]),
None
);
}
#[test]
fn negotiate_with_full_wildcard() {
let accept =
Accept::from_str("text/html, text/*;q=0.7, */*;q=0.1, text/csv;q=0.0").unwrap();
// Pick the literal match
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/html").unwrap(),
MediaType::parse("application/json").unwrap()
])
.unwrap(),
&MediaType::parse("text/html").unwrap()
);
// Pick the only available type that's acceptable by wildcard subtype
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/plain").unwrap(),
MediaType::parse("image/gif").unwrap()
])
.unwrap(),
&MediaType::parse("text/plain").unwrap()
);
// Pick the server's first match of subtype wildcard
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/javascript").unwrap(),
MediaType::parse("text/plain").unwrap()
])
.unwrap(),
&MediaType::parse("text/javascript").unwrap()
);
// Pick the server's first match of full wildcard
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("image/gif").unwrap(),
MediaType::parse("image/png").unwrap()
])
.unwrap(),
&MediaType::parse("image/gif").unwrap()
);
// Exclude q=0 type
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/csv").unwrap(),
MediaType::parse("text/javascript").unwrap()
])
.unwrap(),
&MediaType::parse("text/javascript").unwrap()
);
}
#[test]
fn negotiate_diabolically() {
let accept = Accept::from_str(
"text/*;q=0.3, text/csv;q=0.2, text/plain;q=0.7, text/plain;format=rot13;q=0.7, \
text/plain;format=flowed, text/plain;format=fixed;q=0.4, */*;q=0.5",
)
.unwrap();
// Pick the highest available q
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/html").unwrap(),
MediaType::parse("text/plain").unwrap()
])
.unwrap(),
&MediaType::parse("text/plain").unwrap()
);
// Pick the more-specific match with the same quality
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/plain").unwrap(),
MediaType::parse("text/plain;format=rot13").unwrap(),
])
.unwrap(),
&MediaType::parse("text/plain;format=rot13").unwrap()
);
// Pick the higher-quality match, despite specificity
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/plain").unwrap(),
MediaType::parse("text/plain;format=fixed").unwrap()
])
.unwrap(),
&MediaType::parse("text/plain").unwrap()
);
// This one is the real madness -- disregard a subtype wildcard with a lower
// quality in favour of a full wildcard match
assert_eq!(
accept
.negotiate(&vec![
MediaType::parse("text/html").unwrap(),
MediaType::parse("image/gif").unwrap()
])
.unwrap(),
&MediaType::parse("image/gif").unwrap()
);
}
}