1use std::convert::TryInto;
4use std::fmt;
5use std::sync::RwLock;
6use std::time::SystemTime;
7
8use crate::header::{HeaderValue, SET_COOKIE};
9use bytes::Bytes;
10
11pub trait CookieStore: Send + Sync {
13 fn set_cookies(&self, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>, url: &url::Url);
15 fn cookies(&self, url: &url::Url) -> Option<HeaderValue>;
17}
18
19pub struct Cookie<'a>(cookie_crate::Cookie<'a>);
21
22#[derive(Debug, Default)]
32pub struct Jar(RwLock<cookie_store::CookieStore>);
33
34impl<'a> Cookie<'a> {
37 fn parse(value: &'a HeaderValue) -> Result<Cookie<'a>, CookieParseError> {
38 std::str::from_utf8(value.as_bytes())
39 .map_err(cookie_crate::ParseError::from)
40 .and_then(cookie_crate::Cookie::parse)
41 .map_err(CookieParseError)
42 .map(Cookie)
43 }
44
45 pub fn name(&self) -> &str {
47 self.0.name()
48 }
49
50 pub fn value(&self) -> &str {
52 self.0.value()
53 }
54
55 pub fn http_only(&self) -> bool {
57 self.0.http_only().unwrap_or(false)
58 }
59
60 pub fn secure(&self) -> bool {
62 self.0.secure().unwrap_or(false)
63 }
64
65 pub fn same_site_lax(&self) -> bool {
67 self.0.same_site() == Some(cookie_crate::SameSite::Lax)
68 }
69
70 pub fn same_site_strict(&self) -> bool {
72 self.0.same_site() == Some(cookie_crate::SameSite::Strict)
73 }
74
75 pub fn path(&self) -> Option<&str> {
77 self.0.path()
78 }
79
80 pub fn domain(&self) -> Option<&str> {
82 self.0.domain()
83 }
84
85 pub fn max_age(&self) -> Option<std::time::Duration> {
87 self.0.max_age().map(|d| {
88 d.try_into()
89 .expect("time::Duration into std::time::Duration")
90 })
91 }
92
93 pub fn expires(&self) -> Option<SystemTime> {
95 match self.0.expires() {
96 Some(cookie_crate::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
97 None | Some(cookie_crate::Expiration::Session) => None,
98 }
99 }
100}
101
102impl<'a> fmt::Debug for Cookie<'a> {
103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104 self.0.fmt(f)
105 }
106}
107
108pub(crate) fn extract_response_cookie_headers<'a>(
109 headers: &'a hyper::HeaderMap,
110) -> impl Iterator<Item = &'a HeaderValue> + 'a {
111 headers.get_all(SET_COOKIE).iter()
112}
113
114pub(crate) fn extract_response_cookies<'a>(
115 headers: &'a hyper::HeaderMap,
116) -> impl Iterator<Item = Result<Cookie<'a>, CookieParseError>> + 'a {
117 headers
118 .get_all(SET_COOKIE)
119 .iter()
120 .map(|value| Cookie::parse(value))
121}
122
123pub(crate) struct CookieParseError(cookie_crate::ParseError);
125
126impl<'a> fmt::Debug for CookieParseError {
127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128 self.0.fmt(f)
129 }
130}
131
132impl<'a> fmt::Display for CookieParseError {
133 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134 self.0.fmt(f)
135 }
136}
137
138impl std::error::Error for CookieParseError {}
139
140impl Jar {
143 pub fn add_cookie_str(&self, cookie: &str, url: &url::Url) {
159 let cookies = cookie_crate::Cookie::parse(cookie)
160 .ok()
161 .map(|c| c.into_owned())
162 .into_iter();
163 self.0.write().unwrap().store_response_cookies(cookies, url);
164 }
165}
166
167impl CookieStore for Jar {
168 fn set_cookies(&self, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>, url: &url::Url) {
169 let iter =
170 cookie_headers.filter_map(|val| Cookie::parse(val).map(|c| c.0.into_owned()).ok());
171
172 self.0.write().unwrap().store_response_cookies(iter, url);
173 }
174
175 fn cookies(&self, url: &url::Url) -> Option<HeaderValue> {
176 let s = self
177 .0
178 .read()
179 .unwrap()
180 .get_request_values(url)
181 .map(|(name, value)| format!("{name}={value}"))
182 .collect::<Vec<_>>()
183 .join("; ");
184
185 if s.is_empty() {
186 return None;
187 }
188
189 HeaderValue::from_maybe_shared(Bytes::from(s)).ok()
190 }
191}