1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 07:36:43 +00:00

one more cookie handling fix

This commit is contained in:
Nikolay Kim 2018-03-07 15:41:46 -08:00
parent f55ef3a059
commit 1db1ce1ca3
6 changed files with 23 additions and 60 deletions

View file

@ -214,13 +214,8 @@ impl fmt::Debug for ClientRequest {
let res = write!(f, "\nClientRequest {:?} {}:{}\n", let res = write!(f, "\nClientRequest {:?} {}:{}\n",
self.version, self.method, self.uri); self.version, self.method, self.uri);
let _ = write!(f, " headers:\n"); let _ = write!(f, " headers:\n");
for key in self.headers.keys() { for (key, val) in self.headers.iter() {
let vals: Vec<_> = self.headers.get_all(key).iter().collect(); let _ = write!(f, " {:?}: {:?}\n", key, val);
if vals.len() > 1 {
let _ = write!(f, " {:?}: {:?}\n", key, vals);
} else {
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
}
} }
res res
} }
@ -543,22 +538,10 @@ impl ClientRequestBuilder {
let mut request = self.request.take().expect("cannot reuse request builder"); let mut request = self.request.take().expect("cannot reuse request builder");
// set cookies // set cookies
if let Some(ref jar) = self.cookies { if let Some(ref mut jar) = self.cookies {
let ncookies = jar.iter().count(); for cookie in jar.delta() {
if ncookies > 0 {
let mut payload = String::new();
for (ix, cookie) in jar.iter().enumerate() {
payload.push_str(&cookie.name());
payload.push('=');
payload.push_str(&cookie.value());
// semi-colon delimited, except for final k-v pair
if ix < ncookies - 1 {
payload.push(';');
payload.push(' ');
}
}
request.headers.append( request.headers.append(
header::COOKIE, HeaderValue::from_str(&payload)?); header::COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap());
} }
} }
request.body = body.into(); request.body = body.into();

View file

@ -83,8 +83,7 @@ impl ClientResponse {
let msg = self.as_mut(); let msg = self.as_mut();
let mut cookies = Vec::new(); let mut cookies = Vec::new();
for val in msg.headers.get_all(header::SET_COOKIE).iter() { for val in msg.headers.get_all(header::SET_COOKIE).iter() {
let s = str::from_utf8(val.as_bytes()) let s = str::from_utf8(val.as_bytes()).map_err(CookieParseError::from)?;
.map_err(CookieParseError::from)?;
cookies.push(Cookie::parse_encoded(s)?.into_owned()); cookies.push(Cookie::parse_encoded(s)?.into_owned());
} }
msg.cookies = Some(cookies) msg.cookies = Some(cookies)
@ -110,13 +109,8 @@ impl fmt::Debug for ClientResponse {
let res = write!( let res = write!(
f, "\nClientResponse {:?} {}\n", self.version(), self.status()); f, "\nClientResponse {:?} {}\n", self.version(), self.status());
let _ = write!(f, " headers:\n"); let _ = write!(f, " headers:\n");
for key in self.headers().keys() { for (key, val) in self.headers().iter() {
let vals: Vec<_> = self.headers().get_all(key).iter().collect(); let _ = write!(f, " {:?}: {:?}\n", key, val);
if vals.len() > 1 {
let _ = write!(f, " {:?}: {:?}\n", key, vals);
} else {
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
}
} }
res res
} }

View file

@ -333,12 +333,9 @@ impl<S> HttpRequest<S> {
if self.as_ref().cookies.is_none() { if self.as_ref().cookies.is_none() {
let msg = self.as_mut(); let msg = self.as_mut();
let mut cookies = Vec::new(); let mut cookies = Vec::new();
if let Some(val) = msg.headers.get(header::COOKIE) { for hdr in msg.headers.get_all(header::COOKIE) {
let s = str::from_utf8(val.as_bytes()) let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
.map_err(CookieParseError::from)?; cookies.push(Cookie::parse_encoded(s)?.into_owned());
for cookie in s.split("; ") {
cookies.push(Cookie::parse_encoded(cookie)?.into_owned());
}
} }
msg.cookies = Some(cookies) msg.cookies = Some(cookies)
} }
@ -481,13 +478,8 @@ impl<S> fmt::Debug for HttpRequest<S> {
let _ = write!(f, " params: {:?}\n", self.as_ref().params); let _ = write!(f, " params: {:?}\n", self.as_ref().params);
} }
let _ = write!(f, " headers:\n"); let _ = write!(f, " headers:\n");
for key in self.as_ref().headers.keys() { for (key, val) in self.as_ref().headers.iter() {
let vals: Vec<_> = self.as_ref().headers.get_all(key).iter().collect(); let _ = write!(f, " {:?}: {:?}\n", key, val);
if vals.len() > 1 {
let _ = write!(f, " {:?}: {:?}\n", key, vals);
} else {
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
}
} }
res res
} }
@ -525,8 +517,10 @@ mod tests {
#[test] #[test]
fn test_request_cookies() { fn test_request_cookies() {
let req = TestRequest::with_header( let req = TestRequest::default()
header::COOKIE, "cookie1=value1; cookie2=value2").finish(); .header(header::COOKIE, "cookie1=value1")
.header(header::COOKIE, "cookie2=value2")
.finish();
{ {
let cookies = req.cookies().unwrap(); let cookies = req.cookies().unwrap();
assert_eq!(cookies.len(), 2); assert_eq!(cookies.len(), 2);

View file

@ -206,13 +206,8 @@ impl fmt::Debug for HttpResponse {
self.get_ref().reason.unwrap_or("")); self.get_ref().reason.unwrap_or(""));
let _ = write!(f, " encoding: {:?}\n", self.get_ref().encoding); let _ = write!(f, " encoding: {:?}\n", self.get_ref().encoding);
let _ = write!(f, " headers:\n"); let _ = write!(f, " headers:\n");
for key in self.get_ref().headers.keys() { for (key, val) in self.get_ref().headers.iter() {
let vals: Vec<_> = self.get_ref().headers.get_all(key).iter().collect(); let _ = write!(f, " {:?}: {:?}\n", key, val);
if vals.len() > 1 {
let _ = write!(f, " {:?}: {:?}\n", key, vals);
} else {
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
}
} }
res res
} }

View file

@ -399,13 +399,8 @@ impl<S> fmt::Debug for Field<S> {
let res = write!(f, "\nMultipartField: {}\n", self.ct); let res = write!(f, "\nMultipartField: {}\n", self.ct);
let _ = write!(f, " boundary: {}\n", self.inner.borrow().boundary); let _ = write!(f, " boundary: {}\n", self.inner.borrow().boundary);
let _ = write!(f, " headers:\n"); let _ = write!(f, " headers:\n");
for key in self.headers.keys() { for (key, val) in self.headers.iter() {
let vals: Vec<_> = self.headers.get_all(key).iter().collect(); let _ = write!(f, " {:?}: {:?}\n", key, val);
if vals.len() > 1 {
let _ = write!(f, " {:?}: {:?}\n", key, vals);
} else {
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
}
} }
res res
} }

View file

@ -322,8 +322,10 @@ fn test_body_streaming_implicit() {
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
extern crate env_logger;
#[test] #[test]
fn test_client_cookie_handling() { fn test_client_cookie_handling() {
env_logger::init();
use actix_web::header::Cookie; use actix_web::header::Cookie;
fn err() -> Error { fn err() -> Error {
use std::io::{ErrorKind, Error as IoError}; use std::io::{ErrorKind, Error as IoError};