URL.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  4. * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/ByteString.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Vector.h>
  13. // On Linux distros that use mlibc `basename` is defined as a macro that expands to `__mlibc_gnu_basename` or `__mlibc_gnu_basename_c`, so we undefine it.
  14. #if defined(AK_OS_LINUX) && defined(basename)
  15. # undef basename
  16. #endif
  17. namespace URL {
  18. enum class PercentEncodeSet {
  19. C0Control,
  20. Fragment,
  21. Query,
  22. SpecialQuery,
  23. Path,
  24. Userinfo,
  25. Component,
  26. ApplicationXWWWFormUrlencoded,
  27. EncodeURI
  28. };
  29. enum class ExcludeFragment {
  30. No,
  31. Yes
  32. };
  33. // https://url.spec.whatwg.org/#concept-ipv4
  34. // An IPv4 address is a 32-bit unsigned integer that identifies a network address. [RFC791]
  35. // FIXME: It would be nice if this were an AK::IPv4Address
  36. using IPv4Address = u32;
  37. // https://url.spec.whatwg.org/#concept-ipv6
  38. // An IPv6 address is a 128-bit unsigned integer that identifies a network address. For the purposes of this standard
  39. // it is represented as a list of eight 16-bit unsigned integers, also known as IPv6 pieces. [RFC4291]
  40. // FIXME: It would be nice if this were an AK::IPv6Address
  41. using IPv6Address = Array<u16, 8>;
  42. // https://url.spec.whatwg.org/#concept-host
  43. // A host is a domain, an IP address, an opaque host, or an empty host. Typically a host serves as a network address,
  44. // but it is sometimes used as opaque identifier in URLs where a network address is not necessary.
  45. using Host = Variant<IPv4Address, IPv6Address, String, Empty>;
  46. enum class ApplyPercentDecoding {
  47. Yes,
  48. No
  49. };
  50. void append_percent_encoded_if_necessary(StringBuilder&, u32 code_point, PercentEncodeSet set = PercentEncodeSet::Userinfo);
  51. void append_percent_encoded(StringBuilder&, u32 code_point);
  52. bool code_point_is_in_percent_encode_set(u32 code_point, PercentEncodeSet);
  53. Optional<u16> default_port_for_scheme(StringView);
  54. bool is_special_scheme(StringView);
  55. enum class SpaceAsPlus {
  56. No,
  57. Yes,
  58. };
  59. ByteString percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
  60. ByteString percent_decode(StringView input);
  61. // https://url.spec.whatwg.org/#url-representation
  62. // A URL is a struct that represents a universal identifier. To disambiguate from a valid URL string it can also be referred to as a URL record.
  63. class URL {
  64. friend class Parser;
  65. public:
  66. URL() = default;
  67. URL(StringView);
  68. URL(ByteString const& string)
  69. : URL(string.view())
  70. {
  71. }
  72. URL(String const& string)
  73. : URL(string.bytes_as_string_view())
  74. {
  75. }
  76. bool is_valid() const { return m_valid; }
  77. String const& scheme() const { return m_scheme; }
  78. ErrorOr<String> username() const;
  79. ErrorOr<String> password() const;
  80. Host const& host() const { return m_host; }
  81. ErrorOr<String> serialized_host() const;
  82. ByteString basename() const;
  83. Optional<String> const& query() const { return m_query; }
  84. Optional<String> const& fragment() const { return m_fragment; }
  85. Optional<u16> port() const { return m_port; }
  86. ByteString path_segment_at_index(size_t index) const;
  87. size_t path_segment_count() const { return m_paths.size(); }
  88. u16 port_or_default() const { return m_port.value_or(default_port_for_scheme(m_scheme).value_or(0)); }
  89. bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; }
  90. bool cannot_have_a_username_or_password_or_port() const;
  91. bool includes_credentials() const { return !m_username.is_empty() || !m_password.is_empty(); }
  92. bool is_special() const { return is_special_scheme(m_scheme); }
  93. void set_scheme(String);
  94. ErrorOr<void> set_username(StringView);
  95. ErrorOr<void> set_password(StringView);
  96. void set_host(Host);
  97. void set_port(Optional<u16>);
  98. void set_paths(Vector<ByteString> const&);
  99. void set_query(Optional<String> query) { m_query = move(query); }
  100. void set_fragment(Optional<String> fragment) { m_fragment = move(fragment); }
  101. void set_cannot_be_a_base_url(bool value) { m_cannot_be_a_base_url = value; }
  102. void append_path(StringView);
  103. void append_slash()
  104. {
  105. // NOTE: To indicate that we want to end the path with a slash, we have to append an empty path segment.
  106. m_paths.append(String {});
  107. }
  108. ByteString serialize_path(ApplyPercentDecoding = ApplyPercentDecoding::Yes) const;
  109. ByteString serialize(ExcludeFragment = ExcludeFragment::No) const;
  110. ByteString serialize_for_display() const;
  111. ByteString to_byte_string() const { return serialize(); }
  112. ErrorOr<String> to_string() const;
  113. // HTML origin
  114. ByteString serialize_origin() const;
  115. bool equals(URL const& other, ExcludeFragment = ExcludeFragment::No) const;
  116. URL complete_url(StringView) const;
  117. bool operator==(URL const& other) const { return equals(other, ExcludeFragment::No); }
  118. String const& raw_username() const { return m_username; }
  119. String const& raw_password() const { return m_password; }
  120. private:
  121. bool compute_validity() const;
  122. bool m_valid { false };
  123. // A URL’s scheme is an ASCII string that identifies the type of URL and can be used to dispatch a URL for further processing after parsing. It is initially the empty string.
  124. String m_scheme;
  125. // A URL’s username is an ASCII string identifying a username. It is initially the empty string.
  126. String m_username;
  127. // A URL’s password is an ASCII string identifying a password. It is initially the empty string.
  128. String m_password;
  129. // A URL’s host is null or a host. It is initially null.
  130. Host m_host;
  131. // A URL’s port is either null or a 16-bit unsigned integer that identifies a networking port. It is initially null.
  132. Optional<u16> m_port;
  133. // A URL’s path is either a URL path segment or a list of zero or more URL path segments, usually identifying a location. It is initially « ».
  134. // A URL path segment is an ASCII string. It commonly refers to a directory or a file, but has no predefined meaning.
  135. Vector<String> m_paths;
  136. // A URL’s query is either null or an ASCII string. It is initially null.
  137. Optional<String> m_query;
  138. // A URL’s fragment is either null or an ASCII string that can be used for further processing on the resource the URL’s other components identify. It is initially null.
  139. Optional<String> m_fragment;
  140. bool m_cannot_be_a_base_url { false };
  141. };
  142. URL create_with_url_or_path(ByteString const&);
  143. URL create_with_file_scheme(ByteString const& path, ByteString const& fragment = {}, ByteString const& hostname = {});
  144. URL create_with_help_scheme(ByteString const& path, ByteString const& fragment = {}, ByteString const& hostname = {});
  145. URL create_with_data(StringView mime_type, StringView payload, bool is_base64 = false);
  146. }
  147. template<>
  148. struct AK::Formatter<URL::URL> : AK::Formatter<StringView> {
  149. ErrorOr<void> format(FormatBuilder& builder, URL::URL const& value)
  150. {
  151. return Formatter<StringView>::format(builder, value.serialize());
  152. }
  153. };
  154. template<>
  155. struct AK::Traits<URL::URL> : public AK::DefaultTraits<URL::URL> {
  156. static unsigned hash(URL::URL const& url) { return url.to_byte_string().hash(); }
  157. };