cmCurl.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCurl.h"
  4. #if !defined(CMAKE_USE_SYSTEM_CURL) && !defined(_WIN32) && \
  5. !defined(__APPLE__) && !defined(CURL_CA_BUNDLE) && !defined(CURL_CA_PATH)
  6. # define CMAKE_FIND_CAFILE
  7. #endif
  8. #include "cmStringAlgorithms.h"
  9. #include "cmSystemTools.h"
  10. #if defined(_WIN32)
  11. # include <vector>
  12. # include <windows.h>
  13. # include "cmsys/Encoding.hxx"
  14. #endif
  15. // curl versions before 7.21.5 did not provide this error code
  16. #if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM < 0x071505
  17. # define CURLE_NOT_BUILT_IN 4
  18. #endif
  19. #define check_curl_result(result, errstr) \
  20. do { \
  21. if ((result) != CURLE_OK && (result) != CURLE_NOT_BUILT_IN) { \
  22. e += e.empty() ? "" : "\n"; \
  23. e += (errstr); \
  24. e += ::curl_easy_strerror(result); \
  25. } \
  26. } while (false)
  27. std::string cmCurlSetCAInfo(::CURL* curl, const std::string& cafile)
  28. {
  29. std::string e;
  30. if (!cafile.empty()) {
  31. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, cafile.c_str());
  32. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  33. }
  34. #ifdef CMAKE_FIND_CAFILE
  35. # define CMAKE_CAFILE_FEDORA "/etc/pki/tls/certs/ca-bundle.crt"
  36. else if (cmSystemTools::FileExists(CMAKE_CAFILE_FEDORA, true)) {
  37. ::CURLcode res =
  38. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_FEDORA);
  39. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  40. }
  41. # undef CMAKE_CAFILE_FEDORA
  42. else {
  43. # define CMAKE_CAFILE_COMMON "/etc/ssl/certs/ca-certificates.crt"
  44. if (cmSystemTools::FileExists(CMAKE_CAFILE_COMMON, true)) {
  45. ::CURLcode res =
  46. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_COMMON);
  47. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  48. }
  49. # undef CMAKE_CAFILE_COMMON
  50. # define CMAKE_CAPATH_COMMON "/etc/ssl/certs"
  51. if (cmSystemTools::FileIsDirectory(CMAKE_CAPATH_COMMON)) {
  52. ::CURLcode res =
  53. ::curl_easy_setopt(curl, CURLOPT_CAPATH, CMAKE_CAPATH_COMMON);
  54. check_curl_result(res, "Unable to set TLS/SSL Verify CAPATH: ");
  55. }
  56. # undef CMAKE_CAPATH_COMMON
  57. }
  58. #endif
  59. return e;
  60. }
  61. std::string cmCurlSetNETRCOption(::CURL* curl, const std::string& netrc_level,
  62. const std::string& netrc_file)
  63. {
  64. std::string e;
  65. CURL_NETRC_OPTION curl_netrc_level = CURL_NETRC_LAST;
  66. ::CURLcode res;
  67. if (!netrc_level.empty()) {
  68. if (netrc_level == "OPTIONAL") {
  69. curl_netrc_level = CURL_NETRC_OPTIONAL;
  70. } else if (netrc_level == "REQUIRED") {
  71. curl_netrc_level = CURL_NETRC_REQUIRED;
  72. } else if (netrc_level == "IGNORED") {
  73. curl_netrc_level = CURL_NETRC_IGNORED;
  74. } else {
  75. e = cmStrCat("NETRC accepts OPTIONAL, IGNORED or REQUIRED but got: ",
  76. netrc_level);
  77. return e;
  78. }
  79. }
  80. if (curl_netrc_level != CURL_NETRC_LAST &&
  81. curl_netrc_level != CURL_NETRC_IGNORED) {
  82. res = ::curl_easy_setopt(curl, CURLOPT_NETRC, curl_netrc_level);
  83. check_curl_result(res, "Unable to set netrc level: ");
  84. if (!e.empty()) {
  85. return e;
  86. }
  87. // check to see if a .netrc file has been specified
  88. if (!netrc_file.empty()) {
  89. res = ::curl_easy_setopt(curl, CURLOPT_NETRC_FILE, netrc_file.c_str());
  90. check_curl_result(res, "Unable to set .netrc file path : ");
  91. }
  92. }
  93. return e;
  94. }
  95. std::string cmCurlFixFileURL(std::string url)
  96. {
  97. if (!cmHasLiteralPrefix(url, "file://")) {
  98. return url;
  99. }
  100. // libcurl 7.77 and below accidentally allowed spaces in URLs in some cases.
  101. // One such case was file:// URLs, which CMake has long accepted as a result.
  102. // Explicitly encode spaces for a URL.
  103. cmSystemTools::ReplaceString(url, " ", "%20");
  104. #if defined(_WIN32)
  105. // libcurl doesn't support file:// urls for unicode filenames on Windows.
  106. // Convert string from UTF-8 to ACP if this is a file:// URL.
  107. std::wstring wurl = cmsys::Encoding::ToWide(url);
  108. if (!wurl.empty()) {
  109. int mblen =
  110. WideCharToMultiByte(CP_ACP, 0, wurl.c_str(), -1, NULL, 0, NULL, NULL);
  111. if (mblen > 0) {
  112. std::vector<char> chars(mblen);
  113. mblen = WideCharToMultiByte(CP_ACP, 0, wurl.c_str(), -1, &chars[0],
  114. mblen, NULL, NULL);
  115. if (mblen > 0) {
  116. url = &chars[0];
  117. }
  118. }
  119. }
  120. #endif
  121. return url;
  122. }