Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(headers): Add Pragma header field #267

Merged
merged 1 commit into from
Jan 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use self::host::Host;
pub use self::if_modified_since::IfModifiedSince;
pub use self::last_modified::LastModified;
pub use self::location::Location;
pub use self::pragma::Pragma;
pub use self::referer::Referer;
pub use self::server::Server;
pub use self::set_cookie::SetCookie;
Expand Down Expand Up @@ -157,11 +158,11 @@ mod host;
mod last_modified;
mod if_modified_since;
mod location;
mod pragma;
mod referer;
mod server;
mod set_cookie;
mod transfer_encoding;
mod upgrade;
mod user_agent;
mod vary;

61 changes: 61 additions & 0 deletions src/header/common/pragma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::fmt;
use std::ascii::AsciiExt;

use header::{Header, HeaderFormat, parsing};

/// The `Pragma` header defined by HTTP/1.0.
///
/// > The "Pragma" header field allows backwards compatibility with
/// > HTTP/1.0 caches, so that clients can specify a "no-cache" request
/// > that they will understand (as Cache-Control was not defined until
/// > HTTP/1.1). When the Cache-Control header field is also present and
/// > understood in a request, Pragma is ignored.

/// > In HTTP/1.0, Pragma was defined as an extensible field for
/// > implementation-specified directives for recipients. This
/// > specification deprecates such extensions to improve interoperability.
///
/// Spec: https://tools.ietf.org/html/rfc7234#section-5.4
#[derive(Clone, PartialEq, Debug)]
pub enum Pragma {
/// Corresponds to the `no-cache` value.
NoCache,
/// Every value other than `no-cache`.
Ext(String),
}

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

fn parse_header(raw: &[Vec<u8>]) -> Option<Pragma> {
parsing::from_one_raw_str(raw).and_then(|s: String| {
let slice = &s.to_ascii_lowercase()[];
match slice {
"" => None,
"no-cache" => Some(Pragma::NoCache),
_ => Some(Pragma::Ext(s)),
}
})
}
}

impl HeaderFormat for Pragma {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Pragma::NoCache => write!(f, "no-cache"),
Pragma::Ext(ref string) => write!(f, "{}", string),
}
}
}

#[test]
fn test_parse_header() {
let a: Pragma = Header::parse_header([b"no-cache".to_vec()].as_slice()).unwrap();
let b = Pragma::NoCache;
assert_eq!(a, b);
let c: Pragma = Header::parse_header([b"FoObar".to_vec()].as_slice()).unwrap();
let d = Pragma::Ext("FoObar".to_string());
assert_eq!(c, d);
}