1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-23 00:26:34 +00:00

another attempt to fix cookie handling

This commit is contained in:
Nikolay Kim 2018-03-08 11:16:54 -08:00
parent 1ab676d7eb
commit 395243a539
4 changed files with 19 additions and 8 deletions

View file

@ -2,6 +2,8 @@
## 0.4.6 (2018-03-xx)
* Fix client cookie handling
## 0.4.5 (2018-03-07)

View file

@ -1,4 +1,5 @@
use std::{fmt, mem};
use std::fmt::Write as FmtWrite;
use std::io::Write;
use actix::{Addr, Unsync};
@ -8,6 +9,7 @@ use http::{uri, HeaderMap, Method, Version, Uri, HttpTryFrom, Error as HttpError
use http::header::{self, HeaderName, HeaderValue};
use serde_json;
use serde::Serialize;
use percent_encoding::{USERINFO_ENCODE_SET, percent_encode};
use body::Body;
use error::Error;
@ -539,10 +541,14 @@ impl ClientRequestBuilder {
// set cookies
if let Some(ref mut jar) = self.cookies {
for cookie in jar.delta() {
request.headers.append(
header::COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap());
let mut cookie = String::new();
for c in jar.delta() {
let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET);
let value = percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET);
let _ = write!(&mut cookie, ";{}={}", name, value);
}
request.headers.insert(
header::COOKIE, HeaderValue::from_str(&cookie.as_str()[1..]).unwrap());
}
request.body = body.into();
Ok(request)

View file

@ -335,7 +335,11 @@ impl<S> HttpRequest<S> {
let mut cookies = Vec::new();
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());
for cookie_str in s.split(';').map(|s| s.trim()) {
if !cookie_str.is_empty() {
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned());
}
}
}
msg.cookies = Some(cookies)
}

View file

@ -792,8 +792,8 @@ mod tests {
#[test]
fn test_response_cookies() {
let mut headers = HeaderMap::new();
headers.insert(COOKIE, HeaderValue::from_static("cookie1=value1; HttpOnly;"));
headers.insert(COOKIE, HeaderValue::from_static("cookie2=value2; HttpOnly;"));
headers.insert(COOKIE, HeaderValue::from_static("cookie1=value1"));
headers.insert(COOKIE, HeaderValue::from_static("cookie2=value2"));
let req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
@ -816,8 +816,7 @@ mod tests {
let mut val: Vec<_> = resp.headers().get_all("Set-Cookie")
.iter().map(|v| v.to_str().unwrap().to_owned()).collect();
val.sort();
println!("{:?}", val);
assert!(val[0].starts_with("cookie2=; HttpOnly; Max-Age=0;"));
assert!(val[0].starts_with("cookie2=; Max-Age=0;"));
assert_eq!(
val[1],"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400");
}