cmCTestCurl.cxx 9.1 KB

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