Origin.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #pragma once
  8. #include <AK/ByteString.h>
  9. #include <AK/URL.h>
  10. #include <AK/URLParser.h>
  11. #include <LibIPC/Forward.h>
  12. namespace Web::HTML {
  13. class Origin {
  14. public:
  15. Origin() = default;
  16. Origin(Optional<ByteString> const& scheme, URL::Host const& host, u16 port)
  17. : m_scheme(scheme)
  18. , m_host(host)
  19. , m_port(port)
  20. {
  21. }
  22. // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque
  23. bool is_opaque() const { return !m_scheme.has_value() && m_host.has<Empty>() && m_port == 0; }
  24. StringView scheme() const
  25. {
  26. return m_scheme.map([](auto& str) { return str.view(); }).value_or(StringView {});
  27. }
  28. URL::Host const& host() const { return m_host; }
  29. u16 port() const { return m_port; }
  30. // https://html.spec.whatwg.org/multipage/origin.html#same-origin
  31. bool is_same_origin(Origin const& other) const
  32. {
  33. // 1. If A and B are the same opaque origin, then return true.
  34. if (is_opaque() && other.is_opaque())
  35. return true;
  36. // 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true.
  37. // 3. Return false.
  38. return scheme() == other.scheme()
  39. && host() == other.host()
  40. && port() == other.port();
  41. }
  42. // https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain
  43. bool is_same_origin_domain(Origin const& other) const
  44. {
  45. // 1. If A and B are the same opaque origin, then return true.
  46. if (is_opaque() && other.is_opaque())
  47. return true;
  48. // 2. If A and B are both tuple origins, run these substeps:
  49. if (!is_opaque() && !other.is_opaque()) {
  50. // 1. If A and B's schemes are identical, and their domains are identical and non-null, then return true.
  51. // FIXME: Check domains once supported.
  52. if (scheme() == other.scheme())
  53. return true;
  54. // 2. Otherwise, if A and B are same origin and their domains are identical and null, then return true.
  55. // FIXME: Check domains once supported.
  56. if (is_same_origin(other))
  57. return true;
  58. }
  59. // 3. Return false.
  60. return false;
  61. }
  62. // https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
  63. ByteString serialize() const
  64. {
  65. // 1. If origin is an opaque origin, then return "null"
  66. if (is_opaque())
  67. return "null";
  68. // 2. Otherwise, let result be origin's scheme.
  69. StringBuilder result;
  70. result.append(scheme());
  71. // 3. Append "://" to result.
  72. result.append("://"sv);
  73. // 4. Append origin's host, serialized, to result.
  74. result.append(URLParser::serialize_host(host()).release_value_but_fixme_should_propagate_errors().to_byte_string());
  75. // 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result.
  76. if (port() != 0) {
  77. result.append(':');
  78. result.append(ByteString::number(port()));
  79. }
  80. // 6. Return result
  81. return result.to_byte_string();
  82. }
  83. // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain
  84. Optional<URL::Host> effective_domain() const
  85. {
  86. // 1. If origin is an opaque origin, then return null.
  87. if (is_opaque())
  88. return {};
  89. // FIXME: 2. If origin's domain is non-null, then return origin's domain.
  90. // 3. Return origin's host.
  91. return m_host;
  92. }
  93. bool operator==(Origin const& other) const { return is_same_origin(other); }
  94. private:
  95. Optional<ByteString> m_scheme;
  96. URL::Host m_host;
  97. u16 m_port { 0 };
  98. };
  99. }
  100. namespace AK {
  101. template<>
  102. struct Traits<Web::HTML::Origin> : public DefaultTraits<Web::HTML::Origin> {
  103. static unsigned hash(Web::HTML::Origin const& origin)
  104. {
  105. auto hash_without_host = pair_int_hash(origin.scheme().hash(), origin.port());
  106. if (origin.host().has<Empty>())
  107. return hash_without_host;
  108. return pair_int_hash(hash_without_host, URLParser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash());
  109. }
  110. };
  111. } // namespace AK
  112. namespace IPC {
  113. template<>
  114. ErrorOr<void> encode(Encoder&, Web::HTML::Origin const&);
  115. template<>
  116. ErrorOr<Web::HTML::Origin> decode(Decoder&);
  117. }