cmCTestSubmit.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. /* In windows, this will init the winsock stuff */
  96. ::curl_global_init(CURL_GLOBAL_ALL);
  97. std::string::size_type cc;
  98. for ( cc = 0; cc < files.size(); cc ++ )
  99. {
  100. /* get a curl handle */
  101. curl = curl_easy_init();
  102. if(curl)
  103. {
  104. // Using proxy
  105. if ( m_FTPProxyType > 0 )
  106. {
  107. curl_easy_setopt(curl, CURLOPT_PROXY, m_FTPProxy.c_str());
  108. switch (m_FTPProxyType)
  109. {
  110. case 2:
  111. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  112. break;
  113. case 3:
  114. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  115. break;
  116. default:
  117. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  118. }
  119. }
  120. // enable uploading
  121. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  122. std::string local_file = localprefix + "/" + files[cc];
  123. std::string upload_as = url + "/" + remoteprefix + files[cc];
  124. struct stat st;
  125. if ( ::stat(local_file.c_str(), &st) )
  126. {
  127. return false;
  128. }
  129. ftpfile = ::fopen(local_file.c_str(), "rb");
  130. if ( m_Verbose )
  131. {
  132. std::cout << " Upload file: " << local_file.c_str() << " to "
  133. << upload_as.c_str() << std::endl;
  134. }
  135. if ( m_Verbose )
  136. {
  137. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  138. }
  139. // specify target
  140. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  141. // now specify which file to upload
  142. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  143. // and give the size of the upload (optional)
  144. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, st.st_size);
  145. // Now run off and do what you've been told!
  146. res = ::curl_easy_perform(curl);
  147. fclose(ftpfile);
  148. if ( res )
  149. {
  150. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  151. ::curl_easy_cleanup(curl);
  152. ::curl_global_cleanup();
  153. return false;
  154. }
  155. // always cleanup
  156. ::curl_easy_cleanup(curl);
  157. std::cout << " Uploaded: " + local_file << std::endl;
  158. }
  159. }
  160. ::curl_global_cleanup();
  161. return true;
  162. }
  163. // Uploading files is simpler
  164. bool cmCTestSubmit::SubmitUsingHTTP(const std::string& localprefix,
  165. const std::vector<std::string>& files,
  166. const std::string& remoteprefix,
  167. const std::string& url)
  168. {
  169. CURL *curl;
  170. CURLcode res;
  171. FILE* ftpfile;
  172. /* In windows, this will init the winsock stuff */
  173. ::curl_global_init(CURL_GLOBAL_ALL);
  174. std::string::size_type cc, kk;
  175. for ( cc = 0; cc < files.size(); cc ++ )
  176. {
  177. /* get a curl handle */
  178. curl = curl_easy_init();
  179. if(curl)
  180. {
  181. // Using proxy
  182. if ( m_HTTPProxyType > 0 )
  183. {
  184. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  185. switch (m_HTTPProxyType)
  186. {
  187. case 2:
  188. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  189. break;
  190. case 3:
  191. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  192. break;
  193. default:
  194. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  195. }
  196. }
  197. /* enable uploading */
  198. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  199. /* HTTP PUT please */
  200. curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
  201. if ( m_Verbose )
  202. {
  203. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  204. }
  205. std::string local_file = localprefix + "/" + files[cc];
  206. std::string remote_file = remoteprefix + files[cc];
  207. std::string ofile = "";
  208. for ( kk = 0; kk < remote_file.size(); kk ++ )
  209. {
  210. char c = remote_file[kk];
  211. char hex[4] = { 0, 0, 0, 0 };
  212. hex[0] = c;
  213. switch ( c )
  214. {
  215. case '+':
  216. case '?':
  217. case '/':
  218. case '\\':
  219. case '&':
  220. case ' ':
  221. case '=':
  222. case '%':
  223. sprintf(hex, "%%%02X", (int)c);
  224. ofile.append(hex);
  225. break;
  226. default:
  227. ofile.append(hex);
  228. }
  229. }
  230. std::string upload_as = url + "?FileName=" + ofile;
  231. struct stat st;
  232. if ( ::stat(local_file.c_str(), &st) )
  233. {
  234. return false;
  235. }
  236. ftpfile = ::fopen(local_file.c_str(), "rb");
  237. if ( m_Verbose )
  238. {
  239. std::cout << " Upload file: " << local_file.c_str() << " to "
  240. << upload_as.c_str() << " Size: " << st.st_size << std::endl;
  241. }
  242. // specify target
  243. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  244. // now specify which file to upload
  245. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  246. // and give the size of the upload (optional)
  247. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, st.st_size);
  248. // Now run off and do what you've been told!
  249. res = ::curl_easy_perform(curl);
  250. fclose(ftpfile);
  251. if ( res )
  252. {
  253. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  254. ::curl_easy_cleanup(curl);
  255. ::curl_global_cleanup();
  256. return false;
  257. }
  258. // always cleanup
  259. ::curl_easy_cleanup(curl);
  260. std::cout << " Uploaded: " + local_file << std::endl;
  261. }
  262. }
  263. ::curl_global_cleanup();
  264. return true;
  265. }
  266. bool cmCTestSubmit::TriggerUsingHTTP(const std::vector<std::string>& files,
  267. const std::string& remoteprefix,
  268. const std::string& url)
  269. {
  270. CURL *curl;
  271. /* In windows, this will init the winsock stuff */
  272. ::curl_global_init(CURL_GLOBAL_ALL);
  273. std::string::size_type cc, kk;
  274. for ( cc = 0; cc < files.size(); cc ++ )
  275. {
  276. /* get a curl handle */
  277. curl = curl_easy_init();
  278. if(curl)
  279. {
  280. // Using proxy
  281. if ( m_HTTPProxyType > 0 )
  282. {
  283. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  284. switch (m_HTTPProxyType)
  285. {
  286. case 2:
  287. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  288. break;
  289. case 3:
  290. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  291. break;
  292. default:
  293. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  294. }
  295. }
  296. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
  297. if ( m_Verbose )
  298. {
  299. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  300. }
  301. std::string file = remoteprefix + files[cc];
  302. std::string ofile = "";
  303. for ( kk = 0; kk < file.size(); kk ++ )
  304. {
  305. char c = file[kk];
  306. char hex[4] = { 0, 0, 0, 0 };
  307. hex[0] = c;
  308. switch ( c )
  309. {
  310. case '+':
  311. case '?':
  312. case '/':
  313. case '\\':
  314. case '&':
  315. case ' ':
  316. case '=':
  317. case '%':
  318. sprintf(hex, "%%%02X", (int)c);
  319. ofile.append(hex);
  320. break;
  321. default:
  322. ofile.append(hex);
  323. }
  324. }
  325. std::string turl = url + "?xmlfile=" + ofile;
  326. if ( m_Verbose )
  327. {
  328. std::cout << " Trigger url: " << turl.c_str() << std::endl;
  329. }
  330. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  331. if ( curl_easy_perform(curl) )
  332. {
  333. std::cout << " Error when triggering: " << turl.c_str() << std::endl;
  334. ::curl_easy_cleanup(curl);
  335. ::curl_global_cleanup();
  336. return false;
  337. }
  338. // always cleanup
  339. ::curl_easy_cleanup(curl);
  340. std::cout << std::endl;
  341. }
  342. }
  343. ::curl_global_cleanup();
  344. std::cout << " Dart server triggered..." << std::endl;
  345. return true;
  346. }
  347. bool cmCTestSubmit::SubmitUsingSCP(const std::string&,
  348. const std::vector<std::string>&,
  349. const std::string&,
  350. const std::string&)
  351. {
  352. std::cout << "SubmitUsingSCP is not yet implemented" << std::endl;
  353. return false;
  354. }