Origin.h 4.4 KB

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