DOMURL.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2023, networkException <networkexception@serenityos.org>
  5. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/IPv4Address.h>
  10. #include <AK/IPv6Address.h>
  11. #include <LibURL/Parser.h>
  12. #include <LibWeb/Bindings/DOMURLPrototype.h>
  13. #include <LibWeb/Bindings/Intrinsics.h>
  14. #include <LibWeb/DOMURL/DOMURL.h>
  15. #include <LibWeb/FileAPI/Blob.h>
  16. #include <LibWeb/FileAPI/BlobURLStore.h>
  17. namespace Web::DOMURL {
  18. GC_DEFINE_ALLOCATOR(DOMURL);
  19. GC::Ref<DOMURL> DOMURL::create(JS::Realm& realm, URL::URL url, GC::Ref<URLSearchParams> query)
  20. {
  21. return realm.create<DOMURL>(realm, move(url), query);
  22. }
  23. // https://url.spec.whatwg.org/#api-url-parser
  24. static Optional<URL::URL> parse_api_url(String const& url, Optional<String> const& base)
  25. {
  26. // FIXME: We somewhat awkwardly have two failure states encapsulated in the return type (and convert between them in the steps),
  27. // ideally we'd get rid of URL's valid flag
  28. // 1. Let parsedBase be null.
  29. Optional<URL::URL> parsed_base;
  30. // 2. If base is non-null:
  31. if (base.has_value()) {
  32. // 1. Set parsedBase to the result of running the basic URL parser on base.
  33. auto parsed_base_url = URL::Parser::basic_parse(*base);
  34. // 2. If parsedBase is failure, then return failure.
  35. if (!parsed_base_url.is_valid())
  36. return {};
  37. parsed_base = parsed_base_url;
  38. }
  39. // 3. Return the result of running the basic URL parser on url with parsedBase.
  40. auto parsed = URL::Parser::basic_parse(url, parsed_base);
  41. return parsed.is_valid() ? parsed : Optional<URL::URL> {};
  42. }
  43. // https://url.spec.whatwg.org/#url-initialize
  44. GC::Ref<DOMURL> DOMURL::initialize_a_url(JS::Realm& realm, URL::URL const& url_record)
  45. {
  46. // 1. Let query be urlRecord’s query, if that is non-null; otherwise the empty string.
  47. auto query = url_record.query().value_or(String {});
  48. // 2. Set url’s URL to urlRecord.
  49. // 3. Set url’s query object to a new URLSearchParams object.
  50. auto query_object = URLSearchParams::create(realm, query);
  51. // 4. Initialize url’s query object with query.
  52. auto result_url = DOMURL::create(realm, url_record, move(query_object));
  53. // 5. Set url’s query object’s URL object to url.
  54. result_url->m_query->m_url = result_url;
  55. return result_url;
  56. }
  57. // https://url.spec.whatwg.org/#dom-url-parse
  58. GC::Ptr<DOMURL> DOMURL::parse_for_bindings(JS::VM& vm, String const& url, Optional<String> const& base)
  59. {
  60. auto& realm = *vm.current_realm();
  61. // 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
  62. auto parsed_url = parse_api_url(url, base);
  63. // 2. If parsedURL is failure, then return null.
  64. if (!parsed_url.has_value())
  65. return nullptr;
  66. // 3. Let url be a new URL object.
  67. // 4. Initialize url with parsedURL.
  68. // 5. Return url.
  69. return initialize_a_url(realm, parsed_url.value());
  70. }
  71. // https://url.spec.whatwg.org/#dom-url-url
  72. WebIDL::ExceptionOr<GC::Ref<DOMURL>> DOMURL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base)
  73. {
  74. // 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
  75. auto parsed_url = parse_api_url(url, base);
  76. // 2. If parsedURL is failure, then throw a TypeError.
  77. if (!parsed_url.has_value())
  78. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  79. // 3. Initialize this with parsedURL.
  80. return initialize_a_url(realm, parsed_url.value());
  81. }
  82. DOMURL::DOMURL(JS::Realm& realm, URL::URL url, GC::Ref<URLSearchParams> query)
  83. : PlatformObject(realm)
  84. , m_url(move(url))
  85. , m_query(move(query))
  86. {
  87. }
  88. DOMURL::~DOMURL() = default;
  89. void DOMURL::initialize(JS::Realm& realm)
  90. {
  91. Base::initialize(realm);
  92. WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(DOMURL, URL);
  93. }
  94. void DOMURL::visit_edges(Cell::Visitor& visitor)
  95. {
  96. Base::visit_edges(visitor);
  97. visitor.visit(m_query);
  98. }
  99. // https://w3c.github.io/FileAPI/#dfn-createObjectURL
  100. WebIDL::ExceptionOr<String> DOMURL::create_object_url(JS::VM& vm, GC::Ref<FileAPI::Blob> object)
  101. {
  102. // The createObjectURL(obj) static method must return the result of adding an entry to the blob URL store for obj.
  103. return TRY_OR_THROW_OOM(vm, FileAPI::add_entry_to_blob_url_store(object));
  104. }
  105. // https://w3c.github.io/FileAPI/#dfn-revokeObjectURL
  106. WebIDL::ExceptionOr<void> DOMURL::revoke_object_url(JS::VM& vm, StringView url)
  107. {
  108. // 1. Let url record be the result of parsing url.
  109. auto url_record = parse(url);
  110. // 2. If url record’s scheme is not "blob", return.
  111. if (url_record.scheme() != "blob"sv)
  112. return {};
  113. // 3. Let origin be the origin of url record.
  114. auto origin = url_record.origin();
  115. // 4. Let settings be the current settings object.
  116. auto& settings = HTML::current_principal_settings_object();
  117. // 5. If origin is not same origin with settings’s origin, return.
  118. if (!origin.is_same_origin(settings.origin()))
  119. return {};
  120. // 6. Remove an entry from the Blob URL Store for url.
  121. TRY_OR_THROW_OOM(vm, FileAPI::remove_entry_from_blob_url_store(url));
  122. return {};
  123. }
  124. // https://url.spec.whatwg.org/#dom-url-canparse
  125. bool DOMURL::can_parse(JS::VM&, String const& url, Optional<String> const& base)
  126. {
  127. // 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
  128. auto parsed_url = parse_api_url(url, base);
  129. // 2. If parsedURL is failure, then return false.
  130. if (!parsed_url.has_value())
  131. return false;
  132. // 3. Return true.
  133. return true;
  134. }
  135. // https://url.spec.whatwg.org/#dom-url-href
  136. WebIDL::ExceptionOr<String> DOMURL::href() const
  137. {
  138. auto& vm = realm().vm();
  139. // The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
  140. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize()));
  141. }
  142. // https://url.spec.whatwg.org/#dom-url-tojson
  143. WebIDL::ExceptionOr<String> DOMURL::to_json() const
  144. {
  145. auto& vm = realm().vm();
  146. // The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
  147. return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.serialize()));
  148. }
  149. // https://url.spec.whatwg.org/#ref-for-dom-url-href②
  150. WebIDL::ExceptionOr<void> DOMURL::set_href(String const& href)
  151. {
  152. // 1. Let parsedURL be the result of running the basic URL parser on the given value.
  153. URL::URL parsed_url = href;
  154. // 2. If parsedURL is failure, then throw a TypeError.
  155. if (!parsed_url.is_valid())
  156. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
  157. // 3. Set this’s URL to parsedURL.
  158. m_url = move(parsed_url);
  159. // 4. Empty this’s query object’s list.
  160. m_query->m_list.clear();
  161. // 5. Let query be this’s URL’s query.
  162. auto query = m_url.query();
  163. // 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
  164. if (query.has_value())
  165. m_query->m_list = url_decode(*query);
  166. return {};
  167. }
  168. // https://url.spec.whatwg.org/#dom-url-origin
  169. String DOMURL::origin() const
  170. {
  171. // The origin getter steps are to return the serialization of this’s URL’s origin. [HTML]
  172. return m_url.origin().serialize();
  173. }
  174. // https://url.spec.whatwg.org/#dom-url-protocol
  175. WebIDL::ExceptionOr<String> DOMURL::protocol() const
  176. {
  177. auto& vm = realm().vm();
  178. // The protocol getter steps are to return this’s URL’s scheme, followed by U+003A (:).
  179. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_url.scheme()));
  180. }
  181. // https://url.spec.whatwg.org/#ref-for-dom-url-protocol%E2%91%A0
  182. WebIDL::ExceptionOr<void> DOMURL::set_protocol(String const& protocol)
  183. {
  184. auto& vm = realm().vm();
  185. // The protocol setter steps are to basic URL parse the given value, followed by U+003A (:), with this’s URL as
  186. // url and scheme start state as state override.
  187. (void)URL::Parser::basic_parse(TRY_OR_THROW_OOM(vm, String::formatted("{}:", protocol)), {}, &m_url, URL::Parser::State::SchemeStart);
  188. return {};
  189. }
  190. // https://url.spec.whatwg.org/#dom-url-username
  191. String const& DOMURL::username() const
  192. {
  193. // The username getter steps are to return this’s URL’s username.
  194. return m_url.username();
  195. }
  196. // https://url.spec.whatwg.org/#ref-for-dom-url-username%E2%91%A0
  197. void DOMURL::set_username(String const& username)
  198. {
  199. // 1. If this’s URL cannot have a username/password/port, then return.
  200. if (m_url.cannot_have_a_username_or_password_or_port())
  201. return;
  202. // 2. Set the username given this’s URL and the given value.
  203. m_url.set_username(username);
  204. }
  205. // https://url.spec.whatwg.org/#dom-url-password
  206. String const& DOMURL::password() const
  207. {
  208. // The password getter steps are to return this’s URL’s password.
  209. return m_url.password();
  210. }
  211. // https://url.spec.whatwg.org/#ref-for-dom-url-password%E2%91%A0
  212. void DOMURL::set_password(String const& password)
  213. {
  214. // 1. If this’s URL cannot have a username/password/port, then return.
  215. if (m_url.cannot_have_a_username_or_password_or_port())
  216. return;
  217. // 2. Set the password given this’s URL and the given value.
  218. m_url.set_password(password);
  219. }
  220. // https://url.spec.whatwg.org/#dom-url-host
  221. WebIDL::ExceptionOr<String> DOMURL::host() const
  222. {
  223. auto& vm = realm().vm();
  224. // 1. Let url be this’s URL.
  225. auto& url = m_url;
  226. // 2. If url’s host is null, then return the empty string.
  227. if (url.host().has<Empty>())
  228. return String {};
  229. // 3. If url’s port is null, return url’s host, serialized.
  230. if (!url.port().has_value())
  231. return TRY_OR_THROW_OOM(vm, url.serialized_host());
  232. // 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
  233. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), *url.port()));
  234. }
  235. // https://url.spec.whatwg.org/#dom-url-hostref-for-dom-url-host%E2%91%A0
  236. void DOMURL::set_host(String const& host)
  237. {
  238. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  239. if (m_url.cannot_be_a_base_url())
  240. return;
  241. // 2. Basic URL parse the given value with this’s URL as url and host state as state override.
  242. (void)URL::Parser::basic_parse(host, {}, &m_url, URL::Parser::State::Host);
  243. }
  244. // https://url.spec.whatwg.org/#dom-url-hostname
  245. WebIDL::ExceptionOr<String> DOMURL::hostname() const
  246. {
  247. auto& vm = realm().vm();
  248. // 1. If this’s URL’s host is null, then return the empty string.
  249. if (m_url.host().has<Empty>())
  250. return String {};
  251. // 2. Return this’s URL’s host, serialized.
  252. return TRY_OR_THROW_OOM(vm, m_url.serialized_host());
  253. }
  254. // https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
  255. void DOMURL::set_hostname(String const& hostname)
  256. {
  257. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  258. if (m_url.cannot_be_a_base_url())
  259. return;
  260. // 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
  261. (void)URL::Parser::basic_parse(hostname, {}, &m_url, URL::Parser::State::Hostname);
  262. }
  263. // https://url.spec.whatwg.org/#dom-url-port
  264. WebIDL::ExceptionOr<String> DOMURL::port() const
  265. {
  266. auto& vm = realm().vm();
  267. // 1. If this’s URL’s port is null, then return the empty string.
  268. if (!m_url.port().has_value())
  269. return String {};
  270. // 2. Return this’s URL’s port, serialized.
  271. return TRY_OR_THROW_OOM(vm, String::formatted("{}", *m_url.port()));
  272. }
  273. // https://url.spec.whatwg.org/#ref-for-dom-url-port%E2%91%A0
  274. void DOMURL::set_port(String const& port)
  275. {
  276. // 1. If this’s URL cannot have a username/password/port, then return.
  277. if (m_url.cannot_have_a_username_or_password_or_port())
  278. return;
  279. // 2. If the given value is the empty string, then set this’s URL’s port to null.
  280. if (port.is_empty()) {
  281. m_url.set_port({});
  282. }
  283. // 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
  284. else {
  285. (void)URL::Parser::basic_parse(port, {}, &m_url, URL::Parser::State::Port);
  286. }
  287. }
  288. // https://url.spec.whatwg.org/#dom-url-pathname
  289. String DOMURL::pathname() const
  290. {
  291. // The pathname getter steps are to return the result of URL path serializing this’s URL.
  292. return m_url.serialize_path();
  293. }
  294. // https://url.spec.whatwg.org/#ref-for-dom-url-pathname%E2%91%A0
  295. void DOMURL::set_pathname(String const& pathname)
  296. {
  297. // FIXME: These steps no longer match the speci.
  298. // 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
  299. if (m_url.cannot_be_a_base_url())
  300. return;
  301. // 2. Empty this’s URL’s path.
  302. m_url.set_paths({});
  303. // 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
  304. (void)URL::Parser::basic_parse(pathname, {}, &m_url, URL::Parser::State::PathStart);
  305. }
  306. // https://url.spec.whatwg.org/#dom-url-search
  307. WebIDL::ExceptionOr<String> DOMURL::search() const
  308. {
  309. auto& vm = realm().vm();
  310. // 1. If this’s URL’s query is either null or the empty string, then return the empty string.
  311. if (!m_url.query().has_value() || m_url.query()->is_empty())
  312. return String {};
  313. // 2. Return U+003F (?), followed by this’s URL’s query.
  314. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", *m_url.query()));
  315. }
  316. // https://url.spec.whatwg.org/#ref-for-dom-url-search%E2%91%A0
  317. void DOMURL::set_search(String const& search)
  318. {
  319. // 1. Let url be this’s URL.
  320. auto& url = m_url;
  321. // 2. If the given value is the empty string:
  322. if (search.is_empty()) {
  323. // 1. Set url’s query to null.
  324. url.set_query({});
  325. // 2. Empty this’s query object’s list.
  326. m_query->m_list.clear();
  327. // 3. Potentially strip trailing spaces from an opaque path with this.
  328. strip_trailing_spaces_from_an_opaque_path(*this);
  329. // 4. Return.
  330. return;
  331. }
  332. // 3. Let input be the given value with a single leading U+003F (?) removed, if any.
  333. auto search_as_string_view = search.bytes_as_string_view();
  334. auto input = search_as_string_view.substring_view(search_as_string_view.starts_with('?'));
  335. // 4. Set url’s query to the empty string.
  336. url.set_query(String {});
  337. // 5. Basic URL parse input with url as url and query state as state override.
  338. (void)URL::Parser::basic_parse(input, {}, &url, URL::Parser::State::Query);
  339. // 6. Set this’s query object’s list to the result of parsing input.
  340. m_query->m_list = url_decode(input);
  341. }
  342. // https://url.spec.whatwg.org/#dom-url-searchparams
  343. GC::Ref<URLSearchParams const> DOMURL::search_params() const
  344. {
  345. // The searchParams getter steps are to return this’s query object.
  346. return m_query;
  347. }
  348. // https://url.spec.whatwg.org/#dom-url-hash
  349. WebIDL::ExceptionOr<String> DOMURL::hash() const
  350. {
  351. auto& vm = realm().vm();
  352. // 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
  353. if (!m_url.fragment().has_value() || m_url.fragment()->is_empty())
  354. return String {};
  355. // 2. Return U+0023 (#), followed by this’s URL’s fragment.
  356. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", m_url.fragment()));
  357. }
  358. // https://url.spec.whatwg.org/#ref-for-dom-url-hash%E2%91%A0
  359. void DOMURL::set_hash(String const& hash)
  360. {
  361. // 1. If the given value is the empty string:
  362. if (hash.is_empty()) {
  363. // 1. Set this’s URL’s fragment to null.
  364. m_url.set_fragment({});
  365. // 2. Potentially strip trailing spaces from an opaque path with this.
  366. strip_trailing_spaces_from_an_opaque_path(*this);
  367. // 3. Return.
  368. return;
  369. }
  370. // 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
  371. auto hash_as_string_view = hash.bytes_as_string_view();
  372. auto input = hash_as_string_view.substring_view(hash_as_string_view.starts_with('#'));
  373. // 3. Set this’s URL’s fragment to the empty string.
  374. m_url.set_fragment(String {});
  375. // 4. Basic URL parse input with this’s URL as url and fragment state as state override.
  376. (void)URL::Parser::basic_parse(input, {}, &m_url, URL::Parser::State::Fragment);
  377. }
  378. // https://url.spec.whatwg.org/#concept-domain
  379. bool host_is_domain(URL::Host const& host)
  380. {
  381. // A domain is a non-empty ASCII string that identifies a realm within a network.
  382. return host.has<String>() && host.get<String>() != String {};
  383. }
  384. // https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
  385. void strip_trailing_spaces_from_an_opaque_path(DOMURL& url)
  386. {
  387. // 1. If url’s URL does not have an opaque path, then return.
  388. // FIXME: Reimplement this step once we modernize the URL implementation to meet the spec.
  389. if (!url.cannot_be_a_base_url())
  390. return;
  391. // 2. If url’s URL’s fragment is non-null, then return.
  392. if (url.fragment().has_value())
  393. return;
  394. // 3. If url’s URL’s query is non-null, then return.
  395. if (url.query().has_value())
  396. return;
  397. // 4. Remove all trailing U+0020 SPACE code points from url’s URL’s path.
  398. // NOTE: At index 0 since the first step tells us that the URL only has one path segment.
  399. auto opaque_path = url.path_segment_at_index(0);
  400. auto trimmed_path = opaque_path.trim(" "sv, TrimMode::Right);
  401. url.set_paths({ trimmed_path });
  402. }
  403. // https://url.spec.whatwg.org/#concept-url-parser
  404. URL::URL parse(StringView input, Optional<URL::URL> const& base_url, Optional<StringView> encoding)
  405. {
  406. // FIXME: We should probably have an extended version of URL::URL for LibWeb instead of standalone functions like this.
  407. // 1. Let url be the result of running the basic URL parser on input with base and encoding.
  408. auto url = URL::Parser::basic_parse(input, base_url, {}, {}, encoding);
  409. // 2. If url is failure, return failure.
  410. if (!url.is_valid())
  411. return {};
  412. // 3. If url’s scheme is not "blob", return url.
  413. if (url.scheme() != "blob")
  414. return url;
  415. // 4. Set url’s blob URL entry to the result of resolving the blob URL url, if that did not return failure, and null otherwise.
  416. auto blob_url_entry = FileAPI::resolve_a_blob_url(url);
  417. if (blob_url_entry.has_value()) {
  418. url.set_blob_url_entry(URL::BlobURLEntry {
  419. .type = blob_url_entry->object->type(),
  420. .byte_buffer = MUST(ByteBuffer::copy(blob_url_entry->object->raw_bytes())),
  421. .environment_origin = blob_url_entry->environment->origin(),
  422. });
  423. }
  424. // 5. Return url
  425. return url;
  426. }
  427. }