/rust/registry/src/index.crates.io-1949cf8c6b5b557f/headers-0.4.1/src/common/vary.rs
Line | Count | Source |
1 | | use http::{HeaderName, HeaderValue}; |
2 | | |
3 | | use crate::util::FlatCsv; |
4 | | |
5 | | /// `Vary` header, defined in [RFC7231](https://tools.ietf.org/html/rfc7231#section-7.1.4) |
6 | | /// |
7 | | /// The "Vary" header field in a response describes what parts of a |
8 | | /// request message, aside from the method, Host header field, and |
9 | | /// request target, might influence the origin server's process for |
10 | | /// selecting and representing this response. The value consists of |
11 | | /// either a single asterisk ("*") or a list of header field names |
12 | | /// (case-insensitive). |
13 | | /// |
14 | | /// # ABNF |
15 | | /// |
16 | | /// ```text |
17 | | /// Vary = "*" / 1#field-name |
18 | | /// ``` |
19 | | /// |
20 | | /// # Example values |
21 | | /// |
22 | | /// * `accept-encoding, accept-language` |
23 | | /// |
24 | | /// # Example |
25 | | /// |
26 | | /// ``` |
27 | | /// use headers::Vary; |
28 | | /// |
29 | | /// let vary = Vary::any(); |
30 | | /// ``` |
31 | | #[derive(Debug, Clone, PartialEq)] |
32 | | pub struct Vary(FlatCsv); |
33 | | |
34 | | derive_header! { |
35 | | Vary(_), |
36 | | name: VARY |
37 | | } |
38 | | |
39 | | impl Vary { |
40 | | /// Create a new `Very: *` header. |
41 | 0 | pub fn any() -> Vary { |
42 | 0 | Vary(HeaderValue::from_static("*").into()) |
43 | 0 | } |
44 | | |
45 | | /// Check if this includes `*`. |
46 | 0 | pub fn is_any(&self) -> bool { |
47 | 0 | self.0.iter().any(|val| val == "*") |
48 | 0 | } |
49 | | |
50 | | /// Iterate the header names of this `Vary`. |
51 | 0 | pub fn iter_strs(&self) -> impl Iterator<Item = &str> { |
52 | 0 | self.0.iter() |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl From<HeaderName> for Vary { |
57 | 0 | fn from(name: HeaderName) -> Self { |
58 | 0 | Vary(HeaderValue::from(name).into()) |
59 | 0 | } |
60 | | } |
61 | | |
62 | | /* |
63 | | test_vary { |
64 | | test_header!(test1, vec![b"accept-encoding, accept-language"]); |
65 | | |
66 | | #[test] |
67 | | fn test2() { |
68 | | let mut vary: ::Result<Vary>; |
69 | | |
70 | | vary = Header::parse_header(&"*".into()); |
71 | | assert_eq!(vary.ok(), Some(Vary::Any)); |
72 | | |
73 | | vary = Header::parse_header(&"etag,cookie,allow".into()); |
74 | | assert_eq!(vary.ok(), Some(Vary::Items(vec!["eTag".parse().unwrap(), |
75 | | "cookIE".parse().unwrap(), |
76 | | "AlLOw".parse().unwrap(),]))); |
77 | | } |
78 | | } |
79 | | */ |
80 | | |
81 | | #[cfg(test)] |
82 | | mod tests { |
83 | | use super::*; |
84 | | |
85 | | #[test] |
86 | | fn any_is_any() { |
87 | | assert!(Vary::any().is_any()); |
88 | | } |
89 | | } |