diff --git a/Libraries/LibURL/Origin.cpp b/Libraries/LibURL/Origin.cpp index 725a485485..095c24bca0 100644 --- a/Libraries/LibURL/Origin.cpp +++ b/Libraries/LibURL/Origin.cpp @@ -16,6 +16,76 @@ Origin Origin::create_opaque(OpaqueData::Type type) return Origin { OpaqueData { get_random(), type } }; } +Origin::Origin(Optional const& scheme, Host const& host, Optional port, Optional domain) + : m_state(Tuple { + .scheme = scheme, + .host = host, + .port = move(port), + .domain = move(domain), + }) +{ +} +// https://html.spec.whatwg.org/multipage/origin.html#same-origin +bool Origin::is_same_origin(Origin const& other) const +{ + // 1. If A and B are the same opaque origin, then return true. + if (is_opaque() && other.is_opaque()) + return opaque_data().nonce == other.opaque_data().nonce; + + // 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true. + if (!is_opaque() && !other.is_opaque() + && scheme() == other.scheme() + && host() == other.host() + && port() == other.port()) { + return true; + } + + // 3. Return false. + return false; +} + +// https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain +bool Origin::is_same_origin_domain(Origin const& other) const +{ + // 1. If A and B are the same opaque origin, then return true. + if (is_opaque() && other.is_opaque()) + return opaque_data().nonce == other.opaque_data().nonce; + + // 2. If A and B are both tuple origins, run these substeps: + if (!is_opaque() && !other.is_opaque()) { + // 1. If A and B's schemes are identical, and their domains are identical and non-null, then return true. + if (domain().has_value() + && domain() == other.domain() + && scheme() == other.scheme()) + return true; + + // 2. Otherwise, if A and B are same origin and their domains are identical and null, then return true. + if (!domain().has_value() + && !other.domain().has_value() + && is_same_origin(other)) + return true; + } + + // 3. Return false. + return false; +} + +// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain +Optional Origin::effective_domain() const +{ + // 1. If origin is an opaque origin, then return null. + if (is_opaque()) + return {}; + + // 2. If origin's domain is non-null, then return origin's domain. + auto const& tuple = m_state.get(); + if (tuple.domain.has_value()) + return Host { tuple.domain.value() }; + + // 3. Return origin's host. + return tuple.host; +} + // https://html.spec.whatwg.org/multipage/browsers.html#same-site bool Origin::is_same_site(Origin const& other) const { diff --git a/Libraries/LibURL/Origin.h b/Libraries/LibURL/Origin.h index b3e0eb1f02..ea611e63f5 100644 --- a/Libraries/LibURL/Origin.h +++ b/Libraries/LibURL/Origin.h @@ -35,15 +35,7 @@ public: static Origin create_opaque(OpaqueData::Type = OpaqueData::Type::Standard); - Origin(Optional const& scheme, Host const& host, Optional port, Optional domain = {}) - : m_state(Tuple { - .scheme = scheme, - .host = host, - .port = move(port), - .domain = move(domain), - }) - { - } + Origin(Optional const& scheme, Host const&, Optional port, Optional domain = {}); // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque bool is_opaque() const { return m_state.has(); } @@ -56,49 +48,10 @@ public: OpaqueData const& opaque_data() const { return m_state.get(); } // https://html.spec.whatwg.org/multipage/origin.html#same-origin - bool is_same_origin(Origin const& other) const - { - // 1. If A and B are the same opaque origin, then return true. - if (is_opaque() && other.is_opaque()) - return opaque_data().nonce == other.opaque_data().nonce; - - // 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true. - if (!is_opaque() && !other.is_opaque() - && scheme() == other.scheme() - && host() == other.host() - && port() == other.port()) { - return true; - } - - // 3. Return false. - return false; - } + bool is_same_origin(Origin const& other) const; // https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain - bool is_same_origin_domain(Origin const& other) const - { - // 1. If A and B are the same opaque origin, then return true. - if (is_opaque() && other.is_opaque()) - return opaque_data().nonce == other.opaque_data().nonce; - - // 2. If A and B are both tuple origins, run these substeps: - if (!is_opaque() && !other.is_opaque()) { - // 1. If A and B's schemes are identical, and their domains are identical and non-null, then return true. - if (domain().has_value() - && domain() == other.domain() - && scheme() == other.scheme()) - return true; - - // 2. Otherwise, if A and B are same origin and their domains are identical and null, then return true. - if (!domain().has_value() - && !other.domain().has_value() - && is_same_origin(other)) - return true; - } - - // 3. Return false. - return false; - } + bool is_same_origin_domain(Origin const& other) const; // https://html.spec.whatwg.org/multipage/browsers.html#same-site bool is_same_site(Origin const&) const; @@ -107,20 +60,7 @@ public: String serialize() const; // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain - Optional effective_domain() const - { - // 1. If origin is an opaque origin, then return null. - if (is_opaque()) - return {}; - - // 2. If origin's domain is non-null, then return origin's domain. - auto const& tuple = m_state.get(); - if (tuple.domain.has_value()) - return Host { tuple.domain.value() }; - - // 3. Return origin's host. - return tuple.host; - } + Optional effective_domain() const; bool operator==(Origin const& other) const { return is_same_origin(other); }