2020-04-07 20:56:13 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-02-14 21:52:12 +00:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2020-04-07 20:56:13 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-07 20:56:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2024-10-05 03:14:27 +00:00
|
|
|
#include <LibURL/Host.h>
|
2020-04-07 20:56:13 +00:00
|
|
|
|
2024-10-05 02:33:34 +00:00
|
|
|
namespace URL {
|
2020-04-07 20:56:13 +00:00
|
|
|
|
|
|
|
class Origin {
|
|
|
|
public:
|
2022-03-14 19:21:51 +00:00
|
|
|
Origin() = default;
|
2024-10-05 05:08:18 +00:00
|
|
|
Origin(Optional<ByteString> const& scheme, Host const& host, Optional<u16> port)
|
2022-09-28 23:37:44 +00:00
|
|
|
: m_scheme(scheme)
|
2020-04-07 20:56:13 +00:00
|
|
|
, m_host(host)
|
|
|
|
, m_port(port)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:52:12 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque
|
2024-10-05 05:08:18 +00:00
|
|
|
bool is_opaque() const { return !m_scheme.has_value() && m_host.has<Empty>() && !m_port.has_value(); }
|
2020-04-07 20:56:13 +00:00
|
|
|
|
2023-10-10 11:30:58 +00:00
|
|
|
StringView scheme() const
|
|
|
|
{
|
|
|
|
return m_scheme.map([](auto& str) { return str.view(); }).value_or(StringView {});
|
|
|
|
}
|
2024-10-05 02:33:34 +00:00
|
|
|
Host const& host() const { return m_host; }
|
2024-10-05 05:08:18 +00:00
|
|
|
Optional<u16> port() const { return m_port; }
|
2020-04-07 20:56:13 +00:00
|
|
|
|
2022-02-14 21:54:20 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/origin.html#same-origin
|
|
|
|
bool is_same_origin(Origin const& other) const
|
2020-09-22 16:25:48 +00:00
|
|
|
{
|
2022-02-14 21:54:20 +00:00
|
|
|
// 1. If A and B are the same opaque origin, then return true.
|
|
|
|
if (is_opaque() && other.is_opaque())
|
|
|
|
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.
|
2022-09-28 23:37:44 +00:00
|
|
|
return scheme() == other.scheme()
|
2020-09-22 16:25:48 +00:00
|
|
|
&& host() == other.host()
|
|
|
|
&& port() == other.port();
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:59:47 +00:00
|
|
|
// 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 true;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// FIXME: Check domains once supported.
|
2022-09-28 23:37:44 +00:00
|
|
|
if (scheme() == other.scheme())
|
2022-02-14 21:59:47 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// 2. Otherwise, if A and B are same origin and their domains are identical and null, then return true.
|
|
|
|
// FIXME: Check domains once supported.
|
|
|
|
if (is_same_origin(other))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Return false.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:50:11 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
|
2024-11-23 07:10:34 +00:00
|
|
|
String serialize() const;
|
2022-02-28 13:50:11 +00:00
|
|
|
|
2022-09-14 14:50:47 +00:00
|
|
|
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain
|
2024-10-05 02:33:34 +00:00
|
|
|
Optional<Host> effective_domain() const
|
2022-09-14 14:50:47 +00:00
|
|
|
{
|
|
|
|
// 1. If origin is an opaque origin, then return null.
|
|
|
|
if (is_opaque())
|
2023-07-27 09:40:41 +00:00
|
|
|
return {};
|
2022-09-14 14:50:47 +00:00
|
|
|
|
|
|
|
// FIXME: 2. If origin's domain is non-null, then return origin's domain.
|
|
|
|
|
|
|
|
// 3. Return origin's host.
|
|
|
|
return m_host;
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:54:20 +00:00
|
|
|
bool operator==(Origin const& other) const { return is_same_origin(other); }
|
2022-02-08 18:39:47 +00:00
|
|
|
|
2020-04-07 20:56:13 +00:00
|
|
|
private:
|
2023-12-16 14:19:34 +00:00
|
|
|
Optional<ByteString> m_scheme;
|
2024-10-05 02:33:34 +00:00
|
|
|
Host m_host;
|
2024-10-05 05:08:18 +00:00
|
|
|
Optional<u16> m_port;
|
2020-04-07 20:56:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2022-02-08 18:39:47 +00:00
|
|
|
|
|
|
|
namespace AK {
|
2024-10-05 03:33:30 +00:00
|
|
|
|
2022-02-08 18:39:47 +00:00
|
|
|
template<>
|
2024-10-05 02:33:34 +00:00
|
|
|
struct Traits<URL::Origin> : public DefaultTraits<URL::Origin> {
|
2024-10-05 03:33:30 +00:00
|
|
|
static unsigned hash(URL::Origin const& origin);
|
2022-02-08 18:39:47 +00:00
|
|
|
};
|
2024-10-05 03:33:30 +00:00
|
|
|
|
2022-02-08 18:39:47 +00:00
|
|
|
} // namespace AK
|