mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
LibURL: Implement Site concept
This commit is contained in:
parent
2e64e0b836
commit
b83f015c70
Notes:
github-actions[bot]
2024-11-30 11:23:42 +00:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/LadybirdBrowser/ladybird/commit/b83f015c703 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2610 Reviewed-by: https://github.com/shannonbooth ✅
6 changed files with 146 additions and 1 deletions
|
@ -4,6 +4,7 @@ set(SOURCES
|
|||
Host.cpp
|
||||
Origin.cpp
|
||||
Parser.cpp
|
||||
Site.cpp
|
||||
URL.cpp
|
||||
${PUBLIC_SUFFIX_SOURCES}
|
||||
)
|
||||
|
|
|
@ -7,9 +7,11 @@
|
|||
#pragma once
|
||||
|
||||
namespace URL {
|
||||
class Host;
|
||||
class Origin;
|
||||
class URL;
|
||||
class Parser;
|
||||
class Site;
|
||||
class URL;
|
||||
|
||||
struct BlobURLEntry;
|
||||
}
|
||||
|
|
|
@ -6,9 +6,27 @@
|
|||
|
||||
#include <LibURL/Origin.h>
|
||||
#include <LibURL/Parser.h>
|
||||
#include <LibURL/Site.h>
|
||||
|
||||
namespace URL {
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#same-site
|
||||
bool Origin::is_same_site(Origin const& other) const
|
||||
{
|
||||
// 1. Let siteA be the result of obtaining a site given A.
|
||||
auto site_a = Site::obtain(*this);
|
||||
|
||||
// 2. Let siteB be the result of obtaining a site given B.
|
||||
auto site_b = Site::obtain(other);
|
||||
|
||||
// 3. If siteA is same site with siteB, then return true.
|
||||
if (site_a.is_same_site(site_b))
|
||||
return true;
|
||||
|
||||
// 4. Return false.
|
||||
return false;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
|
||||
String Origin::serialize() const
|
||||
{
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/ByteString.h>
|
||||
#include <LibURL/Forward.h>
|
||||
#include <LibURL/Host.h>
|
||||
|
||||
namespace URL {
|
||||
|
@ -74,6 +75,9 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#same-site
|
||||
bool is_same_site(Origin const&) const;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
|
||||
String serialize() const;
|
||||
|
||||
|
|
83
Libraries/LibURL/Site.cpp
Normal file
83
Libraries/LibURL/Site.cpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibURL/Site.h>
|
||||
|
||||
namespace URL {
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#obtain-a-site
|
||||
Site Site::obtain(Origin const& origin)
|
||||
{
|
||||
// 1. If origin is an opaque origin, then return origin.
|
||||
if (origin.is_opaque())
|
||||
return Site { origin };
|
||||
|
||||
// 2. If origin's host's registrable domain is null, then return (origin's scheme, origin's host).
|
||||
auto origin_scheme = origin.scheme().value_or_lazy_evaluated([]() { return String {}; });
|
||||
|
||||
auto registrable_domain = origin.host().registrable_domain();
|
||||
if (!registrable_domain.has_value())
|
||||
return Site { SchemeAndHost { origin_scheme, origin.host() } };
|
||||
|
||||
// 3. Return (origin's scheme, origin's host's registrable domain).
|
||||
return Site { SchemeAndHost { origin_scheme, Host { *registrable_domain } } };
|
||||
}
|
||||
|
||||
Site::Site(Variant<Origin, SchemeAndHost> value)
|
||||
: m_value(move(value))
|
||||
{
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#concept-site-same-site
|
||||
bool Site::is_same_site(Site const& other) const
|
||||
{
|
||||
// 1. If A and B are the same opaque origin, then return true.
|
||||
// NOTE: Origins in sites are always opaque.
|
||||
// FIXME: Currently all opaque origins are identical, how should we distinguish them?
|
||||
if (m_value.has<Origin>() && other.m_value.has<Origin>())
|
||||
return true;
|
||||
|
||||
// 2. If A or B is an opaque origin, then return false.
|
||||
if (m_value.has<Origin>() || other.m_value.has<Origin>())
|
||||
return false;
|
||||
|
||||
// 3. If A's and B's scheme values are different, then return false.
|
||||
auto& a = m_value.get<SchemeAndHost>();
|
||||
auto& b = other.m_value.get<SchemeAndHost>();
|
||||
if (a.scheme != b.scheme)
|
||||
return false;
|
||||
|
||||
// 4. If A's and B's host values are not equal, then return false.
|
||||
if (a.host != b.host)
|
||||
return false;
|
||||
|
||||
// 5. Return true.
|
||||
return true;
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#serialization-of-a-site
|
||||
String Site::serialize() const
|
||||
{
|
||||
// 1. If site is an opaque origin, then return "null".
|
||||
if (m_value.has<Origin>())
|
||||
return "null"_string;
|
||||
|
||||
// 2. Let result be site[0].
|
||||
auto& [scheme, host] = m_value.get<SchemeAndHost>();
|
||||
StringBuilder result;
|
||||
result.append(scheme);
|
||||
|
||||
// 3. Append "://" to result.
|
||||
result.append("://"sv);
|
||||
|
||||
// 4. Append site[1], serialized, to result.
|
||||
result.append(host.serialize());
|
||||
|
||||
// 5. Return result.
|
||||
return result.to_string_without_validation();
|
||||
}
|
||||
|
||||
}
|
37
Libraries/LibURL/Site.h
Normal file
37
Libraries/LibURL/Site.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Variant.h>
|
||||
#include <LibURL/Host.h>
|
||||
#include <LibURL/Origin.h>
|
||||
|
||||
namespace URL {
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#scheme-and-host
|
||||
// A scheme-and-host is a tuple of a scheme (an ASCII string) and a host (a host).
|
||||
struct SchemeAndHost {
|
||||
String scheme;
|
||||
Host host;
|
||||
};
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/browsers.html#site
|
||||
// A site is an opaque origin or a scheme-and-host.
|
||||
class Site {
|
||||
public:
|
||||
static Site obtain(Origin const&);
|
||||
|
||||
bool is_same_site(Site const& other) const;
|
||||
|
||||
String serialize() const;
|
||||
|
||||
private:
|
||||
Site(Variant<Origin, SchemeAndHost>);
|
||||
Variant<Origin, SchemeAndHost> m_value;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue