adjustments to Cookie and SetCookie
This commit is contained in:
		| @@ -1,33 +1,31 @@ | ||||
| use header::Header; | ||||
| use header::{Header, HeaderFormat}; | ||||
| use std::fmt::{mod, Show}; | ||||
| use std::str::from_utf8; | ||||
| use std::from_str::FromStr; | ||||
| use std::from_str::from_str; | ||||
|  | ||||
| #[cfg(feature = "cookie_rs")] | ||||
| use cookie::Cookie as CookieRs; | ||||
| #[cfg(feature = "cookie_rs")] | ||||
| use cookie::Cookie; | ||||
| use cookie::CookieJar; | ||||
|  | ||||
| /// The `Cookie` header | ||||
| /// The `Cookie` header. Defined in [RFC6265](tools.ietf.org/html/rfc6265#section-5.4): | ||||
| /// | ||||
| /// 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. | ||||
| /// > 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. | ||||
| /// > 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 TypedCookie<T>(pub Vec<T>); | ||||
| pub struct Cookies(pub Vec<Cookie>); | ||||
|  | ||||
| impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> { | ||||
|     fn header_name(_: Option<TypedCookie<T>>) -> &'static str { | ||||
| impl Header for Cookies { | ||||
|     fn header_name(_: Option<Cookies>) -> &'static str { | ||||
|         "Cookie" | ||||
|     } | ||||
|  | ||||
|     fn parse_header(raw: &[Vec<u8>]) -> Option<TypedCookie<T>> { | ||||
|         let mut cookies: Vec<T> = vec![]; | ||||
|     fn parse_header(raw: &[Vec<u8>]) -> Option<Cookies> { | ||||
|         let mut cookies = vec![]; | ||||
|         for cookies_raw in raw.iter() { | ||||
|             match from_utf8(cookies_raw.as_slice()) { | ||||
|             match from_utf8(cookies_raw[]) { | ||||
|                 Some(cookies_str) => { | ||||
|                     for cookie_str in cookies_str.split(';') { | ||||
|                         match from_str(cookie_str.trim()) { | ||||
| @@ -41,17 +39,20 @@ impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> { | ||||
|         } | ||||
|  | ||||
|         if !cookies.is_empty() { | ||||
|             Some(TypedCookie(cookies)) | ||||
|             Some(Cookies(cookies)) | ||||
|         } else { | ||||
|             None | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
| impl HeaderFormat for Cookies { | ||||
|     fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||||
|         let TypedCookie(ref value) = *self; | ||||
|         let last = value.len() - 1; | ||||
|         for (i, cookie) in value.iter().enumerate() { | ||||
|             try!(cookie.fmt(fmt)); | ||||
|         let cookies = &self.0; | ||||
|         let last = cookies.len() - 1; | ||||
|         for (i, cookie) in cookies.iter().enumerate() { | ||||
|             try!(write!(fmt, "{}={}", cookie.name, cookie.value)); | ||||
|             if i < last { | ||||
|                 try!("; ".fmt(fmt)); | ||||
|             } | ||||
| @@ -60,25 +61,37 @@ impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> { | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[cfg(not(feature = "cookie_rs"))] | ||||
| pub type Cookie = TypedCookie<String>; | ||||
|  | ||||
| #[cfg(feature = "cookie_rs")] | ||||
| pub type Cookie = TypedCookie<CookieRs>; | ||||
|  | ||||
| #[cfg(feature = "cookie_rs")] | ||||
| 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 { | ||||
| impl Cookies { | ||||
|     /// This method can be used to create CookieJar that can be used | ||||
|     /// to manipulate cookies and create a corresponding `SetCookie` header afterwards. | ||||
|     pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> { | ||||
|         let mut jar = CookieJar::new(key); | ||||
|         let &TypedCookie(ref cookies) = self; | ||||
|         for cookie in cookies.iter() { | ||||
|         for cookie in self.0.iter() { | ||||
|             jar.add_original((*cookie).clone()); | ||||
|         } | ||||
|  | ||||
|         jar    | ||||
|         jar | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| #[test] | ||||
| fn test_parse() { | ||||
|     let h = Header::parse_header([b"foo=bar; baz=quux".to_vec()][]); | ||||
|     let c1 = Cookie::new("foo".to_string(), "bar".to_string()); | ||||
|     let c2 = Cookie::new("baz".to_string(), "quux".to_string()); | ||||
|     assert_eq!(h, Some(Cookies(vec![c1, c2]))); | ||||
| } | ||||
|  | ||||
| #[test] | ||||
| fn test_fmt() { | ||||
|     use header::Headers; | ||||
|  | ||||
|     let mut cookie = Cookie::new("foo".to_string(), "bar".to_string()); | ||||
|     cookie.httponly = true; | ||||
|     cookie.path = Some("/p".to_string()); | ||||
|     let cookies = Cookies(vec![cookie, Cookie::new("baz".to_string(), "quux".to_string())]); | ||||
|     let mut headers = Headers::new(); | ||||
|     headers.set(cookies); | ||||
|  | ||||
|     assert_eq!(headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n"); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user