Skip to content

Commit

Permalink
src: added URL::FromFilePath method
Browse files Browse the repository at this point in the history
Method returns file URL from native file path.

Backport-PR-URL: #22918
PR-URL: #22251
Reviewed-By: Eugene Ostroukhov <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
alexkozy authored and targos committed Sep 25, 2018

Partially verified

This commit is signed with the committer’s verified signature.
gsmet’s contribution has been verified via GPG key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent e668815 commit fa83382
Showing 3 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
@@ -2348,6 +2348,19 @@ std::string URL::ToFilePath() const {
#endif
}

URL URL::FromFilePath(const std::string& file_path) {
URL url("file://");
std::string escaped_file_path;
for (size_t i = 0; i < file_path.length(); ++i) {
escaped_file_path += file_path[i];
if (file_path[i] == '%')
escaped_file_path += "25";
}
URL::Parse(escaped_file_path.c_str(), escaped_file_path.length(), kPathStart,
&url.context_, true, nullptr, false);
return url;
}

// This function works by calling out to a JS function that creates and
// returns the JS URL object. Be mindful of the JS<->Native boundary
// crossing that is required.
2 changes: 2 additions & 0 deletions src/node_url.h
Original file line number Diff line number Diff line change
@@ -169,6 +169,8 @@ class URL {
// 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);

const Local<Value> ToObject(Environment* env) const;

33 changes: 33 additions & 0 deletions test/cctest/test_url.cc
Original file line number Diff line number Diff line change
@@ -109,3 +109,36 @@ TEST_F(URLTest, ToFilePath) {

#undef T
}

TEST_F(URLTest, FromFilePath) {
URL file_url;
#ifdef _WIN32
file_url = URL::FromFilePath("C:\\Program Files\\");
EXPECT_EQ("file:", file_url.protocol());
EXPECT_EQ("/C:/Program%20Files/", file_url.path());

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

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

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

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

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

0 comments on commit fa83382

Please sign in to comment.