1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

AK: Avoid unnecessary String allocations for URL username and password

Previously, `URLParser` was constructing a new String for every
character of the URL's username and password. This change improves
performance by eliminating those unnecessary String allocations.

A URL with a 100,000 character password can now be parsed in ~30ms vs
~8 seconds previously on my machine.
This commit is contained in:
Tim Ledbetter 2023-10-28 06:26:20 +01:00 committed by Andreas Kling
parent 1ce422db08
commit 2a1fc96650
2 changed files with 56 additions and 8 deletions

View file

@ -542,3 +542,44 @@ TEST_CASE(ipv4_address)
EXPECT(!url.is_valid());
}
}
TEST_CASE(username_and_password)
{
{
constexpr auto url_with_username_and_password = "http://username:password@test.com/index.html"sv;
URL url(url_with_username_and_password);
EXPECT(url.is_valid());
EXPECT_EQ(MUST(url.serialized_host()), "test.com"sv);
EXPECT_EQ(MUST(url.username()), "username"sv);
EXPECT_EQ(MUST(url.password()), "password"sv);
}
{
constexpr auto url_with_percent_encoded_credentials = "http://username%21%24%25:password%21%24%25@test.com/index.html"sv;
URL url(url_with_percent_encoded_credentials);
EXPECT(url.is_valid());
EXPECT_EQ(MUST(url.serialized_host()), "test.com"sv);
EXPECT_EQ(MUST(url.username()), "username!$%"sv);
EXPECT_EQ(MUST(url.password()), "password!$%"sv);
}
{
auto const& username = MUST(String::repeated('a', 50000));
auto const& url_with_long_username = MUST(String::formatted("http://{}:@test.com/index.html", username));
URL url(url_with_long_username);
EXPECT(url.is_valid());
EXPECT_EQ(MUST(url.serialized_host()), "test.com"sv);
EXPECT_EQ(MUST(url.username()), username);
EXPECT(MUST(url.password()).is_empty());
}
{
auto const& password = MUST(String::repeated('a', 50000));
auto const& url_with_long_password = MUST(String::formatted("http://:{}@test.com/index.html", password));
URL url(url_with_long_password);
EXPECT(url.is_valid());
EXPECT_EQ(MUST(url.serialized_host()), "test.com"sv);
EXPECT(MUST(url.username()).is_empty());
EXPECT_EQ(MUST(url.password()), password);
}
}