Add cookie_rs as default feature and implement conditional compilation.
This commit is contained in:
committed by
Sean McArthur
parent
20f156c592
commit
a3fc51611f
@@ -4,6 +4,10 @@ name = "hyper"
|
|||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["Sean McArthur <sean.monstar@gmail.com>"]
|
authors = ["Sean McArthur <sean.monstar@gmail.com>"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
cookie_rs = ["cookie"]
|
||||||
|
default = ["cookie_rs"]
|
||||||
|
|
||||||
[dependencies.url]
|
[dependencies.url]
|
||||||
git = "https://github.com/servo/rust-url"
|
git = "https://github.com/servo/rust-url"
|
||||||
|
|
||||||
@@ -33,3 +37,4 @@ git = "https://github.com/chris-morgan/rust-http"
|
|||||||
|
|
||||||
[dependencies.cookie]
|
[dependencies.cookie]
|
||||||
git = "https://github.com/alexcrichton/cookie-rs"
|
git = "https://github.com/alexcrichton/cookie-rs"
|
||||||
|
optional = true
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
use header::Header;
|
use header::Header;
|
||||||
use std::fmt::{mod, Show};
|
use std::fmt::{mod, Show};
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
use std::from_str::FromStr;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookie_rs")]
|
||||||
use cookie::Cookie as CookieRs;
|
use cookie::Cookie as CookieRs;
|
||||||
|
#[cfg(feature = "cookie_rs")]
|
||||||
use cookie::CookieJar;
|
use cookie::CookieJar;
|
||||||
|
|
||||||
/// The `Cookie` header
|
/// The `Cookie` header
|
||||||
@@ -13,22 +17,22 @@ 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.
|
||||||
#[deriving(Clone, PartialEq, Show)]
|
#[deriving(Clone, PartialEq, Show)]
|
||||||
pub struct Cookie(pub Vec<CookieRs>);
|
pub struct TypedCookie<T>(pub Vec<T>);
|
||||||
|
|
||||||
impl Header for Cookie {
|
impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> {
|
||||||
fn header_name(_: Option<Cookie>) -> &'static str {
|
fn header_name(_: Option<TypedCookie<T>>) -> &'static str {
|
||||||
"Cookie"
|
"Cookie"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookie> {
|
fn parse_header(raw: &[Vec<u8>]) -> Option<TypedCookie<T>> {
|
||||||
let mut cookies: Vec<CookieRs> = vec![];
|
let mut cookies: Vec<T> = vec![];
|
||||||
for cookies_raw in raw.iter() {
|
for cookies_raw in raw.iter() {
|
||||||
match from_utf8(cookies_raw.as_slice()) {
|
match from_utf8(cookies_raw.as_slice()) {
|
||||||
Some(cookies_str) => {
|
Some(cookies_str) => {
|
||||||
for cookie_str in cookies_str.split(';') {
|
for cookie_str in cookies_str.split(';') {
|
||||||
match CookieRs::parse(cookie_str.trim()) {
|
match from_str(cookie_str.trim()) {
|
||||||
Ok(cookie) => cookies.push(cookie),
|
Some(cookie) => cookies.push(cookie),
|
||||||
Err(_) => return None
|
None => return None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -37,14 +41,14 @@ impl Header for Cookie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !cookies.is_empty() {
|
if !cookies.is_empty() {
|
||||||
Some(Cookie(cookies))
|
Some(TypedCookie(cookies))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let Cookie(ref value) = *self;
|
let TypedCookie(ref value) = *self;
|
||||||
let last = value.len() - 1;
|
let last = value.len() - 1;
|
||||||
for (i, cookie) in value.iter().enumerate() {
|
for (i, cookie) in value.iter().enumerate() {
|
||||||
try!(cookie.fmt(fmt));
|
try!(cookie.fmt(fmt));
|
||||||
@@ -56,15 +60,22 @@ impl Header for Cookie {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "cookie_rs"))]
|
||||||
|
pub type Cookie = TypedCookie<String>;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookie_rs")]
|
||||||
|
pub type Cookie = TypedCookie<CookieRs>;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookie_rs")]
|
||||||
impl Cookie {
|
impl Cookie {
|
||||||
/// This method can be used to crate CookieJar that can be used
|
/// This method can be used to crate CookieJar that can be used
|
||||||
/// to manipulate cookies and create corresponding `SetCookie` header afterwards.
|
/// to manipulate cookies and create corresponding `SetCookie` header afterwards.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn to_cookie_jar(&self, key: &[u8]) -> CookieJar {
|
fn to_cookie_jar(&self, key: &[u8]) -> CookieJar {
|
||||||
let mut jar = CookieJar::new(key);
|
let mut jar = CookieJar::new(key);
|
||||||
let &Cookie(ref cookies) = self;
|
let &TypedCookie(ref cookies) = self;
|
||||||
for cookie in cookies.iter() {
|
for cookie in cookies.iter() {
|
||||||
jar.add_original(cookie.clone());
|
jar.add_original((*cookie).clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
jar
|
jar
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use header::Header;
|
use header::Header;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
|
#[cfg(feature = "cookie_rs")]
|
||||||
use cookie::CookieJar;
|
use cookie::CookieJar;
|
||||||
|
|
||||||
/// The `Set-Cookie` header
|
/// The `Set-Cookie` header
|
||||||
@@ -45,6 +47,7 @@ impl SetCookie {
|
|||||||
/// Use this to crate SetCookie header from CookieJar using
|
/// Use this to crate SetCookie header from CookieJar using
|
||||||
/// calculated delta.
|
/// calculated delta.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
#[cfg(feature = "cookie_rs")]
|
||||||
fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
|
fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
|
||||||
SetCookie(jar.delta())
|
SetCookie(jar.delta())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ extern crate "unsafe-any" as uany;
|
|||||||
extern crate "move-acceptor" as macceptor;
|
extern crate "move-acceptor" as macceptor;
|
||||||
extern crate intertwine;
|
extern crate intertwine;
|
||||||
extern crate typeable;
|
extern crate typeable;
|
||||||
extern crate cookie;
|
#[cfg(feature = "cookie_rs")] extern crate cookie;
|
||||||
|
|
||||||
pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port};
|
pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port};
|
||||||
pub use mimewrapper::mime;
|
pub use mimewrapper::mime;
|
||||||
|
|||||||
Reference in New Issue
Block a user