URL.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/URLParser.h>
  8. #include <LibWeb/URL/URL.h>
  9. namespace Web::URL {
  10. DOM::ExceptionOr<NonnullRefPtr<URL>> URL::create_with_global_object(Bindings::WindowObject& window_object, String const& url, String const& base)
  11. {
  12. // 1. Let parsedBase be null.
  13. Optional<AK::URL> parsed_base;
  14. // 2. If base is given, then:
  15. if (!base.is_null()) {
  16. // 1. Let parsedBase be the result of running the basic URL parser on base.
  17. parsed_base = base;
  18. // 2. If parsedBase is failure, then throw a TypeError.
  19. if (!parsed_base->is_valid())
  20. return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid base URL" };
  21. }
  22. // 3. Let parsedURL be the result of running the basic URL parser on url with parsedBase.
  23. AK::URL parsed_url;
  24. if (parsed_base.has_value())
  25. parsed_url = parsed_base->complete_url(url);
  26. else
  27. parsed_url = url;
  28. // 4. If parsedURL is failure, then throw a TypeError.
  29. if (!parsed_url.is_valid())
  30. return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid URL" };
  31. // 5. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise.
  32. auto& query = parsed_url.query().is_null() ? String::empty() : parsed_url.query();
  33. // 6. Set this’s URL to parsedURL.
  34. // 7. Set this’s query object to a new URLSearchParams object.
  35. auto query_object = URLSearchParams::create_with_global_object(window_object, query);
  36. VERIFY(!query_object.is_exception()); // The string variant of the constructor can't throw.
  37. // 8. Initialize this’s query object with query.
  38. auto result_url = URL::create(move(parsed_url), query_object.release_value());
  39. // 9. Set this’s query object’s URL object to this.
  40. result_url->m_query->m_url = result_url;
  41. return result_url;
  42. }
  43. String URL::href() const
  44. {
  45. // return the serialization of this’s URL.
  46. return m_url.serialize();
  47. }
  48. String URL::to_json() const
  49. {
  50. // return the serialization of this’s URL.
  51. return m_url.serialize();
  52. }
  53. DOM::ExceptionOr<void> URL::set_href(String const& href)
  54. {
  55. // 1. Let parsedURL be the result of running the basic URL parser on the given value.
  56. AK::URL parsed_url = href;
  57. // 2. If parsedURL is failure, then throw a TypeError.
  58. if (!parsed_url.is_valid())
  59. return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid URL" };
  60. // 3. Set this’s URL to parsedURL.
  61. m_url = move(parsed_url);
  62. // 4. Empty this’s query object’s list.
  63. m_query->m_list.clear();
  64. // 5. Let query be this’s URL’s query.
  65. auto& query = m_url.query();
  66. // 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
  67. if (!query.is_null())
  68. m_query->m_list = url_decode(query);
  69. return {};
  70. }
  71. String URL::origin() const
  72. {
  73. // return the serialization of this’s URL’s origin.
  74. return m_url.serialize_origin();
  75. }
  76. String URL::protocol() const
  77. {
  78. // return this’s URL’s scheme, followed by U+003A (:).
  79. return String::formatted("{}:", m_url.scheme());
  80. }
  81. void URL::set_protocol(String const& protocol)
  82. {
  83. // basic URL parse the given value, followed by U+003A (:), with this’s URL as url and scheme start state as state override.
  84. auto result_url = URLParser::parse(String::formatted("{}:", protocol), nullptr, m_url, URLParser::State::SchemeStart);
  85. if (result_url.is_valid())
  86. m_url = move(result_url);
  87. }
  88. String URL::username() const
  89. {
  90. // return this’s URL’s username.
  91. return m_url.username();
  92. }
  93. void URL::set_username(const String& username)
  94. {
  95. // 1. If this’s URL cannot have a username/password/port, then return.
  96. if (m_url.cannot_have_a_username_or_password_or_port())
  97. return;
  98. // 2. Set the username given this’s URL and the given value.
  99. m_url.set_username(AK::URL::percent_encode(username, AK::URL::PercentEncodeSet::Userinfo));
  100. }
  101. String URL::password() const
  102. {
  103. // return this’s URL’s password.
  104. return m_url.password();
  105. }
  106. void URL::set_password(String const& password)
  107. {
  108. // 1. If this’s URL cannot have a username/password/port, then return.
  109. if (m_url.cannot_have_a_username_or_password_or_port())
  110. return;
  111. // 2. Set the password given this’s URL and the given value.
  112. m_url.set_password(AK::URL::percent_encode(password, AK::URL::PercentEncodeSet::Userinfo));
  113. }
  114. String URL::host() const
  115. {
  116. // 1. Let url be this’s URL.
  117. auto& url = m_url;
  118. // 2. If url’s host is null, then return the empty string.
  119. if (url.host().is_null())
  120. return String::empty();
  121. // 3. If url’s port is null, return url’s host, serialized.
  122. if (!url.port().has_value())
  123. return url.host();
  124. // 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
  125. return String::formatted("{}:{}", url.host(), *url.port());
  126. }
  127. void URL::set_host(const String& host)
  128. {
  129. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  130. if (m_url.cannot_be_a_base_url())
  131. return;
  132. // 2. Basic URL parse the given value with this’s URL as url and host state as state override.
  133. auto result_url = URLParser::parse(host, nullptr, m_url, URLParser::State::Host);
  134. if (result_url.is_valid())
  135. m_url = move(result_url);
  136. }
  137. String URL::hostname() const
  138. {
  139. // 1. If this’s URL’s host is null, then return the empty string.
  140. if (m_url.host().is_null())
  141. return String::empty();
  142. // 2. Return this’s URL’s host, serialized.
  143. return m_url.host();
  144. }
  145. void URL::set_hostname(String const& hostname)
  146. {
  147. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  148. if (m_url.cannot_be_a_base_url())
  149. return;
  150. // 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
  151. auto result_url = URLParser::parse(hostname, nullptr, m_url, URLParser::State::Hostname);
  152. if (result_url.is_valid())
  153. m_url = move(result_url);
  154. }
  155. String URL::port() const
  156. {
  157. // 1. If this’s URL’s port is null, then return the empty string.
  158. if (!m_url.port().has_value())
  159. return {};
  160. // 2. Return this’s URL’s port, serialized.
  161. return String::formatted("{}", *m_url.port());
  162. }
  163. void URL::set_port(String const& port)
  164. {
  165. // 1. If this’s URL cannot have a username/password/port, then return.
  166. if (m_url.cannot_have_a_username_or_password_or_port())
  167. return;
  168. // 2. If the given value is the empty string, then set this’s URL’s port to null.
  169. if (port.is_empty()) {
  170. m_url.set_port({});
  171. return;
  172. }
  173. // 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
  174. auto result_url = URLParser::parse(port, nullptr, m_url, URLParser::State::Port);
  175. if (result_url.is_valid())
  176. m_url = move(result_url);
  177. }
  178. String URL::pathname() const
  179. {
  180. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return this’s URL’s path[0].
  181. // 2. If this’s URL’s path is empty, then return the empty string.
  182. // 3. Return U+002F (/), followed by the strings in this’s URL’s path (including empty strings), if any, separated from each other by U+002F (/).
  183. return m_url.path();
  184. }
  185. void URL::set_pathname(String const& pathname)
  186. {
  187. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  188. if (m_url.cannot_be_a_base_url())
  189. return;
  190. // 2. Empty this’s URL’s path.
  191. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the path change if the parse failed.
  192. url.set_paths({});
  193. // 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
  194. auto result_url = URLParser::parse(pathname, nullptr, move(url), URLParser::State::PathStart);
  195. if (result_url.is_valid())
  196. m_url = move(result_url);
  197. }
  198. String URL::search() const
  199. {
  200. // 1. If this’s URL’s query is either null or the empty string, then return the empty string.
  201. if (m_url.query().is_null() || m_url.query().is_empty())
  202. return String::empty();
  203. // 2. Return U+003F (?), followed by this’s URL’s query.
  204. return String::formatted("?{}", m_url.query());
  205. }
  206. void URL::set_search(String const& search)
  207. {
  208. // 1. Let url be this’s URL.
  209. auto& url = m_url;
  210. // If the given value is the empty string, set url’s query to null, empty this’s query object’s list, and then return.
  211. if (search.is_empty()) {
  212. url.set_query({});
  213. m_query->m_list.clear();
  214. return;
  215. }
  216. // 2. Let input be the given value with a single leading U+003F (?) removed, if any.
  217. auto input = search.substring_view(search.starts_with('?'));
  218. // 3. Set url’s query to the empty string.
  219. auto url_copy = url; // We copy the URL here to follow other browser's behaviour of reverting the search change if the parse failed.
  220. url_copy.set_query(String::empty());
  221. // 4. Basic URL parse input with url as url and query state as state override.
  222. auto result_url = URLParser::parse(input, nullptr, move(url_copy), URLParser::State::Query);
  223. if (result_url.is_valid()) {
  224. m_url = move(result_url);
  225. // 5. Set this’s query object’s list to the result of parsing input.
  226. m_query->m_list = url_decode(input);
  227. }
  228. }
  229. URLSearchParams const* URL::search_params() const
  230. {
  231. return m_query;
  232. }
  233. String URL::hash() const
  234. {
  235. // 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
  236. if (m_url.fragment().is_null() || m_url.fragment().is_empty())
  237. return String::empty();
  238. // 2. Return U+0023 (#), followed by this’s URL’s fragment.
  239. return String::formatted("#{}", m_url.fragment());
  240. }
  241. void URL::set_hash(String const& hash)
  242. {
  243. // 1. If the given value is the empty string, then set this’s URL’s fragment to null and return.
  244. if (hash.is_empty()) {
  245. m_url.set_fragment({});
  246. return;
  247. }
  248. // 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
  249. auto input = hash.substring_view(hash.starts_with('#'));
  250. // 3. Set this’s URL’s fragment to the empty string.
  251. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the hash change if the parse failed.
  252. url.set_fragment(String::empty());
  253. // 4. Basic URL parse input with this’s URL as url and fragment state as state override.
  254. auto result_url = URLParser::parse(input, nullptr, move(url), URLParser::State::Fragment);
  255. if (result_url.is_valid())
  256. m_url = move(result_url);
  257. }
  258. }