cmCTestCurl.cxx 9.2 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 "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::vector<std::string>::const_iterator i = args.begin();
  55. i != args.end(); ++i) {
  56. if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
  57. this->VerifyPeerOff = true;
  58. }
  59. if (*i == "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. 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 = "";
  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::vector<std::string>::const_iterator h = this->HttpHeaders.begin();
  134. h != this->HttpHeaders.end(); ++h) {
  135. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  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, "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 = "";
  173. cmCTestOptionalLog(this->CTest, DEBUG, "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::vector<std::string>::const_iterator h =
  198. this->HttpHeaders.begin();
  199. h != this->HttpHeaders.end(); ++h) {
  200. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  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 = "";
  226. // this is the default
  227. this->HTTPProxyType = CURLPROXY_HTTP;
  228. this->HTTPProxyAuth = "";
  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. }