diff --git a/src/header/common/cookie.rs b/src/header/common/cookie.rs new file mode 100644 index 00000000..71dbf1dc --- /dev/null +++ b/src/header/common/cookie.rs @@ -0,0 +1,46 @@ +use header::Header; +use std::fmt::{mod, Show}; +use std::str::from_utf8; + +/// The `Cookie` header +/// +/// If the user agent does attach a Cookie header field to an HTTP +/// request, the user agent must send the cookie-string +/// as the value of the header field. +/// +/// When the user agent generates an HTTP request, the user agent MUST NOT +/// attach more than one Cookie header field. +#[deriving(Clone, PartialEq, Show)] +pub struct Cookie(pub Vec); + +impl Header for Cookie { + fn header_name(_: Option) -> &'static str { + "Cookie" + } + + fn parse_header(raw: &[Vec]) -> Option { + let mut cookies: Vec = vec![]; + for cookies_raw in raw.iter() { + match from_utf8(cookies_raw.as_slice()) { + Some(cookies_str) => { + for cookie in cookies_str.split(';') { + cookies.push(cookie.to_string()) + } + }, + None => return None + }; + } + + if !cookies.is_empty() { + Some(Cookie(cookies)) + } else { + None + } + } + + fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let Cookie(ref value) = *self; + value.connect("; ").fmt(fmt) + } +} + diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index d177903a..c98f9db9 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -25,6 +25,11 @@ use std::str::from_utf8; /// Exposes the Accept header. pub mod accept; +/// Exposes the Authorization header. +pub mod authorization; + +/// Exposes the Cookie header. +pub mod cookie; /// Exposes the Connection header. pub mod connection; @@ -41,9 +46,15 @@ pub mod date; /// Exposes the Host header. pub mod host; +/// Exposes the Location header. +pub mod location; + /// Exposes the Server header. pub mod server; +/// Exposes the Set-Cookie header. +pub mod set_cookie; + /// Exposes the TransferEncoding header. pub mod transfer_encoding; @@ -54,11 +65,8 @@ pub mod upgrade; pub mod user_agent; -/// Exposes the Location header. -pub mod location; +pub mod util; -/// Exposes the Authorization header. -pub mod authorization; fn from_comma_delimited(raw: &[Vec]) -> Option> { if raw.len() != 1 { @@ -86,4 +94,3 @@ fn fmt_comma_delimited(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::R } Ok(()) } -pub mod util; diff --git a/src/header/common/set_cookie.rs b/src/header/common/set_cookie.rs new file mode 100644 index 00000000..63614de9 --- /dev/null +++ b/src/header/common/set_cookie.rs @@ -0,0 +1,42 @@ +use header::Header; +use std::fmt; +use std::str::from_utf8; + +/// The `Set-Cookie` header +/// +/// Informally, the Set-Cookie response header contains the header name +/// "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with +/// a name-value-pair, followed by zero or more attribute-value pairs. +#[deriving(Clone, PartialEq, Show)] +pub struct SetCookie(pub Vec); + +impl Header for SetCookie { + fn header_name(_: Option) -> &'static str { + "Set-Cookie" + } + + fn parse_header(raw: &[Vec]) -> Option { + let mut set_cookies: Vec = vec![]; + for set_cookies_raw in raw.iter() { + match from_utf8(set_cookies_raw.as_slice()) { + Some(set_cookies_str) => { + if !set_cookies_str.is_empty() { + set_cookies.push(set_cookies_str.to_string()); + } + }, + None => () + }; + } + + if !set_cookies.is_empty() { + Some(SetCookie(set_cookies)) + } else { + None + } + } + + fn fmt_header(&self, _: &mut fmt::Formatter) -> fmt::Result { + unimplemented!() + } +} +