Skip to content

Commit 767c95d

Browse files
committed
feat(headers): Add Pragma header field
Add the HTTP/1.0 `Pragma` header field used to prevent older Caches, that do not understand the `Cache-Control` header field from caching the ressource. Closes #237
1 parent fb92a26 commit 767c95d

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/header/common/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub use self::host::Host;
2323
pub use self::if_modified_since::IfModifiedSince;
2424
pub use self::last_modified::LastModified;
2525
pub use self::location::Location;
26+
pub use self::pragma::Pragma;
2627
pub use self::referer::Referer;
2728
pub use self::server::Server;
2829
pub use self::set_cookie::SetCookie;
@@ -157,11 +158,11 @@ mod host;
157158
mod last_modified;
158159
mod if_modified_since;
159160
mod location;
161+
mod pragma;
160162
mod referer;
161163
mod server;
162164
mod set_cookie;
163165
mod transfer_encoding;
164166
mod upgrade;
165167
mod user_agent;
166168
mod vary;
167-

src/header/common/pragma.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)