cmCTestCurl.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <cmConfigure.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 = (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::vector<std::string>::const_iterator i = args.begin();
  56. i != args.end(); ++i) {
  57. if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
  58. this->VerifyPeerOff = true;
  59. }
  60. if (*i == "CURLOPT_SSL_VERIFYHOST_OFF") {
  61. this->VerifyHostOff = true;
  62. }
  63. }
  64. }
  65. bool cmCTestCurl::InitCurl()
  66. {
  67. if (!this->Curl) {
  68. return false;
  69. }
  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 = "";
  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. // Be sure to set Content-Type to satisfy fussy modsecurity rules
  131. struct curl_slist* headers =
  132. ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml");
  133. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, headers);
  134. std::vector<char> responseData;
  135. std::vector<char> debugData;
  136. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
  137. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
  138. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  139. // Now run off and do what you've been told!
  140. ::curl_easy_perform(this->Curl);
  141. ::fclose(ftpfile);
  142. ::curl_easy_setopt(this->Curl, CURLOPT_HTTPHEADER, NULL);
  143. ::curl_slist_free_all(headers);
  144. if (!responseData.empty()) {
  145. response = std::string(responseData.begin(), responseData.end());
  146. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  147. "Curl response: [" << response << "]\n", this->Quiet);
  148. }
  149. std::string curlDebug;
  150. if (!debugData.empty()) {
  151. curlDebug = std::string(debugData.begin(), debugData.end());
  152. cmCTestOptionalLog(this->CTest, DEBUG,
  153. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  154. }
  155. if (response.empty()) {
  156. cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n"
  157. << curlDebug);
  158. return false;
  159. }
  160. return true;
  161. }
  162. bool cmCTestCurl::HttpRequest(std::string const& url,
  163. std::string const& fields, std::string& response)
  164. {
  165. response = "";
  166. cmCTestOptionalLog(this->CTest, DEBUG, "HttpRequest\n"
  167. << "url: " << url << "\n"
  168. << "fields " << fields << "\n",
  169. this->Quiet);
  170. if (!this->InitCurl()) {
  171. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  172. return false;
  173. }
  174. curl_easy_setopt(this->Curl, CURLOPT_POST, 1);
  175. curl_easy_setopt(this->Curl, CURLOPT_POSTFIELDS, fields.c_str());
  176. ::curl_easy_setopt(this->Curl, CURLOPT_URL, url.c_str());
  177. ::curl_easy_setopt(this->Curl, CURLOPT_FOLLOWLOCATION, 1);
  178. // set response options
  179. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  180. curlWriteMemoryCallback);
  181. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  182. std::vector<char> responseData;
  183. std::vector<char> debugData;
  184. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
  185. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
  186. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  187. CURLcode res = ::curl_easy_perform(this->Curl);
  188. if (!responseData.empty()) {
  189. response = std::string(responseData.begin(), responseData.end());
  190. cmCTestOptionalLog(this->CTest, DEBUG,
  191. "Curl response: [" << response << "]\n", this->Quiet);
  192. }
  193. if (!debugData.empty()) {
  194. std::string curlDebug = std::string(debugData.begin(), debugData.end());
  195. cmCTestOptionalLog(this->CTest, DEBUG,
  196. "Curl debug: [" << curlDebug << "]\n", this->Quiet);
  197. }
  198. cmCTestOptionalLog(this->CTest, DEBUG, "Curl res: " << res << "\n",
  199. this->Quiet);
  200. return (res == 0);
  201. }
  202. void cmCTestCurl::SetProxyType()
  203. {
  204. this->HTTPProxy = "";
  205. // this is the default
  206. this->HTTPProxyType = CURLPROXY_HTTP;
  207. this->HTTPProxyAuth = "";
  208. if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
  209. std::string port;
  210. if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
  211. this->HTTPProxy += ":";
  212. this->HTTPProxy += port;
  213. }
  214. std::string type;
  215. if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
  216. // HTTP/SOCKS4/SOCKS5
  217. if (type == "HTTP") {
  218. this->HTTPProxyType = CURLPROXY_HTTP;
  219. } else if (type == "SOCKS4") {
  220. this->HTTPProxyType = CURLPROXY_SOCKS4;
  221. } else if (type == "SOCKS5") {
  222. this->HTTPProxyType = CURLPROXY_SOCKS5;
  223. }
  224. }
  225. cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
  226. std::string passwd;
  227. if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
  228. this->HTTPProxyAuth += ":";
  229. this->HTTPProxyAuth += passwd;
  230. }
  231. }
  232. }