mirror of
https://github.com/actix/actix-web.git
synced 2024-12-23 08:36:34 +00:00
another attempt to fix cookie handling
This commit is contained in:
parent
1ab676d7eb
commit
395243a539
4 changed files with 19 additions and 8 deletions
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
## 0.4.6 (2018-03-xx)
|
## 0.4.6 (2018-03-xx)
|
||||||
|
|
||||||
|
* Fix client cookie handling
|
||||||
|
|
||||||
|
|
||||||
## 0.4.5 (2018-03-07)
|
## 0.4.5 (2018-03-07)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::{fmt, mem};
|
use std::{fmt, mem};
|
||||||
|
use std::fmt::Write as FmtWrite;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use actix::{Addr, Unsync};
|
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 http::header::{self, HeaderName, HeaderValue};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use percent_encoding::{USERINFO_ENCODE_SET, percent_encode};
|
||||||
|
|
||||||
use body::Body;
|
use body::Body;
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
@ -539,10 +541,14 @@ impl ClientRequestBuilder {
|
||||||
|
|
||||||
// set cookies
|
// set cookies
|
||||||
if let Some(ref mut jar) = self.cookies {
|
if let Some(ref mut jar) = self.cookies {
|
||||||
for cookie in jar.delta() {
|
let mut cookie = String::new();
|
||||||
request.headers.append(
|
for c in jar.delta() {
|
||||||
header::COOKIE, HeaderValue::from_str(&cookie.to_string()).unwrap());
|
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();
|
request.body = body.into();
|
||||||
Ok(request)
|
Ok(request)
|
||||||
|
|
|
@ -335,7 +335,11 @@ impl<S> HttpRequest<S> {
|
||||||
let mut cookies = Vec::new();
|
let mut cookies = Vec::new();
|
||||||
for hdr in msg.headers.get_all(header::COOKIE) {
|
for hdr in msg.headers.get_all(header::COOKIE) {
|
||||||
let s = str::from_utf8(hdr.as_bytes()).map_err(CookieParseError::from)?;
|
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)
|
msg.cookies = Some(cookies)
|
||||||
}
|
}
|
||||||
|
|
|
@ -792,8 +792,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_response_cookies() {
|
fn test_response_cookies() {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.insert(COOKIE, HeaderValue::from_static("cookie1=value1; HttpOnly;"));
|
headers.insert(COOKIE, HeaderValue::from_static("cookie1=value1"));
|
||||||
headers.insert(COOKIE, HeaderValue::from_static("cookie2=value2; HttpOnly;"));
|
headers.insert(COOKIE, HeaderValue::from_static("cookie2=value2"));
|
||||||
|
|
||||||
let req = HttpRequest::new(
|
let req = HttpRequest::new(
|
||||||
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
|
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")
|
let mut val: Vec<_> = resp.headers().get_all("Set-Cookie")
|
||||||
.iter().map(|v| v.to_str().unwrap().to_owned()).collect();
|
.iter().map(|v| v.to_str().unwrap().to_owned()).collect();
|
||||||
val.sort();
|
val.sort();
|
||||||
println!("{:?}", val);
|
assert!(val[0].starts_with("cookie2=; Max-Age=0;"));
|
||||||
assert!(val[0].starts_with("cookie2=; HttpOnly; Max-Age=0;"));
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
val[1],"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400");
|
val[1],"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue