1use std::cmp::Ordering;
9use std::collections::HashMap;
10use std::collections::hash_map::Entry;
11use std::net::IpAddr;
12use std::time::SystemTime;
13
14use cookie::Cookie;
15use itertools::Itertools;
16use log::info;
17use net_traits::pub_domains::reg_suffix;
18use net_traits::{CookieSource, SiteDescriptor};
19use serde::{Deserialize, Serialize};
20use servo_url::ServoUrl;
21
22use crate::cookie::ServoCookie;
23
24#[derive(Clone, Debug, Deserialize, Serialize)]
25pub struct CookieStorage {
26 version: u32,
27 cookies_map: HashMap<String, Vec<ServoCookie>>,
28 max_per_host: usize,
29}
30
31#[derive(Debug)]
32pub enum RemoveCookieError {
33 Overlapping,
34 NonHTTP,
35}
36
37impl CookieStorage {
38 pub fn new(max_cookies: usize) -> CookieStorage {
39 CookieStorage {
40 version: 1,
41 cookies_map: HashMap::new(),
42 max_per_host: max_cookies,
43 }
44 }
45
46 pub fn remove(
48 &mut self,
49 cookie: &ServoCookie,
50 url: &ServoUrl,
51 source: CookieSource,
52 ) -> Result<Option<ServoCookie>, RemoveCookieError> {
53 let domain = reg_host(cookie.cookie.domain().as_ref().unwrap_or(&""));
54 let cookies = self.cookies_map.entry(domain).or_default();
55
56 if !cookie.cookie.secure().unwrap_or(false) && !url.is_secure_scheme() {
58 let new_domain = cookie.cookie.domain().as_ref().unwrap().to_owned();
59 let new_path = cookie.cookie.path().as_ref().unwrap().to_owned();
60
61 let any_overlapping = cookies.iter().any(|c| {
62 let existing_domain = c.cookie.domain().as_ref().unwrap().to_owned();
63 let existing_path = c.cookie.path().as_ref().unwrap().to_owned();
64
65 c.cookie.name() == cookie.cookie.name() &&
66 c.cookie.secure().unwrap_or(false) &&
67 (ServoCookie::domain_match(new_domain, existing_domain) ||
68 ServoCookie::domain_match(existing_domain, new_domain)) &&
69 ServoCookie::path_match(new_path, existing_path)
70 });
71
72 if any_overlapping {
73 return Err(RemoveCookieError::Overlapping);
74 }
75 }
76
77 let position = cookies.iter().position(|c| {
79 c.cookie.domain() == cookie.cookie.domain() &&
80 c.cookie.path() == cookie.cookie.path() &&
81 c.cookie.name() == cookie.cookie.name()
82 });
83
84 if let Some(ind) = position {
85 let c = cookies.remove(ind);
87
88 if c.cookie.http_only().unwrap_or(false) && source == CookieSource::NonHTTP {
90 cookies.push(c);
92 Err(RemoveCookieError::NonHTTP)
93 } else {
94 Ok(Some(c))
95 }
96 } else {
97 Ok(None)
98 }
99 }
100
101 pub fn delete_cookies_for_sites(&mut self, sites: &Vec<String>) {
102 for site in sites {
107 if let Some(cookies) = self.cookies_map.get_mut(site) {
113 for cookie in cookies.iter_mut() {
114 cookie.set_expiry_time_in_past();
115 }
116 }
117 }
118 }
119
120 pub fn clear_storage(&mut self, url: Option<&ServoUrl>) {
121 if let Some(url) = url {
122 let domain = reg_host(url.host_str().unwrap_or(""));
123 let cookies = self.cookies_map.entry(domain).or_default();
126 for cookie in cookies.iter_mut() {
127 cookie.set_expiry_time_in_past();
128 }
129 } else {
130 self.cookies_map.clear();
131 }
132 }
133
134 pub fn delete_cookie_with_name(&mut self, url: &ServoUrl, name: String) {
135 let domain = reg_host(url.host_str().unwrap_or(""));
136 let cookies = self.cookies_map.entry(domain).or_default();
139 for cookie in cookies.iter_mut() {
140 if cookie.cookie.name() == name {
141 cookie.set_expiry_time_in_past();
142 }
143 }
144 }
145
146 pub fn push(&mut self, mut cookie: ServoCookie, url: &ServoUrl, source: CookieSource) {
148 if cookie.cookie.secure().unwrap_or(false) && !url.is_secure_scheme() {
150 return;
151 }
152
153 let old_cookie = self.remove(&cookie, url, source);
154 if old_cookie.is_err() {
155 return;
157 }
158
159 if let Some(old_cookie) = old_cookie.unwrap() {
161 cookie.creation_time = old_cookie.creation_time;
163 }
164
165 let domain = reg_host(cookie.cookie.domain().as_ref().unwrap_or(&""));
167 let cookies = self.cookies_map.entry(domain).or_default();
168
169 if cookies.len() == self.max_per_host {
170 let old_len = cookies.len();
171 cookies.retain(|c| !is_cookie_expired(c));
172 let new_len = cookies.len();
173
174 if new_len == old_len &&
176 !evict_one_cookie(cookie.cookie.secure().unwrap_or(false), cookies)
177 {
178 return;
179 }
180 }
181 cookies.push(cookie);
182 }
183
184 pub fn cookie_comparator(a: &ServoCookie, b: &ServoCookie) -> Ordering {
185 let a_path_len = a.cookie.path().as_ref().map_or(0, |p| p.len());
186 let b_path_len = b.cookie.path().as_ref().map_or(0, |p| p.len());
187 match a_path_len.cmp(&b_path_len) {
188 Ordering::Equal => a.creation_time.cmp(&b.creation_time),
189 Ordering::Greater => Ordering::Less,
191 Ordering::Less => Ordering::Greater,
192 }
193 }
194
195 pub fn remove_expired_cookies_for_url(&mut self, url: &ServoUrl) {
196 let domain = reg_host(url.host_str().unwrap_or(""));
197 if let Entry::Occupied(mut entry) = self.cookies_map.entry(domain) {
198 let cookies = entry.get_mut();
199 cookies.retain(|c| !is_cookie_expired(c));
200 if cookies.is_empty() {
201 entry.remove_entry();
202 }
203 }
204 }
205
206 pub fn remove_all_expired_cookies(&mut self) {
207 self.cookies_map.retain(|_, cookies| {
208 cookies.retain(|c| !is_cookie_expired(c));
209 !cookies.is_empty()
210 });
211 }
212
213 pub fn cookies_for_url(&mut self, url: &ServoUrl, source: CookieSource) -> Option<String> {
215 let cookie_list = self.cookies_data_for_url(url, source);
217
218 let reducer = |acc: String, cookie: Cookie<'static>| -> String {
219 (match acc.len() {
226 0 => acc,
227 _ => acc + "; ",
228 }) + cookie.name() +
229 "=" +
230 cookie.value()
231 };
232
233 let result = cookie_list.fold("".to_owned(), reducer);
235
236 info!(" === COOKIES SENT: {}", result);
237 match result.len() {
238 0 => None,
239 _ => Some(result),
240 }
241 }
242
243 pub fn query_cookies(&mut self, url: &ServoUrl, name: Option<String>) -> Vec<Cookie<'static>> {
245 let cookie_list = self.cookies_data_for_url(url, CookieSource::NonHTTP);
247
248 if let Some(name) = name {
251 cookie_list.filter(|cookie| cookie.name() == name).collect()
254 } else {
255 cookie_list.collect()
256 }
257
258 }
263
264 pub fn cookies_data_for_url<'a>(
265 &'a mut self,
266 url: &'a ServoUrl,
267 source: CookieSource,
268 ) -> impl Iterator<Item = cookie::Cookie<'static>> + 'a {
269 let domain = reg_host(url.host_str().unwrap_or(""));
270 let cookies = self.cookies_map.entry(domain).or_default();
271
272 cookies
273 .iter_mut()
274 .filter(move |c| c.appropriate_for_url(url, source))
275 .sorted_by(|a: &&mut ServoCookie, b: &&mut ServoCookie| {
276 CookieStorage::cookie_comparator(a, b)
278 })
279 .map(|c| {
280 c.touch();
282 c.cookie.clone()
283 })
284 }
285
286 pub fn cookie_site_descriptors(&self) -> Vec<SiteDescriptor> {
287 self.cookies_map
288 .keys()
289 .cloned()
290 .map(SiteDescriptor::new)
291 .collect()
292 }
293}
294
295fn reg_host(url: &str) -> String {
296 let host_for_ip_parse = url
297 .strip_prefix('[')
298 .and_then(|url| url.strip_suffix(']'))
299 .unwrap_or(url);
300 if let Ok(address) = host_for_ip_parse.parse::<IpAddr>() {
301 return address.to_string().to_lowercase();
302 }
303
304 reg_suffix(url).to_lowercase()
305}
306
307fn is_cookie_expired(cookie: &ServoCookie) -> bool {
308 matches!(cookie.expiry_time, Some(date_time) if date_time <= SystemTime::now())
309}
310
311fn evict_one_cookie(is_secure_cookie: bool, cookies: &mut Vec<ServoCookie>) -> bool {
312 let oldest_accessed = get_oldest_accessed(false, cookies);
314
315 if let Some((index, _)) = oldest_accessed {
316 cookies.remove(index);
317 } else {
318 if !is_secure_cookie {
320 return false;
321 }
322 let oldest_accessed = get_oldest_accessed(true, cookies);
323 if let Some((index, _)) = oldest_accessed {
324 cookies.remove(index);
325 }
326 }
327 true
328}
329
330fn get_oldest_accessed(
331 is_secure_cookie: bool,
332 cookies: &mut [ServoCookie],
333) -> Option<(usize, SystemTime)> {
334 let mut oldest_accessed = None;
335 for (i, c) in cookies.iter().enumerate() {
336 if (c.cookie.secure().unwrap_or(false) == is_secure_cookie) &&
337 oldest_accessed
338 .as_ref()
339 .is_none_or(|(_, current_oldest_time)| c.last_access < *current_oldest_time)
340 {
341 oldest_accessed = Some((i, c.last_access));
342 }
343 }
344 oldest_accessed
345}