cmCTestCurl.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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->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. static size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
  36. void* data)
  37. {
  38. int realsize = (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. static 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. return true;
  89. }
  90. bool cmCTestCurl::UploadFile(std::string const& local_file,
  91. std::string const& url, std::string const& fields,
  92. std::string& response)
  93. {
  94. response = "";
  95. if (!this->InitCurl()) {
  96. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  97. return false;
  98. }
  99. /* enable uploading */
  100. curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
  101. // if there is little to no activity for too long stop submitting
  102. if (this->TimeOutSeconds) {
  103. ::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  104. ::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME,
  105. this->TimeOutSeconds);
  106. }
  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. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Curl response: ["
  147. << response << "]\n");
  148. }
  149. std::string curlDebug;
  150. if (!debugData.empty()) {
  151. curlDebug = std::string(debugData.begin(), debugData.end());
  152. cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
  153. }
  154. if (response.empty()) {
  155. cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n"
  156. << curlDebug);
  157. return false;
  158. }
  159. return true;
  160. }
  161. bool cmCTestCurl::HttpRequest(std::string const& url,
  162. std::string const& fields, std::string& response)
  163. {
  164. response = "";
  165. cmCTestLog(this->CTest, DEBUG, "HttpRequest\n"
  166. << "url: " << url << "\n"
  167. << "fields " << fields << "\n");
  168. if (!this->InitCurl()) {
  169. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  170. return false;
  171. }
  172. curl_easy_setopt(this->Curl, CURLOPT_POST, 1);
  173. curl_easy_setopt(this->Curl, CURLOPT_POSTFIELDS, fields.c_str());
  174. ::curl_easy_setopt(this->Curl, CURLOPT_URL, url.c_str());
  175. ::curl_easy_setopt(this->Curl, CURLOPT_FOLLOWLOCATION, 1);
  176. // set response options
  177. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  178. curlWriteMemoryCallback);
  179. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  180. std::vector<char> responseData;
  181. std::vector<char> debugData;
  182. ::curl_easy_setopt(this->Curl, CURLOPT_FILE, (void*)&responseData);
  183. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
  184. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  185. CURLcode res = ::curl_easy_perform(this->Curl);
  186. if (!responseData.empty()) {
  187. response = std::string(responseData.begin(), responseData.end());
  188. cmCTestLog(this->CTest, DEBUG, "Curl response: [" << response << "]\n");
  189. }
  190. if (!debugData.empty()) {
  191. std::string curlDebug = std::string(debugData.begin(), debugData.end());
  192. cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
  193. }
  194. cmCTestLog(this->CTest, DEBUG, "Curl res: " << res << "\n");
  195. return (res == 0);
  196. }
  197. void cmCTestCurl::SetProxyType()
  198. {
  199. this->HTTPProxy = "";
  200. // this is the default
  201. this->HTTPProxyType = CURLPROXY_HTTP;
  202. this->HTTPProxyAuth = "";
  203. if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
  204. std::string port;
  205. if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
  206. this->HTTPProxy += ":";
  207. this->HTTPProxy += port;
  208. }
  209. std::string type;
  210. if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
  211. // HTTP/SOCKS4/SOCKS5
  212. if (type == "HTTP") {
  213. this->HTTPProxyType = CURLPROXY_HTTP;
  214. } else if (type == "SOCKS4") {
  215. this->HTTPProxyType = CURLPROXY_SOCKS4;
  216. } else if (type == "SOCKS5") {
  217. this->HTTPProxyType = CURLPROXY_SOCKS5;
  218. }
  219. }
  220. cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
  221. std::string passwd;
  222. if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
  223. this->HTTPProxyAuth += ":";
  224. this->HTTPProxyAuth += passwd;
  225. }
  226. }
  227. }