mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
dependencies: Upgrade cookie
and rename Servo's Cookie
to ServoCookie
(#32861)
This changes updates to the new version of the `cookie` crate in Servo which no longer uses the old `time@0.1` data types. This requires using a new version of `time` while we transition off of the old one. This is the first step in that process. In addition, the overloading of the `cookie::Cookie` name was causing a great deal of confusion, so I've renamed the Servo wrapper to `ServoCookie` like we do with `ServoUrl`. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
8f377a0cb1
commit
b6f1e3b22d
16 changed files with 203 additions and 199 deletions
|
@ -14,14 +14,15 @@ use net_traits::pub_domains::reg_suffix;
|
|||
use net_traits::CookieSource;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use servo_url::ServoUrl;
|
||||
use time::{self, Tm};
|
||||
use time::Tm;
|
||||
use time_03::OffsetDateTime;
|
||||
|
||||
use crate::cookie::Cookie;
|
||||
use crate::cookie::ServoCookie;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct CookieStorage {
|
||||
version: u32,
|
||||
cookies_map: HashMap<String, Vec<Cookie>>,
|
||||
cookies_map: HashMap<String, Vec<ServoCookie>>,
|
||||
max_per_host: usize,
|
||||
}
|
||||
|
||||
|
@ -43,10 +44,10 @@ impl CookieStorage {
|
|||
// http://tools.ietf.org/html/rfc6265#section-5.3
|
||||
pub fn remove(
|
||||
&mut self,
|
||||
cookie: &Cookie,
|
||||
cookie: &ServoCookie,
|
||||
url: &ServoUrl,
|
||||
source: CookieSource,
|
||||
) -> Result<Option<Cookie>, RemoveCookieError> {
|
||||
) -> Result<Option<ServoCookie>, RemoveCookieError> {
|
||||
let domain = reg_host(cookie.cookie.domain().as_ref().unwrap_or(&""));
|
||||
let cookies = self.cookies_map.entry(domain).or_default();
|
||||
|
||||
|
@ -61,9 +62,9 @@ impl CookieStorage {
|
|||
|
||||
c.cookie.name() == cookie.cookie.name() &&
|
||||
c.cookie.secure().unwrap_or(false) &&
|
||||
(Cookie::domain_match(new_domain, existing_domain) ||
|
||||
Cookie::domain_match(existing_domain, new_domain)) &&
|
||||
Cookie::path_match(new_path, existing_path)
|
||||
(ServoCookie::domain_match(new_domain, existing_domain) ||
|
||||
ServoCookie::domain_match(existing_domain, new_domain)) &&
|
||||
ServoCookie::path_match(new_path, existing_path)
|
||||
});
|
||||
|
||||
if any_overlapping {
|
||||
|
@ -98,12 +99,12 @@ impl CookieStorage {
|
|||
let domain = reg_host(url.host_str().unwrap_or(""));
|
||||
let cookies = self.cookies_map.entry(domain).or_default();
|
||||
for cookie in cookies.iter_mut() {
|
||||
cookie.set_expiry_time_negative();
|
||||
cookie.set_expiry_time_in_past();
|
||||
}
|
||||
}
|
||||
|
||||
// http://tools.ietf.org/html/rfc6265#section-5.3
|
||||
pub fn push(&mut self, mut cookie: Cookie, url: &ServoUrl, source: CookieSource) {
|
||||
pub fn push(&mut self, mut cookie: ServoCookie, url: &ServoUrl, source: CookieSource) {
|
||||
// https://www.ietf.org/id/draft-ietf-httpbis-cookie-alone-01.txt Step 1
|
||||
if cookie.cookie.secure().unwrap_or(false) && !url.is_secure_scheme() {
|
||||
return;
|
||||
|
@ -140,7 +141,7 @@ impl CookieStorage {
|
|||
cookies.push(cookie);
|
||||
}
|
||||
|
||||
pub fn cookie_comparator(a: &Cookie, b: &Cookie) -> Ordering {
|
||||
pub fn cookie_comparator(a: &ServoCookie, b: &ServoCookie) -> Ordering {
|
||||
let a_path_len = a.cookie.path().as_ref().map_or(0, |p| p.len());
|
||||
let b_path_len = b.cookie.path().as_ref().map_or(0, |p| p.len());
|
||||
match a_path_len.cmp(&b_path_len) {
|
||||
|
@ -168,7 +169,7 @@ impl CookieStorage {
|
|||
|
||||
// http://tools.ietf.org/html/rfc6265#section-5.4
|
||||
pub fn cookies_for_url(&mut self, url: &ServoUrl, source: CookieSource) -> Option<String> {
|
||||
let filterer = |c: &&mut Cookie| -> bool {
|
||||
let filterer = |c: &&mut ServoCookie| -> bool {
|
||||
debug!(
|
||||
" === SENT COOKIE : {} {} {:?} {:?}",
|
||||
c.cookie.name(),
|
||||
|
@ -187,10 +188,10 @@ impl CookieStorage {
|
|||
let domain = reg_host(url.host_str().unwrap_or(""));
|
||||
let cookies = self.cookies_map.entry(domain).or_default();
|
||||
|
||||
let mut url_cookies: Vec<&mut Cookie> = cookies.iter_mut().filter(filterer).collect();
|
||||
let mut url_cookies: Vec<&mut ServoCookie> = cookies.iter_mut().filter(filterer).collect();
|
||||
url_cookies.sort_by(|a, b| CookieStorage::cookie_comparator(a, b));
|
||||
|
||||
let reducer = |acc: String, c: &mut &mut Cookie| -> String {
|
||||
let reducer = |acc: String, c: &mut &mut ServoCookie| -> String {
|
||||
// Step 3
|
||||
c.touch();
|
||||
|
||||
|
@ -215,7 +216,7 @@ impl CookieStorage {
|
|||
&'a mut self,
|
||||
url: &'a ServoUrl,
|
||||
source: CookieSource,
|
||||
) -> impl Iterator<Item = cookie_rs::Cookie<'static>> + 'a {
|
||||
) -> impl Iterator<Item = cookie::Cookie<'static>> + 'a {
|
||||
let domain = reg_host(url.host_str().unwrap_or(""));
|
||||
let cookies = self.cookies_map.entry(domain).or_default();
|
||||
|
||||
|
@ -233,14 +234,11 @@ fn reg_host(url: &str) -> String {
|
|||
reg_suffix(url).to_lowercase()
|
||||
}
|
||||
|
||||
fn is_cookie_expired(cookie: &Cookie) -> bool {
|
||||
match cookie.expiry_time {
|
||||
Some(ref t) => t.to_timespec() <= time::get_time(),
|
||||
None => false,
|
||||
}
|
||||
fn is_cookie_expired(cookie: &ServoCookie) -> bool {
|
||||
matches!(cookie.expiry_time, Some(date_time) if date_time <= OffsetDateTime::now_utc())
|
||||
}
|
||||
|
||||
fn evict_one_cookie(is_secure_cookie: bool, cookies: &mut Vec<Cookie>) -> bool {
|
||||
fn evict_one_cookie(is_secure_cookie: bool, cookies: &mut Vec<ServoCookie>) -> bool {
|
||||
// Remove non-secure cookie with oldest access time
|
||||
let oldest_accessed: Option<(usize, Tm)> = get_oldest_accessed(false, cookies);
|
||||
|
||||
|
@ -259,7 +257,7 @@ fn evict_one_cookie(is_secure_cookie: bool, cookies: &mut Vec<Cookie>) -> bool {
|
|||
true
|
||||
}
|
||||
|
||||
fn get_oldest_accessed(is_secure_cookie: bool, cookies: &mut [Cookie]) -> Option<(usize, Tm)> {
|
||||
fn get_oldest_accessed(is_secure_cookie: bool, cookies: &mut [ServoCookie]) -> Option<(usize, Tm)> {
|
||||
let mut oldest_accessed: Option<(usize, Tm)> = None;
|
||||
for (i, c) in cookies.iter().enumerate() {
|
||||
if (c.cookie.secure().unwrap_or(false) == is_secure_cookie) &&
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue