|
1 | | -use util::QualityValue; |
2 | | -// use HeaderValue; |
3 | | - |
| 1 | +use std::convert::TryFrom; |
| 2 | +use util::{QualityValue, TryFromValues}; |
| 3 | +use HeaderValue; |
4 | 4 |
|
5 | 5 | /// `Accept-Encoding` header, defined in |
6 | 6 | /// [RFC7231](https://tools.ietf.org/html/rfc7231#section-5.3.4) |
7 | | -/// |
| 7 | +/// |
| 8 | +/// The `Accept-Encoding` header field can be used by user agents to |
| 9 | +/// indicate what response content-codings are acceptable in the response. |
| 10 | +/// An "identity" token is used as a synonym for "no encoding" in |
| 11 | +/// order to communicate when no encoding is preferred. |
| 12 | +/// |
| 13 | +/// # ABNF |
| 14 | +/// |
| 15 | +/// ```text |
| 16 | +/// Accept-Encoding = #( codings [ weight ] ) |
| 17 | +/// codings = content-coding / "identity" / "*" |
| 18 | +/// ``` |
| 19 | +/// |
| 20 | +/// # Example Values |
| 21 | +/// |
| 22 | +/// * `gzip` |
| 23 | +/// * `br;q=1.0, gzip;q=0.8` |
| 24 | +/// |
8 | 25 | #[derive(Clone, Debug)] |
9 | 26 | pub struct AcceptEncoding(QualityValue); |
10 | 27 |
|
11 | 28 | derive_header! { |
12 | 29 | AcceptEncoding(_), |
13 | 30 | name: ACCEPT_ENCODING |
14 | 31 | } |
| 32 | + |
| 33 | +impl AcceptEncoding { |
| 34 | + /// Convience method |
| 35 | + #[inline] |
| 36 | + pub fn gzip() -> AcceptEncoding { |
| 37 | + AcceptEncoding(HeaderValue::from_static("gzip").into()) |
| 38 | + } |
| 39 | + |
| 40 | + /// A convience method to create an Accept-Encoding header from pairs of values and qualities |
| 41 | + /// |
| 42 | + /// # Example |
| 43 | + /// |
| 44 | + /// ``` |
| 45 | + /// use headers::AcceptEncoding; |
| 46 | + /// |
| 47 | + /// let pairs = vec![("gzip", 1.0), ("deflate", 0.8)]; |
| 48 | + /// let header = AcceptEncoding::from_quality_pairs(&mut pairs.into_iter()); |
| 49 | + /// ``` |
| 50 | + pub fn from_quality_pairs<'i, I>(pairs: &mut I) -> Result<AcceptEncoding, ::Error> |
| 51 | + where |
| 52 | + I: Iterator<Item = (&'i str, f32)>, |
| 53 | + { |
| 54 | + let values: Vec<HeaderValue> = pairs |
| 55 | + .map(|pair| { |
| 56 | + QualityValue::try_from(pair).map(|qual: QualityValue| HeaderValue::from(qual)) |
| 57 | + }) |
| 58 | + .collect::<Result<Vec<HeaderValue>, ::Error>>()?; |
| 59 | + let value = QualityValue::try_from_values(&mut values.iter())?; |
| 60 | + Ok(AcceptEncoding(value)) |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns the most prefered encoding that is specified by the client, |
| 64 | + /// if one is specified. |
| 65 | + /// |
| 66 | + /// Note: This peeks at the underlying iter, not modifying it. |
| 67 | + /// |
| 68 | + /// # Example |
| 69 | + /// |
| 70 | + /// ``` |
| 71 | + /// use headers::AcceptEncoding; |
| 72 | + /// |
| 73 | + /// let pairs = vec![("gzip", 1.0), ("deflate", 0.8)]; |
| 74 | + /// let accept_enc = AcceptEncoding::from_quality_pairs(&mut pairs.into_iter()).unwrap(); |
| 75 | + /// let mut encodings = accept_enc.sorted_encodings(); |
| 76 | + /// |
| 77 | + /// assert_eq!(accept_enc.prefered_encoding(), Some("gzip")); |
| 78 | + /// ``` |
| 79 | + pub fn prefered_encoding(&self) -> Option<&str> { |
| 80 | + self.0.iter().peekable().peek().map(|s| *s) |
| 81 | + } |
| 82 | + |
| 83 | + /// Returns a quality sorted iterator of the accepted encodings |
| 84 | + /// |
| 85 | + /// # Example |
| 86 | + /// |
| 87 | + /// ``` |
| 88 | + /// use headers::{AcceptEncoding, HeaderValue}; |
| 89 | + /// |
| 90 | + /// let pairs = vec![("gzip", 1.0), ("deflate", 0.8)]; |
| 91 | + /// let accept_enc = AcceptEncoding::from_quality_pairs(&mut pairs.into_iter()).unwrap(); |
| 92 | + /// let mut encodings = accept_enc.sorted_encodings(); |
| 93 | + /// |
| 94 | + /// assert_eq!(encodings.next(), Some("gzip")); |
| 95 | + /// assert_eq!(encodings.next(), Some("deflate")); |
| 96 | + /// assert_eq!(encodings.next(), None); |
| 97 | + /// ``` |
| 98 | + pub fn sorted_encodings(&self) -> impl Iterator<Item = &str> { |
| 99 | + self.0.iter() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[cfg(test)] |
| 104 | +mod tests { |
| 105 | + use super::*; |
| 106 | + use HeaderValue; |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn from_static() { |
| 110 | + let val = HeaderValue::from_static("deflate, gzip;q=1.0, br;q=0.9"); |
| 111 | + let accept_enc = AcceptEncoding(val.into()); |
| 112 | + |
| 113 | + assert_eq!(accept_enc.prefered_encoding(), Some("deflate")); |
| 114 | + |
| 115 | + let mut encodings = accept_enc.sorted_encodings(); |
| 116 | + assert_eq!(encodings.next(), Some("deflate")); |
| 117 | + assert_eq!(encodings.next(), Some("gzip")); |
| 118 | + assert_eq!(encodings.next(), Some("br")); |
| 119 | + assert_eq!(encodings.next(), None); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn from_pairs() { |
| 124 | + let pairs = vec![("gzip", 1.0), ("br", 0.9)]; |
| 125 | + let accept_enc = AcceptEncoding::from_quality_pairs(&mut pairs.into_iter()).unwrap(); |
| 126 | + |
| 127 | + assert_eq!(accept_enc.prefered_encoding(), Some("gzip")); |
| 128 | + |
| 129 | + let mut encodings = accept_enc.sorted_encodings(); |
| 130 | + assert_eq!(encodings.next(), Some("gzip")); |
| 131 | + assert_eq!(encodings.next(), Some("br")); |
| 132 | + assert_eq!(encodings.next(), None); |
| 133 | + } |
| 134 | +} |
0 commit comments