cmCTestCurl.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmCTestCurl.h"
  4. #include <cstdio>
  5. #include <ostream>
  6. #include <cmext/algorithm>
  7. #include "cmCTest.h"
  8. #include "cmCurl.h"
  9. #include "cmList.h"
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. #include "cmValue.h"
  13. namespace {
  14. bool const TLS_VERIFY_DEFAULT = true;
  15. int const TLS_VERSION_DEFAULT = CURL_SSLVERSION_TLSv1_2;
  16. }
  17. cmCTestCurl::cmCTestCurl(cmCTest* ctest)
  18. : CTest(ctest)
  19. , CurlOpts(ctest)
  20. {
  21. this->SetProxyType();
  22. // In windows, this will init the winsock stuff
  23. cm_curl_global_init(CURL_GLOBAL_ALL);
  24. this->Curl = cm_curl_easy_init();
  25. }
  26. cmCTestCurl::~cmCTestCurl()
  27. {
  28. ::curl_easy_cleanup(this->Curl);
  29. ::curl_global_cleanup();
  30. }
  31. std::string cmCTestCurl::Escape(std::string const& source)
  32. {
  33. char* data1 = curl_easy_escape(this->Curl, source.c_str(), 0);
  34. std::string ret = data1;
  35. curl_free(data1);
  36. return ret;
  37. }
  38. namespace {
  39. size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
  40. void* data)
  41. {
  42. int realsize = static_cast<int>(size * nmemb);
  43. char const* chPtr = static_cast<char*>(ptr);
  44. cm::append(*static_cast<std::vector<char>*>(data), chPtr, chPtr + realsize);
  45. return realsize;
  46. }
  47. size_t curlDebugCallback(CURL* /*unused*/, curl_infotype /*unused*/,
  48. char* chPtr, size_t size, void* data)
  49. {
  50. cm::append(*static_cast<std::vector<char>*>(data), chPtr, chPtr + size);
  51. return 0;
  52. }
  53. }
  54. cmCTestCurlOpts::cmCTestCurlOpts(cmCTest* ctest)
  55. {
  56. this->TLSVersionOpt =
  57. cmCurlParseTLSVersion(ctest->GetCTestConfiguration("TLSVersion"));
  58. if (!this->TLSVersionOpt.has_value()) {
  59. this->TLSVersionOpt = TLS_VERSION_DEFAULT;
  60. }
  61. std::string tlsVerify = ctest->GetCTestConfiguration("TLSVerify");
  62. if (!tlsVerify.empty()) {
  63. this->TLSVerifyOpt = cmIsOn(tlsVerify);
  64. } else {
  65. cmList args{ ctest->GetCTestConfiguration("CurlOptions") };
  66. for (std::string const& arg : args) {
  67. if (arg == "CURLOPT_SSL_VERIFYPEER_OFF") {
  68. this->TLSVerifyOpt = false;
  69. }
  70. if (arg == "CURLOPT_SSL_VERIFYHOST_OFF") {
  71. this->VerifyHostOff = true;
  72. }
  73. }
  74. }
  75. if (!this->TLSVerifyOpt.has_value()) {
  76. this->TLSVerifyOpt = TLS_VERIFY_DEFAULT;
  77. }
  78. }
  79. bool cmCTestCurl::InitCurl()
  80. {
  81. if (!this->Curl) {
  82. return false;
  83. }
  84. cmCurlSetCAInfo(this->Curl);
  85. if (this->CurlOpts.TLSVersionOpt.has_value()) {
  86. curl_easy_setopt(this->Curl, CURLOPT_SSLVERSION,
  87. *this->CurlOpts.TLSVersionOpt);
  88. }
  89. if (this->CurlOpts.TLSVerifyOpt.has_value()) {
  90. curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER,
  91. *this->CurlOpts.TLSVerifyOpt ? 1 : 0);
  92. }
  93. if (this->CurlOpts.VerifyHostOff) {
  94. curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
  95. }
  96. if (!this->HTTPProxy.empty()) {
  97. curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  98. curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
  99. if (!this->HTTPProxyAuth.empty()) {
  100. curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
  101. this->HTTPProxyAuth.c_str());
  102. }
  103. }
  104. if (this->UseHttp10) {
  105. curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  106. }
  107. // enable HTTP ERROR parsing
  108. curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  109. // if there is little to no activity for too long stop submitting
  110. if (this->TimeOutSeconds) {
  111. curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  112. curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME, this->TimeOutSeconds);
  113. }
  114. return true;
  115. }
  116. bool cmCTestCurl::UploadFile(std::string const& local_file,
  117. std::string const& url, std::string const& fields,
  118. std::string& response)
  119. {
  120. response.clear();
  121. if (!this->InitCurl()) {
  122. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed\n");
  123. return false;
  124. }
  125. /* enable uploading */
  126. curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
  127. ::curl_easy_setopt(this->Curl, CURLOPT_VERBOSE, 1);
  128. FILE* ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  129. if (!ftpfile) {
  130. cmCTestLog(this->CTest, ERROR_MESSAGE,
  131. "Could not open file for upload: " << local_file << "\n");
  132. return false;
  133. }
  134. // set the url
  135. std::string upload_url = cmStrCat(url, '?', fields);
  136. ::curl_easy_setopt(this->Curl, CURLOPT_URL, upload_url.c_str());
  137. // now specify which file to upload
  138. ::curl_easy_setopt(this->Curl, CURLOPT_INFILE, ftpfile);
  139. unsigned long filelen = cmSystemTools::FileLength(local_file);
  140. // and give the size of the upload (optional)
  141. ::curl_easy_setopt(this->Curl, CURLOPT_INFILESIZE,
  142. static_cast<long>(filelen));
  143. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  144. curlWriteMemoryCallback);
  145. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  146. // Set Content-Type to satisfy fussy modsecurity rules.
  147. struct curl_slist* headers =
  148. ::curl_slist_append(nullptr, "Content-Type: text/xml");
  149. // Add any additional headers that the user specified.
  150. for (std::string const& h : this->HttpHeaders) {
  151. cmCTestOptionalLog(this->CTest, DEBUG,
  152. " Add HTTP Header: \"" << h << "\"" << std::endl,
  153. this->Quiet);
  154. headers = ::curl_slist_append(headers, h.c_str());
  155. }
  156. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
  157. std::vector<char> responseData;
  158. std::vector<char> debugData;
  159. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  160. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
  161. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  162. // Now run off and do what you've been told!
  163. ::curl_easy_perform(this->Curl);
  164. ::fclose(ftpfile);
  165. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, nullptr);
  166. ::curl_slist_free_all(headers);
  167. if (!responseData.empty()) {
  168. response = std::string(responseData.begin(), responseData.end());
  169. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  170. "Curl response: [" << response << "]\n", this->Quiet);
  171. }
  172. std::string curlDebug;
  173. if (!debugData.empty()) {
  174. curlDebug = std::string(debugData.begin(), debugData.end());
  175. cmCTestOptionalLog(this->CTest, DEBUG,
  176. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  177. }
  178. if (response.empty()) {
  179. cmCTestLog(this->CTest, ERROR_MESSAGE,
  180. "No response from server.\n"
  181. << curlDebug << std::endl);
  182. return false;
  183. }
  184. return true;
  185. }
  186. bool cmCTestCurl::HttpRequest(std::string const& url,
  187. std::string const& fields, std::string& response)
  188. {
  189. response.clear();
  190. cmCTestOptionalLog(this->CTest, DEBUG,
  191. "HttpRequest\n"
  192. << "url: " << url << "\n"
  193. << "fields " << fields << "\n",
  194. this->Quiet);
  195. if (!this->InitCurl()) {
  196. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed\n");
  197. return false;
  198. }
  199. curl_easy_setopt(this->Curl, CURLOPT_POST, 1);
  200. curl_easy_setopt(this->Curl, CURLOPT_POSTFIELDS, fields.c_str());
  201. ::curl_easy_setopt(this->Curl, CURLOPT_URL, url.c_str());
  202. ::curl_easy_setopt(this->Curl, CURLOPT_FOLLOWLOCATION, 1);
  203. // set response options
  204. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  205. curlWriteMemoryCallback);
  206. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  207. std::vector<char> responseData;
  208. std::vector<char> debugData;
  209. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  210. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
  211. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  212. // Add headers if any were specified.
  213. struct curl_slist* headers = nullptr;
  214. if (!this->HttpHeaders.empty()) {
  215. for (std::string const& h : this->HttpHeaders) {
  216. cmCTestOptionalLog(this->CTest, DEBUG,
  217. " Add HTTP Header: \"" << h << "\"" << std::endl,
  218. this->Quiet);
  219. headers = ::curl_slist_append(headers, h.c_str());
  220. }
  221. }
  222. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
  223. CURLcode res = ::curl_easy_perform(this->Curl);
  224. ::curl_slist_free_all(headers);
  225. if (!responseData.empty()) {
  226. response = std::string(responseData.begin(), responseData.end());
  227. cmCTestOptionalLog(this->CTest, DEBUG,
  228. "Curl response: [" << response << "]\n", this->Quiet);
  229. }
  230. if (!debugData.empty()) {
  231. std::string curlDebug = std::string(debugData.begin(), debugData.end());
  232. cmCTestOptionalLog(this->CTest, DEBUG,
  233. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  234. }
  235. cmCTestOptionalLog(this->CTest, DEBUG, "Curl res: " << res << "\n",
  236. this->Quiet);
  237. return (res == 0);
  238. }
  239. void cmCTestCurl::SetProxyType()
  240. {
  241. this->HTTPProxy.clear();
  242. // this is the default
  243. this->HTTPProxyType = CURLPROXY_HTTP;
  244. this->HTTPProxyAuth.clear();
  245. if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
  246. std::string port;
  247. if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
  248. this->HTTPProxy += ":";
  249. this->HTTPProxy += port;
  250. }
  251. std::string type;
  252. if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
  253. // HTTP/SOCKS4/SOCKS5
  254. if (type == "HTTP") {
  255. this->HTTPProxyType = CURLPROXY_HTTP;
  256. } else if (type == "SOCKS4") {
  257. this->HTTPProxyType = CURLPROXY_SOCKS4;
  258. } else if (type == "SOCKS5") {
  259. this->HTTPProxyType = CURLPROXY_SOCKS5;
  260. }
  261. }
  262. cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
  263. std::string passwd;
  264. if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
  265. this->HTTPProxyAuth += ":";
  266. this->HTTPProxyAuth += passwd;
  267. }
  268. }
  269. }