-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
src: fix and cleanup URL::SerializeURL #41759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b47430c
c9ea544
2fe8dcf
438849a
6552e41
08ec1d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
#include <cmath> | ||
#include <cstdio> | ||
#include <numeric> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
@@ -1545,44 +1546,61 @@ void URL::Parse(const char* input, | |
} // NOLINT(readability/fn_size) | ||
|
||
// https://url.spec.whatwg.org/#url-serializing | ||
std::string URL::SerializeURL(const struct url_data* url, | ||
std::string URL::SerializeURL(const url_data& url, | ||
bool exclude = false) { | ||
std::string output = url->scheme; | ||
if (url->flags & URL_FLAGS_HAS_HOST) { | ||
std::string output; | ||
output.reserve( | ||
10 + // We generally insert < 10 separator characters between URL parts | ||
url.scheme.size() + | ||
url.username.size() + | ||
url.password.size() + | ||
url.host.size() + | ||
url.query.size() + | ||
url.fragment.size() + | ||
url.href.size() + | ||
std::accumulate( | ||
url.path.begin(), | ||
url.path.end(), | ||
0, | ||
[](size_t sum, const auto& str) { return sum + str.size(); })); | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
output += url.scheme; | ||
if (url.flags & URL_FLAGS_HAS_HOST) { | ||
output += "//"; | ||
if (url->flags & URL_FLAGS_HAS_USERNAME || | ||
url->flags & URL_FLAGS_HAS_PASSWORD) { | ||
if (url->flags & URL_FLAGS_HAS_USERNAME) { | ||
output += url->username; | ||
if (url.flags & URL_FLAGS_HAS_USERNAME || | ||
url.flags & URL_FLAGS_HAS_PASSWORD) { | ||
if (url.flags & URL_FLAGS_HAS_USERNAME) { | ||
output += url.username; | ||
} | ||
if (url->flags & URL_FLAGS_HAS_PASSWORD) { | ||
output += ":" + url->password; | ||
if (url.flags & URL_FLAGS_HAS_PASSWORD) { | ||
output += ":" + url.password; | ||
} | ||
output += "@"; | ||
} | ||
output += url->host; | ||
if (url->port != -1) { | ||
output += ":" + std::to_string(url->port); | ||
output += url.host; | ||
if (url.port != -1) { | ||
output += ":" + std::to_string(url.port); | ||
} | ||
} | ||
if (url->flags & URL_FLAGS_CANNOT_BE_BASE) { | ||
output += url->path[0]; | ||
if (url.flags & URL_FLAGS_CANNOT_BE_BASE) { | ||
output += url.path[0]; | ||
} else { | ||
if (!(url->flags & URL_FLAGS_HAS_HOST) && | ||
url->path.size() > 1 && | ||
url->path[0].empty()) { | ||
if (!(url.flags & URL_FLAGS_HAS_HOST) && | ||
url.path.size() > 1 && | ||
url.path[0].empty()) { | ||
output += "/."; | ||
} | ||
for (size_t i = 1; i < url->path.size(); i++) { | ||
output += "/" + url->path[i]; | ||
for (size_t i = 1; i < url.path.size(); i++) { | ||
output += "/" + url.path[i]; | ||
} | ||
} | ||
if (url->flags & URL_FLAGS_HAS_QUERY) { | ||
output = "?" + url->query; | ||
if (url.flags & URL_FLAGS_HAS_QUERY) { | ||
output += "?" + url.query; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo indeed. Do we have tests for SerializeURL? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not explicitly, I think. And I don't quite know where to put regression tests for this either. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this something that should be added to https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json? |
||
} | ||
if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) { | ||
output = "#" + url->fragment; | ||
if (!exclude && (url.flags & URL_FLAGS_HAS_FRAGMENT)) { | ||
output += "#" + url.fragment; | ||
} | ||
output.shrink_to_fit(); | ||
return output; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.