cmCTestCurl.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "cmCTest.h"
  5. #include "cmCurl.h"
  6. #include "cmSystemTools.h"
  7. #include <ostream>
  8. #include <stdio.h>
  9. cmCTestCurl::cmCTestCurl(cmCTest* ctest)
  10. {
  11. this->CTest = ctest;
  12. this->SetProxyType();
  13. this->UseHttp10 = false;
  14. // In windows, this will init the winsock stuff
  15. ::curl_global_init(CURL_GLOBAL_ALL);
  16. // default is to verify https
  17. this->VerifyPeerOff = false;
  18. this->VerifyHostOff = false;
  19. this->Quiet = false;
  20. this->TimeOutSeconds = 0;
  21. this->Curl = 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. std::vector<char>* vec = static_cast<std::vector<char>*>(data);
  41. const char* chPtr = static_cast<char*>(ptr);
  42. vec->insert(vec->end(), 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. std::vector<char>* vec = static_cast<std::vector<char>*>(data);
  49. vec->insert(vec->end(), 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 = url;
  118. upload_url += "?";
  119. upload_url += fields;
  120. ::curl_easy_setopt(this->Curl, CURLOPT_URL, upload_url.c_str());
  121. // now specify which file to upload
  122. ::curl_easy_setopt(this->Curl, CURLOPT_INFILE, ftpfile);
  123. unsigned long filelen = cmSystemTools::FileLength(local_file);
  124. // and give the size of the upload (optional)
  125. ::curl_easy_setopt(this->Curl, CURLOPT_INFILESIZE,
  126. static_cast<long>(filelen));
  127. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  128. curlWriteMemoryCallback);
  129. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  130. // Set Content-Type to satisfy fussy modsecurity rules.
  131. struct curl_slist* headers =
  132. ::curl_slist_append(nullptr, "Content-Type: text/xml");
  133. // Add any additional headers that the user specified.
  134. for (std::string const& h : this->HttpHeaders) {
  135. cmCTestOptionalLog(this->CTest, DEBUG,
  136. " Add HTTP Header: \"" << h << "\"" << std::endl,
  137. this->Quiet);
  138. headers = ::curl_slist_append(headers, h.c_str());
  139. }
  140. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
  141. std::vector<char> responseData;
  142. std::vector<char> debugData;
  143. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  144. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
  145. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  146. // Now run off and do what you've been told!
  147. ::curl_easy_perform(this->Curl);
  148. ::fclose(ftpfile);
  149. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, NULL);
  150. ::curl_slist_free_all(headers);
  151. if (!responseData.empty()) {
  152. response = std::string(responseData.begin(), responseData.end());
  153. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  154. "Curl response: [" << response << "]\n", this->Quiet);
  155. }
  156. std::string curlDebug;
  157. if (!debugData.empty()) {
  158. curlDebug = std::string(debugData.begin(), debugData.end());
  159. cmCTestOptionalLog(this->CTest, DEBUG,
  160. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  161. }
  162. if (response.empty()) {
  163. cmCTestLog(this->CTest, ERROR_MESSAGE,
  164. "No response from server.\n"
  165. << curlDebug);
  166. return false;
  167. }
  168. return true;
  169. }
  170. bool cmCTestCurl::HttpRequest(std::string const& url,
  171. std::string const& fields, std::string& response)
  172. {
  173. response.clear();
  174. cmCTestOptionalLog(this->CTest, DEBUG,
  175. "HttpRequest\n"
  176. << "url: " << url << "\n"
  177. << "fields " << fields << "\n",
  178. this->Quiet);
  179. if (!this->InitCurl()) {
  180. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  181. return false;
  182. }
  183. curl_easy_setopt(this->Curl, CURLOPT_POST, 1);
  184. curl_easy_setopt(this->Curl, CURLOPT_POSTFIELDS, fields.c_str());
  185. ::curl_easy_setopt(this->Curl, CURLOPT_URL, url.c_str());
  186. ::curl_easy_setopt(this->Curl, CURLOPT_FOLLOWLOCATION, 1);
  187. // set response options
  188. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  189. curlWriteMemoryCallback);
  190. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  191. std::vector<char> responseData;
  192. std::vector<char> debugData;
  193. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  194. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
  195. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  196. // Add headers if any were specified.
  197. struct curl_slist* headers = nullptr;
  198. if (!this->HttpHeaders.empty()) {
  199. for (std::string const& h : this->HttpHeaders) {
  200. cmCTestOptionalLog(this->CTest, DEBUG,
  201. " Add HTTP Header: \"" << h << "\"" << std::endl,
  202. this->Quiet);
  203. headers = ::curl_slist_append(headers, h.c_str());
  204. }
  205. }
  206. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
  207. CURLcode res = ::curl_easy_perform(this->Curl);
  208. ::curl_slist_free_all(headers);
  209. if (!responseData.empty()) {
  210. response = std::string(responseData.begin(), responseData.end());
  211. cmCTestOptionalLog(this->CTest, DEBUG,
  212. "Curl response: [" << response << "]\n", this->Quiet);
  213. }
  214. if (!debugData.empty()) {
  215. std::string curlDebug = std::string(debugData.begin(), debugData.end());
  216. cmCTestOptionalLog(this->CTest, DEBUG,
  217. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  218. }
  219. cmCTestOptionalLog(this->CTest, DEBUG, "Curl res: " << res << "\n",
  220. this->Quiet);
  221. return (res == 0);
  222. }
  223. void cmCTestCurl::SetProxyType()
  224. {
  225. this->HTTPProxy.clear();
  226. // this is the default
  227. this->HTTPProxyType = CURLPROXY_HTTP;
  228. this->HTTPProxyAuth.clear();
  229. if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
  230. std::string port;
  231. if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
  232. this->HTTPProxy += ":";
  233. this->HTTPProxy += port;
  234. }
  235. std::string type;
  236. if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
  237. // HTTP/SOCKS4/SOCKS5
  238. if (type == "HTTP") {
  239. this->HTTPProxyType = CURLPROXY_HTTP;
  240. } else if (type == "SOCKS4") {
  241. this->HTTPProxyType = CURLPROXY_SOCKS4;
  242. } else if (type == "SOCKS5") {
  243. this->HTTPProxyType = CURLPROXY_SOCKS5;
  244. }
  245. }
  246. cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
  247. std::string passwd;
  248. if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
  249. this->HTTPProxyAuth += ":";
  250. this->HTTPProxyAuth += passwd;
  251. }
  252. }
  253. }