URL.cpp 11 KB

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