diff --git a/Cargo.toml b/Cargo.toml index e8994c2b..9b8777d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,5 @@ git = "https://github.com/carllerche/curl-rust" [dev-dependencies.http] git = "https://github.com/chris-morgan/rust-http" +[dependencies.cookie] +git = "https://github.com/alexcrichton/cookie-rs" \ No newline at end of file diff --git a/src/header/common/cookie.rs b/src/header/common/cookie.rs index 71dbf1dc..a8d710c0 100644 --- a/src/header/common/cookie.rs +++ b/src/header/common/cookie.rs @@ -1,6 +1,8 @@ use header::Header; use std::fmt::{mod, Show}; use std::str::from_utf8; +use cookie::Cookie as CookieRs; +use cookie::CookieJar; /// The `Cookie` header /// @@ -11,7 +13,7 @@ use std::str::from_utf8; /// 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); +pub struct Cookie(pub Vec); impl Header for Cookie { fn header_name(_: Option) -> &'static str { @@ -19,12 +21,15 @@ impl Header for Cookie { } fn parse_header(raw: &[Vec]) -> Option { - let mut cookies: Vec = vec![]; + 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()) + for cookie_str in cookies_str.split(';') { + match CookieRs::parse(cookie_str.trim()) { + Ok(cookie) => cookies.push(cookie), + Err(_) => return None + } } }, None => return None @@ -40,7 +45,29 @@ impl Header for Cookie { fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let Cookie(ref value) = *self; - value.connect("; ").fmt(fmt) + let last = value.len() - 1; + for (i, cookie) in value.iter().enumerate() { + try!(cookie.fmt(fmt)); + if i < last { + try!("; ".fmt(fmt)); + } + } + Ok(()) + } +} + +impl Cookie { + /// This method can be used to crate CookieJar that can be used + /// to manipulate cookies and create corresponding `SetCookie` header afterwards. + #[allow(dead_code)] + fn to_cookie_jar(&self, key: &[u8]) -> CookieJar { + let mut jar = CookieJar::new(key); + let &Cookie(ref cookies) = self; + for cookie in cookies.iter() { + jar.add_original(cookie.clone()); + } + + jar } } diff --git a/src/lib.rs b/src/lib.rs index f174a4b8..3e9c8f47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,6 +136,7 @@ extern crate "unsafe-any" as uany; extern crate "move-acceptor" as macceptor; extern crate intertwine; extern crate typeable; +extern crate cookie; pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port}; pub use mimewrapper::mime;