diff --git a/src/node_url.cc b/src/node_url.cc index b8edc0c21d7e5b..5d420240645e78 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -1859,68 +1859,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(SetURLConstructor); } -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; diff --git a/src/node_url.h b/src/node_url.h index d7b9a1c368cdae..c6802df54c83d1 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -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); diff --git a/test/cctest/test_url.cc b/test/cctest/test_url.cc index f2430b3d506ac1..080129b3ddd1ab 100644 --- a/test/cctest/test_url.cc +++ b/test/cctest/test_url.cc @@ -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