adjustments to Cookie and SetCookie
This commit is contained in:
@@ -4,10 +4,6 @@ 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"
|
||||||
|
|
||||||
@@ -37,4 +33,3 @@ 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,33 +1,31 @@
|
|||||||
use header::Header;
|
use header::{Header, HeaderFormat};
|
||||||
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;
|
use std::from_str::from_str;
|
||||||
|
|
||||||
#[cfg(feature = "cookie_rs")]
|
use cookie::Cookie;
|
||||||
use cookie::Cookie as CookieRs;
|
|
||||||
#[cfg(feature = "cookie_rs")]
|
|
||||||
use cookie::CookieJar;
|
use cookie::CookieJar;
|
||||||
|
|
||||||
/// The `Cookie` header
|
/// The `Cookie` header. Defined in [RFC6265](tools.ietf.org/html/rfc6265#section-5.4):
|
||||||
///
|
///
|
||||||
/// If the user agent does attach a Cookie header field to an HTTP
|
/// > If the user agent does attach a Cookie header field to an HTTP
|
||||||
/// request, the user agent must send the cookie-string
|
/// > request, the user agent must send the cookie-string
|
||||||
/// as the value of the header field.
|
/// > as the value of the header field.
|
||||||
///
|
///
|
||||||
/// 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 TypedCookie<T>(pub Vec<T>);
|
pub struct Cookies(pub Vec<Cookie>);
|
||||||
|
|
||||||
impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> {
|
impl Header for Cookies {
|
||||||
fn header_name(_: Option<TypedCookie<T>>) -> &'static str {
|
fn header_name(_: Option<Cookies>) -> &'static str {
|
||||||
"Cookie"
|
"Cookie"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_header(raw: &[Vec<u8>]) -> Option<TypedCookie<T>> {
|
fn parse_header(raw: &[Vec<u8>]) -> Option<Cookies> {
|
||||||
let mut cookies: Vec<T> = vec![];
|
let mut cookies = 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[]) {
|
||||||
Some(cookies_str) => {
|
Some(cookies_str) => {
|
||||||
for cookie_str in cookies_str.split(';') {
|
for cookie_str in cookies_str.split(';') {
|
||||||
match from_str(cookie_str.trim()) {
|
match from_str(cookie_str.trim()) {
|
||||||
@@ -41,17 +39,20 @@ impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !cookies.is_empty() {
|
if !cookies.is_empty() {
|
||||||
Some(TypedCookie(cookies))
|
Some(Cookies(cookies))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HeaderFormat for Cookies {
|
||||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let TypedCookie(ref value) = *self;
|
let cookies = &self.0;
|
||||||
let last = value.len() - 1;
|
let last = cookies.len() - 1;
|
||||||
for (i, cookie) in value.iter().enumerate() {
|
for (i, cookie) in cookies.iter().enumerate() {
|
||||||
try!(cookie.fmt(fmt));
|
try!(write!(fmt, "{}={}", cookie.name, cookie.value));
|
||||||
if i < last {
|
if i < last {
|
||||||
try!("; ".fmt(fmt));
|
try!("; ".fmt(fmt));
|
||||||
}
|
}
|
||||||
@@ -60,25 +61,37 @@ impl<T: FromStr + Show + Clone + Send + Sync> Header for TypedCookie<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "cookie_rs"))]
|
impl Cookies {
|
||||||
pub type Cookie = TypedCookie<String>;
|
/// This method can be used to create CookieJar that can be used
|
||||||
|
/// to manipulate cookies and create a corresponding `SetCookie` header afterwards.
|
||||||
#[cfg(feature = "cookie_rs")]
|
pub fn to_cookie_jar(&self, key: &[u8]) -> CookieJar<'static> {
|
||||||
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 mut jar = CookieJar::new(key);
|
||||||
let &TypedCookie(ref cookies) = self;
|
for cookie in self.0.iter() {
|
||||||
for cookie in cookies.iter() {
|
|
||||||
jar.add_original((*cookie).clone());
|
jar.add_original((*cookie).clone());
|
||||||
}
|
}
|
||||||
|
jar
|
||||||
jar
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse() {
|
||||||
|
let h = Header::parse_header([b"foo=bar; baz=quux".to_vec()][]);
|
||||||
|
let c1 = Cookie::new("foo".to_string(), "bar".to_string());
|
||||||
|
let c2 = Cookie::new("baz".to_string(), "quux".to_string());
|
||||||
|
assert_eq!(h, Some(Cookies(vec![c1, c2])));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_fmt() {
|
||||||
|
use header::Headers;
|
||||||
|
|
||||||
|
let mut cookie = Cookie::new("foo".to_string(), "bar".to_string());
|
||||||
|
cookie.httponly = true;
|
||||||
|
cookie.path = Some("/p".to_string());
|
||||||
|
let cookies = Cookies(vec![cookie, Cookie::new("baz".to_string(), "quux".to_string())]);
|
||||||
|
let mut headers = Headers::new();
|
||||||
|
headers.set(cookies);
|
||||||
|
|
||||||
|
assert_eq!(headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
pub use self::accept::Accept;
|
pub use self::accept::Accept;
|
||||||
pub use self::authorization::Authorization;
|
pub use self::authorization::Authorization;
|
||||||
|
pub use self::cookie::Cookies;
|
||||||
pub use self::connection::Connection;
|
pub use self::connection::Connection;
|
||||||
pub use self::content_length::ContentLength;
|
pub use self::content_length::ContentLength;
|
||||||
pub use self::content_type::ContentType;
|
pub use self::content_type::ContentType;
|
||||||
@@ -18,6 +19,7 @@ pub use self::transfer_encoding::TransferEncoding;
|
|||||||
pub use self::upgrade::Upgrade;
|
pub use self::upgrade::Upgrade;
|
||||||
pub use self::user_agent::UserAgent;
|
pub use self::user_agent::UserAgent;
|
||||||
pub use self::server::Server;
|
pub use self::server::Server;
|
||||||
|
pub use self::set_cookie::SetCookie;
|
||||||
|
|
||||||
use std::fmt::{mod, Show};
|
use std::fmt::{mod, Show};
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
@@ -31,6 +33,12 @@ pub mod authorization;
|
|||||||
/// Exposes the Cookie header.
|
/// Exposes the Cookie header.
|
||||||
pub mod cookie;
|
pub mod cookie;
|
||||||
|
|
||||||
|
/// Exposes the Cookie header.
|
||||||
|
pub mod cookie;
|
||||||
|
|
||||||
|
/// Exposes the Set-Cookie header.
|
||||||
|
pub mod set_cookie;
|
||||||
|
|
||||||
/// Exposes the Connection header.
|
/// Exposes the Connection header.
|
||||||
pub mod connection;
|
pub mod connection;
|
||||||
|
|
||||||
@@ -64,10 +72,8 @@ pub mod upgrade;
|
|||||||
/// Exposes the UserAgent header.
|
/// Exposes the UserAgent header.
|
||||||
pub mod user_agent;
|
pub mod user_agent;
|
||||||
|
|
||||||
|
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
|
|
||||||
fn from_comma_delimited<T: FromStr>(raw: &[Vec<u8>]) -> Option<Vec<T>> {
|
fn from_comma_delimited<T: FromStr>(raw: &[Vec<u8>]) -> Option<Vec<T>> {
|
||||||
if raw.len() != 1 {
|
if raw.len() != 1 {
|
||||||
return None;
|
return None;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use header::Header;
|
use header::{Header, HeaderFormat};
|
||||||
use std::fmt;
|
use std::fmt::{mod, Show};
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
#[cfg(feature = "cookie_rs")]
|
use cookie::Cookie;
|
||||||
use cookie::CookieJar;
|
use cookie::CookieJar;
|
||||||
|
|
||||||
/// The `Set-Cookie` header
|
/// The `Set-Cookie` header
|
||||||
@@ -11,7 +11,7 @@ use cookie::CookieJar;
|
|||||||
/// "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with
|
/// "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with
|
||||||
/// a name-value-pair, followed by zero or more attribute-value pairs.
|
/// a name-value-pair, followed by zero or more attribute-value pairs.
|
||||||
#[deriving(Clone, PartialEq, Show)]
|
#[deriving(Clone, PartialEq, Show)]
|
||||||
pub struct SetCookie(pub Vec<String>);
|
pub struct SetCookie(pub Vec<Cookie>);
|
||||||
|
|
||||||
impl Header for SetCookie {
|
impl Header for SetCookie {
|
||||||
fn header_name(_: Option<SetCookie>) -> &'static str {
|
fn header_name(_: Option<SetCookie>) -> &'static str {
|
||||||
@@ -19,15 +19,16 @@ impl Header for SetCookie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_header(raw: &[Vec<u8>]) -> Option<SetCookie> {
|
fn parse_header(raw: &[Vec<u8>]) -> Option<SetCookie> {
|
||||||
let mut set_cookies: Vec<String> = vec![];
|
let mut set_cookies = vec![];
|
||||||
for set_cookies_raw in raw.iter() {
|
for set_cookies_raw in raw.iter() {
|
||||||
match from_utf8(set_cookies_raw.as_slice()) {
|
match from_utf8(set_cookies_raw[]) {
|
||||||
Some(set_cookies_str) => {
|
Some(s) if !s.is_empty() => {
|
||||||
if !set_cookies_str.is_empty() {
|
match from_str(s) {
|
||||||
set_cookies.push(set_cookies_str.to_string());
|
Some(cookie) => set_cookies.push(cookie),
|
||||||
|
None => ()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => ()
|
_ => ()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,18 +39,51 @@ impl Header for SetCookie {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_header(&self, _: &mut fmt::Formatter) -> fmt::Result {
|
}
|
||||||
unimplemented!()
|
|
||||||
|
impl HeaderFormat for SetCookie {
|
||||||
|
|
||||||
|
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
for (i, cookie) in self.0.iter().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
try!(f.write(b"\r\nSet-Cookie: "));
|
||||||
|
}
|
||||||
|
try!(cookie.fmt(f));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl SetCookie {
|
impl SetCookie {
|
||||||
/// Use this to crate SetCookie header from CookieJar using
|
/// Use this to create SetCookie header from CookieJar using
|
||||||
/// calculated delta.
|
/// calculated delta.
|
||||||
#[allow(dead_code)]
|
pub fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
|
||||||
#[cfg(feature = "cookie_rs")]
|
//FIXME: https://github.com/alexcrichton/cookie-rs/issues/2
|
||||||
fn from_cookie_jar(jar: &CookieJar) -> SetCookie {
|
SetCookie(jar.delta().into_iter().map(|s| from_str(s[]).unwrap()).collect())
|
||||||
SetCookie(jar.delta())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse() {
|
||||||
|
let h = Header::parse_header([b"foo=bar; HttpOnly".to_vec()][]);
|
||||||
|
let mut c1 = Cookie::new("foo".to_string(), "bar".to_string());
|
||||||
|
c1.httponly = true;
|
||||||
|
|
||||||
|
assert_eq!(h, Some(SetCookie(vec![c1])));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_fmt() {
|
||||||
|
use header::Headers;
|
||||||
|
|
||||||
|
let mut cookie = Cookie::new("foo".to_string(), "bar".to_string());
|
||||||
|
cookie.httponly = true;
|
||||||
|
cookie.path = Some("/p".to_string());
|
||||||
|
let cookies = SetCookie(vec![cookie, Cookie::new("baz".to_string(), "quux".to_string())]);
|
||||||
|
let mut headers = Headers::new();
|
||||||
|
headers.set(cookies);
|
||||||
|
|
||||||
|
assert_eq!(headers.to_string()[], "Set-Cookie: foo=bar; HttpOnly; Path=/p\r\nSet-Cookie: baz=quux; Path=/\r\n");
|
||||||
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
#[cfg(feature = "cookie_rs")] extern crate cookie;
|
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