cmCTestSubmit.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /* get a curl handle */
  162. curl = curl_easy_init();
  163. if(curl)
  164. {
  165. // Using proxy
  166. if ( m_HTTPProxyType > 0 )
  167. {
  168. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  169. switch (m_HTTPProxyType)
  170. {
  171. case 2:
  172. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  173. break;
  174. case 3:
  175. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  176. break;
  177. default:
  178. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  179. }
  180. }
  181. std::string::size_type cc;
  182. for ( cc = 0; cc < files.size(); cc ++ )
  183. {
  184. std::string local_file = localprefix + "/" + files[cc];
  185. std::string upload_as = url;
  186. std::string remote_file = remoteprefix + files[cc];
  187. struct HttpPost *formpost=NULL;
  188. struct HttpPost *lastptr=NULL;
  189. /* Fill in the file upload field */
  190. curl_formadd(&formpost,
  191. &lastptr,
  192. CURLFORM_COPYNAME, "FileData",
  193. CURLFORM_FILE, local_file.c_str(),
  194. CURLFORM_END);
  195. curl_formadd(&formpost,
  196. &lastptr,
  197. CURLFORM_COPYNAME, "FileName",
  198. CURLFORM_COPYCONTENTS, remote_file.c_str(),
  199. CURLFORM_END);
  200. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  201. std::cout << "upload file: " << local_file.c_str() << " to "
  202. << upload_as.c_str() << std::endl;
  203. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  204. // specify target
  205. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  206. // Now run off and do what you've been told!
  207. res = ::curl_easy_perform(curl);
  208. if ( res )
  209. {
  210. std::cout << "Error when uploading" << std::endl;
  211. ::curl_easy_cleanup(curl);
  212. ::curl_global_cleanup();
  213. return false;
  214. }
  215. }
  216. // always cleanup
  217. ::curl_easy_cleanup(curl);
  218. }
  219. ::curl_global_cleanup();
  220. return true;
  221. }
  222. bool cmCTestSubmit::TriggerUsingHTTP(const std::vector<std::string>& files,
  223. const std::string& remoteprefix,
  224. const std::string& url)
  225. {
  226. CURL *curl;
  227. CURLcode res = CURLcode();
  228. FILE* ftpfile;
  229. /* In windows, this will init the winsock stuff */
  230. ::curl_global_init(CURL_GLOBAL_ALL);
  231. /* get a curl handle */
  232. curl = curl_easy_init();
  233. if(curl)
  234. {
  235. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  236. std::string::size_type cc, kk;
  237. for ( cc = 0; cc < files.size(); cc ++ )
  238. {
  239. std::string file = remoteprefix + files[cc];
  240. std::string ofile = "";
  241. for ( kk = 0; kk < file.size(); kk ++ )
  242. {
  243. char c = file[kk];
  244. char hex[4] = { 0, 0, 0, 0 };
  245. hex[0] = c;
  246. switch ( c )
  247. {
  248. case '+':
  249. case '?':
  250. case '/':
  251. case '\\':
  252. case '&':
  253. case ' ':
  254. case '=':
  255. case '%':
  256. sprintf(hex, "%%%02X", (int)c);
  257. ofile.append(hex);
  258. break;
  259. break;
  260. default:
  261. ofile.append(hex);
  262. }
  263. }
  264. std::string turl = url + "?xmlfile=" + ofile;
  265. std::cout << "Trigger url: " << turl.c_str() << std::endl;
  266. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  267. res = curl_easy_perform(curl);
  268. if ( res )
  269. {
  270. std::cout << "Error when uploading" << std::endl;
  271. ::curl_easy_cleanup(curl);
  272. ::curl_global_cleanup();
  273. return false;
  274. }
  275. }
  276. // always cleanup
  277. ::curl_easy_cleanup(curl);
  278. }
  279. ::curl_global_cleanup();
  280. return true;
  281. }
  282. bool cmCTestSubmit::SubmitUsingSCP(const std::string& localprefix,
  283. const std::vector<std::string>& files,
  284. const std::string& remoteprefix,
  285. const std::string& url)
  286. {
  287. std::cout << "SubmitUsingSCP is not yet implemented" << std::endl;
  288. return false;
  289. }