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
use header::{Header, HeaderFormat};
use std::fmt::{self};
use header::parsing::{from_comma_delimited, fmt_comma_delimited, from_one_raw_str};
use unicase::UniCase;

/// The `Allow` header.
/// See also https://tools.ietf.org/html/rfc7231#section-7.1.4

#[derive(Clone, PartialEq, Debug)]
pub enum Vary {
    /// This corresponds to '*'.
    Any,
    /// The header field names which will influence the response representation.
    Headers(Vec<UniCase<String>>),
}

impl Header for Vary {
    fn header_name() -> &'static str {
        "Vary"
    }

    fn parse_header(raw: &[Vec<u8>]) -> Option<Vary> {
        from_one_raw_str(raw).and_then(|s: String| {
            let slice = &s[..];
            match slice {
                "" => None,
                "*" => Some(Vary::Any),
                _ => from_comma_delimited(raw).map(|vec| Vary::Headers(vec)),
            }
        })
    }
}

impl HeaderFormat for Vary {
    fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Vary::Any => { write!(fmt, "*") }
            Vary::Headers(ref fields) => { fmt_comma_delimited(fmt, &fields[..]) }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Vary;
    use header::Header;

    #[test]
    fn test_vary() {
        let mut vary: Option<Vary>;

        vary = Header::parse_header([b"*".to_vec()].as_slice());
        assert_eq!(vary, Some(Vary::Any));

        vary = Header::parse_header([b"etag,cookie,allow".to_vec()].as_slice());
        assert_eq!(vary, Some(Vary::Headers(vec!["eTag".parse().unwrap(),
                                                 "cookIE".parse().unwrap(),
                                                 "AlLOw".parse().unwrap(),])));
    }
}