Skip to content
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

src: fix and cleanup URL::SerializeURL #41759

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <cmath>
#include <cstdio>
#include <numeric>
#include <string>
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
#include <vector>

Expand Down Expand Up @@ -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 +
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo indeed. Do we have tests for SerializeURL?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) {
output = "#" + url->fragment;
if (!exclude && url.flags & URL_FLAGS_HAS_FRAGMENT) {
addaleax marked this conversation as resolved.
Show resolved Hide resolved
output += "#" + url.fragment;
}
output.shrink_to_fit();
return output;
}

Expand Down
6 changes: 3 additions & 3 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class URL {
const struct url_data* base,
bool has_base);

static std::string SerializeURL(const struct url_data* url, bool exclude);
static std::string SerializeURL(const url_data& url, bool exclude);

URL(const char* input, const size_t len) {
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
Expand Down Expand Up @@ -174,7 +174,7 @@ class URL {
}

std::string href() const {
return SerializeURL(&context_, false);
return SerializeURL(context_, false);
}

// Get the path of the file: URL in a format consumable by native file system
Expand All @@ -193,7 +193,7 @@ class URL {
URL() : URL("") {}

private:
struct url_data context_;
url_data context_;
};

} // namespace url
Expand Down