1
0

cmCTestSubmit.cxx 11 KB

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