Add cookie-rs and use cookie::Cookie in Cookie header.
This commit is contained in:
committed by
Sean McArthur
parent
a85cc476e1
commit
0a624b10e0
@@ -31,3 +31,5 @@ git = "https://github.com/carllerche/curl-rust"
|
||||
[dev-dependencies.http]
|
||||
git = "https://github.com/chris-morgan/rust-http"
|
||||
|
||||
[dependencies.cookie]
|
||||
git = "https://github.com/alexcrichton/cookie-rs"
|
||||
@@ -1,6 +1,8 @@
|
||||
use header::Header;
|
||||
use std::fmt::{mod, Show};
|
||||
use std::str::from_utf8;
|
||||
use cookie::Cookie as CookieRs;
|
||||
use cookie::CookieJar;
|
||||
|
||||
/// The `Cookie` header
|
||||
///
|
||||
@@ -11,7 +13,7 @@ use std::str::from_utf8;
|
||||
/// 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 Cookie(pub Vec<String>);
|
||||
pub struct Cookie(pub Vec<CookieRs>);
|
||||
|
||||
impl Header for Cookie {
|
||||
fn header_name(_: Option<Cookie>) -> &'static str {
|
||||
@@ -19,12 +21,15 @@ impl Header for Cookie {
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookie> {
|
||||
let mut cookies: Vec<String> = vec![];
|
||||
let mut cookies: Vec<CookieRs> = vec![];
|
||||
for cookies_raw in raw.iter() {
|
||||
match from_utf8(cookies_raw.as_slice()) {
|
||||
Some(cookies_str) => {
|
||||
for cookie in cookies_str.split(';') {
|
||||
cookies.push(cookie.to_string())
|
||||
for cookie_str in cookies_str.split(';') {
|
||||
match CookieRs::parse(cookie_str.trim()) {
|
||||
Ok(cookie) => cookies.push(cookie),
|
||||
Err(_) => return None
|
||||
}
|
||||
}
|
||||
},
|
||||
None => return None
|
||||
@@ -40,7 +45,29 @@ impl Header for Cookie {
|
||||
|
||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
let Cookie(ref value) = *self;
|
||||
value.connect("; ").fmt(fmt)
|
||||
let last = value.len() - 1;
|
||||
for (i, cookie) in value.iter().enumerate() {
|
||||
try!(cookie.fmt(fmt));
|
||||
if i < last {
|
||||
try!("; ".fmt(fmt));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
let mut jar = CookieJar::new(key);
|
||||
let &Cookie(ref cookies) = self;
|
||||
for cookie in cookies.iter() {
|
||||
jar.add_original(cookie.clone());
|
||||
}
|
||||
|
||||
jar
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -136,6 +136,7 @@ extern crate "unsafe-any" as uany;
|
||||
extern crate "move-acceptor" as macceptor;
|
||||
extern crate intertwine;
|
||||
extern crate typeable;
|
||||
extern crate cookie;
|
||||
|
||||
pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port};
|
||||
pub use mimewrapper::mime;
|
||||
|
||||
Reference in New Issue
Block a user