Skip to content

Commit a72a0b3

Browse files
IdanHotrflynn89
authored andcommitted
LibWeb+LibWebView: Move RFC6265 helpers to LibWeb
This will make them usable by LibWeb's CookieStore code as well.
1 parent 9369224 commit a72a0b3

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

Libraries/LibWeb/Cookie/Cookie.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,45 @@ SameSite same_site_from_string(StringView same_site_mode)
5757
return SameSite::Default;
5858
}
5959

60+
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.2
61+
Optional<String> canonicalize_domain(const URL::URL& url)
62+
{
63+
if (!url.host().has_value())
64+
return {};
65+
66+
// 1. Convert the host name to a sequence of individual domain name labels.
67+
// 2. Convert each label that is not a Non-Reserved LDH (NR-LDH) label, to an A-label (see Section 2.3.2.1 of
68+
// [RFC5890] for the former and latter), or to a "punycode label" (a label resulting from the "ToASCII" conversion
69+
// in Section 4 of [RFC3490]), as appropriate (see Section 6.3 of this specification).
70+
// 3. Concatenate the resulting labels, separated by a %x2E (".") character.
71+
// FIXME: Implement the above conversions.
72+
73+
return MUST(url.serialized_host().to_lowercase());
74+
}
75+
76+
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.4
77+
bool path_matches(StringView request_path, StringView cookie_path)
78+
{
79+
// A request-path path-matches a given cookie-path if at least one of the following conditions holds:
80+
81+
// * The cookie-path and the request-path are identical.
82+
if (request_path == cookie_path)
83+
return true;
84+
85+
if (request_path.starts_with(cookie_path)) {
86+
// * The cookie-path is a prefix of the request-path, and the last character of the cookie-path is %x2F ("/").
87+
if (cookie_path.ends_with('/'))
88+
return true;
89+
90+
// * The cookie-path is a prefix of the request-path, and the first character of the request-path that is not
91+
// included in the cookie-path is a %x2F ("/") character.
92+
if (request_path[cookie_path.length()] == '/')
93+
return true;
94+
}
95+
96+
return false;
97+
}
98+
6099
}
61100

62101
template<>

Libraries/LibWeb/Cookie/Cookie.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <AK/String.h>
1010
#include <AK/Time.h>
1111
#include <LibIPC/Forward.h>
12+
#include <LibURL/URL.h>
1213

1314
namespace Web::Cookie {
1415

@@ -46,6 +47,9 @@ struct Cookie {
4647
StringView same_site_to_string(SameSite same_site_mode);
4748
SameSite same_site_from_string(StringView same_site_mode);
4849

50+
Optional<String> canonicalize_domain(const URL::URL& url);
51+
bool path_matches(StringView request_path, StringView cookie_path);
52+
4953
}
5054

5155
namespace IPC {

Libraries/LibWebView/CookieJar.cpp

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ String CookieJar::get_cookie(const URL::URL& url, Web::Cookie::Source source)
9090
{
9191
m_transient_storage.purge_expired_cookies();
9292

93-
auto domain = canonicalize_domain(url);
93+
auto domain = Web::Cookie::canonicalize_domain(url);
9494
if (!domain.has_value())
9595
return {};
9696

@@ -119,7 +119,7 @@ String CookieJar::get_cookie(const URL::URL& url, Web::Cookie::Source source)
119119

120120
void CookieJar::set_cookie(const URL::URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source)
121121
{
122-
auto domain = canonicalize_domain(url);
122+
auto domain = Web::Cookie::canonicalize_domain(url);
123123
if (!domain.has_value())
124124
return;
125125

@@ -194,7 +194,7 @@ Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies()
194194
// https://w3c.github.io/webdriver/#dfn-associated-cookies
195195
Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies_webdriver(URL::URL const& url)
196196
{
197-
auto domain = canonicalize_domain(url);
197+
auto domain = Web::Cookie::canonicalize_domain(url);
198198
if (!domain.has_value())
199199
return {};
200200

@@ -203,7 +203,7 @@ Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies_webdriver(URL::URL const&
203203

204204
Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies_cookiestore(URL::URL const& url)
205205
{
206-
auto domain = canonicalize_domain(url);
206+
auto domain = Web::Cookie::canonicalize_domain(url);
207207
if (!domain.has_value())
208208
return {};
209209

@@ -212,7 +212,7 @@ Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies_cookiestore(URL::URL cons
212212

213213
Optional<Web::Cookie::Cookie> CookieJar::get_named_cookie(URL::URL const& url, StringView name)
214214
{
215-
auto domain = canonicalize_domain(url);
215+
auto domain = Web::Cookie::canonicalize_domain(url);
216216
if (!domain.has_value())
217217
return {};
218218

@@ -231,45 +231,6 @@ void CookieJar::expire_cookies_with_time_offset(AK::Duration offset)
231231
m_transient_storage.purge_expired_cookies(offset);
232232
}
233233

234-
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.2
235-
Optional<String> CookieJar::canonicalize_domain(const URL::URL& url)
236-
{
237-
if (!url.host().has_value())
238-
return {};
239-
240-
// 1. Convert the host name to a sequence of individual domain name labels.
241-
// 2. Convert each label that is not a Non-Reserved LDH (NR-LDH) label, to an A-label (see Section 2.3.2.1 of
242-
// [RFC5890] for the former and latter), or to a "punycode label" (a label resulting from the "ToASCII" conversion
243-
// in Section 4 of [RFC3490]), as appropriate (see Section 6.3 of this specification).
244-
// 3. Concatenate the resulting labels, separated by a %x2E (".") character.
245-
// FIXME: Implement the above conversions.
246-
247-
return MUST(url.serialized_host().to_lowercase());
248-
}
249-
250-
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.4
251-
bool CookieJar::path_matches(StringView request_path, StringView cookie_path)
252-
{
253-
// A request-path path-matches a given cookie-path if at least one of the following conditions holds:
254-
255-
// * The cookie-path and the request-path are identical.
256-
if (request_path == cookie_path)
257-
return true;
258-
259-
if (request_path.starts_with(cookie_path)) {
260-
// * The cookie-path is a prefix of the request-path, and the last character of the cookie-path is %x2F ("/").
261-
if (cookie_path.ends_with('/'))
262-
return true;
263-
264-
// * The cookie-path is a prefix of the request-path, and the first character of the request-path that is not
265-
// included in the cookie-path is a %x2F ("/") character.
266-
if (request_path[cookie_path.length()] == '/')
267-
return true;
268-
}
269-
270-
return false;
271-
}
272-
273234
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#name-storage-model
274235
void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL::URL& url, String canonicalized_domain, Web::Cookie::Source source)
275236
{
@@ -433,7 +394,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
433394
return IterationDecision::Continue;
434395

435396
// 4. The path of the newly-created cookie path-matches the path of the existing cookie.
436-
if (!path_matches(cookie.path, old_cookie.path))
397+
if (!Web::Cookie::path_matches(cookie.path, old_cookie.path))
437398
return IterationDecision::Continue;
438399

439400
ignore_cookie = true;
@@ -561,7 +522,7 @@ Vector<Web::Cookie::Cookie> CookieJar::get_matching_cookies(const URL::URL& url,
561522
return;
562523

563524
// * The retrieval's URI's path path-matches the cookie's path.
564-
if (!path_matches(url.serialize_path(), cookie.path))
525+
if (!Web::Cookie::path_matches(url.serialize_path(), cookie.path))
565526
return;
566527

567528
// * If the cookie's secure-only-flag is true, then the retrieval's URI must denote a "secure" connection (as

Libraries/LibWebView/CookieJar.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ class CookieJar {
104104
AK_MAKE_NONCOPYABLE(CookieJar);
105105
AK_MAKE_NONMOVABLE(CookieJar);
106106

107-
static Optional<String> canonicalize_domain(const URL::URL& url);
108-
static bool path_matches(StringView request_path, StringView cookie_path);
109-
110107
enum class MatchingCookiesSpecMode {
111108
RFC6265,
112109
WebDriver,

0 commit comments

Comments
 (0)