Skip to content

Commit 92f43cf

Browse files
committed
refactor(headers): Rename Cookies header to Cookie
`Cookie` is the actual name of the header and since all other header structs use the exact camel-cased version of their name using a different name here is very inconvienient and confusing. You will encounter weird errors if you try to use `Cookie` as the header. For this reason rename `Cookies` as discussed on IRC with @seanmonstar and @reem and use `CookiePair` for real cookies. BREAKING CHANGE: Change header `Cookie` to `Cookie`
1 parent fb92a26 commit 92f43cf

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/header/common/cookie.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use header::{Header, HeaderFormat};
22
use std::fmt;
33
use std::str::from_utf8;
44

5-
use cookie::Cookie;
5+
use cookie::Cookie as CookiePair;
66
use cookie::CookieJar;
77

88
/// The `Cookie` header. Defined in [RFC6265](tools.ietf.org/html/rfc6265#section-5.4):
@@ -14,16 +14,16 @@ use cookie::CookieJar;
1414
/// > When the user agent generates an HTTP request, the user agent MUST NOT
1515
/// > attach more than one Cookie header field.
1616
#[derive(Clone, PartialEq, Debug)]
17-
pub struct Cookies(pub Vec<Cookie>);
17+
pub struct Cookie(pub Vec<CookiePair>);
1818

19-
deref!(Cookies => Vec<Cookie>);
19+
deref!(Cookie => Vec<CookiePair>);
2020

21-
impl Header for Cookies {
21+
impl Header for Cookie {
2222
fn header_name() -> &'static str {
2323
"Cookie"
2424
}
2525

26-
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookies> {
26+
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookie> {
2727
let mut cookies = Vec::with_capacity(raw.len());
2828
for cookies_raw in raw.iter() {
2929
match from_utf8(&cookies_raw[]) {
@@ -40,14 +40,14 @@ impl Header for Cookies {
4040
}
4141

4242
if !cookies.is_empty() {
43-
Some(Cookies(cookies))
43+
Some(Cookie(cookies))
4444
} else {
4545
None
4646
}
4747
}
4848
}
4949

50-
impl HeaderFormat for Cookies {
50+
impl HeaderFormat for Cookie {
5151
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
5252
let cookies = &self.0;
5353
let last = cookies.len() - 1;
@@ -61,7 +61,7 @@ impl HeaderFormat for Cookies {
6161
}
6262
}
6363

64-
impl Cookies {
64+
impl Cookie {
6565
/// This method can be used to create CookieJar that can be used
6666
/// to manipulate cookies and create a corresponding `SetCookie` header afterwards.
6767
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
@@ -74,44 +74,43 @@ impl Cookies {
7474

7575
/// Extracts all cookies from `CookieJar` and creates Cookie header.
7676
/// Useful for clients.
77-
pub fn from_cookie_jar(jar: &CookieJar) -> Cookies {
78-
Cookies(jar.iter().collect())
77+
pub fn from_cookie_jar(jar: &CookieJar) -> Cookie {
78+
Cookie(jar.iter().collect())
7979
}
8080
}
8181

8282

8383
#[test]
8484
fn test_parse() {
8585
let h = Header::parse_header(&[b"foo=bar; baz=quux".to_vec()][]);
86-
let c1 = Cookie::new("foo".to_string(), "bar".to_string());
87-
let c2 = Cookie::new("baz".to_string(), "quux".to_string());
88-
assert_eq!(h, Some(Cookies(vec![c1, c2])));
86+
let c1 = CookiePair::new("foo".to_string(), "bar".to_string());
87+
let c2 = CookiePair::new("baz".to_string(), "quux".to_string());
88+
assert_eq!(h, Some(Cookie(vec![c1, c2])));
8989
}
9090

9191
#[test]
9292
fn test_fmt() {
9393
use header::Headers;
9494

95-
let mut cookie = Cookie::new("foo".to_string(), "bar".to_string());
96-
cookie.httponly = true;
97-
cookie.path = Some("/p".to_string());
98-
let cookies = Cookies(vec![cookie, Cookie::new("baz".to_string(), "quux".to_string())]);
95+
let mut cookie_pair = CookiePair::new("foo".to_string(), "bar".to_string());
96+
cookie_pair.httponly = true;
97+
cookie_pair.path = Some("/p".to_string());
98+
let cookie_header = Cookie(vec![cookie_pair, CookiePair::new("baz".to_string(), "quux".to_string())]);
9999
let mut headers = Headers::new();
100-
headers.set(cookies);
100+
headers.set(cookie_header);
101101

102102
assert_eq!(&headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
103103
}
104104

105105
#[test]
106106
fn cookie_jar() {
107-
let cookie = Cookie::new("foo".to_string(), "bar".to_string());
108-
let cookies = Cookies(vec![cookie]);
109-
let jar = cookies.to_cookie_jar(&[]);
110-
let new_cookies = Cookies::from_cookie_jar(&jar);
107+
let cookie_pair = CookiePair::new("foo".to_string(), "bar".to_string());
108+
let cookie_header = Cookie(vec![cookie_pair]);
109+
let jar = cookie_header.to_cookie_jar(&[]);
110+
let new_cookie_header = Cookie::from_cookie_jar(&jar);
111111

112-
assert_eq!(cookies, new_cookies);
112+
assert_eq!(cookie_header, new_cookie_header);
113113
}
114114

115115

116-
bench_header!(bench, Cookies, { vec![b"foo=bar; baz=quux".to_vec()] });
117-
116+
bench_header!(bench, Cookie, { vec![b"foo=bar; baz=quux".to_vec()] });

src/header/common/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use self::cache_control::{CacheControl, CacheDirective};
1515
pub use self::connection::{Connection, ConnectionOption};
1616
pub use self::content_length::ContentLength;
1717
pub use self::content_type::ContentType;
18-
pub use self::cookie::Cookies;
18+
pub use self::cookie::Cookie;
1919
pub use self::date::Date;
2020
pub use self::etag::Etag;
2121
pub use self::expires::Expires;
@@ -164,4 +164,3 @@ mod transfer_encoding;
164164
mod upgrade;
165165
mod user_agent;
166166
mod vary;
167-

0 commit comments

Comments
 (0)