cmCTestCurl.cxx 9.4 KB

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