cmCTestSubmit.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestSubmit.h"
  14. #include "cmSystemTools.h"
  15. #include "curl/curl.h"
  16. #include <sys/stat.h>
  17. cmCTestSubmit::cmCTestSubmit() : m_HTTPProxy(), m_FTPProxy()
  18. {
  19. m_Verbose = false;
  20. m_HTTPProxy = "";
  21. m_HTTPProxyType = 0;
  22. if ( getenv("HTTP_PROXY") )
  23. {
  24. m_HTTPProxyType = 1;
  25. m_HTTPProxy = getenv("HTTP_PROXY");
  26. if ( getenv("HTTP_PROXY_PORT") )
  27. {
  28. m_HTTPProxy += ":";
  29. m_HTTPProxy += getenv("HTTP_PROXY_PORT");
  30. }
  31. if ( getenv("HTTP_PROXY_TYPE") )
  32. {
  33. std::string type = getenv("HTTP_PROXY_TYPE");
  34. // HTTP/SOCKS4/SOCKS5
  35. if ( type == "HTTP" )
  36. {
  37. m_HTTPProxyType = 1;
  38. }
  39. else if ( type == "SOCKS4" )
  40. {
  41. m_HTTPProxyType = 2;
  42. }
  43. else if ( type == "SOCKS5" )
  44. {
  45. m_HTTPProxyType = 3;
  46. }
  47. }
  48. }
  49. m_FTPProxy = "";
  50. m_FTPProxyType = 0;
  51. if ( getenv("FTP_PROXY") )
  52. {
  53. m_FTPProxyType = 1;
  54. m_FTPProxy = getenv("FTP_PROXY");
  55. if ( getenv("FTP_PROXY_PORT") )
  56. {
  57. m_FTPProxy += ":";
  58. m_FTPProxy += getenv("FTP_PROXY_PORT");
  59. }
  60. if ( getenv("FTP_PROXY_TYPE") )
  61. {
  62. std::string type = getenv("FTP_PROXY_TYPE");
  63. // HTTP/SOCKS4/SOCKS5
  64. if ( type == "HTTP" )
  65. {
  66. m_FTPProxyType = 1;
  67. }
  68. else if ( type == "SOCKS4" )
  69. {
  70. m_FTPProxyType = 2;
  71. }
  72. else if ( type == "SOCKS5" )
  73. {
  74. m_FTPProxyType = 3;
  75. }
  76. }
  77. }
  78. if ( m_HTTPProxy.size() > 0 )
  79. {
  80. std::cout << " Use HTTP Proxy: " << m_HTTPProxy << std::endl;
  81. }
  82. if ( m_FTPProxy.size() > 0 )
  83. {
  84. std::cout << " Use FTP Proxy: " << m_FTPProxy << std::endl;
  85. }
  86. }
  87. bool cmCTestSubmit::SubmitUsingFTP(const std::string& localprefix,
  88. const std::vector<std::string>& files,
  89. const std::string& remoteprefix,
  90. const std::string& url)
  91. {
  92. CURL *curl;
  93. CURLcode res;
  94. FILE* ftpfile;
  95. char error_buffer[1024];
  96. /* In windows, this will init the winsock stuff */
  97. ::curl_global_init(CURL_GLOBAL_ALL);
  98. std::string::size_type cc;
  99. for ( cc = 0; cc < files.size(); cc ++ )
  100. {
  101. /* get a curl handle */
  102. curl = curl_easy_init();
  103. if(curl)
  104. {
  105. // Using proxy
  106. if ( m_FTPProxyType > 0 )
  107. {
  108. curl_easy_setopt(curl, CURLOPT_PROXY, m_FTPProxy.c_str());
  109. switch (m_FTPProxyType)
  110. {
  111. case 2:
  112. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  113. break;
  114. case 3:
  115. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  116. break;
  117. default:
  118. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  119. }
  120. }
  121. // enable uploading
  122. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  123. std::string local_file = localprefix + "/" + files[cc];
  124. std::string upload_as = url + "/" + remoteprefix + files[cc];
  125. struct stat st;
  126. if ( ::stat(local_file.c_str(), &st) )
  127. {
  128. return false;
  129. }
  130. ftpfile = ::fopen(local_file.c_str(), "rb");
  131. if ( m_Verbose )
  132. {
  133. std::cout << " Upload file: " << local_file.c_str() << " to "
  134. << upload_as.c_str() << std::endl;
  135. }
  136. if ( m_Verbose )
  137. {
  138. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  139. }
  140. // specify target
  141. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  142. // now specify which file to upload
  143. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  144. // and give the size of the upload (optional)
  145. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  146. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  147. // Now run off and do what you've been told!
  148. res = ::curl_easy_perform(curl);
  149. fclose(ftpfile);
  150. if ( res )
  151. {
  152. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  153. std::cout << " Error message was: " << error_buffer << std::endl;
  154. ::curl_easy_cleanup(curl);
  155. ::curl_global_cleanup();
  156. return false;
  157. }
  158. // always cleanup
  159. ::curl_easy_cleanup(curl);
  160. std::cout << " Uploaded: " + local_file << std::endl;
  161. }
  162. }
  163. ::curl_global_cleanup();
  164. return true;
  165. }
  166. // Uploading files is simpler
  167. bool cmCTestSubmit::SubmitUsingHTTP(const std::string& localprefix,
  168. const std::vector<std::string>& files,
  169. const std::string& remoteprefix,
  170. const std::string& url)
  171. {
  172. CURL *curl;
  173. CURLcode res;
  174. FILE* ftpfile;
  175. /* In windows, this will init the winsock stuff */
  176. ::curl_global_init(CURL_GLOBAL_ALL);
  177. std::string::size_type cc, kk;
  178. for ( cc = 0; cc < files.size(); cc ++ )
  179. {
  180. /* get a curl handle */
  181. curl = curl_easy_init();
  182. if(curl)
  183. {
  184. // Using proxy
  185. if ( m_HTTPProxyType > 0 )
  186. {
  187. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  188. switch (m_HTTPProxyType)
  189. {
  190. case 2:
  191. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  192. break;
  193. case 3:
  194. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  195. break;
  196. default:
  197. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  198. }
  199. }
  200. /* enable uploading */
  201. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  202. /* HTTP PUT please */
  203. curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
  204. if ( m_Verbose )
  205. {
  206. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  207. }
  208. std::string local_file = localprefix + "/" + files[cc];
  209. std::string remote_file = remoteprefix + files[cc];
  210. std::string ofile = "";
  211. for ( kk = 0; kk < remote_file.size(); kk ++ )
  212. {
  213. char c = remote_file[kk];
  214. char hex[4] = { 0, 0, 0, 0 };
  215. hex[0] = c;
  216. switch ( c )
  217. {
  218. case '+':
  219. case '?':
  220. case '/':
  221. case '\\':
  222. case '&':
  223. case ' ':
  224. case '=':
  225. case '%':
  226. sprintf(hex, "%%%02X", (int)c);
  227. ofile.append(hex);
  228. break;
  229. default:
  230. ofile.append(hex);
  231. }
  232. }
  233. std::string upload_as = url + "?FileName=" + ofile;
  234. struct stat st;
  235. if ( ::stat(local_file.c_str(), &st) )
  236. {
  237. return false;
  238. }
  239. ftpfile = ::fopen(local_file.c_str(), "rb");
  240. if ( m_Verbose )
  241. {
  242. std::cout << " Upload file: " << local_file.c_str() << " to "
  243. << upload_as.c_str() << " Size: " << st.st_size << std::endl;
  244. }
  245. // specify target
  246. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  247. // now specify which file to upload
  248. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  249. // and give the size of the upload (optional)
  250. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  251. // Now run off and do what you've been told!
  252. res = ::curl_easy_perform(curl);
  253. fclose(ftpfile);
  254. if ( res )
  255. {
  256. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  257. ::curl_easy_cleanup(curl);
  258. ::curl_global_cleanup();
  259. return false;
  260. }
  261. // always cleanup
  262. ::curl_easy_cleanup(curl);
  263. std::cout << " Uploaded: " + local_file << std::endl;
  264. }
  265. }
  266. ::curl_global_cleanup();
  267. return true;
  268. }
  269. bool cmCTestSubmit::TriggerUsingHTTP(const std::vector<std::string>& files,
  270. const std::string& remoteprefix,
  271. const std::string& url)
  272. {
  273. CURL *curl;
  274. /* In windows, this will init the winsock stuff */
  275. ::curl_global_init(CURL_GLOBAL_ALL);
  276. std::string::size_type cc, kk;
  277. for ( cc = 0; cc < files.size(); cc ++ )
  278. {
  279. /* get a curl handle */
  280. curl = curl_easy_init();
  281. if(curl)
  282. {
  283. // Using proxy
  284. if ( m_HTTPProxyType > 0 )
  285. {
  286. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  287. switch (m_HTTPProxyType)
  288. {
  289. case 2:
  290. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  291. break;
  292. case 3:
  293. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  294. break;
  295. default:
  296. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  297. }
  298. }
  299. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
  300. if ( m_Verbose )
  301. {
  302. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  303. }
  304. std::string file = remoteprefix + files[cc];
  305. std::string ofile = "";
  306. for ( kk = 0; kk < file.size(); kk ++ )
  307. {
  308. char c = file[kk];
  309. char hex[4] = { 0, 0, 0, 0 };
  310. hex[0] = c;
  311. switch ( c )
  312. {
  313. case '+':
  314. case '?':
  315. case '/':
  316. case '\\':
  317. case '&':
  318. case ' ':
  319. case '=':
  320. case '%':
  321. sprintf(hex, "%%%02X", (int)c);
  322. ofile.append(hex);
  323. break;
  324. default:
  325. ofile.append(hex);
  326. }
  327. }
  328. std::string turl = url + "?xmlfile=" + ofile;
  329. if ( m_Verbose )
  330. {
  331. std::cout << " Trigger url: " << turl.c_str() << std::endl;
  332. }
  333. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  334. if ( curl_easy_perform(curl) )
  335. {
  336. std::cout << " Error when triggering: " << turl.c_str() << std::endl;
  337. ::curl_easy_cleanup(curl);
  338. ::curl_global_cleanup();
  339. return false;
  340. }
  341. // always cleanup
  342. ::curl_easy_cleanup(curl);
  343. std::cout << std::endl;
  344. }
  345. }
  346. ::curl_global_cleanup();
  347. std::cout << " Dart server triggered..." << std::endl;
  348. return true;
  349. }
  350. bool cmCTestSubmit::SubmitUsingSCP(const std::string&,
  351. const std::vector<std::string>&,
  352. const std::string&,
  353. const std::string&)
  354. {
  355. std::cout << "SubmitUsingSCP is not yet implemented" << std::endl;
  356. return false;
  357. }