LibURL: Make percent_encode return a String

This simplifies a bunch of places which were needing to error check and
convert from a ByteString to String.
This commit is contained in:
Shannon Booth 2024-08-10 13:12:19 +12:00 committed by Andreas Kling
commit 84a7fead0e
Notes: github-actions[bot] 2024-08-10 08:47:40 +00:00
9 changed files with 23 additions and 26 deletions

View file

@ -75,7 +75,7 @@ static Optional<Host> parse_opaque_host(StringView input)
// currently report validation errors, they are only useful for debugging efforts in the URL parsing code. // currently report validation errors, they are only useful for debugging efforts in the URL parsing code.
// 4. Return the result of running UTF-8 percent-encode on input using the C0 control percent-encode set. // 4. Return the result of running UTF-8 percent-encode on input using the C0 control percent-encode set.
return String::from_byte_string(percent_encode(input, PercentEncodeSet::C0Control)).release_value_but_fixme_should_propagate_errors(); return percent_encode(input, PercentEncodeSet::C0Control);
} }
struct ParsedIPv4Number { struct ParsedIPv4Number {

View file

@ -59,21 +59,19 @@ void URL::set_scheme(String scheme)
} }
// https://url.spec.whatwg.org/#set-the-username // https://url.spec.whatwg.org/#set-the-username
ErrorOr<void> URL::set_username(StringView username) void URL::set_username(StringView username)
{ {
// To set the username given a url and username, set urls username to the result of running UTF-8 percent-encode on username using the userinfo percent-encode set. // To set the username given a url and username, set urls username to the result of running UTF-8 percent-encode on username using the userinfo percent-encode set.
m_data->username = TRY(String::from_byte_string(percent_encode(username, PercentEncodeSet::Userinfo))); m_data->username = percent_encode(username, PercentEncodeSet::Userinfo);
m_data->valid = compute_validity(); m_data->valid = compute_validity();
return {};
} }
// https://url.spec.whatwg.org/#set-the-password // https://url.spec.whatwg.org/#set-the-password
ErrorOr<void> URL::set_password(StringView password) void URL::set_password(StringView password)
{ {
// To set the password given a url and password, set urls password to the result of running UTF-8 percent-encode on password using the userinfo percent-encode set. // To set the password given a url and password, set urls password to the result of running UTF-8 percent-encode on password using the userinfo percent-encode set.
m_data->password = TRY(String::from_byte_string(percent_encode(password, PercentEncodeSet::Userinfo))); m_data->password = percent_encode(password, PercentEncodeSet::Userinfo);
m_data->valid = compute_validity(); m_data->valid = compute_validity();
return {};
} }
void URL::set_host(Host host) void URL::set_host(Host host)
@ -103,13 +101,13 @@ void URL::set_paths(Vector<ByteString> const& paths)
m_data->paths.clear_with_capacity(); m_data->paths.clear_with_capacity();
m_data->paths.ensure_capacity(paths.size()); m_data->paths.ensure_capacity(paths.size());
for (auto const& segment : paths) for (auto const& segment : paths)
m_data->paths.unchecked_append(String::from_byte_string(percent_encode(segment, PercentEncodeSet::Path)).release_value_but_fixme_should_propagate_errors()); m_data->paths.unchecked_append(percent_encode(segment, PercentEncodeSet::Path));
m_data->valid = compute_validity(); m_data->valid = compute_validity();
} }
void URL::append_path(StringView path) void URL::append_path(StringView path)
{ {
m_data->paths.append(String::from_byte_string(percent_encode(path, PercentEncodeSet::Path)).release_value_but_fixme_should_propagate_errors()); m_data->paths.append(percent_encode(path, PercentEncodeSet::Path));
} }
// https://url.spec.whatwg.org/#cannot-have-a-username-password-port // https://url.spec.whatwg.org/#cannot-have-a-username-password-port
@ -444,7 +442,7 @@ void append_percent_encoded_if_necessary(StringBuilder& builder, u32 code_point,
builder.append_code_point(code_point); builder.append_code_point(code_point);
} }
ByteString percent_encode(StringView input, PercentEncodeSet set, SpaceAsPlus space_as_plus) String percent_encode(StringView input, PercentEncodeSet set, SpaceAsPlus space_as_plus)
{ {
StringBuilder builder; StringBuilder builder;
for (auto code_point : Utf8View(input)) { for (auto code_point : Utf8View(input)) {
@ -453,7 +451,7 @@ ByteString percent_encode(StringView input, PercentEncodeSet set, SpaceAsPlus sp
else else
append_percent_encoded_if_necessary(builder, code_point, set); append_percent_encoded_if_necessary(builder, code_point, set);
} }
return builder.to_byte_string(); return MUST(builder.to_string());
} }
ByteString percent_decode(StringView input) ByteString percent_decode(StringView input)

View file

@ -70,7 +70,7 @@ enum class SpaceAsPlus {
No, No,
Yes, Yes,
}; };
ByteString percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No); String percent_encode(StringView input, PercentEncodeSet set = PercentEncodeSet::Userinfo, SpaceAsPlus = SpaceAsPlus::No);
ByteString percent_decode(StringView input); ByteString percent_decode(StringView input);
template<typename T> template<typename T>
@ -140,8 +140,8 @@ public:
bool is_special() const { return is_special_scheme(m_data->scheme); } bool is_special() const { return is_special_scheme(m_data->scheme); }
void set_scheme(String); void set_scheme(String);
ErrorOr<void> set_username(StringView); void set_username(StringView);
ErrorOr<void> set_password(StringView); void set_password(StringView);
void set_host(Host); void set_host(Host);
void set_port(Optional<u16>); void set_port(Optional<u16>);
void set_paths(Vector<ByteString> const&); void set_paths(Vector<ByteString> const&);

View file

@ -260,7 +260,7 @@ void DOMURL::set_username(String const& username)
return; return;
// 2. Set the username given thiss URL and the given value. // 2. Set the username given thiss URL and the given value.
MUST(m_url.set_username(username)); m_url.set_username(username);
} }
// https://url.spec.whatwg.org/#dom-url-password // https://url.spec.whatwg.org/#dom-url-password
@ -278,7 +278,7 @@ void DOMURL::set_password(String const& password)
return; return;
// 2. Set the password given thiss URL and the given value. // 2. Set the password given thiss URL and the given value.
MUST(m_url.set_password(password)); m_url.set_password(password);
} }
// https://url.spec.whatwg.org/#dom-url-host // https://url.spec.whatwg.org/#dom-url-host

View file

@ -2024,10 +2024,10 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fet
auto password = ByteString::empty(); auto password = ByteString::empty();
// 3. Set the username given requests current URL and username. // 3. Set the username given requests current URL and username.
MUST(request->current_url().set_username(username)); request->current_url().set_username(username);
// 4. Set the password given requests current URL and password. // 4. Set the password given requests current URL and password.
MUST(request->current_url().set_password(password)); request->current_url().set_password(password);
} }
// 4. Set response to the result of running HTTP-network-or-cache fetch given fetchParams and true. // 4. Set response to the result of running HTTP-network-or-cache fetch given fetchParams and true.

View file

@ -845,8 +845,7 @@ ErrorOr<void> HTMLFormElement::mail_as_body(URL::URL parsed_action, Vector<XHR::
// 2. Set body to the result of running UTF-8 percent-encode on body using the default encode set. [URL] // 2. Set body to the result of running UTF-8 percent-encode on body using the default encode set. [URL]
// NOTE: body is already UTF-8 encoded due to using AK::String, so we only have to do the percent encoding. // NOTE: body is already UTF-8 encoded due to using AK::String, so we only have to do the percent encoding.
// NOTE: "default encode set" links to "path percent-encode-set": https://url.spec.whatwg.org/#default-encode-set // NOTE: "default encode set" links to "path percent-encode-set": https://url.spec.whatwg.org/#default-encode-set
auto percent_encoded_body = URL::percent_encode(body, URL::PercentEncodeSet::Path); body = URL::percent_encode(body, URL::PercentEncodeSet::Path);
body = TRY(String::from_utf8(percent_encoded_body.view()));
break; break;
} }
default: default:

View file

@ -114,7 +114,7 @@ void HTMLHyperlinkElementUtils::set_username(StringView username)
return; return;
// 4. Set the username given thiss URL and the given value. // 4. Set the username given thiss URL and the given value.
MUST(url->set_username(username)); url->set_username(username);
// 5. Update href. // 5. Update href.
update_href(); update_href();
@ -151,7 +151,7 @@ void HTMLHyperlinkElementUtils::set_password(StringView password)
return; return;
// 4. Set the password, given url and the given value. // 4. Set the password, given url and the given value.
MUST(url->set_password(password)); url->set_password(password);
// 5. Update href. // 5. Update href.
update_href(); update_href();

View file

@ -210,10 +210,10 @@ Optional<URL::URL> strip_url_for_use_as_referrer(Optional<URL::URL> url, OriginO
return {}; return {};
// 3. Set urls username to the empty string. // 3. Set urls username to the empty string.
MUST(url->set_username(""sv)); url->set_username(""sv);
// 4. Set urls password to the empty string. // 4. Set urls password to the empty string.
MUST(url->set_password(""sv)); url->set_password(""sv);
// 5. Set urls fragment to null. // 5. Set urls fragment to null.
url->set_fragment({}); url->set_fragment({});

View file

@ -495,10 +495,10 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method_string, Stri
if (!parsed_url.host().has<Empty>()) { if (!parsed_url.host().has<Empty>()) {
// 1. If the username argument is not null, set the username given parsedURL and username. // 1. If the username argument is not null, set the username given parsedURL and username.
if (username.has_value()) if (username.has_value())
MUST(parsed_url.set_username(username.value())); parsed_url.set_username(username.value());
// 2. If the password argument is not null, set the password given parsedURL and password. // 2. If the password argument is not null, set the password given parsedURL and password.
if (password.has_value()) if (password.has_value())
MUST(parsed_url.set_password(password.value())); parsed_url.set_password(password.value());
} }
// 9. If async is false, the current global object is a Window object, and either thiss timeout is // 9. If async is false, the current global object is a Window object, and either thiss timeout is