From 1db1ce1ca375ff4bc2d6a716758a0f27c7f23458 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 7 Mar 2018 15:41:46 -0800 Subject: [PATCH] one more cookie handling fix --- src/client/request.rs | 27 +++++---------------------- src/client/response.rs | 12 +++--------- src/httprequest.rs | 24 +++++++++--------------- src/httpresponse.rs | 9 ++------- src/multipart.rs | 9 ++------- tests/test_client.rs | 2 ++ 6 files changed, 23 insertions(+), 60 deletions(-) diff --git a/src/client/request.rs b/src/client/request.rs index a63d739aa..1368dc58f 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -214,13 +214,8 @@ impl fmt::Debug for ClientRequest { let res = write!(f, "\nClientRequest {:?} {}:{}\n", self.version, self.method, self.uri); let _ = write!(f, " headers:\n"); - for key in self.headers.keys() { - let vals: Vec<_> = self.headers.get_all(key).iter().collect(); - if vals.len() > 1 { - let _ = write!(f, " {:?}: {:?}\n", key, vals); - } else { - let _ = write!(f, " {:?}: {:?}\n", key, vals[0]); - } + for (key, val) in self.headers.iter() { + let _ = write!(f, " {:?}: {:?}\n", key, val); } res } @@ -543,22 +538,10 @@ impl ClientRequestBuilder { let mut request = self.request.take().expect("cannot reuse request builder"); // set cookies - if let Some(ref jar) = self.cookies { - let ncookies = jar.iter().count(); - 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(' '); - } - } + if let Some(ref mut jar) = self.cookies { + for cookie in jar.delta() { request.headers.append( - header::COOKIE, HeaderValue::from_str(&payload)?); + header::COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap()); } } request.body = body.into(); diff --git a/src/client/response.rs b/src/client/response.rs index 944b4c839..d1d7370c0 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -83,8 +83,7 @@ impl ClientResponse { let msg = self.as_mut(); let mut cookies = Vec::new(); for val in msg.headers.get_all(header::SET_COOKIE).iter() { - let s = str::from_utf8(val.as_bytes()) - .map_err(CookieParseError::from)?; + let s = str::from_utf8(val.as_bytes()).map_err(CookieParseError::from)?; cookies.push(Cookie::parse_encoded(s)?.into_owned()); } msg.cookies = Some(cookies) @@ -110,13 +109,8 @@ impl fmt::Debug for ClientResponse { let res = write!( f, "\nClientResponse {:?} {}\n", self.version(), self.status()); let _ = write!(f, " headers:\n"); - for key in self.headers().keys() { - let vals: Vec<_> = self.headers().get_all(key).iter().collect(); - if vals.len() > 1 { - let _ = write!(f, " {:?}: {:?}\n", key, vals); - } else { - let _ = write!(f, " {:?}: {:?}\n", key, vals[0]); - } + for (key, val) in self.headers().iter() { + let _ = write!(f, " {:?}: {:?}\n", key, val); } res } diff --git a/src/httprequest.rs b/src/httprequest.rs index 6f30dc010..5203b8cca 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -333,12 +333,9 @@ impl HttpRequest { if self.as_ref().cookies.is_none() { let msg = self.as_mut(); let mut cookies = Vec::new(); - if let Some(val) = msg.headers.get(header::COOKIE) { - let s = str::from_utf8(val.as_bytes()) - .map_err(CookieParseError::from)?; - for cookie in s.split("; ") { - cookies.push(Cookie::parse_encoded(cookie)?.into_owned()); - } + for hdr in msg.headers.get_all(header::COOKIE) { + let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?; + cookies.push(Cookie::parse_encoded(s)?.into_owned()); } msg.cookies = Some(cookies) } @@ -481,13 +478,8 @@ impl fmt::Debug for HttpRequest { let _ = write!(f, " params: {:?}\n", self.as_ref().params); } let _ = write!(f, " headers:\n"); - for key in self.as_ref().headers.keys() { - let vals: Vec<_> = self.as_ref().headers.get_all(key).iter().collect(); - if vals.len() > 1 { - let _ = write!(f, " {:?}: {:?}\n", key, vals); - } else { - let _ = write!(f, " {:?}: {:?}\n", key, vals[0]); - } + for (key, val) in self.as_ref().headers.iter() { + let _ = write!(f, " {:?}: {:?}\n", key, val); } res } @@ -525,8 +517,10 @@ mod tests { #[test] fn test_request_cookies() { - let req = TestRequest::with_header( - header::COOKIE, "cookie1=value1; cookie2=value2").finish(); + let req = TestRequest::default() + .header(header::COOKIE, "cookie1=value1") + .header(header::COOKIE, "cookie2=value2") + .finish(); { let cookies = req.cookies().unwrap(); assert_eq!(cookies.len(), 2); diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 154734c3d..04538ae74 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -206,13 +206,8 @@ impl fmt::Debug for HttpResponse { self.get_ref().reason.unwrap_or("")); let _ = write!(f, " encoding: {:?}\n", self.get_ref().encoding); let _ = write!(f, " headers:\n"); - for key in self.get_ref().headers.keys() { - let vals: Vec<_> = self.get_ref().headers.get_all(key).iter().collect(); - if vals.len() > 1 { - let _ = write!(f, " {:?}: {:?}\n", key, vals); - } else { - let _ = write!(f, " {:?}: {:?}\n", key, vals[0]); - } + for (key, val) in self.get_ref().headers.iter() { + let _ = write!(f, " {:?}: {:?}\n", key, val); } res } diff --git a/src/multipart.rs b/src/multipart.rs index 6211f6116..f70c78a78 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -399,13 +399,8 @@ impl fmt::Debug for Field { let res = write!(f, "\nMultipartField: {}\n", self.ct); let _ = write!(f, " boundary: {}\n", self.inner.borrow().boundary); let _ = write!(f, " headers:\n"); - for key in self.headers.keys() { - let vals: Vec<_> = self.headers.get_all(key).iter().collect(); - if vals.len() > 1 { - let _ = write!(f, " {:?}: {:?}\n", key, vals); - } else { - let _ = write!(f, " {:?}: {:?}\n", key, vals[0]); - } + for (key, val) in self.headers.iter() { + let _ = write!(f, " {:?}: {:?}\n", key, val); } res } diff --git a/tests/test_client.rs b/tests/test_client.rs index ef34d39a5..af4a4e50c 100644 --- a/tests/test_client.rs +++ b/tests/test_client.rs @@ -322,8 +322,10 @@ fn test_body_streaming_implicit() { assert_eq!(bytes, Bytes::from_static(STR.as_ref())); } +extern crate env_logger; #[test] fn test_client_cookie_handling() { + env_logger::init(); use actix_web::header::Cookie; fn err() -> Error { use std::io::{ErrorKind, Error as IoError};