refactor(header): change Cookie and SetCookie to use String
This removes the cookie crate, since it has an optional dependency on openssl, which can cause massive breakage if toggled on. Instead, the `Cookie` and `SetCookie` headers now just use a `String`. Anyone can create any typed header, so it is easy to plug in different implementations. BREAKING CHANGE: The `Cookie` and `SetCookie` headers no longer use the cookie crate. New headers can be written for any header, or the ones provided in hyper can be accessed as strings.
This commit is contained in:
@@ -26,10 +26,6 @@ tokio-service = "0.1"
|
||||
unicase = "1.0"
|
||||
url = "1.0"
|
||||
|
||||
[dependencies.cookie]
|
||||
version = "0.3"
|
||||
default-features = false
|
||||
|
||||
[dev-dependencies]
|
||||
num_cpus = "1.0"
|
||||
pretty_env_logger = "0.1"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use header::{Header, Raw, CookiePair, CookieJar};
|
||||
use header::{Header, Raw};
|
||||
use std::fmt::{self, Display};
|
||||
use std::str::from_utf8;
|
||||
|
||||
@@ -21,7 +21,6 @@ use std::str::from_utf8;
|
||||
/// # extern crate cookie;
|
||||
/// # fn main() {
|
||||
/// use hyper::header::{Headers, Cookie};
|
||||
/// use cookie::Cookie as CookiePair;
|
||||
///
|
||||
/// let mut headers = Headers::new();
|
||||
///
|
||||
@@ -33,9 +32,9 @@ use std::str::from_utf8;
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct Cookie(pub Vec<CookiePair>);
|
||||
pub struct Cookie(pub Vec<String>);
|
||||
|
||||
__hyper__deref!(Cookie => Vec<CookiePair>);
|
||||
__hyper__deref!(Cookie => Vec<String>);
|
||||
|
||||
impl Header for Cookie {
|
||||
fn header_name() -> &'static str {
|
||||
@@ -48,11 +47,7 @@ impl Header for Cookie {
|
||||
for cookies_raw in raw.iter() {
|
||||
let cookies_str = try!(from_utf8(&cookies_raw[..]));
|
||||
for cookie_str in cookies_str.split(';') {
|
||||
if let Ok(cookie) = cookie_str.trim().parse() {
|
||||
cookies.push(cookie);
|
||||
} else {
|
||||
return Err(::Error::Header);
|
||||
}
|
||||
cookies.push(cookie_str.trim().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,64 +64,10 @@ impl Header for Cookie {
|
||||
if i != 0 {
|
||||
try!(f.write_str("; "));
|
||||
}
|
||||
try!(Display::fmt(&cookie.pair(), f));
|
||||
try!(Display::fmt(&cookie, f));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Cookie {
|
||||
/// 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);
|
||||
for cookie in self.iter() {
|
||||
jar.add_original(cookie.clone());
|
||||
}
|
||||
jar
|
||||
}
|
||||
|
||||
/// Extracts all cookies from `CookieJar` and creates Cookie header.
|
||||
/// Useful for clients.
|
||||
pub fn from_cookie_jar(jar: &CookieJar) -> Cookie {
|
||||
Cookie(jar.iter().collect())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let h = Header::parse_header(&b"foo=bar; baz=quux".as_ref().into());
|
||||
let c1 = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
let c2 = CookiePair::new("baz".to_owned(), "quux".to_owned());
|
||||
assert_eq!(h.ok(), Some(Cookie(vec![c1, c2])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fmt() {
|
||||
use header::Headers;
|
||||
|
||||
let mut cookie_pair = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
cookie_pair.httponly = true;
|
||||
cookie_pair.path = Some("/p".to_owned());
|
||||
let cookie_header = Cookie(vec![
|
||||
cookie_pair,
|
||||
CookiePair::new("baz".to_owned(),"quux".to_owned())]);
|
||||
let mut headers = Headers::new();
|
||||
headers.set(cookie_header);
|
||||
|
||||
assert_eq!(&headers.to_string()[..], "Cookie: foo=bar; baz=quux\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cookie_jar() {
|
||||
let cookie_pair = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
let cookie_header = Cookie(vec![cookie_pair]);
|
||||
let jar = cookie_header.to_cookie_jar(&[]);
|
||||
let new_cookie_header = Cookie::from_cookie_jar(&jar);
|
||||
|
||||
assert_eq!(cookie_header, new_cookie_header);
|
||||
}
|
||||
|
||||
|
||||
bench_header!(bench, Cookie, { vec![b"foo=bar; baz=quux".to_vec()] });
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use header::{Header, Raw, CookiePair, CookieJar};
|
||||
use header::{Header, Raw};
|
||||
use std::fmt::{self, Display};
|
||||
use std::str::from_utf8;
|
||||
|
||||
@@ -61,26 +61,20 @@ use std::str::from_utf8;
|
||||
/// // extern crate cookie;
|
||||
///
|
||||
/// use hyper::header::{Headers, SetCookie};
|
||||
/// use cookie::Cookie as CookiePair;
|
||||
///
|
||||
/// let mut headers = Headers::new();
|
||||
/// let mut cookie = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
///
|
||||
/// cookie.path = Some("/path".to_owned());
|
||||
/// cookie.domain = Some("example.com".to_owned());
|
||||
///
|
||||
/// headers.set(
|
||||
/// SetCookie(vec![
|
||||
/// cookie,
|
||||
/// CookiePair::new("baz".to_owned(), "quux".to_owned()),
|
||||
/// String::from("foo=bar; Path=/path; Domain=example.com")
|
||||
/// ])
|
||||
/// );
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct SetCookie(pub Vec<CookiePair>);
|
||||
pub struct SetCookie(pub Vec<String>);
|
||||
|
||||
__hyper__deref!(SetCookie => Vec<CookiePair>);
|
||||
__hyper__deref!(SetCookie => Vec<String>);
|
||||
|
||||
impl Header for SetCookie {
|
||||
fn header_name() -> &'static str {
|
||||
@@ -92,9 +86,7 @@ impl Header for SetCookie {
|
||||
let mut set_cookies = Vec::with_capacity(raw.len());
|
||||
for set_cookies_raw in raw {
|
||||
if let Ok(s) = from_utf8(&set_cookies_raw[..]) {
|
||||
if let Ok(cookie) = s.parse() {
|
||||
set_cookies.push(cookie);
|
||||
}
|
||||
set_cookies.push(s.trim().to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,61 +107,3 @@ impl Header for SetCookie {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl SetCookie {
|
||||
/// Use this to create SetCookie header from CookieJar using
|
||||
/// calculated delta.
|
||||
pub fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
|
||||
SetCookie(jar.delta())
|
||||
}
|
||||
|
||||
/// Use this on client to apply changes from SetCookie to CookieJar.
|
||||
/// Note that this will `panic!` if `CookieJar` is not root.
|
||||
pub fn apply_to_cookie_jar(&self, jar: &mut CookieJar) {
|
||||
for cookie in self.iter() {
|
||||
jar.add_original(cookie.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let h = Header::parse_header(&"foo=bar; HttpOnly".into());
|
||||
let mut c1 = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
c1.httponly = true;
|
||||
|
||||
assert_eq!(h.ok(), Some(SetCookie(vec![c1])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fmt() {
|
||||
use header::Headers;
|
||||
|
||||
let mut cookie = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
cookie.httponly = true;
|
||||
cookie.path = Some("/p".to_owned());
|
||||
let cookies = SetCookie(vec![cookie, CookiePair::new("baz".to_owned(), "quux".to_owned())]);
|
||||
let mut headers = Headers::new();
|
||||
headers.set(cookies);
|
||||
|
||||
assert_eq!(
|
||||
&headers.to_string()[..],
|
||||
"Set-Cookie: foo=bar; HttpOnly; Path=/p\r\nSet-Cookie: baz=quux\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cookie_jar() {
|
||||
let jar = CookieJar::new(b"secret");
|
||||
let cookie = CookiePair::new("foo".to_owned(), "bar".to_owned());
|
||||
jar.add(cookie);
|
||||
|
||||
let cookies = SetCookie::from_cookie_jar(&jar);
|
||||
|
||||
let mut new_jar = CookieJar::new(b"secret");
|
||||
cookies.apply_to_cookie_jar(&mut new_jar);
|
||||
|
||||
assert_eq!(jar.find("foo"), new_jar.find("foo"));
|
||||
assert_eq!(jar.iter().collect::<Vec<CookiePair>>(), new_jar.iter().collect::<Vec<CookiePair>>());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
pub use self::charset::Charset;
|
||||
pub use cookie::Cookie as CookiePair;
|
||||
pub use cookie::CookieJar;
|
||||
pub use self::encoding::Encoding;
|
||||
pub use self::entity::EntityTag;
|
||||
pub use self::httpdate::HttpDate;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
//! [Server](server/index.html), along with a
|
||||
//! [typed Headers system](header/index.html).
|
||||
|
||||
extern crate cookie;
|
||||
extern crate futures;
|
||||
extern crate futures_cpupool;
|
||||
extern crate httparse;
|
||||
@@ -27,6 +26,7 @@ extern crate time;
|
||||
#[macro_use] extern crate tokio_core as tokio;
|
||||
extern crate tokio_proto;
|
||||
extern crate tokio_service;
|
||||
#[macro_use] extern crate url;
|
||||
extern crate unicase;
|
||||
#[macro_use] extern crate url;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user