URL.cpp 11 KB

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