/src/serenity/Userland/Libraries/LibURL/Origin.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/ByteString.h> |
11 | | #include <LibURL/Host.h> |
12 | | |
13 | | namespace URL { |
14 | | |
15 | | class Origin { |
16 | | public: |
17 | 0 | Origin() = default; |
18 | | Origin(Optional<ByteString> const& scheme, Host const& host, Optional<u16> port) |
19 | 0 | : m_scheme(scheme) |
20 | 0 | , m_host(host) |
21 | 0 | , m_port(port) |
22 | 0 | { |
23 | 0 | } |
24 | | |
25 | | // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque |
26 | 0 | bool is_opaque() const { return !m_scheme.has_value() && m_host.has<Empty>() && !m_port.has_value(); } |
27 | | |
28 | | StringView scheme() const |
29 | 0 | { |
30 | 0 | return m_scheme.map([](auto& str) { return str.view(); }).value_or(StringView {}); |
31 | 0 | } |
32 | 0 | Host const& host() const { return m_host; } |
33 | 0 | Optional<u16> port() const { return m_port; } |
34 | | |
35 | | // https://html.spec.whatwg.org/multipage/origin.html#same-origin |
36 | | bool is_same_origin(Origin const& other) const |
37 | 0 | { |
38 | 0 | // 1. If A and B are the same opaque origin, then return true. |
39 | 0 | if (is_opaque() && other.is_opaque()) |
40 | 0 | return true; |
41 | 0 |
|
42 | 0 | // 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true. |
43 | 0 | // 3. Return false. |
44 | 0 | return scheme() == other.scheme() |
45 | 0 | && host() == other.host() |
46 | 0 | && port() == other.port(); |
47 | 0 | } |
48 | | |
49 | | // https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain |
50 | | bool is_same_origin_domain(Origin const& other) const |
51 | 0 | { |
52 | 0 | // 1. If A and B are the same opaque origin, then return true. |
53 | 0 | if (is_opaque() && other.is_opaque()) |
54 | 0 | return true; |
55 | 0 |
|
56 | 0 | // 2. If A and B are both tuple origins, run these substeps: |
57 | 0 | if (!is_opaque() && !other.is_opaque()) { |
58 | 0 | // 1. If A and B's schemes are identical, and their domains are identical and non-null, then return true. |
59 | 0 | // FIXME: Check domains once supported. |
60 | 0 | if (scheme() == other.scheme()) |
61 | 0 | return true; |
62 | 0 |
|
63 | 0 | // 2. Otherwise, if A and B are same origin and their domains are identical and null, then return true. |
64 | 0 | // FIXME: Check domains once supported. |
65 | 0 | if (is_same_origin(other)) |
66 | 0 | return true; |
67 | 0 | } |
68 | 0 |
|
69 | 0 | // 3. Return false. |
70 | 0 | return false; |
71 | 0 | } |
72 | | |
73 | | // https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin |
74 | | ByteString serialize() const; |
75 | | |
76 | | // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain |
77 | | Optional<Host> effective_domain() const |
78 | 0 | { |
79 | 0 | // 1. If origin is an opaque origin, then return null. |
80 | 0 | if (is_opaque()) |
81 | 0 | return {}; |
82 | 0 |
|
83 | 0 | // FIXME: 2. If origin's domain is non-null, then return origin's domain. |
84 | 0 |
|
85 | 0 | // 3. Return origin's host. |
86 | 0 | return m_host; |
87 | 0 | } |
88 | | |
89 | 0 | bool operator==(Origin const& other) const { return is_same_origin(other); } |
90 | | |
91 | | private: |
92 | | Optional<ByteString> m_scheme; |
93 | | Host m_host; |
94 | | Optional<u16> m_port; |
95 | | }; |
96 | | |
97 | | } |
98 | | |
99 | | namespace AK { |
100 | | |
101 | | template<> |
102 | | struct Traits<URL::Origin> : public DefaultTraits<URL::Origin> { |
103 | | static unsigned hash(URL::Origin const& origin); |
104 | | }; |
105 | | |
106 | | } // namespace AK |