cmCTestSubmit.cxx 10 KB

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