Add cookie_rs as default feature and implement conditional compilation.

This commit is contained in:
Stanislav Panferov
2014-10-08 11:38:01 +04:00
committed by Sean McArthur
parent 20f156c592
commit a3fc51611f
4 changed files with 33 additions and 14 deletions

View File

@@ -4,6 +4,10 @@ name = "hyper"
version = "0.0.1"
authors = ["Sean McArthur <sean.monstar@gmail.com>"]
[features]
cookie_rs = ["cookie"]
default = ["cookie_rs"]
[dependencies.url]
git = "https://github.com/servo/rust-url"
@@ -32,4 +36,5 @@ git = "https://github.com/carllerche/curl-rust"
git = "https://github.com/chris-morgan/rust-http"
[dependencies.cookie]
git = "https://github.com/alexcrichton/cookie-rs"
git = "https://github.com/alexcrichton/cookie-rs"
optional = true

View File

@@ -1,7 +1,11 @@
use header::Header;
use std::fmt::{mod, Show};
use std::str::from_utf8;
use std::from_str::FromStr;
#[cfg(feature = "cookie_rs")]
use cookie::Cookie as CookieRs;
#[cfg(feature = "cookie_rs")]
use cookie::CookieJar;
/// The `Cookie` header
@@ -13,22 +17,22 @@ use cookie::CookieJar;
/// 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<CookieRs>);
pub struct TypedCookie<T>(pub Vec<T>);
impl Header for Cookie {
fn header_name(_: Option<Cookie>) -> &'static str {
impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> {
fn header_name(_: Option<TypedCookie<T>>) -> &'static str {
"Cookie"
}
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookie> {
let mut cookies: Vec<CookieRs> = vec![];
fn parse_header(raw: &[Vec<u8>]) -> Option<TypedCookie<T>> {
let mut cookies: Vec<T> = vec![];
for cookies_raw in raw.iter() {
match from_utf8(cookies_raw.as_slice()) {
Some(cookies_str) => {
for cookie_str in cookies_str.split(';') {
match CookieRs::parse(cookie_str.trim()) {
Ok(cookie) => cookies.push(cookie),
Err(_) => return None
match from_str(cookie_str.trim()) {
Some(cookie) => cookies.push(cookie),
None => return None
}
}
},
@@ -37,14 +41,14 @@ impl Header for Cookie {
}
if !cookies.is_empty() {
Some(Cookie(cookies))
Some(TypedCookie(cookies))
} else {
None
}
}
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;
for (i, cookie) in value.iter().enumerate() {
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 {
/// 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;
let &TypedCookie(ref cookies) = self;
for cookie in cookies.iter() {
jar.add_original(cookie.clone());
jar.add_original((*cookie).clone());
}
jar

View File

@@ -1,6 +1,8 @@
use header::Header;
use std::fmt;
use std::str::from_utf8;
#[cfg(feature = "cookie_rs")]
use cookie::CookieJar;
/// The `Set-Cookie` header
@@ -45,6 +47,7 @@ impl SetCookie {
/// Use this to crate SetCookie header from CookieJar using
/// calculated delta.
#[allow(dead_code)]
#[cfg(feature = "cookie_rs")]
fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
SetCookie(jar.delta())
}

View File

@@ -136,7 +136,7 @@ extern crate "unsafe-any" as uany;
extern crate "move-acceptor" as macceptor;
extern crate intertwine;
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 mimewrapper::mime;