|
| 1 | +use std::fmt; |
| 2 | +use std::ascii::AsciiExt; |
| 3 | + |
| 4 | +use header::{Header, HeaderFormat, parsing}; |
| 5 | + |
| 6 | +/// The `Pragma` header defined by HTTP/1.0. |
| 7 | +/// |
| 8 | +/// > The "Pragma" header field allows backwards compatibility with |
| 9 | +/// > HTTP/1.0 caches, so that clients can specify a "no-cache" request |
| 10 | +/// > that they will understand (as Cache-Control was not defined until |
| 11 | +/// > HTTP/1.1). When the Cache-Control header field is also present and |
| 12 | +/// > understood in a request, Pragma is ignored. |
| 13 | +
|
| 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 |
| 19 | +#[derive(Clone, PartialEq, Debug)] |
| 20 | +pub enum Pragma { |
| 21 | + /// Corresponds to the `no-cache` value. |
| 22 | + NoCache, |
| 23 | + /// Every value other than `no-cache`. |
| 24 | + Ext(String), |
| 25 | +} |
| 26 | + |
| 27 | +impl Header for Pragma { |
| 28 | + fn header_name() -> &'static str { |
| 29 | + "Pragma" |
| 30 | + } |
| 31 | + |
| 32 | + fn parse_header(raw: &[Vec<u8>]) -> Option<Pragma> { |
| 33 | + parsing::from_one_raw_str(raw).and_then(|s: String| { |
| 34 | + let slice = &s.to_ascii_lowercase()[]; |
| 35 | + match slice { |
| 36 | + "" => None, |
| 37 | + "no-cache" => Some(Pragma::NoCache), |
| 38 | + _ => Some(Pragma::Ext(s)), |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl HeaderFormat for Pragma { |
| 45 | + fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 46 | + match *self { |
| 47 | + Pragma::NoCache => write!(f, "no-cache"), |
| 48 | + Pragma::Ext(ref string) => write!(f, "{}", string), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_parse_header() { |
| 55 | + let a: Pragma = Header::parse_header([b"no-cache".to_vec()].as_slice()).unwrap(); |
| 56 | + let b = Pragma::NoCache; |
| 57 | + assert_eq!(a, b); |
| 58 | + let c: Pragma = Header::parse_header([b"FoObar".to_vec()].as_slice()).unwrap(); |
| 59 | + let d = Pragma::Ext("FoObar".to_string()); |
| 60 | + assert_eq!(c, d); |
| 61 | +} |
0 commit comments