|
29 | 29 | #include "node_metadata.h" |
30 | 30 | #include "node_process-inl.h" |
31 | 31 | #include "node_stat_watcher.h" |
| 32 | +#include "node_url.h" |
32 | 33 | #include "permission/permission.h" |
33 | 34 | #include "util-inl.h" |
34 | 35 |
|
@@ -2847,123 +2848,6 @@ static void GetFormatOfExtensionlessFile( |
2847 | 2848 | return args.GetReturnValue().Set(EXTENSIONLESS_FORMAT_JAVASCRIPT); |
2848 | 2849 | } |
2849 | 2850 |
|
2850 | | -static bool FileURLToPath( |
2851 | | - Environment* env, |
2852 | | - const ada::url_aggregator& file_url, |
2853 | | - /* The linter can't detect the assign for result_file_path |
2854 | | - So we need to ignore since it suggest to put const */ |
2855 | | - // NOLINTNEXTLINE(runtime/references) |
2856 | | - std::string& result_file_path) { |
2857 | | - if (file_url.type != ada::scheme::FILE) { |
2858 | | - env->isolate()->ThrowException(ERR_INVALID_URL_SCHEME(env->isolate())); |
2859 | | - |
2860 | | - return false; |
2861 | | - } |
2862 | | - |
2863 | | - std::string_view pathname = file_url.get_pathname(); |
2864 | | -#ifdef _WIN32 |
2865 | | - size_t first_percent = std::string::npos; |
2866 | | - size_t pathname_size = pathname.size(); |
2867 | | - std::string pathname_escaped_slash; |
2868 | | - |
2869 | | - for (size_t i = 0; i < pathname_size; i++) { |
2870 | | - if (pathname[i] == '/') { |
2871 | | - pathname_escaped_slash += '\\'; |
2872 | | - } else { |
2873 | | - pathname_escaped_slash += pathname[i]; |
2874 | | - } |
2875 | | - |
2876 | | - if (pathname[i] != '%') continue; |
2877 | | - |
2878 | | - if (first_percent == std::string::npos) { |
2879 | | - first_percent = i; |
2880 | | - } |
2881 | | - |
2882 | | - // just safe-guard against access the pathname |
2883 | | - // outside the bounds |
2884 | | - if ((i + 2) >= pathname_size) continue; |
2885 | | - |
2886 | | - char third = pathname[i + 2] | 0x20; |
2887 | | - |
2888 | | - bool is_slash = pathname[i + 1] == '2' && third == 102; |
2889 | | - bool is_forward_slash = pathname[i + 1] == '5' && third == 99; |
2890 | | - |
2891 | | - if (!is_slash && !is_forward_slash) continue; |
2892 | | - |
2893 | | - env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( |
2894 | | - env->isolate(), |
2895 | | - "File URL path must not include encoded \\ or / characters")); |
2896 | | - |
2897 | | - return false; |
2898 | | - } |
2899 | | - |
2900 | | - std::string_view hostname = file_url.get_hostname(); |
2901 | | - std::string decoded_pathname = ada::unicode::percent_decode( |
2902 | | - std::string_view(pathname_escaped_slash), first_percent); |
2903 | | - |
2904 | | - if (hostname.size() > 0) { |
2905 | | - // If hostname is set, then we have a UNC path |
2906 | | - // Pass the hostname through domainToUnicode just in case |
2907 | | - // it is an IDN using punycode encoding. We do not need to worry |
2908 | | - // about percent encoding because the URL parser will have |
2909 | | - // already taken care of that for us. Note that this only |
2910 | | - // causes IDNs with an appropriate `xn--` prefix to be decoded. |
2911 | | - result_file_path = |
2912 | | - "\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname; |
2913 | | - |
2914 | | - return true; |
2915 | | - } |
2916 | | - |
2917 | | - char letter = decoded_pathname[1] | 0x20; |
2918 | | - char sep = decoded_pathname[2]; |
2919 | | - |
2920 | | - // a..z A..Z |
2921 | | - if (letter < 'a' || letter > 'z' || sep != ':') { |
2922 | | - env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( |
2923 | | - env->isolate(), "File URL path must be absolute")); |
2924 | | - |
2925 | | - return false; |
2926 | | - } |
2927 | | - |
2928 | | - result_file_path = decoded_pathname.substr(1); |
2929 | | - |
2930 | | - return true; |
2931 | | -#else // _WIN32 |
2932 | | - std::string_view hostname = file_url.get_hostname(); |
2933 | | - |
2934 | | - if (hostname.size() > 0) { |
2935 | | - std::string error_message = |
2936 | | - std::string("File URL host must be \"localhost\" or empty on ") + |
2937 | | - std::string(per_process::metadata.platform); |
2938 | | - env->isolate()->ThrowException( |
2939 | | - ERR_INVALID_FILE_URL_HOST(env->isolate(), error_message.c_str())); |
2940 | | - |
2941 | | - return false; |
2942 | | - } |
2943 | | - |
2944 | | - size_t first_percent = std::string::npos; |
2945 | | - for (size_t i = 0; (i + 2) < pathname.size(); i++) { |
2946 | | - if (pathname[i] != '%') continue; |
2947 | | - |
2948 | | - if (first_percent == std::string::npos) { |
2949 | | - first_percent = i; |
2950 | | - } |
2951 | | - |
2952 | | - if (pathname[i + 1] == '2' && (pathname[i + 2] | 0x20) == 102) { |
2953 | | - env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( |
2954 | | - env->isolate(), |
2955 | | - "File URL path must not include encoded / characters")); |
2956 | | - |
2957 | | - return false; |
2958 | | - } |
2959 | | - } |
2960 | | - |
2961 | | - result_file_path = ada::unicode::percent_decode(pathname, first_percent); |
2962 | | - |
2963 | | - return true; |
2964 | | -#endif // _WIN32 |
2965 | | -} |
2966 | | - |
2967 | 2851 | BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile( |
2968 | 2852 | Environment* env, const std::string& file_path) { |
2969 | 2853 | THROW_IF_INSUFFICIENT_PERMISSIONS( |
@@ -3051,7 +2935,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { |
3051 | 2935 | return; |
3052 | 2936 | } |
3053 | 2937 |
|
3054 | | - if (!FileURLToPath(env, file_path_url.value(), initial_file_path)) return; |
| 2938 | + if (!node::url::FileURLToPath( |
| 2939 | + env, file_path_url.value(), initial_file_path)) |
| 2940 | + return; |
3055 | 2941 |
|
3056 | 2942 | FromNamespacedPath(&initial_file_path); |
3057 | 2943 |
|
@@ -3085,7 +2971,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { |
3085 | 2971 | return; |
3086 | 2972 | } |
3087 | 2973 |
|
3088 | | - if (!FileURLToPath(env, file_path_url.value(), initial_file_path)) return; |
| 2974 | + if (!node::url::FileURLToPath(env, file_path_url.value(), initial_file_path)) |
| 2975 | + return; |
3089 | 2976 |
|
3090 | 2977 | FromNamespacedPath(&initial_file_path); |
3091 | 2978 |
|
@@ -3112,7 +2999,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { |
3112 | 2999 | std::string module_path; |
3113 | 3000 | std::string module_base; |
3114 | 3001 |
|
3115 | | - if (!FileURLToPath(env, package_json_url.value(), module_path)) return; |
| 3002 | + if (!node::url::FileURLToPath(env, package_json_url.value(), module_path)) |
| 3003 | + return; |
3116 | 3004 |
|
3117 | 3005 | if (args.Length() >= 3 && !args[2]->IsNullOrUndefined() && |
3118 | 3006 | args[2]->IsString()) { |
@@ -3127,7 +3015,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { |
3127 | 3015 | return; |
3128 | 3016 | } |
3129 | 3017 |
|
3130 | | - if (!FileURLToPath(env, base_url.value(), module_base)) return; |
| 3018 | + if (!node::url::FileURLToPath(env, base_url.value(), module_base)) return; |
3131 | 3019 | } else { |
3132 | 3020 | std::string err_arg_message = |
3133 | 3021 | "The \"base\" argument must be of type string or an instance of URL."; |
|