mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-06 08:10:02 +00:00
LibURL: Clarify whether an Origin is opaque
Origins are immutable and we know on construction whether an Origin is opaque. This also removes an implicit reliance on Host's Empty state.
This commit is contained in:
parent
3124dca528
commit
8b984c0c57
Notes:
github-actions[bot]
2024-11-30 11:24:16 +00:00
Author: https://github.com/AtkinsSJ
Commit: 8b984c0c57
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2610
Reviewed-by: https://github.com/shannonbooth ✅
4 changed files with 40 additions and 19 deletions
|
@ -100,6 +100,10 @@ ErrorOr<URL::URL> decode(Decoder& decoder)
|
||||||
template<>
|
template<>
|
||||||
ErrorOr<URL::Origin> decode(Decoder& decoder)
|
ErrorOr<URL::Origin> decode(Decoder& decoder)
|
||||||
{
|
{
|
||||||
|
auto is_opaque = TRY(decoder.decode<bool>());
|
||||||
|
if (is_opaque)
|
||||||
|
return URL::Origin {};
|
||||||
|
|
||||||
auto scheme = TRY(decoder.decode<ByteString>());
|
auto scheme = TRY(decoder.decode<ByteString>());
|
||||||
auto host = TRY(decoder.decode<URL::Host>());
|
auto host = TRY(decoder.decode<URL::Host>());
|
||||||
auto port = TRY(decoder.decode<Optional<u16>>());
|
auto port = TRY(decoder.decode<Optional<u16>>());
|
||||||
|
|
|
@ -119,9 +119,14 @@ ErrorOr<void> encode(Encoder& encoder, URL::URL const& value)
|
||||||
template<>
|
template<>
|
||||||
ErrorOr<void> encode(Encoder& encoder, URL::Origin const& origin)
|
ErrorOr<void> encode(Encoder& encoder, URL::Origin const& origin)
|
||||||
{
|
{
|
||||||
TRY(encoder.encode<ByteString>(origin.scheme()));
|
if (origin.is_opaque()) {
|
||||||
TRY(encoder.encode(origin.host()));
|
TRY(encoder.encode(true));
|
||||||
TRY(encoder.encode(origin.port()));
|
} else {
|
||||||
|
TRY(encoder.encode(false));
|
||||||
|
TRY(encoder.encode<ByteString>(origin.scheme()));
|
||||||
|
TRY(encoder.encode(origin.host()));
|
||||||
|
TRY(encoder.encode(origin.port()));
|
||||||
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,13 +42,15 @@ namespace AK {
|
||||||
|
|
||||||
unsigned Traits<URL::Origin>::hash(URL::Origin const& origin)
|
unsigned Traits<URL::Origin>::hash(URL::Origin const& origin)
|
||||||
{
|
{
|
||||||
|
if (origin.is_opaque())
|
||||||
|
return 0;
|
||||||
|
|
||||||
unsigned hash = origin.scheme().hash();
|
unsigned hash = origin.scheme().hash();
|
||||||
|
|
||||||
if (origin.port().has_value())
|
if (origin.port().has_value())
|
||||||
hash = pair_int_hash(hash, *origin.port());
|
hash = pair_int_hash(hash, *origin.port());
|
||||||
|
|
||||||
if (!origin.host().has<Empty>())
|
hash = pair_int_hash(hash, URL::Parser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash());
|
||||||
hash = pair_int_hash(hash, URL::Parser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash());
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,21 +16,23 @@ class Origin {
|
||||||
public:
|
public:
|
||||||
Origin() = default;
|
Origin() = default;
|
||||||
Origin(Optional<ByteString> const& scheme, Host const& host, Optional<u16> port)
|
Origin(Optional<ByteString> const& scheme, Host const& host, Optional<u16> port)
|
||||||
: m_scheme(scheme)
|
: m_state(State {
|
||||||
, m_host(host)
|
.scheme = scheme,
|
||||||
, m_port(port)
|
.host = host,
|
||||||
|
.port = move(port),
|
||||||
|
})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque
|
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque
|
||||||
bool is_opaque() const { return !m_scheme.has_value() && m_host.has<Empty>() && !m_port.has_value(); }
|
bool is_opaque() const { return !m_state.has_value(); }
|
||||||
|
|
||||||
StringView scheme() const
|
StringView scheme() const
|
||||||
{
|
{
|
||||||
return m_scheme.map([](auto& str) { return str.view(); }).value_or(StringView {});
|
return m_state->scheme.map([](auto& str) { return str.view(); }).value_or(StringView {});
|
||||||
}
|
}
|
||||||
Host const& host() const { return m_host; }
|
Host const& host() const { return m_state->host; }
|
||||||
Optional<u16> port() const { return m_port; }
|
Optional<u16> port() const { return m_state->port; }
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/origin.html#same-origin
|
// https://html.spec.whatwg.org/multipage/origin.html#same-origin
|
||||||
bool is_same_origin(Origin const& other) const
|
bool is_same_origin(Origin const& other) const
|
||||||
|
@ -40,10 +42,15 @@ public:
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true.
|
// 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true.
|
||||||
// 3. Return false.
|
if (!is_opaque() && !other.is_opaque()
|
||||||
return scheme() == other.scheme()
|
&& scheme() == other.scheme()
|
||||||
&& host() == other.host()
|
&& host() == other.host()
|
||||||
&& port() == other.port();
|
&& port() == other.port()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Return false.
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain
|
// https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain
|
||||||
|
@ -83,15 +90,18 @@ public:
|
||||||
// FIXME: 2. If origin's domain is non-null, then return origin's domain.
|
// FIXME: 2. If origin's domain is non-null, then return origin's domain.
|
||||||
|
|
||||||
// 3. Return origin's host.
|
// 3. Return origin's host.
|
||||||
return m_host;
|
return m_state->host;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(Origin const& other) const { return is_same_origin(other); }
|
bool operator==(Origin const& other) const { return is_same_origin(other); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Optional<ByteString> m_scheme;
|
struct State {
|
||||||
Host m_host;
|
Optional<ByteString> scheme;
|
||||||
Optional<u16> m_port;
|
Host host;
|
||||||
|
Optional<u16> port;
|
||||||
|
};
|
||||||
|
Optional<State> m_state;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue