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 IfMatch header #313

Merged
merged 1 commit into from
Feb 14, 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
68 changes: 68 additions & 0 deletions src/header/common/if_match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use header::{EntityTag, Header, HeaderFormat};
use header::parsing::{from_comma_delimited, fmt_comma_delimited, from_one_raw_str};
use std::fmt;

/// The `If-Match` header
///
/// The `If-Match` request-header field is used with a method to make
/// it conditional. The client provides a list of entity tags, and
/// the request is only executed if one of those tags matches the
/// current entity.
///
/// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.24
#[derive(Clone, PartialEq, Debug)]
pub enum IfMatch {
/// This corresponds to '*'.
Any,
/// The header field names which will influence the response representation.
EntityTags(Vec<EntityTag>)
}

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

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

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

#[test]
fn test_parse_header() {
{
let a: IfMatch = Header::parse_header(
[b"*".to_vec()].as_slice()).unwrap();
assert_eq!(a, IfMatch::Any);
}
{
let a: IfMatch = Header::parse_header(
[b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\"".to_vec()].as_slice()).unwrap();
let b = IfMatch::EntityTags(
vec![EntityTag{weak:false, tag: "xyzzy".to_string()},
EntityTag{weak:false, tag: "r2d2xxxx".to_string()},
EntityTag{weak:false, tag: "c3piozzzz".to_string()}]);
assert_eq!(a, b);
}
}

bench_header!(star, IfMatch, { vec![b"*".to_vec()] });
bench_header!(single , IfMatch, { vec![b"\"xyzzy\"".to_vec()] });
bench_header!(multi, IfMatch,
{ vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\"".to_vec()] });
2 changes: 2 additions & 0 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use self::date::Date;
pub use self::etag::Etag;
pub use self::expires::Expires;
pub use self::host::Host;
pub use self::if_match::IfMatch;
pub use self::if_modified_since::IfModifiedSince;
pub use self::if_none_match::IfNoneMatch;
pub use self::if_unmodified_since::IfUnmodifiedSince;
Expand Down Expand Up @@ -157,6 +158,7 @@ mod date;
mod etag;
mod expires;
mod host;
mod if_match;
mod last_modified;
mod if_modified_since;
mod if_none_match;
Expand Down