Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
src: integrate URL::href() and use in inspector
  • Loading branch information
watilde committed Nov 1, 2020
commit 0e37d9e03d9b2581c783cf101ed2542e438cbba9
4 changes: 1 addition & 3 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,7 @@ class NodeInspectorClient : public V8InspectorClient {
if (!IsFilePath(resource_name))
return nullptr;
node::url::URL url = node::url::URL::FromFilePath(resource_name);
// TODO(ak239spb): replace this code with url.href().
// Refs: https://github.com/nodejs/node/issues/22610
return Utf8ToStringView(url.protocol() + "/" + url.path());
return Utf8ToStringView(url.href());
}

node::Environment* env_;
Expand Down
42 changes: 42 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,48 @@ 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,
bool exclude = false) {
std::string 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_PASSWORD) {
output += ":" + url->password;
}
output += "@";
}
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];
} else {
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];
}
}
if (url->flags & URL_FLAGS_HAS_QUERY) {
output = "?" + url->query;
}
if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) {
output = "#" + url->fragment;
}
return output;
}

namespace {
void SetArgs(Environment* env,
Local<Value> argv[ARG_COUNT],
Expand Down
7 changes: 7 additions & 0 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct url_data {
std::string query;
std::string fragment;
std::vector<std::string> path;
std::string href;
};

class URL {
Expand All @@ -83,6 +84,8 @@ class URL {
const struct url_data* base,
bool has_base);

static std::string SerializeURL(const struct 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 @@ -160,6 +163,10 @@ class URL {
return ret;
}

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

// Get the path of the file: URL in a format consumable by native file system
// APIs. Returns an empty string if something went wrong.
std::string ToFilePath() const;
Expand Down
6 changes: 6 additions & 0 deletions test/cctest/test_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,31 @@ TEST_F(URLTest, FromFilePath) {
file_url = URL::FromFilePath("C:\\Program Files\\");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("//C:/Program%20Files/", file_url.path());
EXPECT_EQ("file:///C:/Program%20Files/", file_url.href());

file_url = URL::FromFilePath("C:\\a\\b\\c");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("//C:/a/b/c", file_url.path());
EXPECT_EQ("file:///C:/a/b/c", file_url.href());

file_url = URL::FromFilePath("b:\\a\\%%.js");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("//b:/a/%25%25.js", file_url.path());
EXPECT_EQ("file:///b:/a/%25%25.js", file_url.href());
#else
file_url = URL::FromFilePath("/");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("//", file_url.path());
EXPECT_EQ("file:///", file_url.href());

file_url = URL::FromFilePath("/a/b/c");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("//a/b/c", file_url.path());
EXPECT_EQ("file:///a/b/c", file_url.href());

file_url = URL::FromFilePath("/a/%%.js");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("//a/%25%25.js", file_url.path());
EXPECT_EQ("file:///a/%25%25.js", file_url.href());
#endif
}