Skip to content

Commit

Permalink
url: remove unused URL::ToFilePath()
Browse files Browse the repository at this point in the history
PR-URL: #46487
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
anonrig authored and MylesBorins committed Feb 18, 2023
1 parent 9a604d6 commit 129c9e7
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 95 deletions.
62 changes: 0 additions & 62 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1847,68 +1847,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(DomainToUnicode);
}

std::string URL::ToFilePath() const {
if (context_.scheme != "file:") {
return "";
}

#ifdef _WIN32
const char* slash = "\\";
auto is_slash = [] (char ch) {
return ch == '/' || ch == '\\';
};
#else
const char* slash = "/";
auto is_slash = [] (char ch) {
return ch == '/';
};
if ((context_.flags & URL_FLAGS_HAS_HOST) &&
context_.host.length() > 0) {
return "";
}
#endif
std::string decoded_path;
for (const std::string& part : context_.path) {
std::string decoded = PercentDecode(part.c_str(), part.length());
for (char& ch : decoded) {
if (is_slash(ch)) {
return "";
}
}
decoded_path += slash + decoded;
}

#ifdef _WIN32
// TODO(TimothyGu): Use "\\?\" long paths on Windows.

// If hostname is set, then we have a UNC path. Pass the hostname through
// ToUnicode just in case it is an IDN using punycode encoding. We do not
// need to worry about percent encoding because the URL parser will have
// already taken care of that for us. Note that this only causes IDNs with an
// appropriate `xn--` prefix to be decoded.
if ((context_.flags & URL_FLAGS_HAS_HOST) &&
context_.host.length() > 0) {
std::string unicode_host;
if (!ToUnicode(context_.host, &unicode_host)) {
return "";
}
return "\\\\" + unicode_host + decoded_path;
}
// Otherwise, it's a local path that requires a drive letter.
if (decoded_path.length() < 3) {
return "";
}
if (decoded_path[2] != ':' ||
!IsASCIIAlpha(decoded_path[1])) {
return "";
}
// Strip out the leading '\'.
return decoded_path.substr(1);
#else
return decoded_path;
#endif
}

URL URL::FromFilePath(const std::string& file_path) {
URL url("file://");
std::string escaped_file_path;
Expand Down
3 changes: 0 additions & 3 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ class URL {
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;
// Get the file URL from native file system path.
static URL FromFilePath(const std::string& file_path);

Expand Down
30 changes: 0 additions & 30 deletions test/cctest/test_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,6 @@ TEST_F(URLTest, TruncatedAfterProtocol2) {
EXPECT_EQ(simple.path(), "");
}

TEST_F(URLTest, ToFilePath) {
#define T(url, path) EXPECT_EQ(path, URL(url).ToFilePath())
T("http://example.org/foo/bar", "");

#ifdef _WIN32
T("file:///C:/Program%20Files/", "C:\\Program Files\\");
T("file:///C:/a/b/c?query#fragment", "C:\\a\\b\\c");
T("file://host/path/a/b/c?query#fragment", "\\\\host\\path\\a\\b\\c");
#if defined(NODE_HAVE_I18N_SUPPORT)
T("file://xn--weird-prdj8vva.com/host/a", "\\\\wͪ͊eiͬ͋rd.com\\host\\a");
#else
T("file://xn--weird-prdj8vva.com/host/a",
"\\\\xn--weird-prdj8vva.com\\host\\a");
#endif
T("file:///C:/a%2Fb", "");
T("file:///", "");
T("file:///home", "");
#else
T("file:///", "/");
T("file:///home/user?query#fragment", "/home/user");
T("file:///home/user/?query#fragment", "/home/user/");
T("file:///home/user/%20space", "/home/user/ space");
T("file:///home/us%5Cer", "/home/us\\er");
T("file:///home/us%2Fer", "");
T("file://host/path", "");
#endif

#undef T
}

TEST_F(URLTest, FromFilePath) {
URL file_url;
#ifdef _WIN32
Expand Down

0 comments on commit 129c9e7

Please sign in to comment.