Skip to content

Commit 5908a3d

Browse files
committed
correctly parse multiple values out of the Cookie header
1 parent 28e5904 commit 5908a3d

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/cookies/middleware.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::response::CookieEvent;
22
use crate::utils::BoxFuture;
33
use crate::{Middleware, Next, Request};
44

5-
use cookie::CookieJar;
5+
use cookie::{Cookie, CookieJar};
66
use http_types::headers;
77

88
use std::sync::{Arc, RwLock};
@@ -77,16 +77,21 @@ pub(crate) struct CookieData {
7777

7878
impl CookieData {
7979
pub(crate) fn from_request<S>(req: &Request<S>) -> Self {
80-
let cookie_jar = req.request.cookies().and_then(|cookies| {
81-
let mut jar = CookieJar::new();
82-
for cookie in cookies.into_iter() {
83-
jar.add_original(cookie.into_owned());
84-
}
80+
let mut jar = CookieJar::new();
8581

86-
Ok(jar)
87-
});
88-
let content = Arc::new(RwLock::new(cookie_jar.unwrap_or_default()));
82+
if let Some(cookie_headers) = req.header(&headers::COOKIE) {
83+
for cookie_header in cookie_headers {
84+
// spec says there should be only one, so this is permissive
85+
for pair in cookie_header.as_str().split(";").map(str::trim) {
86+
if let Ok(cookie) = Cookie::parse(String::from(pair)) {
87+
jar.add_original(cookie);
88+
}
89+
}
90+
}
91+
}
8992

90-
CookieData { content }
93+
CookieData {
94+
content: Arc::new(RwLock::new(jar)),
95+
}
9196
}
9297
}

0 commit comments

Comments
 (0)