Skip to content

add vary header, first draft #193

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

Merged
merged 1 commit into from
Dec 13, 2014
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
4 changes: 4 additions & 0 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub use self::location::Location;
pub use self::transfer_encoding::TransferEncoding;
pub use self::upgrade::Upgrade;
pub use self::user_agent::UserAgent;
pub use self::vary::Vary;
pub use self::server::Server;
pub use self::set_cookie::SetCookie;

Expand Down Expand Up @@ -132,4 +133,7 @@ pub mod upgrade;
/// Exposes the UserAgent header.
pub mod user_agent;

/// Exposes the Vary header.
pub mod vary;

pub mod util;
59 changes: 59 additions & 0 deletions src/header/common/vary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use header::{Header, HeaderFormat, CaseInsensitive};
use std::fmt::{mod};
use super::util::{from_comma_delimited, fmt_comma_delimited, from_one_raw_str};

/// The `Allow` header.
/// See also https://tools.ietf.org/html/rfc7231#section-7.1.4

#[deriving(Clone, PartialEq, Show)]
pub enum Vary {
/// This corresponds to '*'.
Any,
/// The header field names which will influence the response representation.
Headers(Vec<CaseInsensitive>),
}

impl Header for Vary {
fn header_name(_: Option<Vary>) -> &'static str {
"Vary"
}

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

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

#[cfg(test)]
mod tests {
use super::Vary;
use header::{Header, CaseInsensitive};

#[test]
fn test_vary() {
let mut vary: Option<Vary>;

vary = Header::parse_header([b"*".to_vec()].as_slice());
assert_eq!(vary, Some(Vary::Any));

vary = Header::parse_header([b"etag,cookie,allow".to_vec()].as_slice());
assert_eq!(vary, Some(Vary::Headers(vec![from_str::<CaseInsensitive>("eTag").unwrap(),
from_str::<CaseInsensitive>("cookIE").unwrap(),
from_str::<CaseInsensitive>("AlLOw").unwrap(),])));
}
}
11 changes: 9 additions & 2 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::borrow::Cow::{Borrowed, Owned};
use std::fmt::{mod, Show};
use std::intrinsics::TypeId;
use std::raw::TraitObject;
use std::str::SendStr;
use std::str::{SendStr, FromStr};
use std::collections::HashMap;
use std::collections::hash_map::{Entries, Occupied, Vacant};
use std::{hash, mem};
Expand Down Expand Up @@ -446,8 +446,15 @@ impl fmt::Show for Box<HeaderFormat + Send + Sync> {
}
}

/// Case-insensitive string.
//#[deriving(Clone)]
struct CaseInsensitive(SendStr);
pub struct CaseInsensitive(SendStr);

impl FromStr for CaseInsensitive {
fn from_str(s: &str) -> Option<CaseInsensitive> {
Some(CaseInsensitive(Owned(s.to_string())))
}
}

impl Clone for CaseInsensitive {
fn clone(&self) -> CaseInsensitive {
Expand Down