cmFileCommand: Adopt file:// URL compatibility helper

Migrate the helper back from `cmCurl` because its only remaining
functionality is specific to the `file(DOWNLOAD)` and `file(UPLOAD)`
commands.
This commit is contained in:
Brad King
2026-02-09 09:50:56 -05:00
parent 6fe7acfe1d
commit 55842d16ac
3 changed files with 12 additions and 17 deletions

View File

@@ -227,20 +227,6 @@ std::string cmCurlSetNETRCOption(::CURL* curl, std::string const& netrc_level,
return e;
}
std::string cmCurlFixFileURL(std::string url)
{
if (!cmHasLiteralPrefix(url, "file://")) {
return url;
}
// libcurl 7.77 and below accidentally allowed spaces in URLs in some cases.
// One such case was file:// URLs, which CMake has long accepted as a result.
// Explicitly encode spaces for a URL.
cmSystemTools::ReplaceString(url, " ", "%20");
return url;
}
::CURL* cm_curl_easy_init()
{
::CURL* curl = curl_easy_init();

View File

@@ -21,7 +21,6 @@ cm::optional<std::string> cmCurlPrintTLSVersion(int curl_tls_version);
std::string cmCurlSetCAInfo(::CURL* curl, std::string const& cafile = {});
std::string cmCurlSetNETRCOption(::CURL* curl, std::string const& netrc_level,
std::string const& netrc_file);
std::string cmCurlFixFileURL(std::string url);
::CURLcode cm_curl_global_init(long flags);
::CURL* cm_curl_easy_init();

View File

@@ -1698,6 +1698,16 @@ std::string const TLS_VERSION_DEFAULT = "1.2";
// Stuff for curl download/upload
using cmFileCommandVectorOfChar = std::vector<char>;
std::string CurlCompatFileURL(std::string url)
{
if (cmHasLiteralPrefix(url, "file://")) {
// libcurl 7.77 and below accidentally allowed spaces in file:// URLs,
// which CMake has long accepted as a result. Explicitly encode spaces.
cmSystemTools::ReplaceString(url, " ", "%20");
}
return url;
}
size_t cmWriteToFileCallback(void* ptr, size_t size, size_t nmemb, void* data)
{
int realsize = static_cast<int>(size * nmemb);
@@ -2139,7 +2149,7 @@ bool HandleDownloadCommand(std::vector<std::string> const& args,
}
}
url = cmCurlFixFileURL(url);
url = CurlCompatFileURL(url);
::CURL* curl;
cm_curl_global_init(CURL_GLOBAL_DEFAULT);
@@ -2541,7 +2551,7 @@ bool HandleUploadCommand(std::vector<std::string> const& args,
unsigned long file_size = cmsys::SystemTools::FileLength(filename);
url = cmCurlFixFileURL(url);
url = CurlCompatFileURL(url);
::CURL* curl;
cm_curl_global_init(CURL_GLOBAL_DEFAULT);