Skip to content

Commit f22701f

Browse files
committed
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.
1 parent cb48452 commit f22701f

File tree

5 files changed

+10
-141
lines changed

5 files changed

+10
-141
lines changed

Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ typeable = "0.1"
2424
unicase = "1.0"
2525
url = "1.0"
2626

27-
[dependencies.cookie]
28-
version = "0.5"
29-
default-features = false
30-
3127
[dependencies.solicit]
3228
version = "0.4"
3329
default-features = false

src/header/common/cookie.rs

+5-64
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use header::{Header, HeaderFormat, CookiePair, CookieJar};
1+
use header::{Header, HeaderFormat};
22
use std::fmt::{self, Display};
33
use std::str::from_utf8;
44

@@ -21,7 +21,6 @@ use std::str::from_utf8;
2121
/// # extern crate cookie;
2222
/// # fn main() {
2323
/// use hyper::header::{Headers, Cookie};
24-
/// use cookie::Cookie as CookiePair;
2524
///
2625
/// let mut headers = Headers::new();
2726
///
@@ -33,9 +32,9 @@ use std::str::from_utf8;
3332
/// # }
3433
/// ```
3534
#[derive(Clone, PartialEq, Debug)]
36-
pub struct Cookie(pub Vec<CookiePair>);
35+
pub struct Cookie(pub Vec<String>);
3736

38-
__hyper__deref!(Cookie => Vec<CookiePair>);
37+
__hyper__deref!(Cookie => Vec<String>);
3938

4039
impl Header for Cookie {
4140
fn header_name() -> &'static str {
@@ -47,11 +46,7 @@ impl Header for Cookie {
4746
for cookies_raw in raw.iter() {
4847
let cookies_str = try!(from_utf8(&cookies_raw[..]));
4948
for cookie_str in cookies_str.split(';') {
50-
if let Ok(cookie) = cookie_str.trim().parse() {
51-
cookies.push(cookie);
52-
} else {
53-
return Err(::Error::Header);
54-
}
49+
cookies.push(cookie_str.trim().to_owned())
5550
}
5651
}
5752

@@ -70,64 +65,10 @@ impl HeaderFormat for Cookie {
7065
if i != 0 {
7166
try!(f.write_str("; "));
7267
}
73-
try!(Display::fmt(&cookie.pair(), f));
68+
try!(Display::fmt(&cookie, f));
7469
}
7570
Ok(())
7671
}
7772
}
7873

79-
impl Cookie {
80-
/// This method can be used to create CookieJar that can be used
81-
/// to manipulate cookies and create a corresponding `SetCookie` header afterwards.
82-
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
83-
let mut jar = CookieJar::new(key);
84-
for cookie in self.iter() {
85-
jar.add_original(cookie.clone());
86-
}
87-
jar
88-
}
89-
90-
/// Extracts all cookies from `CookieJar` and creates Cookie header.
91-
/// Useful for clients.
92-
pub fn from_cookie_jar(jar: &CookieJar) -> Cookie {
93-
Cookie(jar.iter().collect())
94-
}
95-
}
96-
97-
98-
#[test]
99-
fn test_parse() {
100-
let h = Header::parse_header(&[b"foo=bar; baz=quux".to_vec()][..]);
101-
let c1 = CookiePair::new("foo".to_owned(), "bar".to_owned());
102-
let c2 = CookiePair::new("baz".to_owned(), "quux".to_owned());
103-
assert_eq!(h.ok(), Some(Cookie(vec![c1, c2])));
104-
}
105-
106-
#[test]
107-
fn test_fmt() {
108-
use header::Headers;
109-
110-
let mut cookie_pair = CookiePair::new("foo".to_owned(), "bar".to_owned());
111-
cookie_pair.httponly = true;
112-
cookie_pair.path = Some("/p".to_owned());
113-
let cookie_header = Cookie(vec![
114-
cookie_pair,
115-
CookiePair::new("baz".to_owned(),"quux".to_owned())]);
116-
let mut headers = Headers::new();
117-
headers.set(cookie_header);
118-
119-
assert_eq!(&headers.to_string()[..], "Cookie: foo=bar; baz=quux\r\n");
120-
}
121-
122-
#[test]
123-
fn cookie_jar() {
124-
let cookie_pair = CookiePair::new("foo".to_owned(), "bar".to_owned());
125-
let cookie_header = Cookie(vec![cookie_pair]);
126-
let jar = cookie_header.to_cookie_jar(&[]);
127-
let new_cookie_header = Cookie::from_cookie_jar(&jar);
128-
129-
assert_eq!(cookie_header, new_cookie_header);
130-
}
131-
132-
13374
bench_header!(bench, Cookie, { vec![b"foo=bar; baz=quux".to_vec()] });

src/header/common/set_cookie.rs

+5-70
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use header::{Header, HeaderFormat, CookiePair, CookieJar};
1+
use header::{Header, HeaderFormat};
22
use std::fmt::{self, Display};
33
use std::str::from_utf8;
44

@@ -61,26 +61,20 @@ use std::str::from_utf8;
6161
/// // extern crate cookie;
6262
///
6363
/// use hyper::header::{Headers, SetCookie};
64-
/// use cookie::Cookie as CookiePair;
6564
///
6665
/// let mut headers = Headers::new();
67-
/// let mut cookie = CookiePair::new("foo".to_owned(), "bar".to_owned());
68-
///
69-
/// cookie.path = Some("/path".to_owned());
70-
/// cookie.domain = Some("example.com".to_owned());
7166
///
7267
/// headers.set(
7368
/// SetCookie(vec![
74-
/// cookie,
75-
/// CookiePair::new("baz".to_owned(), "quux".to_owned()),
69+
/// String::from("foo=bar; Path=/path; Domain=example.com")
7670
/// ])
7771
/// );
7872
/// # }
7973
/// ```
8074
#[derive(Clone, PartialEq, Debug)]
81-
pub struct SetCookie(pub Vec<CookiePair>);
75+
pub struct SetCookie(pub Vec<String>);
8276

83-
__hyper__deref!(SetCookie => Vec<CookiePair>);
77+
__hyper__deref!(SetCookie => Vec<String>);
8478

8579
impl Header for SetCookie {
8680
fn header_name() -> &'static str {
@@ -91,9 +85,7 @@ impl Header for SetCookie {
9185
let mut set_cookies = Vec::with_capacity(raw.len());
9286
for set_cookies_raw in raw {
9387
if let Ok(s) = from_utf8(&set_cookies_raw[..]) {
94-
if let Ok(cookie) = s.parse() {
95-
set_cookies.push(cookie);
96-
}
88+
set_cookies.push(s.trim().to_owned());
9789
}
9890
}
9991

@@ -119,60 +111,3 @@ impl HeaderFormat for SetCookie {
119111
}
120112
}
121113

122-
123-
impl SetCookie {
124-
/// Use this to create SetCookie header from CookieJar using
125-
/// calculated delta.
126-
pub fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
127-
SetCookie(jar.delta())
128-
}
129-
130-
/// Use this on client to apply changes from SetCookie to CookieJar.
131-
/// Note that this will `panic!` if `CookieJar` is not root.
132-
pub fn apply_to_cookie_jar(&self, jar: &mut CookieJar) {
133-
for cookie in self.iter() {
134-
jar.add_original(cookie.clone())
135-
}
136-
}
137-
}
138-
139-
140-
#[test]
141-
fn test_parse() {
142-
let h = Header::parse_header(&[b"foo=bar; HttpOnly".to_vec()][..]);
143-
let mut c1 = CookiePair::new("foo".to_owned(), "bar".to_owned());
144-
c1.httponly = true;
145-
146-
assert_eq!(h.ok(), Some(SetCookie(vec![c1])));
147-
}
148-
149-
#[test]
150-
fn test_fmt() {
151-
use header::Headers;
152-
153-
let mut cookie = CookiePair::new("foo".to_owned(), "bar".to_owned());
154-
cookie.httponly = true;
155-
cookie.path = Some("/p".to_owned());
156-
let cookies = SetCookie(vec![cookie, CookiePair::new("baz".to_owned(), "quux".to_owned())]);
157-
let mut headers = Headers::new();
158-
headers.set(cookies);
159-
160-
assert_eq!(
161-
&headers.to_string()[..],
162-
"Set-Cookie: foo=bar; HttpOnly; Path=/p\r\nSet-Cookie: baz=quux\r\n");
163-
}
164-
165-
#[test]
166-
fn cookie_jar() {
167-
let jar = CookieJar::new(b"secret");
168-
let cookie = CookiePair::new("foo".to_owned(), "bar".to_owned());
169-
jar.add(cookie);
170-
171-
let cookies = SetCookie::from_cookie_jar(&jar);
172-
173-
let mut new_jar = CookieJar::new(b"secret");
174-
cookies.apply_to_cookie_jar(&mut new_jar);
175-
176-
assert_eq!(jar.find("foo"), new_jar.find("foo"));
177-
assert_eq!(jar.iter().collect::<Vec<CookiePair>>(), new_jar.iter().collect::<Vec<CookiePair>>());
178-
}

src/header/shared/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
pub use self::charset::Charset;
2-
pub use cookie::Cookie as CookiePair;
3-
pub use cookie::CookieJar;
42
pub use self::encoding::Encoding;
53
pub use self::entity::EntityTag;
64
pub use self::httpdate::HttpDate;

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
extern crate rustc_serialize as serialize;
132132
extern crate time;
133133
#[macro_use] extern crate url;
134-
extern crate cookie;
135134
extern crate unicase;
136135
extern crate httparse;
137136
extern crate num_cpus;

0 commit comments

Comments
 (0)