Origin.h 4.0 KB

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