1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-29 12:17:36 +00:00

AK: Port URL username/password from DeprecatedString to String

And for cases that just need to check whether the password/username is
empty, add a raw_{password,username} helper to avoid any allocation.
This commit is contained in:
Shannon Booth 2023-08-12 16:52:38 +12:00 committed by Andrew Kaster
parent 6b29dc3e46
commit 55a01e72ca
11 changed files with 44 additions and 39 deletions

View file

@ -36,14 +36,14 @@ URL URL::complete_url(StringView relative_url) const
return URLParser::basic_parse(relative_url, *this);
}
DeprecatedString URL::username() const
ErrorOr<String> URL::username() const
{
return percent_decode(m_username);
return String::from_deprecated_string(percent_decode(m_username));
}
DeprecatedString URL::password() const
ErrorOr<String> URL::password() const
{
return percent_decode(m_password);
return String::from_deprecated_string(percent_decode(m_password));
}
DeprecatedString URL::path_segment_at_index(size_t index) const
@ -92,19 +92,21 @@ void URL::set_scheme(DeprecatedString scheme)
}
// https://url.spec.whatwg.org/#set-the-username
void URL::set_username(StringView username)
ErrorOr<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.
m_username = deprecated_string_percent_encode(username, PercentEncodeSet::Userinfo);
m_username = TRY(String::from_deprecated_string(deprecated_string_percent_encode(username, PercentEncodeSet::Userinfo)));
m_valid = compute_validity();
return {};
}
// https://url.spec.whatwg.org/#set-the-password
void URL::set_password(StringView password)
ErrorOr<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.
m_password = deprecated_string_percent_encode(password, PercentEncodeSet::Userinfo);
m_password = TRY(String::from_deprecated_string(deprecated_string_percent_encode(password, PercentEncodeSet::Userinfo)));
m_valid = compute_validity();
return {};
}
void URL::set_host(Host host)

View file

@ -77,8 +77,8 @@ public:
No
};
DeprecatedString const& scheme() const { return m_scheme; }
DeprecatedString username() const;
DeprecatedString password() const;
ErrorOr<String> username() const;
ErrorOr<String> password() const;
Host const& host() const { return m_host; }
ErrorOr<String> serialized_host() const;
DeprecatedString basename() const;
@ -98,8 +98,8 @@ public:
bool is_special() const { return is_special_scheme(m_scheme); }
void set_scheme(DeprecatedString);
void set_username(StringView);
void set_password(StringView);
ErrorOr<void> set_username(StringView);
ErrorOr<void> set_password(StringView);
void set_host(Host);
void set_port(Optional<u16>);
void set_paths(Vector<DeprecatedString> const&);
@ -151,6 +151,9 @@ public:
static bool code_point_is_in_percent_encode_set(u32 code_point, URL::PercentEncodeSet);
String const& raw_username() const { return m_username; }
String const& raw_password() const { return m_password; }
private:
bool compute_validity() const;
@ -163,10 +166,10 @@ private:
DeprecatedString m_scheme;
// A URLs username is an ASCII string identifying a username. It is initially the empty string.
DeprecatedString m_username;
String m_username;
// A URLs password is an ASCII string identifying a password. It is initially the empty string.
DeprecatedString m_password;
String m_password;
// A URLs host is null or a host. It is initially null.
Host m_host;

View file

@ -1102,15 +1102,15 @@ URL URLParser::basic_parse(StringView raw_input, Optional<URL> const& base_url,
builder.clear();
// 3. If passwordTokenSeen is true, then append encodedCodePoints to urls password.
if (password_token_seen) {
builder.append(url->password());
builder.append(url->m_password);
URL::append_percent_encoded_if_necessary(builder, c, URL::PercentEncodeSet::Userinfo);
url->m_password = builder.string_view();
url->m_password = builder.to_string().release_value_but_fixme_should_propagate_errors();
}
// 4. Otherwise, append encodedCodePoints to urls username.
else {
builder.append(url->username());
builder.append(url->m_username);
URL::append_percent_encoded_if_necessary(builder, c, URL::PercentEncodeSet::Userinfo);
url->m_username = builder.string_view();
url->m_username = builder.to_string().release_value_but_fixme_should_propagate_errors();
}
}