cmCTestCurl.cxx 9.4 KB

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