cmCTestSubmit.cxx 11 KB

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