hyper_sync/header/common/
pragma.rs

1use std::fmt;
2#[allow(unused, deprecated)]
3use std::ascii::AsciiExt;
4
5use header::{Header, Raw, parsing};
6
7/// The `Pragma` header defined by HTTP/1.0.
8///
9/// > The "Pragma" header field allows backwards compatibility with
10/// > HTTP/1.0 caches, so that clients can specify a "no-cache" request
11/// > that they will understand (as Cache-Control was not defined until
12/// > HTTP/1.1).  When the Cache-Control header field is also present and
13/// > understood in a request, Pragma is ignored.
14/// > In HTTP/1.0, Pragma was defined as an extensible field for
15/// > implementation-specified directives for recipients.  This
16/// > specification deprecates such extensions to improve interoperability.
17///
18/// Spec: [https://tools.ietf.org/html/rfc7234#section-5.4][url]
19///
20/// [url]: https://tools.ietf.org/html/rfc7234#section-5.4
21///
22/// # Examples
23///
24/// ```
25/// use hyper_sync::header::{Headers, Pragma};
26///
27/// let mut headers = Headers::new();
28/// headers.set(Pragma::NoCache);
29/// ```
30///
31/// ```
32/// use hyper_sync::header::{Headers, Pragma};
33///
34/// let mut headers = Headers::new();
35/// headers.set(Pragma::Ext("foobar".to_owned()));
36/// ```
37#[derive(Clone, PartialEq, Debug)]
38pub enum Pragma {
39    /// Corresponds to the `no-cache` value.
40    NoCache,
41    /// Every value other than `no-cache`.
42    Ext(String),
43}
44
45impl Header for Pragma {
46    fn header_name() -> &'static str {
47        static NAME: &'static str = "Pragma";
48        NAME
49    }
50
51    fn parse_header(raw: &Raw) -> ::Result<Pragma> {
52        parsing::from_one_raw_str(raw).and_then(|s: String| {
53            let slice = &s.to_ascii_lowercase()[..];
54            match slice {
55                "no-cache" => Ok(Pragma::NoCache),
56                _ => Ok(Pragma::Ext(s)),
57            }
58        })
59    }
60
61    fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
62        f.fmt_line(self)
63    }
64}
65
66impl fmt::Display for Pragma {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        f.write_str(match *self {
69            Pragma::NoCache => "no-cache",
70            Pragma::Ext(ref string) => &string[..],
71        })
72    }
73}
74
75#[test]
76fn test_parse_header() {
77    let a: Pragma = Header::parse_header(&"no-cache".into()).unwrap();
78    let b = Pragma::NoCache;
79    assert_eq!(a, b);
80    let c: Pragma = Header::parse_header(&"FoObar".into()).unwrap();
81    let d = Pragma::Ext("FoObar".to_owned());
82    assert_eq!(c, d);
83    let e: ::Result<Pragma> = Header::parse_header(&"".into());
84    assert_eq!(e.ok(), None);
85}