Merge pull request #271 from pyfisch/cookie
refactor(headers): Rename `Cookies` header to `Cookie`
This commit is contained in:
@@ -2,7 +2,7 @@ use header::{Header, HeaderFormat};
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
use cookie::Cookie;
|
use cookie::Cookie as CookiePair;
|
||||||
use cookie::CookieJar;
|
use cookie::CookieJar;
|
||||||
|
|
||||||
/// The `Cookie` header. Defined in [RFC6265](tools.ietf.org/html/rfc6265#section-5.4):
|
/// The `Cookie` header. Defined in [RFC6265](tools.ietf.org/html/rfc6265#section-5.4):
|
||||||
@@ -14,16 +14,16 @@ use cookie::CookieJar;
|
|||||||
/// > When the user agent generates an HTTP request, the user agent MUST NOT
|
/// > When the user agent generates an HTTP request, the user agent MUST NOT
|
||||||
/// > attach more than one Cookie header field.
|
/// > attach more than one Cookie header field.
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub struct Cookies(pub Vec<Cookie>);
|
pub struct Cookie(pub Vec<CookiePair>);
|
||||||
|
|
||||||
deref!(Cookies => Vec<Cookie>);
|
deref!(Cookie => Vec<CookiePair>);
|
||||||
|
|
||||||
impl Header for Cookies {
|
impl Header for Cookie {
|
||||||
fn header_name() -> &'static str {
|
fn header_name() -> &'static str {
|
||||||
"Cookie"
|
"Cookie"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookies> {
|
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookie> {
|
||||||
let mut cookies = Vec::with_capacity(raw.len());
|
let mut cookies = Vec::with_capacity(raw.len());
|
||||||
for cookies_raw in raw.iter() {
|
for cookies_raw in raw.iter() {
|
||||||
match from_utf8(&cookies_raw[]) {
|
match from_utf8(&cookies_raw[]) {
|
||||||
@@ -40,14 +40,14 @@ impl Header for Cookies {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !cookies.is_empty() {
|
if !cookies.is_empty() {
|
||||||
Some(Cookies(cookies))
|
Some(Cookie(cookies))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HeaderFormat for Cookies {
|
impl HeaderFormat for Cookie {
|
||||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let cookies = &self.0;
|
let cookies = &self.0;
|
||||||
let last = cookies.len() - 1;
|
let last = cookies.len() - 1;
|
||||||
@@ -61,7 +61,7 @@ impl HeaderFormat for Cookies {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cookies {
|
impl Cookie {
|
||||||
/// This method can be used to create CookieJar that can be used
|
/// This method can be used to create CookieJar that can be used
|
||||||
/// to manipulate cookies and create a corresponding `SetCookie` header afterwards.
|
/// to manipulate cookies and create a corresponding `SetCookie` header afterwards.
|
||||||
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
|
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
|
||||||
@@ -74,8 +74,8 @@ impl Cookies {
|
|||||||
|
|
||||||
/// Extracts all cookies from `CookieJar` and creates Cookie header.
|
/// Extracts all cookies from `CookieJar` and creates Cookie header.
|
||||||
/// Useful for clients.
|
/// Useful for clients.
|
||||||
pub fn from_cookie_jar(jar: &CookieJar) -> Cookies {
|
pub fn from_cookie_jar(jar: &CookieJar) -> Cookie {
|
||||||
Cookies(jar.iter().collect())
|
Cookie(jar.iter().collect())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,35 +83,34 @@ impl Cookies {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_parse() {
|
fn test_parse() {
|
||||||
let h = Header::parse_header(&[b"foo=bar; baz=quux".to_vec()][]);
|
let h = Header::parse_header(&[b"foo=bar; baz=quux".to_vec()][]);
|
||||||
let c1 = Cookie::new("foo".to_string(), "bar".to_string());
|
let c1 = CookiePair::new("foo".to_string(), "bar".to_string());
|
||||||
let c2 = Cookie::new("baz".to_string(), "quux".to_string());
|
let c2 = CookiePair::new("baz".to_string(), "quux".to_string());
|
||||||
assert_eq!(h, Some(Cookies(vec![c1, c2])));
|
assert_eq!(h, Some(Cookie(vec![c1, c2])));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fmt() {
|
fn test_fmt() {
|
||||||
use header::Headers;
|
use header::Headers;
|
||||||
|
|
||||||
let mut cookie = Cookie::new("foo".to_string(), "bar".to_string());
|
let mut cookie_pair = CookiePair::new("foo".to_string(), "bar".to_string());
|
||||||
cookie.httponly = true;
|
cookie_pair.httponly = true;
|
||||||
cookie.path = Some("/p".to_string());
|
cookie_pair.path = Some("/p".to_string());
|
||||||
let cookies = Cookies(vec![cookie, Cookie::new("baz".to_string(), "quux".to_string())]);
|
let cookie_header = Cookie(vec![cookie_pair, CookiePair::new("baz".to_string(), "quux".to_string())]);
|
||||||
let mut headers = Headers::new();
|
let mut headers = Headers::new();
|
||||||
headers.set(cookies);
|
headers.set(cookie_header);
|
||||||
|
|
||||||
assert_eq!(&headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
|
assert_eq!(&headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cookie_jar() {
|
fn cookie_jar() {
|
||||||
let cookie = Cookie::new("foo".to_string(), "bar".to_string());
|
let cookie_pair = CookiePair::new("foo".to_string(), "bar".to_string());
|
||||||
let cookies = Cookies(vec![cookie]);
|
let cookie_header = Cookie(vec![cookie_pair]);
|
||||||
let jar = cookies.to_cookie_jar(&[]);
|
let jar = cookie_header.to_cookie_jar(&[]);
|
||||||
let new_cookies = Cookies::from_cookie_jar(&jar);
|
let new_cookie_header = Cookie::from_cookie_jar(&jar);
|
||||||
|
|
||||||
assert_eq!(cookies, new_cookies);
|
assert_eq!(cookie_header, new_cookie_header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bench_header!(bench, Cookies, { vec![b"foo=bar; baz=quux".to_vec()] });
|
bench_header!(bench, Cookie, { vec![b"foo=bar; baz=quux".to_vec()] });
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ pub use self::cache_control::{CacheControl, CacheDirective};
|
|||||||
pub use self::connection::{Connection, ConnectionOption};
|
pub use self::connection::{Connection, ConnectionOption};
|
||||||
pub use self::content_length::ContentLength;
|
pub use self::content_length::ContentLength;
|
||||||
pub use self::content_type::ContentType;
|
pub use self::content_type::ContentType;
|
||||||
pub use self::cookie::Cookies;
|
pub use self::cookie::Cookie;
|
||||||
pub use self::date::Date;
|
pub use self::date::Date;
|
||||||
pub use self::etag::Etag;
|
pub use self::etag::Etag;
|
||||||
pub use self::expires::Expires;
|
pub use self::expires::Expires;
|
||||||
|
|||||||
Reference in New Issue
Block a user