1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00

LibWeb: Add interface for 'concept-url-parser'

This does not implement extra functionality on top of the basic parser,
but allows multiple places in LibWeb to call the 'correct' interface for
when it is fully implemented.
This commit is contained in:
Shannon Booth 2023-07-15 14:24:58 +12:00 committed by Andreas Kling
parent 7ef4689383
commit 6fecd8cc44
6 changed files with 37 additions and 11 deletions

View file

@ -6,7 +6,6 @@
#include <AK/Debug.h>
#include <AK/TypeCasts.h>
#include <AK/URLParser.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/VM.h>
@ -14,6 +13,7 @@
#include <LibWeb/Fetch/Infrastructure/FetchParams.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/URL/URL.h>
namespace Web::Fetch::Infrastructure {
@ -113,7 +113,7 @@ ErrorOr<Optional<AK::URL>> Response::location_url(Optional<String> const& reques
return Optional<AK::URL> {};
// 3. If location is a header value, then set location to the result of parsing location with responses URL.
auto location = AK::URLParser::parse(location_values.first(), url());
auto location = URL::parse(location_values.first(), url());
if (!location.is_valid())
return Error::from_string_view("Invalid 'Location' header URL"sv);

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URLParser.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/RequestPrototype.h>
@ -18,6 +17,7 @@
#include <LibWeb/Fetch/Request.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWeb/URL/URL.h>
namespace Web::Fetch {
@ -121,7 +121,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
// 5. If input is a string, then:
if (input.has<String>()) {
// 1. Let parsedURL be the result of parsing input with baseURL.
auto parsed_url = URLParser::parse(input.get<String>(), base_url);
auto parsed_url = URL::parse(input.get<String>(), base_url);
// 2. If parsedURL is failure, then throw a TypeError.
if (!parsed_url.is_valid())
@ -299,7 +299,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
// 3. Otherwise:
else {
// 1. Let parsedReferrer be the result of parsing referrer with baseURL.
auto parsed_referrer = URLParser::parse(referrer, base_url);
auto parsed_referrer = URL::parse(referrer, base_url);
// 2. If parsedReferrer is failure, then throw a TypeError.
if (!parsed_referrer.is_valid())

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URLParser.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MainThreadVM.h>
@ -15,6 +14,7 @@
#include <LibWeb/Fetch/Response.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/Infra/JSON.h>
#include <LibWeb/URL/URL.h>
namespace Web::Fetch {
@ -174,7 +174,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(JS::VM& vm, S
// 1. Let parsedURL be the result of parsing url with current settings objects API base URL.
auto api_base_url = HTML::current_settings_object().api_base_url();
auto parsed_url = URLParser::parse(url, api_base_url);
auto parsed_url = URL::parse(url, api_base_url);
// 2. If parsedURL is failure, then throw a TypeError.
if (!parsed_url.is_valid())

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URLParser.h>
#include <LibJS/Runtime/ModuleRequest.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/DOM/Document.h>
@ -25,6 +24,7 @@
#include <LibWeb/Loader/LoadRequest.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/URL/URL.h>
namespace Web::HTML {
@ -170,7 +170,7 @@ WebIDL::ExceptionOr<Optional<AK::URL>> resolve_imports_match(DeprecatedString co
VERIFY(resolution_result->serialize().ends_with("/"sv));
// 5. Let url be the result of URL parsing afterPrefix with resolutionResult.
auto url = URLParser::parse(after_prefix, *resolution_result);
auto url = URL::parse(after_prefix, *resolution_result);
// 6. If url is failure, then throw a TypeError indicating that resolution of normalizedSpecifier was blocked since the afterPrefix portion
// could not be URL-parsed relative to the resolutionResult mapped to by the specifierKey prefix.
@ -200,7 +200,7 @@ Optional<AK::URL> resolve_url_like_module_specifier(DeprecatedString const& spec
// 1. If specifier starts with "/", "./", or "../", then:
if (specifier.starts_with("/"sv) || specifier.starts_with("./"sv) || specifier.starts_with("../"sv)) {
// 1. Let url be the result of URL parsing specifier with baseURL.
auto url = URLParser::parse(specifier, base_url);
auto url = URL::parse(specifier, base_url);
// 2. If url is failure, then return null.
if (!url.is_valid())
@ -211,7 +211,7 @@ Optional<AK::URL> resolve_url_like_module_specifier(DeprecatedString const& spec
}
// 2. Let url be the result of URL parsing specifier (with no base URL).
auto url = URLParser::parse(specifier);
auto url = URL::parse(specifier);
// 3. If url is failure, then return null.
if (!url.is_valid())

View file

@ -490,4 +490,27 @@ bool host_is_domain(StringView host)
&& !IPv6Address::from_string(host).has_value();
}
// https://url.spec.whatwg.org/#concept-url-parser
AK::URL parse(StringView input, Optional<AK::URL> const& base_url)
{
// FIXME: We should probably have an extended version of AK::URL for LibWeb instead of standalone functions like this.
// 1. Let url be the result of running the basic URL parser on input with base and encoding.
auto url = URLParser::basic_parse(input, base_url);
// 2. If url is failure, return failure.
if (url.is_valid())
return {};
// 3. If urls scheme is not "blob",
if (url.scheme() != "blob")
return url;
// FIXME: 4. Set urls blob URL entry to the result of resolving the blob URL url,
// FIXME: 5. if that did not return failure, and null otherwise.
// 6. Return url
return url;
}
}

View file

@ -77,4 +77,7 @@ private:
HTML::Origin url_origin(AK::URL const&);
bool host_is_domain(StringView host);
// https://url.spec.whatwg.org/#concept-url-parser
AK::URL parse(StringView input, Optional<AK::URL> const& base_url = {});
}