Origin.h 4.3 KB

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