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 "cmCTest.h"
  5. #include "cmSystemTools.h"
  6. #include <ostream>
  7. #include <stdio.h>
  8. cmCTestCurl::cmCTestCurl(cmCTest* ctest)
  9. {
  10. this->CTest = ctest;
  11. this->SetProxyType();
  12. this->UseHttp10 = false;
  13. // In windows, this will init the winsock stuff
  14. ::curl_global_init(CURL_GLOBAL_ALL);
  15. // default is to verify https
  16. this->VerifyPeerOff = false;
  17. this->VerifyHostOff = false;
  18. this->Quiet = false;
  19. this->TimeOutSeconds = 0;
  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. std::vector<char>* vec = static_cast<std::vector<char>*>(data);
  40. const char* chPtr = static_cast<char*>(ptr);
  41. vec->insert(vec->end(), 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. std::vector<char>* vec = static_cast<std::vector<char>*>(data);
  48. vec->insert(vec->end(), 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. if (this->VerifyPeerOff) {
  69. curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
  70. }
  71. if (this->VerifyHostOff) {
  72. curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
  73. }
  74. if (!this->HTTPProxy.empty()) {
  75. curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  76. curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
  77. if (!this->HTTPProxyAuth.empty()) {
  78. curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
  79. this->HTTPProxyAuth.c_str());
  80. }
  81. }
  82. if (this->UseHttp10) {
  83. curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  84. }
  85. // enable HTTP ERROR parsing
  86. curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  87. // if there is little to no activity for too long stop submitting
  88. if (this->TimeOutSeconds) {
  89. curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  90. curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME, this->TimeOutSeconds);
  91. }
  92. return true;
  93. }
  94. bool cmCTestCurl::UploadFile(std::string const& local_file,
  95. std::string const& url, std::string const& fields,
  96. std::string& response)
  97. {
  98. response.clear();
  99. if (!this->InitCurl()) {
  100. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  101. return false;
  102. }
  103. /* enable uploading */
  104. curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
  105. /* HTTP PUT please */
  106. ::curl_easy_setopt(this->Curl, CURLOPT_PUT, 1);
  107. ::curl_easy_setopt(this->Curl, CURLOPT_VERBOSE, 1);
  108. FILE* ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  109. if (!ftpfile) {
  110. cmCTestLog(this->CTest, ERROR_MESSAGE,
  111. "Could not open file for upload: " << local_file << "\n");
  112. return false;
  113. }
  114. // set the url
  115. std::string upload_url = url;
  116. upload_url += "?";
  117. upload_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, HANDLER_OUTPUT,
  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, "No response from server.\n"
  162. << curlDebug);
  163. return false;
  164. }
  165. return true;
  166. }
  167. bool cmCTestCurl::HttpRequest(std::string const& url,
  168. std::string const& fields, std::string& response)
  169. {
  170. response.clear();
  171. cmCTestOptionalLog(this->CTest, DEBUG, "HttpRequest\n"
  172. << "url: " << url << "\n"
  173. << "fields " << fields << "\n",
  174. this->Quiet);
  175. if (!this->InitCurl()) {
  176. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  177. return false;
  178. }
  179. curl_easy_setopt(this->Curl, CURLOPT_POST, 1);
  180. curl_easy_setopt(this->Curl, CURLOPT_POSTFIELDS, fields.c_str());
  181. ::curl_easy_setopt(this->Curl, CURLOPT_URL, url.c_str());
  182. ::curl_easy_setopt(this->Curl, CURLOPT_FOLLOWLOCATION, 1);
  183. // set response options
  184. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  185. curlWriteMemoryCallback);
  186. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  187. std::vector<char> responseData;
  188. std::vector<char> debugData;
  189. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, &responseData);
  190. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, &debugData);
  191. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  192. // Add headers if any were specified.
  193. struct curl_slist* headers = nullptr;
  194. if (!this->HttpHeaders.empty()) {
  195. for (std::string const& h : this->HttpHeaders) {
  196. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  197. " Add HTTP Header: \"" << h << "\"" << std::endl,
  198. this->Quiet);
  199. headers = ::curl_slist_append(headers, h.c_str());
  200. }
  201. }
  202. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
  203. CURLcode res = ::curl_easy_perform(this->Curl);
  204. ::curl_slist_free_all(headers);
  205. if (!responseData.empty()) {
  206. response = std::string(responseData.begin(), responseData.end());
  207. cmCTestOptionalLog(this->CTest, DEBUG,
  208. "Curl response: [" << response << "]\n", this->Quiet);
  209. }
  210. if (!debugData.empty()) {
  211. std::string curlDebug = std::string(debugData.begin(), debugData.end());
  212. cmCTestOptionalLog(this->CTest, DEBUG,
  213. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  214. }
  215. cmCTestOptionalLog(this->CTest, DEBUG, "Curl res: " << res << "\n",
  216. this->Quiet);
  217. return (res == 0);
  218. }
  219. void cmCTestCurl::SetProxyType()
  220. {
  221. this->HTTPProxy.clear();
  222. // this is the default
  223. this->HTTPProxyType = CURLPROXY_HTTP;
  224. this->HTTPProxyAuth.clear();
  225. if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
  226. std::string port;
  227. if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
  228. this->HTTPProxy += ":";
  229. this->HTTPProxy += port;
  230. }
  231. std::string type;
  232. if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
  233. // HTTP/SOCKS4/SOCKS5
  234. if (type == "HTTP") {
  235. this->HTTPProxyType = CURLPROXY_HTTP;
  236. } else if (type == "SOCKS4") {
  237. this->HTTPProxyType = CURLPROXY_SOCKS4;
  238. } else if (type == "SOCKS5") {
  239. this->HTTPProxyType = CURLPROXY_SOCKS5;
  240. }
  241. }
  242. cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
  243. std::string passwd;
  244. if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
  245. this->HTTPProxyAuth += ":";
  246. this->HTTPProxyAuth += passwd;
  247. }
  248. }
  249. }