1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:17:36 +00:00

LibWeb: Port Cookie from DeprecatedString to String

This commit is contained in:
Shannon Booth 2023-11-20 21:09:58 +13:00 committed by Andreas Kling
parent e3bc8610a9
commit e28fb5c64c
4 changed files with 51 additions and 51 deletions

View file

@ -12,24 +12,24 @@
namespace Web::Cookie { namespace Web::Cookie {
static DeprecatedString time_to_string(UnixDateTime const& time) static String time_to_string(UnixDateTime const& time)
{ {
// FIXME: This roundabout formatting should not be necessary; it also loses precision. // FIXME: This roundabout formatting should not be necessary; it also loses precision.
auto local_time = Core::DateTime::from_timestamp(time.seconds_since_epoch()); auto local_time = Core::DateTime::from_timestamp(time.seconds_since_epoch());
return local_time.to_deprecated_string("%Y-%m-%d %H:%M:%S %Z"sv); return MUST(local_time.to_string("%Y-%m-%d %H:%M:%S %Z"sv));
} }
DeprecatedString Cookie::creation_time_to_string() const String Cookie::creation_time_to_string() const
{ {
return time_to_string(creation_time); return time_to_string(creation_time);
} }
DeprecatedString Cookie::last_access_time_to_string() const String Cookie::last_access_time_to_string() const
{ {
return time_to_string(last_access_time); return time_to_string(last_access_time);
} }
DeprecatedString Cookie::expiry_time_to_string() const String Cookie::expiry_time_to_string() const
{ {
return time_to_string(expiry_time); return time_to_string(expiry_time);
} }
@ -84,10 +84,10 @@ ErrorOr<void> IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie)
template<> template<>
ErrorOr<Web::Cookie::Cookie> IPC::decode(Decoder& decoder) ErrorOr<Web::Cookie::Cookie> IPC::decode(Decoder& decoder)
{ {
auto name = TRY(decoder.decode<DeprecatedString>()); auto name = TRY(decoder.decode<String>());
auto value = TRY(decoder.decode<DeprecatedString>()); auto value = TRY(decoder.decode<String>());
auto domain = TRY(decoder.decode<DeprecatedString>()); auto domain = TRY(decoder.decode<String>());
auto path = TRY(decoder.decode<DeprecatedString>()); auto path = TRY(decoder.decode<String>());
auto creation_time = TRY(decoder.decode<UnixDateTime>()); auto creation_time = TRY(decoder.decode<UnixDateTime>());
auto expiry_time = TRY(decoder.decode<UnixDateTime>()); auto expiry_time = TRY(decoder.decode<UnixDateTime>());
auto host_only = TRY(decoder.decode<bool>()); auto host_only = TRY(decoder.decode<bool>());

View file

@ -6,7 +6,7 @@
#pragma once #pragma once
#include <AK/DeprecatedString.h> #include <AK/String.h>
#include <AK/Time.h> #include <AK/Time.h>
#include <LibIPC/Forward.h> #include <LibIPC/Forward.h>
@ -25,18 +25,18 @@ enum class Source {
}; };
struct Cookie { struct Cookie {
DeprecatedString creation_time_to_string() const; String creation_time_to_string() const;
DeprecatedString last_access_time_to_string() const; String last_access_time_to_string() const;
DeprecatedString expiry_time_to_string() const; String expiry_time_to_string() const;
DeprecatedString name; String name;
DeprecatedString value; String value;
SameSite same_site; SameSite same_site;
UnixDateTime creation_time {}; UnixDateTime creation_time {};
UnixDateTime last_access_time {}; UnixDateTime last_access_time {};
UnixDateTime expiry_time {}; UnixDateTime expiry_time {};
DeprecatedString domain {}; String domain {};
DeprecatedString path {}; String path {};
bool secure { false }; bool secure { false };
bool http_only { false }; bool http_only { false };
bool host_only { false }; bool host_only { false };

View file

@ -196,7 +196,7 @@ Optional<Web::Cookie::Cookie> CookieJar::get_named_cookie(URL const& url, Deprec
auto cookie_list = get_matching_cookies(url, domain.value(), Web::Cookie::Source::Http, MatchingCookiesSpecMode::WebDriver); auto cookie_list = get_matching_cookies(url, domain.value(), Web::Cookie::Source::Http, MatchingCookiesSpecMode::WebDriver);
for (auto const& cookie : cookie_list) { for (auto const& cookie : cookie_list) {
if (cookie.name == name) if (cookie.name == name.view())
return cookie; return cookie;
} }
@ -290,7 +290,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
// https://tools.ietf.org/html/rfc6265#section-5.3 // https://tools.ietf.org/html/rfc6265#section-5.3
// 2. Create a new cookie with name cookie-name, value cookie-value. Set the creation-time and the last-access-time to the current date and time. // 2. Create a new cookie with name cookie-name, value cookie-value. Set the creation-time and the last-access-time to the current date and time.
Web::Cookie::Cookie cookie { parsed_cookie.name, parsed_cookie.value, parsed_cookie.same_site_attribute }; Web::Cookie::Cookie cookie { MUST(String::from_deprecated_string(parsed_cookie.name)), MUST(String::from_deprecated_string(parsed_cookie.value)), parsed_cookie.same_site_attribute };
cookie.creation_time = UnixDateTime::now(); cookie.creation_time = UnixDateTime::now();
cookie.last_access_time = cookie.creation_time; cookie.last_access_time = cookie.creation_time;
@ -313,15 +313,15 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
// 4. If the cookie-attribute-list contains an attribute with an attribute-name of "Domain": // 4. If the cookie-attribute-list contains an attribute with an attribute-name of "Domain":
if (parsed_cookie.domain.has_value()) { if (parsed_cookie.domain.has_value()) {
// Let the domain-attribute be the attribute-value of the last attribute in the cookie-attribute-list with an attribute-name of "Domain". // Let the domain-attribute be the attribute-value of the last attribute in the cookie-attribute-list with an attribute-name of "Domain".
cookie.domain = parsed_cookie.domain.value(); cookie.domain = MUST(String::from_deprecated_string(parsed_cookie.domain.value()));
} }
// 5. If the user agent is configured to reject "public suffixes" and the domain-attribute is a public suffix: // 5. If the user agent is configured to reject "public suffixes" and the domain-attribute is a public suffix:
if (is_public_suffix(cookie.domain)) { if (is_public_suffix(cookie.domain)) {
// If the domain-attribute is identical to the canonicalized request-host: // If the domain-attribute is identical to the canonicalized request-host:
if (cookie.domain == canonicalized_domain) { if (cookie.domain == canonicalized_domain.view()) {
// Let the domain-attribute be the empty string. // Let the domain-attribute be the empty string.
cookie.domain = DeprecatedString::empty(); cookie.domain = String {};
} }
// Otherwise: // Otherwise:
else { else {
@ -333,7 +333,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
// 6. If the domain-attribute is non-empty: // 6. If the domain-attribute is non-empty:
if (!cookie.domain.is_empty()) { if (!cookie.domain.is_empty()) {
// If the canonicalized request-host does not domain-match the domain-attribute: Ignore the cookie entirely and abort these steps. // If the canonicalized request-host does not domain-match the domain-attribute: Ignore the cookie entirely and abort these steps.
if (!domain_matches(canonicalized_domain, cookie.domain)) if (!domain_matches(canonicalized_domain, cookie.domain.to_deprecated_string()))
return; return;
// Set the cookie's host-only-flag to false. Set the cookie's domain to the domain-attribute. // Set the cookie's host-only-flag to false. Set the cookie's domain to the domain-attribute.
@ -341,15 +341,15 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
} else { } else {
// Set the cookie's host-only-flag to true. Set the cookie's domain to the canonicalized request-host. // Set the cookie's host-only-flag to true. Set the cookie's domain to the canonicalized request-host.
cookie.host_only = true; cookie.host_only = true;
cookie.domain = move(canonicalized_domain); cookie.domain = MUST(String::from_deprecated_string(canonicalized_domain));
} }
// 7. If the cookie-attribute-list contains an attribute with an attribute-name of "Path": // 7. If the cookie-attribute-list contains an attribute with an attribute-name of "Path":
if (parsed_cookie.path.has_value()) { if (parsed_cookie.path.has_value()) {
// Set the cookie's path to attribute-value of the last attribute in the cookie-attribute-list with an attribute-name of "Path". // Set the cookie's path to attribute-value of the last attribute in the cookie-attribute-list with an attribute-name of "Path".
cookie.path = parsed_cookie.path.value(); cookie.path = MUST(String::from_deprecated_string(parsed_cookie.path.value()));
} else { } else {
cookie.path = default_path(url); cookie.path = MUST(String::from_deprecated_string(default_path(url)));
} }
// 8. If the cookie-attribute-list contains an attribute with an attribute-name of "Secure", set the cookie's secure-only-flag to true. // 8. If the cookie-attribute-list contains an attribute with an attribute-name of "Secure", set the cookie's secure-only-flag to true.
@ -403,13 +403,13 @@ Vector<Web::Cookie::Cookie> CookieJar::get_matching_cookies(const URL& url, Depr
select_all_cookies_from_database([&](auto cookie) { select_all_cookies_from_database([&](auto cookie) {
// Either: The cookie's host-only-flag is true and the canonicalized request-host is identical to the cookie's domain. // Either: The cookie's host-only-flag is true and the canonicalized request-host is identical to the cookie's domain.
// Or: The cookie's host-only-flag is false and the canonicalized request-host domain-matches the cookie's domain. // Or: The cookie's host-only-flag is false and the canonicalized request-host domain-matches the cookie's domain.
bool is_host_only_and_has_identical_domain = cookie.host_only && (canonicalized_domain == cookie.domain); bool is_host_only_and_has_identical_domain = cookie.host_only && (canonicalized_domain.view() == cookie.domain);
bool is_not_host_only_and_domain_matches = !cookie.host_only && domain_matches(canonicalized_domain, cookie.domain); bool is_not_host_only_and_domain_matches = !cookie.host_only && domain_matches(canonicalized_domain, cookie.domain.to_deprecated_string());
if (!is_host_only_and_has_identical_domain && !is_not_host_only_and_domain_matches) if (!is_host_only_and_has_identical_domain && !is_not_host_only_and_domain_matches)
return; return;
// The request-uri's path path-matches the cookie's path. // The request-uri's path path-matches the cookie's path.
if (!path_matches(url.serialize_path(), cookie.path)) if (!path_matches(url.serialize_path(), cookie.path.to_deprecated_string()))
return; return;
// If the cookie's secure-only-flag is true, then the request-uri's scheme must denote a "secure" protocol. // If the cookie's secure-only-flag is true, then the request-uri's scheme must denote a "secure" protocol.
@ -429,13 +429,13 @@ Vector<Web::Cookie::Cookie> CookieJar::get_matching_cookies(const URL& url, Depr
// 2. The user agent SHOULD sort the cookie-list in the following order: // 2. The user agent SHOULD sort the cookie-list in the following order:
// - Cookies with longer paths are listed before cookies with shorter paths. // - Cookies with longer paths are listed before cookies with shorter paths.
// - Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times. // - Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times.
auto cookie_path_length = cookie.path.length(); auto cookie_path_length = cookie.path.bytes().size();
auto cookie_creation_time = cookie.creation_time; auto cookie_creation_time = cookie.creation_time;
cookie_list.insert_before_matching(move(cookie), [cookie_path_length, cookie_creation_time](auto const& entry) { cookie_list.insert_before_matching(move(cookie), [cookie_path_length, cookie_creation_time](auto const& entry) {
if (cookie_path_length > entry.path.length()) { if (cookie_path_length > entry.path.bytes().size()) {
return true; return true;
} else if (cookie_path_length == entry.path.length()) { } else if (cookie_path_length == entry.path.bytes().size()) {
if (cookie_creation_time < entry.creation_time) if (cookie_creation_time < entry.creation_time)
return true; return true;
} }
@ -466,7 +466,7 @@ static ErrorOr<Web::Cookie::Cookie> parse_cookie(ReadonlySpan<SQL::Value> row)
if (value.type() != SQL::SQLType::Text) if (value.type() != SQL::SQLType::Text)
return Error::from_string_view(name); return Error::from_string_view(name);
field = value.to_deprecated_string(); field = MUST(String::from_deprecated_string(value.to_deprecated_string()));
return {}; return {};
}; };
@ -525,21 +525,21 @@ void CookieJar::insert_cookie_into_database(Web::Cookie::Cookie const& cookie)
[&](PersistedStorage& storage) { [&](PersistedStorage& storage) {
storage.database.execute_statement( storage.database.execute_statement(
storage.statements.insert_cookie, {}, [this]() { purge_expired_cookies(); }, {}, storage.statements.insert_cookie, {}, [this]() { purge_expired_cookies(); }, {},
cookie.name, cookie.name.to_deprecated_string(),
cookie.value, cookie.value.to_deprecated_string(),
to_underlying(cookie.same_site), to_underlying(cookie.same_site),
cookie.creation_time.seconds_since_epoch(), cookie.creation_time.seconds_since_epoch(),
cookie.last_access_time.seconds_since_epoch(), cookie.last_access_time.seconds_since_epoch(),
cookie.expiry_time.seconds_since_epoch(), cookie.expiry_time.seconds_since_epoch(),
cookie.domain, cookie.domain.to_deprecated_string(),
cookie.path, cookie.path.to_deprecated_string(),
cookie.secure, cookie.secure,
cookie.http_only, cookie.http_only,
cookie.host_only, cookie.host_only,
cookie.persistent); cookie.persistent);
}, },
[&](TransientStorage& storage) { [&](TransientStorage& storage) {
CookieStorageKey key { cookie.name, cookie.domain, cookie.path }; CookieStorageKey key { cookie.name.to_deprecated_string(), cookie.domain.to_deprecated_string(), cookie.path.to_deprecated_string() };
storage.set(key, cookie); storage.set(key, cookie);
}); });
} }
@ -550,7 +550,7 @@ void CookieJar::update_cookie_in_database(Web::Cookie::Cookie const& cookie)
[&](PersistedStorage& storage) { [&](PersistedStorage& storage) {
storage.database.execute_statement( storage.database.execute_statement(
storage.statements.update_cookie, {}, [this]() { purge_expired_cookies(); }, {}, storage.statements.update_cookie, {}, [this]() { purge_expired_cookies(); }, {},
cookie.value, cookie.value.to_deprecated_string(),
to_underlying(cookie.same_site), to_underlying(cookie.same_site),
cookie.creation_time.seconds_since_epoch(), cookie.creation_time.seconds_since_epoch(),
cookie.last_access_time.seconds_since_epoch(), cookie.last_access_time.seconds_since_epoch(),
@ -559,12 +559,12 @@ void CookieJar::update_cookie_in_database(Web::Cookie::Cookie const& cookie)
cookie.http_only, cookie.http_only,
cookie.host_only, cookie.host_only,
cookie.persistent, cookie.persistent,
cookie.name, cookie.name.to_deprecated_string(),
cookie.domain, cookie.domain.to_deprecated_string(),
cookie.path); cookie.path.to_deprecated_string());
}, },
[&](TransientStorage& storage) { [&](TransientStorage& storage) {
CookieStorageKey key { cookie.name, cookie.domain, cookie.path }; CookieStorageKey key { cookie.name.to_deprecated_string(), cookie.domain.to_deprecated_string(), cookie.path.to_deprecated_string() };
storage.set(key, cookie); storage.set(key, cookie);
}); });
} }
@ -601,12 +601,12 @@ void CookieJar::select_cookie_from_database(Web::Cookie::Cookie cookie, OnCookie
on_complete_without_results(move(wrapped_cookie->cookie)); on_complete_without_results(move(wrapped_cookie->cookie));
}, },
{}, {},
wrapped_cookie->cookie.name, wrapped_cookie->cookie.name.to_deprecated_string(),
wrapped_cookie->cookie.domain, wrapped_cookie->cookie.domain.to_deprecated_string(),
wrapped_cookie->cookie.path); wrapped_cookie->cookie.path.to_deprecated_string());
}, },
[&](TransientStorage& storage) { [&](TransientStorage& storage) {
CookieStorageKey key { cookie.name, cookie.domain, cookie.path }; CookieStorageKey key { cookie.name.to_deprecated_string(), cookie.domain.to_deprecated_string(), cookie.path.to_deprecated_string() };
if (auto it = storage.find(key); it != storage.end()) if (auto it = storage.find(key); it != storage.end())
on_result(cookie, it->value); on_result(cookie, it->value);

View file

@ -51,10 +51,10 @@ namespace WebContent {
static JsonValue serialize_cookie(Web::Cookie::Cookie const& cookie) static JsonValue serialize_cookie(Web::Cookie::Cookie const& cookie)
{ {
JsonObject serialized_cookie; JsonObject serialized_cookie;
serialized_cookie.set("name"sv, cookie.name); serialized_cookie.set("name"sv, cookie.name.to_deprecated_string());
serialized_cookie.set("value"sv, cookie.value); serialized_cookie.set("value"sv, cookie.value.to_deprecated_string());
serialized_cookie.set("path"sv, cookie.path); serialized_cookie.set("path"sv, cookie.path.to_deprecated_string());
serialized_cookie.set("domain"sv, cookie.domain); serialized_cookie.set("domain"sv, cookie.domain.to_deprecated_string());
serialized_cookie.set("secure"sv, cookie.secure); serialized_cookie.set("secure"sv, cookie.secure);
serialized_cookie.set("httpOnly"sv, cookie.http_only); serialized_cookie.set("httpOnly"sv, cookie.http_only);
serialized_cookie.set("expiry"sv, cookie.expiry_time.seconds_since_epoch()); serialized_cookie.set("expiry"sv, cookie.expiry_time.seconds_since_epoch());