Origin.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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& protocol, String const& host, u16 port)
  14. : m_protocol(protocol)
  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_protocol.is_null() && m_host.is_null() && m_port == 0; }
  21. String const& protocol() const { return m_protocol; }
  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 protocol() == other.protocol()
  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 (protocol() == other.protocol())
  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(protocol());
  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. bool operator==(Origin const& other) const { return is_same_origin(other); }
  78. bool operator!=(Origin const& other) const { return !is_same_origin(other); }
  79. private:
  80. String m_protocol;
  81. String m_host;
  82. u16 m_port { 0 };
  83. };
  84. }
  85. namespace AK {
  86. template<>
  87. struct Traits<Web::HTML::Origin> : public GenericTraits<Web::HTML::Origin> {
  88. static unsigned hash(Web::HTML::Origin const& origin)
  89. {
  90. return pair_int_hash(origin.protocol().hash(), pair_int_hash(int_hash(origin.port()), origin.host().hash()));
  91. }
  92. };
  93. } // namespace AK