2020-04-07 22:56:13 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-02-14 21:52:12 +00:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2020-04-07 22:56:13 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-07 22:56:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
|
|
|
|
|
class Origin {
|
|
|
|
|
public:
|
2020-09-22 18:25:48 +02:00
|
|
|
Origin() { }
|
2020-04-07 22:56:13 +02:00
|
|
|
Origin(const String& protocol, const String& host, u16 port)
|
|
|
|
|
: m_protocol(protocol)
|
|
|
|
|
, 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
|
|
|
|
|
bool is_opaque() const { return m_protocol.is_null() && m_host.is_null() && m_port == 0; }
|
2020-04-07 22:56:13 +02:00
|
|
|
|
|
|
|
|
const String& protocol() const { return m_protocol; }
|
|
|
|
|
const String& host() const { return m_host; }
|
|
|
|
|
u16 port() const { return m_port; }
|
|
|
|
|
|
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 18:25:48 +02: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.
|
2020-09-22 18:25:48 +02:00
|
|
|
return protocol() == other.protocol()
|
|
|
|
|
&& host() == other.host()
|
|
|
|
|
&& port() == other.port();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 21:54:20 +00:00
|
|
|
bool operator==(Origin const& other) const { return is_same_origin(other); }
|
|
|
|
|
bool operator!=(Origin const& other) const { return !is_same_origin(other); }
|
2022-02-08 19:39:47 +01:00
|
|
|
|
2020-04-07 22:56:13 +02:00
|
|
|
private:
|
|
|
|
|
String m_protocol;
|
|
|
|
|
String m_host;
|
|
|
|
|
u16 m_port { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2022-02-08 19:39:47 +01:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
template<>
|
|
|
|
|
struct Traits<Web::Origin> : public GenericTraits<Web::Origin> {
|
|
|
|
|
static unsigned hash(Web::Origin const& origin)
|
|
|
|
|
{
|
|
|
|
|
return pair_int_hash(origin.protocol().hash(), pair_int_hash(int_hash(origin.port()), origin.host().hash()));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} // namespace AK
|