diff options
Diffstat (limited to 'Userland/Libraries/LibWeb/Cookie')
-rw-r--r-- | Userland/Libraries/LibWeb/Cookie/Cookie.cpp | 29 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Cookie/Cookie.h | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Cookie/ParsedCookie.h | 8 |
4 files changed, 55 insertions, 22 deletions
diff --git a/Userland/Libraries/LibWeb/Cookie/Cookie.cpp b/Userland/Libraries/LibWeb/Cookie/Cookie.cpp index 229ca38090..bbef4fb5c6 100644 --- a/Userland/Libraries/LibWeb/Cookie/Cookie.cpp +++ b/Userland/Libraries/LibWeb/Cookie/Cookie.cpp @@ -1,15 +1,38 @@ /* * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org> + * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #include "Cookie.h" +#include <LibCore/DateTime.h> #include <LibIPC/Decoder.h> #include <LibIPC/Encoder.h> namespace Web::Cookie { +static DeprecatedString time_to_string(Time const& time) +{ + auto local_time = Core::DateTime::from_timestamp(time.to_seconds()); + return local_time.to_deprecated_string("%Y-%m-%d %H:%M:%S %Z"sv); +} + +DeprecatedString Cookie::creation_time_to_string() const +{ + return time_to_string(creation_time); +} + +DeprecatedString Cookie::last_access_time_to_string() const +{ + return time_to_string(last_access_time); +} + +DeprecatedString Cookie::expiry_time_to_string() const +{ + return time_to_string(expiry_time); +} + StringView same_site_to_string(SameSite same_site) { switch (same_site) { @@ -64,11 +87,11 @@ ErrorOr<Web::Cookie::Cookie> IPC::decode(Decoder& decoder) auto value = TRY(decoder.decode<DeprecatedString>()); auto domain = TRY(decoder.decode<DeprecatedString>()); auto path = TRY(decoder.decode<DeprecatedString>()); - auto creation_time = TRY(decoder.decode<Core::DateTime>()); - auto expiry_time = TRY(decoder.decode<Core::DateTime>()); + auto creation_time = TRY(decoder.decode<Time>()); + auto expiry_time = TRY(decoder.decode<Time>()); auto host_only = TRY(decoder.decode<bool>()); auto http_only = TRY(decoder.decode<bool>()); - auto last_access_time = TRY(decoder.decode<Core::DateTime>()); + auto last_access_time = TRY(decoder.decode<Time>()); auto persistent = TRY(decoder.decode<bool>()); auto secure = TRY(decoder.decode<bool>()); auto same_site = TRY(decoder.decode<Web::Cookie::SameSite>()); diff --git a/Userland/Libraries/LibWeb/Cookie/Cookie.h b/Userland/Libraries/LibWeb/Cookie/Cookie.h index c3b4c9768c..e54d168621 100644 --- a/Userland/Libraries/LibWeb/Cookie/Cookie.h +++ b/Userland/Libraries/LibWeb/Cookie/Cookie.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> + * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,7 +7,7 @@ #pragma once #include <AK/DeprecatedString.h> -#include <LibCore/DateTime.h> +#include <AK/Time.h> #include <LibIPC/Forward.h> namespace Web::Cookie { @@ -25,12 +25,16 @@ enum class Source { }; struct Cookie { + DeprecatedString creation_time_to_string() const; + DeprecatedString last_access_time_to_string() const; + DeprecatedString expiry_time_to_string() const; + DeprecatedString name; DeprecatedString value; SameSite same_site; - Core::DateTime creation_time {}; - Core::DateTime last_access_time {}; - Core::DateTime expiry_time {}; + Time creation_time {}; + Time last_access_time {}; + Time expiry_time {}; DeprecatedString domain {}; DeprecatedString path {}; bool secure { false }; diff --git a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp index e04870f1bb..0d6102b114 100644 --- a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp +++ b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> + * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,6 +8,7 @@ #include <AK/DateConstants.h> #include <AK/Function.h> #include <AK/StdLibExtras.h> +#include <AK/Time.h> #include <AK/Vector.h> #include <LibIPC/Decoder.h> #include <LibIPC/Encoder.h> @@ -26,7 +27,7 @@ static void on_path_attribute(ParsedCookie& parsed_cookie, StringView attribute_ static void on_secure_attribute(ParsedCookie& parsed_cookie); static void on_http_only_attribute(ParsedCookie& parsed_cookie); static void on_same_site_attribute(ParsedCookie& parsed_cookie, StringView attribute_value); -static Optional<Core::DateTime> parse_date_time(StringView date_string); +static Optional<Time> parse_date_time(StringView date_string); Optional<ParsedCookie> parse_cookie(DeprecatedString const& cookie_string) { @@ -153,7 +154,7 @@ void on_expires_attribute(ParsedCookie& parsed_cookie, StringView attribute_valu { // https://tools.ietf.org/html/rfc6265#section-5.2.1 if (auto expiry_time = parse_date_time(attribute_value); expiry_time.has_value()) - parsed_cookie.expiry_time_from_expires_attribute = move(*expiry_time); + parsed_cookie.expiry_time_from_expires_attribute = expiry_time.release_value(); } void on_max_age_attribute(ParsedCookie& parsed_cookie, StringView attribute_value) @@ -168,11 +169,10 @@ void on_max_age_attribute(ParsedCookie& parsed_cookie, StringView attribute_valu if (auto delta_seconds = attribute_value.to_int(); delta_seconds.has_value()) { if (*delta_seconds <= 0) { // If delta-seconds is less than or equal to zero (0), let expiry-time be the earliest representable date and time. - parsed_cookie.expiry_time_from_max_age_attribute = Core::DateTime::from_timestamp(0); + parsed_cookie.expiry_time_from_max_age_attribute = Time::min(); } else { // Otherwise, let the expiry-time be the current date and time plus delta-seconds seconds. - time_t now = Core::DateTime::now().timestamp(); - parsed_cookie.expiry_time_from_max_age_attribute = Core::DateTime::from_timestamp(now + *delta_seconds); + parsed_cookie.expiry_time_from_max_age_attribute = Time::now_realtime() + Time::from_seconds(*delta_seconds); } } } @@ -236,7 +236,7 @@ void on_same_site_attribute(ParsedCookie& parsed_cookie, StringView attribute_va parsed_cookie.same_site_attribute = same_site_from_string(attribute_value); } -Optional<Core::DateTime> parse_date_time(StringView date_string) +Optional<Time> parse_date_time(StringView date_string) { // https://tools.ietf.org/html/rfc6265#section-5.1.1 unsigned hour = 0; @@ -341,8 +341,14 @@ Optional<Core::DateTime> parse_date_time(StringView date_string) if (second > 59) return {}; + // 6. Let the parsed-cookie-date be the date whose day-of-month, month, year, hour, minute, and second (in UTC) are the + // day-of-month-value, the month-value, the year-value, the hour-value, the minute-value, and the second-value, respectively. + // If no such date exists, abort these steps and fail to parse the cookie-date. // FIXME: Fail on dates that do not exist. - return Core::DateTime::create(year, month, day_of_month, hour, minute, second); + auto parsed_cookie_date = Time::from_timestamp(year, month, day_of_month, hour, minute, second, 0); + + // 7. Return the parsed-cookie-date as the result of this algorithm. + return parsed_cookie_date; } } @@ -368,8 +374,8 @@ ErrorOr<Web::Cookie::ParsedCookie> IPC::decode(Decoder& decoder) { auto name = TRY(decoder.decode<DeprecatedString>()); auto value = TRY(decoder.decode<DeprecatedString>()); - auto expiry_time_from_expires_attribute = TRY(decoder.decode<Optional<Core::DateTime>>()); - auto expiry_time_from_max_age_attribute = TRY(decoder.decode<Optional<Core::DateTime>>()); + auto expiry_time_from_expires_attribute = TRY(decoder.decode<Optional<Time>>()); + auto expiry_time_from_max_age_attribute = TRY(decoder.decode<Optional<Time>>()); auto domain = TRY(decoder.decode<Optional<DeprecatedString>>()); auto path = TRY(decoder.decode<Optional<DeprecatedString>>()); auto secure_attribute_present = TRY(decoder.decode<bool>()); diff --git a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h index 07cc36b292..5779f8d5ee 100644 --- a/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h +++ b/Userland/Libraries/LibWeb/Cookie/ParsedCookie.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> + * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,7 +8,7 @@ #include <AK/DeprecatedString.h> #include <AK/Optional.h> -#include <LibCore/DateTime.h> +#include <AK/Time.h> #include <LibIPC/Forward.h> #include <LibWeb/Cookie/Cookie.h> @@ -18,8 +18,8 @@ struct ParsedCookie { DeprecatedString name; DeprecatedString value; SameSite same_site_attribute { SameSite::Default }; - Optional<Core::DateTime> expiry_time_from_expires_attribute {}; - Optional<Core::DateTime> expiry_time_from_max_age_attribute {}; + Optional<Time> expiry_time_from_expires_attribute {}; + Optional<Time> expiry_time_from_max_age_attribute {}; Optional<DeprecatedString> domain {}; Optional<DeprecatedString> path {}; bool secure_attribute_present { false }; |