URL.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/IPv4Address.h>
  8. #include <AK/IPv6Address.h>
  9. #include <AK/URLParser.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/URL/URL.h>
  12. namespace Web::URL {
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> URL::create(JS::Realm& realm, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
  14. {
  15. return MUST_OR_THROW_OOM(realm.heap().allocate<URL>(realm, realm, move(url), move(query)));
  16. }
  17. WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> URL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base)
  18. {
  19. auto& vm = realm.vm();
  20. // 1. Let parsedBase be null.
  21. Optional<AK::URL> parsed_base;
  22. // 2. If base is given, then:
  23. if (base.has_value()) {
  24. // 1. Let parsedBase be the result of running the basic URL parser on base.
  25. parsed_base = base.value();
  26. // 2. If parsedBase is failure, then throw a TypeError.
  27. if (!parsed_base->is_valid())
  28. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid base URL"sv };
  29. }
  30. // 3. Let parsedURL be the result of running the basic URL parser on url with parsedBase.
  31. AK::URL parsed_url;
  32. if (parsed_base.has_value())
  33. parsed_url = parsed_base->complete_url(url);
  34. else
  35. parsed_url = url;
  36. // 4. If parsedURL is failure, then throw a TypeError.
  37. if (!parsed_url.is_valid())
  38. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  39. // 5. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise.
  40. auto query = parsed_url.query().is_null() ? String {} : TRY_OR_THROW_OOM(vm, String::from_deprecated_string(parsed_url.query()));
  41. // 6. Set this’s URL to parsedURL.
  42. // 7. Set this’s query object to a new URLSearchParams object.
  43. auto query_object = MUST(URLSearchParams::construct_impl(realm, query));
  44. // 8. Initialize this’s query object with query.
  45. auto result_url = TRY(URL::create(realm, move(parsed_url), move(query_object)));
  46. // 9. Set this’s query object’s URL object to this.
  47. result_url->m_query->m_url = result_url;
  48. return result_url;
  49. }
  50. URL::URL(JS::Realm& realm, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
  51. : PlatformObject(realm)
  52. , m_url(move(url))
  53. , m_query(move(query))
  54. {
  55. }
  56. URL::~URL() = default;
  57. JS::ThrowCompletionOr<void> URL::initialize(JS::Realm& realm)
  58. {
  59. MUST_OR_THROW_OOM(Base::initialize(realm));
  60. set_prototype(&Bindings::ensure_web_prototype<Bindings::URLPrototype>(realm, "URL"));
  61. return {};
  62. }
  63. void URL::visit_edges(Cell::Visitor& visitor)
  64. {
  65. Base::visit_edges(visitor);
  66. visitor.visit(m_query.ptr());
  67. }
  68. WebIDL::ExceptionOr<String> URL::href() const
  69. {
  70. auto& vm = realm().vm();
  71. // return the serialization of this’s URL.
  72. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize()));
  73. }
  74. WebIDL::ExceptionOr<String> URL::to_json() const
  75. {
  76. auto& vm = realm().vm();
  77. // return the serialization of this’s URL.
  78. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize()));
  79. }
  80. WebIDL::ExceptionOr<void> URL::set_href(String const& href)
  81. {
  82. auto& vm = realm().vm();
  83. // 1. Let parsedURL be the result of running the basic URL parser on the given value.
  84. AK::URL parsed_url = href;
  85. // 2. If parsedURL is failure, then throw a TypeError.
  86. if (!parsed_url.is_valid())
  87. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  88. // 3. Set this’s URL to parsedURL.
  89. m_url = move(parsed_url);
  90. // 4. Empty this’s query object’s list.
  91. m_query->m_list.clear();
  92. // 5. Let query be this’s URL’s query.
  93. auto& query = m_url.query();
  94. // 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
  95. if (!query.is_null())
  96. m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(query));
  97. return {};
  98. }
  99. WebIDL::ExceptionOr<String> URL::origin() const
  100. {
  101. auto& vm = realm().vm();
  102. // return the serialization of this’s URL’s origin.
  103. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize_origin()));
  104. }
  105. WebIDL::ExceptionOr<String> URL::protocol() const
  106. {
  107. auto& vm = realm().vm();
  108. // return this’s URL’s scheme, followed by U+003A (:).
  109. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_url.scheme()));
  110. }
  111. WebIDL::ExceptionOr<void> URL::set_protocol(String const& protocol)
  112. {
  113. auto& vm = realm().vm();
  114. // basic URL parse the given value, followed by U+003A (:), with this’s URL as url and scheme start state as state override.
  115. auto result_url = URLParser::parse(TRY_OR_THROW_OOM(vm, String::formatted("{}:", protocol)), {}, m_url, URLParser::State::SchemeStart);
  116. if (result_url.is_valid())
  117. m_url = move(result_url);
  118. return {};
  119. }
  120. WebIDL::ExceptionOr<String> URL::username() const
  121. {
  122. auto& vm = realm().vm();
  123. // return this’s URL’s username.
  124. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.username()));
  125. }
  126. void URL::set_username(String const& username)
  127. {
  128. // 1. If this’s URL cannot have a username/password/port, then return.
  129. if (m_url.cannot_have_a_username_or_password_or_port())
  130. return;
  131. // 2. Set the username given this’s URL and the given value.
  132. m_url.set_username(AK::URL::percent_encode(username, AK::URL::PercentEncodeSet::Userinfo));
  133. }
  134. WebIDL::ExceptionOr<String> URL::password() const
  135. {
  136. auto& vm = realm().vm();
  137. // return this’s URL’s password.
  138. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.password()));
  139. }
  140. void URL::set_password(String const& password)
  141. {
  142. // 1. If this’s URL cannot have a username/password/port, then return.
  143. if (m_url.cannot_have_a_username_or_password_or_port())
  144. return;
  145. // 2. Set the password given this’s URL and the given value.
  146. m_url.set_password(AK::URL::percent_encode(password, AK::URL::PercentEncodeSet::Userinfo));
  147. }
  148. WebIDL::ExceptionOr<String> URL::host() const
  149. {
  150. auto& vm = realm().vm();
  151. // 1. Let url be this’s URL.
  152. auto& url = m_url;
  153. // 2. If url’s host is null, then return the empty string.
  154. if (url.host().is_null())
  155. return String {};
  156. // 3. If url’s port is null, return url’s host, serialized.
  157. if (!url.port().has_value())
  158. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
  159. // 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
  160. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host(), *url.port()));
  161. }
  162. void URL::set_host(String const& host)
  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 host state as state override.
  168. auto result_url = URLParser::parse(host, {}, m_url, URLParser::State::Host);
  169. if (result_url.is_valid())
  170. m_url = move(result_url);
  171. }
  172. WebIDL::ExceptionOr<String> URL::hostname() const
  173. {
  174. auto& vm = realm().vm();
  175. // 1. If this’s URL’s host is null, then return the empty string.
  176. if (m_url.host().is_null())
  177. return String {};
  178. // 2. Return this’s URL’s host, serialized.
  179. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.host()));
  180. }
  181. void URL::set_hostname(String const& hostname)
  182. {
  183. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  184. if (m_url.cannot_be_a_base_url())
  185. return;
  186. // 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
  187. auto result_url = URLParser::parse(hostname, {}, m_url, URLParser::State::Hostname);
  188. if (result_url.is_valid())
  189. m_url = move(result_url);
  190. }
  191. WebIDL::ExceptionOr<String> URL::port() const
  192. {
  193. auto& vm = realm().vm();
  194. // 1. If this’s URL’s port is null, then return the empty string.
  195. if (!m_url.port().has_value())
  196. return String {};
  197. // 2. Return this’s URL’s port, serialized.
  198. return TRY_OR_THROW_OOM(vm, String::formatted("{}", *m_url.port()));
  199. }
  200. void URL::set_port(String const& port)
  201. {
  202. // 1. If this’s URL cannot have a username/password/port, then return.
  203. if (m_url.cannot_have_a_username_or_password_or_port())
  204. return;
  205. // 2. If the given value is the empty string, then set this’s URL’s port to null.
  206. if (port.is_empty()) {
  207. m_url.set_port({});
  208. return;
  209. }
  210. // 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
  211. auto result_url = URLParser::parse(port, {}, m_url, URLParser::State::Port);
  212. if (result_url.is_valid())
  213. m_url = move(result_url);
  214. }
  215. WebIDL::ExceptionOr<String> URL::pathname() const
  216. {
  217. auto& vm = realm().vm();
  218. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return this’s URL’s path[0].
  219. // 2. If this’s URL’s path is empty, then return the empty string.
  220. // 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 (/).
  221. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.path()));
  222. }
  223. void URL::set_pathname(String const& pathname)
  224. {
  225. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  226. if (m_url.cannot_be_a_base_url())
  227. return;
  228. // 2. Empty this’s URL’s path.
  229. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the path change if the parse failed.
  230. url.set_paths({});
  231. // 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
  232. auto result_url = URLParser::parse(pathname, {}, move(url), URLParser::State::PathStart);
  233. if (result_url.is_valid())
  234. m_url = move(result_url);
  235. }
  236. WebIDL::ExceptionOr<String> URL::search() const
  237. {
  238. auto& vm = realm().vm();
  239. // 1. If this’s URL’s query is either null or the empty string, then return the empty string.
  240. if (m_url.query().is_null() || m_url.query().is_empty())
  241. return String {};
  242. // 2. Return U+003F (?), followed by this’s URL’s query.
  243. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", m_url.query()));
  244. }
  245. WebIDL::ExceptionOr<void> URL::set_search(String const& search)
  246. {
  247. auto& vm = realm().vm();
  248. // 1. Let url be this’s URL.
  249. auto& url = m_url;
  250. // If the given value is the empty string, set url’s query to null, empty this’s query object’s list, and then return.
  251. if (search.is_empty()) {
  252. url.set_query({});
  253. m_query->m_list.clear();
  254. return {};
  255. }
  256. // 2. Let input be the given value with a single leading U+003F (?) removed, if any.
  257. auto search_as_string_view = search.bytes_as_string_view();
  258. auto input = search_as_string_view.substring_view(search_as_string_view.starts_with('?'));
  259. // 3. Set url’s query to the empty string.
  260. auto url_copy = url; // We copy the URL here to follow other browser's behaviour of reverting the search change if the parse failed.
  261. url_copy.set_query(DeprecatedString::empty());
  262. // 4. Basic URL parse input with url as url and query state as state override.
  263. auto result_url = URLParser::parse(input, {}, move(url_copy), URLParser::State::Query);
  264. if (result_url.is_valid()) {
  265. m_url = move(result_url);
  266. // 5. Set this’s query object’s list to the result of parsing input.
  267. m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(input));
  268. }
  269. return {};
  270. }
  271. URLSearchParams const* URL::search_params() const
  272. {
  273. return m_query;
  274. }
  275. WebIDL::ExceptionOr<String> URL::hash() const
  276. {
  277. auto& vm = realm().vm();
  278. // 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
  279. if (m_url.fragment().is_null() || m_url.fragment().is_empty())
  280. return String {};
  281. // 2. Return U+0023 (#), followed by this’s URL’s fragment.
  282. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", m_url.fragment()));
  283. }
  284. void URL::set_hash(String const& hash)
  285. {
  286. // 1. If the given value is the empty string, then set this’s URL’s fragment to null and return.
  287. if (hash.is_empty()) {
  288. m_url.set_fragment({});
  289. return;
  290. }
  291. // 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
  292. auto hash_as_string_view = hash.bytes_as_string_view();
  293. auto input = hash_as_string_view.substring_view(hash_as_string_view.starts_with('#'));
  294. // 3. Set this’s URL’s fragment to the empty string.
  295. auto url = m_url; // We copy the URL here to follow other browser's behaviour of reverting the hash change if the parse failed.
  296. url.set_fragment(DeprecatedString::empty());
  297. // 4. Basic URL parse input with this’s URL as url and fragment state as state override.
  298. auto result_url = URLParser::parse(input, {}, move(url), URLParser::State::Fragment);
  299. if (result_url.is_valid())
  300. m_url = move(result_url);
  301. }
  302. // https://url.spec.whatwg.org/#concept-url-origin
  303. HTML::Origin url_origin(AK::URL const& url)
  304. {
  305. // FIXME: We should probably have an extended version of AK::URL for LibWeb instead of standalone functions like this.
  306. // The origin of a URL url is the origin returned by running these steps, switching on url’s scheme:
  307. // "blob"
  308. if (url.scheme() == "blob"sv) {
  309. // FIXME: Support 'blob://' URLs
  310. return HTML::Origin {};
  311. }
  312. // "ftp"
  313. // "http"
  314. // "https"
  315. // "ws"
  316. // "wss"
  317. if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
  318. // Return the tuple origin (url’s scheme, url’s host, url’s port, null).
  319. return HTML::Origin(url.scheme(), url.host(), url.port().value_or(0));
  320. }
  321. // "file"
  322. if (url.scheme() == "file"sv) {
  323. // Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  324. // Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
  325. return HTML::Origin(url.scheme(), DeprecatedString(), 0);
  326. }
  327. // Return a new opaque origin.
  328. return HTML::Origin {};
  329. }
  330. // https://url.spec.whatwg.org/#concept-domain
  331. bool host_is_domain(StringView host)
  332. {
  333. // A domain is a non-empty ASCII string that identifies a realm within a network.
  334. return !host.is_empty()
  335. && !IPv4Address::from_string(host).has_value()
  336. && !IPv6Address::from_string(host).has_value();
  337. }
  338. }