Skip to content

Commit

Permalink
src: integrate URL::href() and use in inspector
Browse files Browse the repository at this point in the history
PR-URL: #35912
Refs: #22610
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
watilde authored and codebytere committed Nov 22, 2020
1 parent 8bbdbcc commit 6d1b1c7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
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
}

0 comments on commit 6d1b1c7

Please sign in to comment.