cmCTestCurl.cxx 9.1 KB

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