1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-30 03:50:42 +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",
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();

View file

@ -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
}

View file

@ -333,12 +333,9 @@ impl<S> HttpRequest<S> {
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<S> fmt::Debug for HttpRequest<S> {
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);

View file

@ -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
}

View file

@ -399,13 +399,8 @@ impl<S> fmt::Debug for Field<S> {
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
}

View file

@ -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};