1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 10:02:38 +01:00
actix-web/actix-http/src/header/shared/quality_item.rs

299 lines
8.8 KiB
Rust
Raw Normal View History

use std::{cmp, convert::TryFrom as _, fmt, str};
use crate::error::ParseError;
use super::Quality;
2019-02-07 22:24:24 +01:00
/// Represents an item with a quality value as defined
/// in [RFC 7231 §5.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1).
2019-02-07 22:24:24 +01:00
///
/// # Parsing and Formatting
/// This wrapper be used to parse header value items that have a q-factor annotation as well as
/// serialize items with a their q-factor.
2019-02-07 22:24:24 +01:00
///
/// # Ordering
/// Since this context of use for this type is header value items, ordering is defined for
/// `QualityItem`s but _only_ considers the item's quality. Order of appearance should be used as
/// the secondary sorting parameter; i.e., a stable sort over the quality values will produce a
/// correctly sorted sequence.
2019-02-07 22:24:24 +01:00
///
/// # Examples
/// ```
/// # use actix_http::header::{QualityItem, q};
/// let q_item: QualityItem<String> = "hello;q=0.3".parse().unwrap();
/// assert_eq!(&q_item.item, "hello");
/// assert_eq!(q_item.quality, q(0.3));
2019-02-07 22:24:24 +01:00
///
/// // note that format is normalized compared to parsed item
/// assert_eq!(q_item.to_string(), "hello; q=0.3");
///
/// // item with q=0.3 is greater than item with q=0.1
/// let q_item_fallback: QualityItem<String> = "abc;q=0.1".parse().unwrap();
/// assert!(q_item > q_item_fallback);
/// ```
2021-12-02 16:25:39 +01:00
#[derive(Debug, Clone, PartialEq, Eq)]
2019-02-07 22:24:24 +01:00
pub struct QualityItem<T> {
/// The wrapped contents of the field.
2019-02-07 22:24:24 +01:00
pub item: T,
2019-02-07 22:24:24 +01:00
/// The quality (client or server preference) for the value.
pub quality: Quality,
}
impl<T> QualityItem<T> {
/// Constructs a new `QualityItem` from an item and a quality value.
///
/// The item can be of any type. The quality should be a value in the range [0, 1].
pub fn new(item: T, quality: Quality) -> Self {
2019-02-07 22:24:24 +01:00
QualityItem { item, quality }
}
/// Constructs a new `QualityItem` from an item, using the maximum q-value.
pub fn max(item: T) -> Self {
Self::new(item, Quality::MAX)
}
/// Constructs a new `QualityItem` from an item, using the minimum q-value.
pub fn min(item: T) -> Self {
Self::new(item, Quality::MIN)
}
2019-02-07 22:24:24 +01:00
}
impl<T: PartialEq> PartialOrd for QualityItem<T> {
2019-02-07 22:24:24 +01:00
fn partial_cmp(&self, other: &QualityItem<T>) -> Option<cmp::Ordering> {
self.quality.partial_cmp(&other.quality)
}
}
impl<T: fmt::Display> fmt::Display for QualityItem<T> {
2019-12-07 19:46:51 +01:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-02-07 22:24:24 +01:00
fmt::Display::fmt(&self.item, f)?;
match self.quality {
// q-factor value is implied for max value
Quality::MAX => Ok(()),
Quality::MIN => f.write_str("; q=0"),
q => write!(f, "; q={}", q),
2019-02-07 22:24:24 +01:00
}
}
}
impl<T: str::FromStr> str::FromStr for QualityItem<T> {
type Err = ParseError;
2019-02-07 22:24:24 +01:00
fn from_str(q_item_str: &str) -> Result<Self, Self::Err> {
if !q_item_str.is_ascii() {
return Err(ParseError::Header);
2019-02-07 22:24:24 +01:00
}
// set defaults used if quality-item parsing fails, i.e., item has no q attribute
let mut raw_item = q_item_str;
let mut quality = Quality::MAX;
2019-02-07 22:24:24 +01:00
let parts = q_item_str
.rsplit_once(';')
.map(|(item, q_attr)| (item.trim(), q_attr.trim()));
if let Some((val, q_attr)) = parts {
// example for item with q-factor:
//
// gzip;q=0.65
// ^^^^ val
// ^^^^^^ q_attr
// ^^ q
// ^^^^ q_val
if q_attr.len() < 2 {
// Can't possibly be an attribute since an attribute needs at least a name followed
// by an equals sign. And bare identifiers are forbidden.
return Err(ParseError::Header);
2019-02-07 22:24:24 +01:00
}
let q = &q_attr[0..2];
if q == "q=" || q == "Q=" {
let q_val = &q_attr[2..];
if q_val.len() > 5 {
// longer than 5 indicates an over-precise q-factor
return Err(ParseError::Header);
2019-02-07 22:24:24 +01:00
}
let q_value = q_val.parse::<f32>().map_err(|_| ParseError::Header)?;
2021-12-08 07:01:11 +01:00
let q_value = Quality::try_from(q_value).map_err(|_| ParseError::Header)?;
quality = q_value;
raw_item = val;
2019-02-07 22:24:24 +01:00
}
}
let item = raw_item.parse::<T>().map_err(|_| ParseError::Header)?;
Ok(QualityItem::new(item, quality))
}
2019-02-07 22:24:24 +01:00
}
#[cfg(test)]
mod tests {
use super::*;
// copy of encoding from actix-web headers
2021-11-16 23:10:30 +01:00
#[allow(clippy::enum_variant_names)] // allow Encoding prefix on EncodingExt
#[derive(Clone, PartialEq, Debug)]
pub enum Encoding {
Chunked,
Brotli,
Gzip,
Deflate,
Compress,
Identity,
Trailers,
EncodingExt(String),
}
impl fmt::Display for Encoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Encoding::*;
f.write_str(match *self {
Chunked => "chunked",
Brotli => "br",
Gzip => "gzip",
Deflate => "deflate",
Compress => "compress",
Identity => "identity",
Trailers => "trailers",
EncodingExt(ref s) => s.as_ref(),
})
}
}
impl str::FromStr for Encoding {
type Err = crate::error::ParseError;
fn from_str(s: &str) -> Result<Encoding, crate::error::ParseError> {
use Encoding::*;
match s {
"chunked" => Ok(Chunked),
"br" => Ok(Brotli),
"deflate" => Ok(Deflate),
"gzip" => Ok(Gzip),
"compress" => Ok(Compress),
"identity" => Ok(Identity),
"trailers" => Ok(Trailers),
_ => Ok(EncodingExt(s.to_owned())),
}
}
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_fmt_q_1() {
use Encoding::*;
let x = QualityItem::max(Chunked);
2019-02-07 22:24:24 +01:00
assert_eq!(format!("{}", x), "chunked");
}
#[test]
fn test_quality_item_fmt_q_0001() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
let x = QualityItem::new(Chunked, Quality(1));
assert_eq!(format!("{}", x), "chunked; q=0.001");
}
#[test]
fn test_quality_item_fmt_q_05() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
// Custom value
let x = QualityItem {
item: EncodingExt("identity".to_owned()),
quality: Quality(500),
};
assert_eq!(format!("{}", x), "identity; q=0.5");
}
#[test]
fn test_quality_item_fmt_q_0() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
// Custom value
let x = QualityItem {
item: EncodingExt("identity".to_owned()),
quality: Quality(0),
};
assert_eq!(x.to_string(), "identity; q=0");
}
#[test]
fn test_quality_item_from_str1() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
let x: Result<QualityItem<Encoding>, _> = "chunked".parse();
assert_eq!(
x.unwrap(),
QualityItem {
item: Chunked,
quality: Quality(1000),
}
);
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_from_str2() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
let x: Result<QualityItem<Encoding>, _> = "chunked; q=1".parse();
assert_eq!(
x.unwrap(),
QualityItem {
item: Chunked,
quality: Quality(1000),
}
);
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_from_str3() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
let x: Result<QualityItem<Encoding>, _> = "gzip; q=0.5".parse();
assert_eq!(
x.unwrap(),
QualityItem {
item: Gzip,
quality: Quality(500),
}
);
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_from_str4() {
use Encoding::*;
2019-02-07 22:24:24 +01:00
let x: Result<QualityItem<Encoding>, _> = "gzip; q=0.273".parse();
assert_eq!(
x.unwrap(),
QualityItem {
item: Gzip,
quality: Quality(273),
}
);
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_from_str5() {
let x: Result<QualityItem<Encoding>, _> = "gzip; q=0.2739999".parse();
assert!(x.is_err());
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_from_str6() {
let x: Result<QualityItem<Encoding>, _> = "gzip; q=2".parse();
assert!(x.is_err());
}
2019-02-07 22:24:24 +01:00
#[test]
fn test_quality_item_ordering() {
let x: QualityItem<Encoding> = "gzip; q=0.5".parse().ok().unwrap();
let y: QualityItem<Encoding> = "gzip; q=0.273".parse().ok().unwrap();
let comparison_result: bool = x.gt(&y);
assert!(comparison_result)
2019-02-07 22:24:24 +01:00
}
#[test]
fn test_fuzzing_bugs() {
assert!("99999;".parse::<QualityItem<String>>().is_err());
assert!("\x0d;;;=\u{d6aa}==".parse::<QualityItem<String>>().is_err())
}
}