cmCTestCurl.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2015 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestCurl.h"
  11. #include "cmCTest.h"
  12. #include "cmSystemTools.h"
  13. #include <cmConfigure.h>
  14. #include <ostream>
  15. #include <stdio.h>
  16. cmCTestCurl::cmCTestCurl(cmCTest* ctest)
  17. {
  18. this->CTest = ctest;
  19. this->SetProxyType();
  20. this->UseHttp10 = false;
  21. // In windows, this will init the winsock stuff
  22. ::curl_global_init(CURL_GLOBAL_ALL);
  23. // default is to verify https
  24. this->VerifyPeerOff = false;
  25. this->VerifyHostOff = false;
  26. this->TimeOutSeconds = 0;
  27. this->Curl = curl_easy_init();
  28. }
  29. cmCTestCurl::~cmCTestCurl()
  30. {
  31. ::curl_easy_cleanup(this->Curl);
  32. ::curl_global_cleanup();
  33. }
  34. std::string cmCTestCurl::Escape(std::string const& source)
  35. {
  36. char* data1 = curl_easy_escape(this->Curl, source.c_str(), 0);
  37. std::string ret = data1;
  38. curl_free(data1);
  39. return ret;
  40. }
  41. namespace {
  42. static size_t curlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb,
  43. void* data)
  44. {
  45. int realsize = (int)(size * nmemb);
  46. std::vector<char>* vec = static_cast<std::vector<char>*>(data);
  47. const char* chPtr = static_cast<char*>(ptr);
  48. vec->insert(vec->end(), chPtr, chPtr + realsize);
  49. return realsize;
  50. }
  51. static size_t curlDebugCallback(CURL* /*unused*/, curl_infotype /*unused*/,
  52. char* chPtr, size_t size, void* data)
  53. {
  54. std::vector<char>* vec = static_cast<std::vector<char>*>(data);
  55. vec->insert(vec->end(), chPtr, chPtr + size);
  56. return size;
  57. }
  58. }
  59. void cmCTestCurl::SetCurlOptions(std::vector<std::string> const& args)
  60. {
  61. for (std::vector<std::string>::const_iterator i = args.begin();
  62. i != args.end(); ++i) {
  63. if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
  64. this->VerifyPeerOff = true;
  65. }
  66. if (*i == "CURLOPT_SSL_VERIFYHOST_OFF") {
  67. this->VerifyHostOff = true;
  68. }
  69. }
  70. }
  71. bool cmCTestCurl::InitCurl()
  72. {
  73. if (!this->Curl) {
  74. return false;
  75. }
  76. if (this->VerifyPeerOff) {
  77. curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
  78. }
  79. if (this->VerifyHostOff) {
  80. curl_easy_setopt(this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
  81. }
  82. if (!this->HTTPProxy.empty()) {
  83. curl_easy_setopt(this->Curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  84. curl_easy_setopt(this->Curl, CURLOPT_PROXYTYPE, this->HTTPProxyType);
  85. if (!this->HTTPProxyAuth.empty()) {
  86. curl_easy_setopt(this->Curl, CURLOPT_PROXYUSERPWD,
  87. this->HTTPProxyAuth.c_str());
  88. }
  89. }
  90. if (this->UseHttp10) {
  91. curl_easy_setopt(this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  92. }
  93. // enable HTTP ERROR parsing
  94. curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  95. return true;
  96. }
  97. bool cmCTestCurl::UploadFile(std::string const& local_file,
  98. std::string const& url, std::string const& fields,
  99. std::string& response)
  100. {
  101. response = "";
  102. if (!this->InitCurl()) {
  103. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initialization of curl failed");
  104. return false;
  105. }
  106. /* enable uploading */
  107. curl_easy_setopt(this->Curl, CURLOPT_UPLOAD, 1);
  108. // if there is little to no activity for too long stop submitting
  109. if (this->TimeOutSeconds) {
  110. ::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  111. ::curl_easy_setopt(this->Curl, CURLOPT_LOW_SPEED_TIME,
  112. this->TimeOutSeconds);
  113. }
  114. /* HTTP PUT please */
  115. ::curl_easy_setopt(this->Curl, CURLOPT_PUT, 1);
  116. ::curl_easy_setopt(this->Curl, CURLOPT_VERBOSE, 1);
  117. FILE* ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  118. if (!ftpfile) {
  119. cmCTestLog(this->CTest, ERROR_MESSAGE,
  120. "Could not open file for upload: " << local_file << "\n");
  121. return false;
  122. }
  123. // set the url
  124. std::string upload_url = url;
  125. upload_url += "?";
  126. upload_url += fields;
  127. ::curl_easy_setopt(this->Curl, CURLOPT_URL, upload_url.c_str());
  128. // now specify which file to upload
  129. ::curl_easy_setopt(this->Curl, CURLOPT_INFILE, ftpfile);
  130. unsigned long filelen = cmSystemTools::FileLength(local_file);
  131. // and give the size of the upload (optional)
  132. ::curl_easy_setopt(this->Curl, CURLOPT_INFILESIZE,
  133. static_cast<long>(filelen));
  134. ::curl_easy_setopt(this->Curl, CURLOPT_WRITEFUNCTION,
  135. curlWriteMemoryCallback);
  136. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGFUNCTION, curlDebugCallback);
  137. // Be sure to set Content-Type to satisfy fussy modsecurity rules
  138. struct curl_slist* headers =
  139. ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml");
  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, (void*)&responseData);
  144. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&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. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Curl response: ["
  154. << response << "]\n");
  155. }
  156. std::string curlDebug;
  157. if (!debugData.empty()) {
  158. curlDebug = std::string(debugData.begin(), debugData.end());
  159. cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
  160. }
  161. if (response.empty()) {
  162. cmCTestLog(this->CTest, ERROR_MESSAGE, "No response from server.\n"
  163. << curlDebug);
  164. return false;
  165. }
  166. return true;
  167. }
  168. bool cmCTestCurl::HttpRequest(std::string const& url,
  169. std::string const& fields, std::string& response)
  170. {
  171. response = "";
  172. cmCTestLog(this->CTest, DEBUG, "HttpRequest\n"
  173. << "url: " << url << "\n"
  174. << "fields " << fields << "\n");
  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, (void*)&responseData);
  190. ::curl_easy_setopt(this->Curl, CURLOPT_DEBUGDATA, (void*)&debugData);
  191. ::curl_easy_setopt(this->Curl, CURLOPT_FAILONERROR, 1);
  192. CURLcode res = ::curl_easy_perform(this->Curl);
  193. if (!responseData.empty()) {
  194. response = std::string(responseData.begin(), responseData.end());
  195. cmCTestLog(this->CTest, DEBUG, "Curl response: [" << response << "]\n");
  196. }
  197. if (!debugData.empty()) {
  198. std::string curlDebug = std::string(debugData.begin(), debugData.end());
  199. cmCTestLog(this->CTest, DEBUG, "Curl debug: [" << curlDebug << "]\n");
  200. }
  201. cmCTestLog(this->CTest, DEBUG, "Curl res: " << res << "\n");
  202. return (res == 0);
  203. }
  204. void cmCTestCurl::SetProxyType()
  205. {
  206. this->HTTPProxy = "";
  207. // this is the default
  208. this->HTTPProxyType = CURLPROXY_HTTP;
  209. this->HTTPProxyAuth = "";
  210. if (cmSystemTools::GetEnv("HTTP_PROXY", this->HTTPProxy)) {
  211. std::string port;
  212. if (cmSystemTools::GetEnv("HTTP_PROXY_PORT", port)) {
  213. this->HTTPProxy += ":";
  214. this->HTTPProxy += port;
  215. }
  216. std::string type;
  217. if (cmSystemTools::GetEnv("HTTP_PROXY_TYPE", type)) {
  218. // HTTP/SOCKS4/SOCKS5
  219. if (type == "HTTP") {
  220. this->HTTPProxyType = CURLPROXY_HTTP;
  221. } else if (type == "SOCKS4") {
  222. this->HTTPProxyType = CURLPROXY_SOCKS4;
  223. } else if (type == "SOCKS5") {
  224. this->HTTPProxyType = CURLPROXY_SOCKS5;
  225. }
  226. }
  227. cmSystemTools::GetEnv("HTTP_PROXY_USER", this->HTTPProxyAuth);
  228. std::string passwd;
  229. if (cmSystemTools::GetEnv("HTTP_PROXY_PASSWD", passwd)) {
  230. this->HTTPProxyAuth += ":";
  231. this->HTTPProxyAuth += passwd;
  232. }
  233. }
  234. }