1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2023, networkException <networkexception@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/IPv4Address.h>
#include <AK/IPv6Address.h>
#include <AK/URLParser.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/URL/URL.h>
namespace Web::URL {
WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> URL::create(JS::Realm& realm, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
{
return MUST_OR_THROW_OOM(realm.heap().allocate<URL>(realm, realm, move(url), move(query)));
}
// https://url.spec.whatwg.org/#api-url-parser
static Optional<AK::URL> parse_api_url(String const& url, Optional<String> const& base)
{
// FIXME: We somewhat awkwardly have two failure states encapsulated in the return type (and convert between them in the steps),
// ideally we'd get rid of URL's valid flag
// 1. Let parsedBase be null.
Optional<AK::URL> parsed_base;
// 2. If base is non-null:
if (base.has_value()) {
// 1. Set parsedBase to the result of running the basic URL parser on base.
auto parsed_base_url = URLParser::parse(*base);
// 2. If parsedBase is failure, then return failure.
if (!parsed_base_url.is_valid())
return {};
parsed_base = parsed_base_url;
}
// 3. Return the result of running the basic URL parser on url with parsedBase.
auto parsed = URLParser::parse(url, parsed_base);
return parsed.is_valid() ? parsed : Optional<AK::URL> {};
}
// https://url.spec.whatwg.org/#dom-url-url
WebIDL::ExceptionOr<JS::NonnullGCPtr<URL>> URL::construct_impl(JS::Realm& realm, String const& url, Optional<String> const& base)
{
auto& vm = realm.vm();
// 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
auto parsed_url = parse_api_url(url, base);
// 2. If parsedURL is failure, then throw a TypeError.
if (!parsed_url.has_value())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
// 3. Let query be parsedURL’s query, if that is non-null, and the empty string otherwise.
auto query = parsed_url->query().is_null() ? String {} : TRY_OR_THROW_OOM(vm, String::from_deprecated_string(parsed_url->query()));
// 4. Set this’s URL to parsedURL.
// 5. Set this’s query object to a new URLSearchParams object.
auto query_object = MUST(URLSearchParams::construct_impl(realm, query));
// 6. Initialize this’s query object with query.
auto result_url = TRY(URL::create(realm, parsed_url.release_value(), move(query_object)));
// 7. Set this’s query object’s URL object to this.
result_url->m_query->m_url = result_url;
return result_url;
}
URL::URL(JS::Realm& realm, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
: PlatformObject(realm)
, m_url(move(url))
, m_query(move(query))
{
}
URL::~URL() = default;
JS::ThrowCompletionOr<void> URL::initialize(JS::Realm& realm)
{
MUST_OR_THROW_OOM(Base::initialize(realm));
set_prototype(&Bindings::ensure_web_prototype<Bindings::URLPrototype>(realm, "URL"));
return {};
}
void URL::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_query.ptr());
}
// https://url.spec.whatwg.org/#dom-url-canparse
bool URL::can_parse(JS::VM&, String const& url, Optional<String> const& base)
{
// 1. Let parsedURL be the result of running the API URL parser on url with base, if given.
auto parsed_url = parse_api_url(url, base);
// 2. If parsedURL is failure, then return false.
if (!parsed_url.has_value())
return false;
// 3. Return true.
return true;
}
// https://url.spec.whatwg.org/#dom-url-href
WebIDL::ExceptionOr<String> URL::href() const
{
auto& vm = realm().vm();
// The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize()));
}
// https://url.spec.whatwg.org/#dom-url-tojson
WebIDL::ExceptionOr<String> URL::to_json() const
{
auto& vm = realm().vm();
// The href getter steps and the toJSON() method steps are to return the serialization of this’s URL.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-href②
WebIDL::ExceptionOr<void> URL::set_href(String const& href)
{
auto& vm = realm().vm();
// 1. Let parsedURL be the result of running the basic URL parser on the given value.
AK::URL parsed_url = href;
// 2. If parsedURL is failure, then throw a TypeError.
if (!parsed_url.is_valid())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid URL"sv };
// 3. Set this’s URL to parsedURL.
m_url = move(parsed_url);
// 4. Empty this’s query object’s list.
m_query->m_list.clear();
// 5. Let query be this’s URL’s query.
auto query = m_url.query();
// 6. If query is non-null, then set this’s query object’s list to the result of parsing query.
if (!query.is_null())
m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(query));
return {};
}
// https://url.spec.whatwg.org/#dom-url-origin
WebIDL::ExceptionOr<String> URL::origin() const
{
auto& vm = realm().vm();
// The origin getter steps are to return the serialization of this’s URL’s origin. [HTML]
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize_origin()));
}
// https://url.spec.whatwg.org/#dom-url-protocol
WebIDL::ExceptionOr<String> URL::protocol() const
{
auto& vm = realm().vm();
// The protocol getter steps are to return this’s URL’s scheme, followed by U+003A (:).
return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_url.scheme()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-protocol%E2%91%A0
WebIDL::ExceptionOr<void> URL::set_protocol(String const& protocol)
{
auto& vm = realm().vm();
// The protocol setter steps are to basic URL parse the given value, followed by U+003A (:), with this’s URL as
// url and scheme start state as state override.
auto result_url = URLParser::parse(TRY_OR_THROW_OOM(vm, String::formatted("{}:", protocol)), {}, m_url, URLParser::State::SchemeStart);
if (result_url.is_valid())
m_url = move(result_url);
return {};
}
// https://url.spec.whatwg.org/#dom-url-username
WebIDL::ExceptionOr<String> URL::username() const
{
auto& vm = realm().vm();
// The username getter steps are to return this’s URL’s username.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.username()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-username%E2%91%A0
void URL::set_username(String const& username)
{
// 1. If this’s URL cannot have a username/password/port, then return.
if (m_url.cannot_have_a_username_or_password_or_port())
return;
// 2. Set the username given this’s URL and the given value.
m_url.set_username(username.to_deprecated_string(), AK::URL::ApplyPercentEncoding::Yes);
}
// https://url.spec.whatwg.org/#dom-url-password
WebIDL::ExceptionOr<String> URL::password() const
{
auto& vm = realm().vm();
// The password getter steps are to return this’s URL’s password.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.password()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-password%E2%91%A0
void URL::set_password(String const& password)
{
// 1. If this’s URL cannot have a username/password/port, then return.
if (m_url.cannot_have_a_username_or_password_or_port())
return;
// 2. Set the password given this’s URL and the given value.
m_url.set_password(password.to_deprecated_string(), AK::URL::ApplyPercentEncoding::Yes);
}
// https://url.spec.whatwg.org/#dom-url-host
WebIDL::ExceptionOr<String> URL::host() const
{
auto& vm = realm().vm();
// 1. Let url be this’s URL.
auto& url = m_url;
// 2. If url’s host is null, then return the empty string.
if (url.host().is_null())
return String {};
// 3. If url’s port is null, return url’s host, serialized.
if (!url.port().has_value())
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
// 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host(), *url.port()));
}
// https://url.spec.whatwg.org/#dom-url-hostref-for-dom-url-host%E2%91%A0
void URL::set_host(String const& host)
{
// 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
if (m_url.cannot_be_a_base_url())
return;
// 2. Basic URL parse the given value with this’s URL as url and host state as state override.
auto result_url = URLParser::parse(host, {}, m_url, URLParser::State::Host);
if (result_url.is_valid())
m_url = move(result_url);
}
// https://url.spec.whatwg.org/#dom-url-hostname
WebIDL::ExceptionOr<String> URL::hostname() const
{
auto& vm = realm().vm();
// 1. If this’s URL’s host is null, then return the empty string.
if (m_url.host().is_null())
return String {};
// 2. Return this’s URL’s host, serialized.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.host()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
void URL::set_hostname(String const& hostname)
{
// 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
if (m_url.cannot_be_a_base_url())
return;
// 2. Basic URL parse the given value with this’s URL as url and hostname state as state override.
auto result_url = URLParser::parse(hostname, {}, m_url, URLParser::State::Hostname);
if (result_url.is_valid())
m_url = move(result_url);
}
// https://url.spec.whatwg.org/#ref-for-dom-url-hostname①
WebIDL::ExceptionOr<String> URL::port() const
{
auto& vm = realm().vm();
// 1. If this’s URL’s port is null, then return the empty string.
if (!m_url.port().has_value())
return String {};
// 2. Return this’s URL’s port, serialized.
return TRY_OR_THROW_OOM(vm, String::formatted("{}", *m_url.port()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-port%E2%91%A0
void URL::set_port(String const& port)
{
// 1. If this’s URL cannot have a username/password/port, then return.
if (m_url.cannot_have_a_username_or_password_or_port())
return;
// 2. If the given value is the empty string, then set this’s URL’s port to null.
if (port.is_empty()) {
m_url.set_port({});
}
// 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override.
else {
auto result_url = URLParser::parse(port, {}, m_url, URLParser::State::Port);
if (result_url.is_valid())
m_url = move(result_url);
}
}
// https://url.spec.whatwg.org/#dom-url-pathname
WebIDL::ExceptionOr<String> URL::pathname() const
{
auto& vm = realm().vm();
// The pathname getter steps are to return the result of URL path serializing this’s URL.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_url.serialize_path()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-pathname%E2%91%A0
void URL::set_pathname(String const& pathname)
{
// FIXME: These steps no longer match the speci.
// 1. If this’s URL’s cannot-be-a-base-URL is true, then return.
if (m_url.cannot_be_a_base_url())
return;
// 2. Empty this’s URL’s path.
auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the path change if the parse failed.
url.set_paths({});
// 3. Basic URL parse the given value with this’s URL as url and path start state as state override.
auto result_url = URLParser::parse(pathname, {}, move(url), URLParser::State::PathStart);
if (result_url.is_valid())
m_url = move(result_url);
}
// https://url.spec.whatwg.org/#dom-url-search
WebIDL::ExceptionOr<String> URL::search() const
{
auto& vm = realm().vm();
// 1. If this’s URL’s query is either null or the empty string, then return the empty string.
if (m_url.query().is_null() || m_url.query().is_empty())
return String {};
// 2. Return U+003F (?), followed by this’s URL’s query.
return TRY_OR_THROW_OOM(vm, String::formatted("?{}", m_url.query()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-search%E2%91%A0
WebIDL::ExceptionOr<void> URL::set_search(String const& search)
{
auto& vm = realm().vm();
// 1. Let url be this’s URL.
auto& url = m_url;
// 2. If the given value is the empty string:
if (search.is_empty()) {
// 1. Set url’s query to null.
url.set_query({});
// 2. Empty this’s query object’s list.
m_query->m_list.clear();
// FIXME: 3. Potentially strip trailing spaces from an opaque path with this.
// 4. Return.
return {};
}
// 3. Let input be the given value with a single leading U+003F (?) removed, if any.
auto search_as_string_view = search.bytes_as_string_view();
auto input = search_as_string_view.substring_view(search_as_string_view.starts_with('?'));
// 4. Set url’s query to the empty string.
auto url_copy = url; // We copy the URL here to follow other browser's behavior of reverting the search change if the parse failed.
url_copy.set_query(DeprecatedString::empty());
// 5. Basic URL parse input with url as url and query state as state override.
auto result_url = URLParser::parse(input, {}, move(url_copy), URLParser::State::Query);
if (result_url.is_valid()) {
m_url = move(result_url);
// 6. Set this’s query object’s list to the result of parsing input.
m_query->m_list = TRY_OR_THROW_OOM(vm, url_decode(input));
}
return {};
}
// https://url.spec.whatwg.org/#dom-url-searchparams
JS::NonnullGCPtr<URLSearchParams const> URL::search_params() const
{
// The searchParams getter steps are to return this’s query object.
return m_query;
}
// https://url.spec.whatwg.org/#dom-url-hash
WebIDL::ExceptionOr<String> URL::hash() const
{
auto& vm = realm().vm();
// 1. If this’s URL’s fragment is either null or the empty string, then return the empty string.
if (m_url.fragment().is_null() || m_url.fragment().is_empty())
return String {};
// 2. Return U+0023 (#), followed by this’s URL’s fragment.
return TRY_OR_THROW_OOM(vm, String::formatted("#{}", m_url.fragment()));
}
// https://url.spec.whatwg.org/#ref-for-dom-url-hash%E2%91%A0
void URL::set_hash(String const& hash)
{
// 1. If the given value is the empty string:
if (hash.is_empty()) {
// 1. Set this’s URL’s fragment to null.
m_url.set_fragment({});
// FIXME: 2. Potentially strip trailing spaces from an opaque path with this.
// 3. Return.
return;
}
// 2. Let input be the given value with a single leading U+0023 (#) removed, if any.
auto hash_as_string_view = hash.bytes_as_string_view();
auto input = hash_as_string_view.substring_view(hash_as_string_view.starts_with('#'));
// 3. Set this’s URL’s fragment to the empty string.
auto url = m_url; // We copy the URL here to follow other browser's behavior of reverting the hash change if the parse failed.
url.set_fragment(DeprecatedString::empty());
// 4. Basic URL parse input with this’s URL as url and fragment state as state override.
auto result_url = URLParser::parse(input, {}, move(url), URLParser::State::Fragment);
if (result_url.is_valid())
m_url = move(result_url);
}
// https://url.spec.whatwg.org/#concept-url-origin
HTML::Origin url_origin(AK::URL const& url)
{
// FIXME: We should probably have an extended version of AK::URL for LibWeb instead of standalone functions like this.
// The origin of a URL url is the origin returned by running these steps, switching on url’s scheme:
// -> "blob"
if (url.scheme() == "blob"sv) {
// FIXME: Support 'blob://' URLs
return HTML::Origin {};
}
// -> "ftp"
// -> "http"
// -> "https"
// -> "ws"
// -> "wss"
if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
// Return the tuple origin (url’s scheme, url’s host, url’s port, null).
return HTML::Origin(url.scheme(), url.host(), url.port().value_or(0));
}
// -> "file"
if (url.scheme() == "file"sv) {
// Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
// Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
return HTML::Origin(url.scheme(), DeprecatedString(), 0);
}
// -> Otherwise
// Return a new opaque origin.
return HTML::Origin {};
}
// https://url.spec.whatwg.org/#concept-domain
bool host_is_domain(StringView host)
{
// A domain is a non-empty ASCII string that identifies a realm within a network.
return !host.is_empty()
&& !IPv4Address::from_string(host).has_value()
&& !IPv6Address::from_string(host).has_value();
}
}
|