Refactor and document cookie-list retrieval (#38070)

This refactors some of the cookie retrieval mechanism to be less
repetitive and separates the cookie-list from the cookie-string which
will also be needed for Cookie Store.

Testing: No new behavior, should be covered by existing WPT tests.

Signed-off-by: Sebastian C <sebsebmc@gmail.com>
This commit is contained in:
Sebastian C 2025-07-14 17:15:27 -05:00 committed by GitHub
parent 6ea7638c21
commit f13311a00e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 30 deletions

View file

@ -10,6 +10,7 @@ use std::net::{Ipv4Addr, Ipv6Addr};
use std::time::SystemTime;
use cookie::Cookie;
use log::{Level, debug, log_enabled};
use net_traits::CookieSource;
use net_traits::pub_domains::is_pub_domain;
use nom::IResult;
@ -322,7 +323,21 @@ impl ServoCookie {
/// <http://tools.ietf.org/html/rfc6265#section-5.4> step 1
pub fn appropriate_for_url(&self, url: &ServoUrl, source: CookieSource) -> bool {
if log_enabled!(Level::Debug) {
debug!(
" === SENT COOKIE : {} {} {:?} {:?}",
self.cookie.name(),
self.cookie.value(),
self.cookie.domain(),
self.cookie.path()
);
}
let domain = url.host_str();
// Either: The cookie's host-only-flag is true and the canonicalized host of the
// retrieval's URI is identical to the cookie's domain
// Or: The cookie's host-only-flag is false and the canonicalized host of the
// retrieval's URI domain-matches the cookie's domain
if self.host_only {
if self.cookie.domain() != domain {
return false;
@ -333,18 +348,24 @@ impl ServoCookie {
}
}
// The retrieval's URI's path path-matches the cookie's path.
if let Some(cookie_path) = self.cookie.path() {
if !ServoCookie::path_match(url.path(), cookie_path) {
return false;
}
}
// If the cookie's secure-only-flag is true, then the retrieval's URI must denote a "secure" connection
if self.cookie.secure().unwrap_or(false) && !url.is_secure_scheme() {
return false;
}
// If the cookie's http-only-flag is true, then exclude the cookie if the retrieval's type is "non-HTTP"
if self.cookie.http_only().unwrap_or(false) && source == CookieSource::NonHTTP {
return false;
}
// TODO: Apply same site checks
// TOOD: Apply Partitioning checks
true
}