add vary header, first draft

This commit is contained in:
Rohan Prinja
2014-12-12 15:34:43 +05:30
parent 5e560cb1c1
commit 258e739ef8
3 changed files with 72 additions and 2 deletions

View File

@@ -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;
@@ -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
src/header/common/vary.rs Normal file
View File

@@ -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(),])));
}
}

View File

@@ -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};
@@ -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 {