Skip to content

Commit 6d1b1c7

Browse files
watildecodebytere
authored andcommitted
src: integrate URL::href() and use in inspector
PR-URL: #35912 Refs: #22610 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 8bbdbcc commit 6d1b1c7

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/inspector_agent.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,7 @@ class NodeInspectorClient : public V8InspectorClient {
638638
if (!IsFilePath(resource_name))
639639
return nullptr;
640640
node::url::URL url = node::url::URL::FromFilePath(resource_name);
641-
// TODO(ak239spb): replace this code with url.href().
642-
// Refs: https://github.com/nodejs/node/issues/22610
643-
return Utf8ToStringView(url.protocol() + "/" + url.path());
641+
return Utf8ToStringView(url.href());
644642
}
645643

646644
node::Environment* env_;

src/node_url.cc

+42
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,48 @@ void URL::Parse(const char* input,
20812081
}
20822082
} // NOLINT(readability/fn_size)
20832083

2084+
// https://url.spec.whatwg.org/#url-serializing
2085+
std::string URL::SerializeURL(const struct url_data* url,
2086+
bool exclude = false) {
2087+
std::string output = url->scheme;
2088+
if (url->flags & URL_FLAGS_HAS_HOST) {
2089+
output += "//";
2090+
if (url->flags & URL_FLAGS_HAS_USERNAME ||
2091+
url->flags & URL_FLAGS_HAS_PASSWORD) {
2092+
if (url->flags & URL_FLAGS_HAS_USERNAME) {
2093+
output += url->username;
2094+
}
2095+
if (url->flags & URL_FLAGS_HAS_PASSWORD) {
2096+
output += ":" + url->password;
2097+
}
2098+
output += "@";
2099+
}
2100+
output += url->host;
2101+
if (url->port != -1) {
2102+
output += ":" + std::to_string(url->port);
2103+
}
2104+
}
2105+
if (url->flags & URL_FLAGS_CANNOT_BE_BASE) {
2106+
output += url->path[0];
2107+
} else {
2108+
if (!(url->flags & URL_FLAGS_HAS_HOST) &&
2109+
url->path.size() > 1 &&
2110+
url->path[0].empty()) {
2111+
output += "/.";
2112+
}
2113+
for (size_t i = 1; i < url->path.size(); i++) {
2114+
output += "/" + url->path[i];
2115+
}
2116+
}
2117+
if (url->flags & URL_FLAGS_HAS_QUERY) {
2118+
output = "?" + url->query;
2119+
}
2120+
if (!exclude && url->flags & URL_FLAGS_HAS_FRAGMENT) {
2121+
output = "#" + url->fragment;
2122+
}
2123+
return output;
2124+
}
2125+
20842126
namespace {
20852127
void SetArgs(Environment* env,
20862128
Local<Value> argv[ARG_COUNT],

src/node_url.h

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct url_data {
7171
std::string query;
7272
std::string fragment;
7373
std::vector<std::string> path;
74+
std::string href;
7475
};
7576

7677
class URL {
@@ -83,6 +84,8 @@ class URL {
8384
const struct url_data* base,
8485
bool has_base);
8586

87+
static std::string SerializeURL(const struct url_data* url, bool exclude);
88+
8689
URL(const char* input, const size_t len) {
8790
Parse(input, len, kUnknownState, &context_, false, nullptr, false);
8891
}
@@ -160,6 +163,10 @@ class URL {
160163
return ret;
161164
}
162165

166+
std::string href() const {
167+
return SerializeURL(&context_, false);
168+
}
169+
163170
// Get the path of the file: URL in a format consumable by native file system
164171
// APIs. Returns an empty string if something went wrong.
165172
std::string ToFilePath() const;

test/cctest/test_url.cc

+6
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,31 @@ TEST_F(URLTest, FromFilePath) {
137137
file_url = URL::FromFilePath("C:\\Program Files\\");
138138
EXPECT_EQ("file:", file_url.protocol());
139139
EXPECT_EQ("//C:/Program%20Files/", file_url.path());
140+
EXPECT_EQ("file:///C:/Program%20Files/", file_url.href());
140141

141142
file_url = URL::FromFilePath("C:\\a\\b\\c");
142143
EXPECT_EQ("file:", file_url.protocol());
143144
EXPECT_EQ("//C:/a/b/c", file_url.path());
145+
EXPECT_EQ("file:///C:/a/b/c", file_url.href());
144146

145147
file_url = URL::FromFilePath("b:\\a\\%%.js");
146148
EXPECT_EQ("file:", file_url.protocol());
147149
EXPECT_EQ("//b:/a/%25%25.js", file_url.path());
150+
EXPECT_EQ("file:///b:/a/%25%25.js", file_url.href());
148151
#else
149152
file_url = URL::FromFilePath("/");
150153
EXPECT_EQ("file:", file_url.protocol());
151154
EXPECT_EQ("//", file_url.path());
155+
EXPECT_EQ("file:///", file_url.href());
152156

153157
file_url = URL::FromFilePath("/a/b/c");
154158
EXPECT_EQ("file:", file_url.protocol());
155159
EXPECT_EQ("//a/b/c", file_url.path());
160+
EXPECT_EQ("file:///a/b/c", file_url.href());
156161

157162
file_url = URL::FromFilePath("/a/%%.js");
158163
EXPECT_EQ("file:", file_url.protocol());
159164
EXPECT_EQ("//a/%25%25.js", file_url.path());
165+
EXPECT_EQ("file:///a/%25%25.js", file_url.href());
160166
#endif
161167
}

0 commit comments

Comments
 (0)