cmCTestSubmitHandler.cxx 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestSubmitHandler.h"
  11. #include "cmCTest.h"
  12. #include "cmCTestCurl.h"
  13. #include "cmCTestScriptHandler.h"
  14. #include "cmCurl.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmState.h"
  17. #include "cmSystemTools.h"
  18. #include "cmXMLParser.h"
  19. #include "cmake.h"
  20. #include <cm_curl.h>
  21. #include <cm_jsoncpp_reader.h>
  22. #include <cm_jsoncpp_value.h>
  23. #include <cmsys/Process.h>
  24. #include <sstream>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #if defined(CTEST_USE_XMLRPC)
  28. #include "cmVersion.h"
  29. #include <cm_xmlrpc.h>
  30. #include <sys/stat.h>
  31. #endif
  32. #define SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT 120
  33. typedef std::vector<char> cmCTestSubmitHandlerVectorOfChar;
  34. class cmCTestSubmitHandler::ResponseParser : public cmXMLParser
  35. {
  36. public:
  37. ResponseParser() { this->Status = STATUS_OK; }
  38. ~ResponseParser() CM_OVERRIDE {}
  39. public:
  40. enum StatusType
  41. {
  42. STATUS_OK,
  43. STATUS_WARNING,
  44. STATUS_ERROR
  45. };
  46. StatusType Status;
  47. std::string CDashVersion;
  48. std::string Filename;
  49. std::string MD5;
  50. std::string Message;
  51. private:
  52. std::vector<char> CurrentValue;
  53. std::string GetCurrentValue()
  54. {
  55. std::string val;
  56. if (!this->CurrentValue.empty()) {
  57. val.assign(&this->CurrentValue[0], this->CurrentValue.size());
  58. }
  59. return val;
  60. }
  61. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  62. {
  63. this->CurrentValue.clear();
  64. if (name == "cdash") {
  65. this->CDashVersion = this->FindAttribute(atts, "version");
  66. }
  67. }
  68. void CharacterDataHandler(const char* data, int length) CM_OVERRIDE
  69. {
  70. this->CurrentValue.insert(this->CurrentValue.end(), data, data + length);
  71. }
  72. void EndElement(const std::string& name) CM_OVERRIDE
  73. {
  74. if (name == "status") {
  75. std::string status = cmSystemTools::UpperCase(this->GetCurrentValue());
  76. if (status == "OK" || status == "SUCCESS") {
  77. this->Status = STATUS_OK;
  78. } else if (status == "WARNING") {
  79. this->Status = STATUS_WARNING;
  80. } else {
  81. this->Status = STATUS_ERROR;
  82. }
  83. } else if (name == "filename") {
  84. this->Filename = this->GetCurrentValue();
  85. } else if (name == "md5") {
  86. this->MD5 = this->GetCurrentValue();
  87. } else if (name == "message") {
  88. this->Message = this->GetCurrentValue();
  89. }
  90. }
  91. };
  92. static size_t cmCTestSubmitHandlerWriteMemoryCallback(void* ptr, size_t size,
  93. size_t nmemb, void* data)
  94. {
  95. int realsize = (int)(size * nmemb);
  96. cmCTestSubmitHandlerVectorOfChar* vec =
  97. static_cast<cmCTestSubmitHandlerVectorOfChar*>(data);
  98. const char* chPtr = static_cast<char*>(ptr);
  99. vec->insert(vec->end(), chPtr, chPtr + realsize);
  100. return realsize;
  101. }
  102. static size_t cmCTestSubmitHandlerCurlDebugCallback(CURL* /*unused*/,
  103. curl_infotype /*unused*/,
  104. char* chPtr, size_t size,
  105. void* data)
  106. {
  107. cmCTestSubmitHandlerVectorOfChar* vec =
  108. static_cast<cmCTestSubmitHandlerVectorOfChar*>(data);
  109. vec->insert(vec->end(), chPtr, chPtr + size);
  110. return size;
  111. }
  112. cmCTestSubmitHandler::cmCTestSubmitHandler()
  113. : HTTPProxy()
  114. , FTPProxy()
  115. {
  116. this->Initialize();
  117. }
  118. void cmCTestSubmitHandler::Initialize()
  119. {
  120. // We submit all available parts by default.
  121. for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
  122. p = cmCTest::Part(p + 1)) {
  123. this->SubmitPart[p] = true;
  124. }
  125. this->CDash = false;
  126. this->HasWarnings = false;
  127. this->HasErrors = false;
  128. this->Superclass::Initialize();
  129. this->HTTPProxy = "";
  130. this->HTTPProxyType = 0;
  131. this->HTTPProxyAuth = "";
  132. this->FTPProxy = "";
  133. this->FTPProxyType = 0;
  134. this->LogFile = CM_NULLPTR;
  135. this->Files.clear();
  136. }
  137. bool cmCTestSubmitHandler::SubmitUsingFTP(const std::string& localprefix,
  138. const std::set<std::string>& files,
  139. const std::string& remoteprefix,
  140. const std::string& url)
  141. {
  142. CURL* curl;
  143. CURLcode res;
  144. FILE* ftpfile;
  145. char error_buffer[1024];
  146. /* In windows, this will init the winsock stuff */
  147. ::curl_global_init(CURL_GLOBAL_ALL);
  148. cmCTest::SetOfStrings::const_iterator file;
  149. for (file = files.begin(); file != files.end(); ++file) {
  150. /* get a curl handle */
  151. curl = curl_easy_init();
  152. if (curl) {
  153. // Using proxy
  154. if (this->FTPProxyType > 0) {
  155. curl_easy_setopt(curl, CURLOPT_PROXY, this->FTPProxy.c_str());
  156. switch (this->FTPProxyType) {
  157. case 2:
  158. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  159. break;
  160. case 3:
  161. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  162. break;
  163. default:
  164. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  165. }
  166. }
  167. // enable uploading
  168. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  169. // if there is little to no activity for too long stop submitting
  170. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  171. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,
  172. SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
  173. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  174. std::string local_file = *file;
  175. if (!cmSystemTools::FileExists(local_file.c_str())) {
  176. local_file = localprefix + "/" + *file;
  177. }
  178. std::string upload_as =
  179. url + "/" + remoteprefix + cmSystemTools::GetFilenameName(*file);
  180. if (!cmSystemTools::FileExists(local_file.c_str())) {
  181. cmCTestLog(this->CTest, ERROR_MESSAGE,
  182. " Cannot find file: " << local_file << std::endl);
  183. ::curl_easy_cleanup(curl);
  184. ::curl_global_cleanup();
  185. return false;
  186. }
  187. unsigned long filelen = cmSystemTools::FileLength(local_file);
  188. ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  189. *this->LogFile << "\tUpload file: " << local_file << " to " << upload_as
  190. << std::endl;
  191. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  192. " Upload file: " << local_file << " to "
  193. << upload_as << std::endl,
  194. this->Quiet);
  195. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  196. // specify target
  197. ::curl_easy_setopt(curl, CURLOPT_URL, upload_as.c_str());
  198. // now specify which file to upload
  199. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  200. // and give the size of the upload (optional)
  201. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(filelen));
  202. // and give curl the buffer for errors
  203. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  204. // specify handler for output
  205. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  206. cmCTestSubmitHandlerWriteMemoryCallback);
  207. ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  208. cmCTestSubmitHandlerCurlDebugCallback);
  209. /* we pass our 'chunk' struct to the callback function */
  210. cmCTestSubmitHandlerVectorOfChar chunk;
  211. cmCTestSubmitHandlerVectorOfChar chunkDebug;
  212. ::curl_easy_setopt(curl, CURLOPT_FILE, (void*)&chunk);
  213. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void*)&chunkDebug);
  214. // Now run off and do what you've been told!
  215. res = ::curl_easy_perform(curl);
  216. if (!chunk.empty()) {
  217. cmCTestOptionalLog(this->CTest, DEBUG, "CURL output: ["
  218. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  219. << "]" << std::endl,
  220. this->Quiet);
  221. }
  222. if (!chunkDebug.empty()) {
  223. cmCTestOptionalLog(
  224. this->CTest, DEBUG, "CURL debug output: ["
  225. << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
  226. << std::endl,
  227. this->Quiet);
  228. }
  229. fclose(ftpfile);
  230. if (res) {
  231. cmCTestLog(this->CTest, ERROR_MESSAGE, " Error when uploading file: "
  232. << local_file << std::endl);
  233. cmCTestLog(this->CTest, ERROR_MESSAGE,
  234. " Error message was: " << error_buffer << std::endl);
  235. *this->LogFile << " Error when uploading file: " << local_file
  236. << std::endl
  237. << " Error message was: " << error_buffer << std::endl
  238. << " Curl output was: ";
  239. // avoid dereference of empty vector
  240. if (!chunk.empty()) {
  241. *this->LogFile << cmCTestLogWrite(&*chunk.begin(), chunk.size());
  242. cmCTestLog(this->CTest, ERROR_MESSAGE, "CURL output: ["
  243. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  244. << std::endl);
  245. }
  246. *this->LogFile << std::endl;
  247. ::curl_easy_cleanup(curl);
  248. ::curl_global_cleanup();
  249. return false;
  250. }
  251. // always cleanup
  252. ::curl_easy_cleanup(curl);
  253. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  254. " Uploaded: " + local_file << std::endl,
  255. this->Quiet);
  256. }
  257. }
  258. ::curl_global_cleanup();
  259. return true;
  260. }
  261. // Uploading files is simpler
  262. bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix,
  263. const std::set<std::string>& files,
  264. const std::string& remoteprefix,
  265. const std::string& url)
  266. {
  267. CURL* curl;
  268. CURLcode res;
  269. FILE* ftpfile;
  270. char error_buffer[1024];
  271. struct curl_slist* headers =
  272. ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml");
  273. /* In windows, this will init the winsock stuff */
  274. ::curl_global_init(CURL_GLOBAL_ALL);
  275. std::string dropMethod(this->CTest->GetCTestConfiguration("DropMethod"));
  276. std::string curlopt(this->CTest->GetCTestConfiguration("CurlOptions"));
  277. std::vector<std::string> args;
  278. cmSystemTools::ExpandListArgument(curlopt, args);
  279. bool verifyPeerOff = false;
  280. bool verifyHostOff = false;
  281. for (std::vector<std::string>::iterator i = args.begin(); i != args.end();
  282. ++i) {
  283. if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
  284. verifyPeerOff = true;
  285. }
  286. if (*i == "CURLOPT_SSL_VERIFYHOST_OFF") {
  287. verifyHostOff = true;
  288. }
  289. }
  290. std::string::size_type kk;
  291. cmCTest::SetOfStrings::const_iterator file;
  292. for (file = files.begin(); file != files.end(); ++file) {
  293. /* get a curl handle */
  294. curl = curl_easy_init();
  295. if (curl) {
  296. cmCurlSetCAInfo(curl);
  297. if (verifyPeerOff) {
  298. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  299. " Set CURLOPT_SSL_VERIFYPEER to off\n",
  300. this->Quiet);
  301. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  302. }
  303. if (verifyHostOff) {
  304. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  305. " Set CURLOPT_SSL_VERIFYHOST to off\n",
  306. this->Quiet);
  307. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  308. }
  309. // Using proxy
  310. if (this->HTTPProxyType > 0) {
  311. curl_easy_setopt(curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  312. switch (this->HTTPProxyType) {
  313. case 2:
  314. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  315. break;
  316. case 3:
  317. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  318. break;
  319. default:
  320. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  321. if (!this->HTTPProxyAuth.empty()) {
  322. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,
  323. this->HTTPProxyAuth.c_str());
  324. }
  325. }
  326. }
  327. if (this->CTest->ShouldUseHTTP10()) {
  328. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  329. }
  330. // enable HTTP ERROR parsing
  331. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  332. /* enable uploading */
  333. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  334. // if there is little to no activity for too long stop submitting
  335. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  336. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,
  337. SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
  338. /* HTTP PUT please */
  339. ::curl_easy_setopt(curl, CURLOPT_PUT, 1);
  340. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  341. // Be sure to set Content-Type to satisfy fussy modsecurity rules
  342. ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  343. std::string local_file = *file;
  344. if (!cmSystemTools::FileExists(local_file.c_str())) {
  345. local_file = localprefix + "/" + *file;
  346. }
  347. std::string remote_file =
  348. remoteprefix + cmSystemTools::GetFilenameName(*file);
  349. *this->LogFile << "\tUpload file: " << local_file << " to "
  350. << remote_file << std::endl;
  351. std::string ofile = "";
  352. for (kk = 0; kk < remote_file.size(); kk++) {
  353. char c = remote_file[kk];
  354. char hexCh[4] = { 0, 0, 0, 0 };
  355. hexCh[0] = c;
  356. switch (c) {
  357. case '+':
  358. case '?':
  359. case '/':
  360. case '\\':
  361. case '&':
  362. case ' ':
  363. case '=':
  364. case '%':
  365. sprintf(hexCh, "%%%02X", (int)c);
  366. ofile.append(hexCh);
  367. break;
  368. default:
  369. ofile.append(hexCh);
  370. }
  371. }
  372. std::string upload_as = url +
  373. ((url.find('?') == std::string::npos) ? '?' : '&') + "FileName=" +
  374. ofile;
  375. upload_as += "&MD5=";
  376. if (cmSystemTools::IsOn(this->GetOption("InternalTest"))) {
  377. upload_as += "bad_md5sum";
  378. } else {
  379. char md5[33];
  380. cmSystemTools::ComputeFileMD5(local_file, md5);
  381. md5[32] = 0;
  382. upload_as += md5;
  383. }
  384. if (!cmSystemTools::FileExists(local_file.c_str())) {
  385. cmCTestLog(this->CTest, ERROR_MESSAGE,
  386. " Cannot find file: " << local_file << std::endl);
  387. ::curl_easy_cleanup(curl);
  388. ::curl_slist_free_all(headers);
  389. ::curl_global_cleanup();
  390. return false;
  391. }
  392. unsigned long filelen = cmSystemTools::FileLength(local_file);
  393. ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  394. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  395. " Upload file: " << local_file << " to "
  396. << upload_as << " Size: "
  397. << filelen << std::endl,
  398. this->Quiet);
  399. // specify target
  400. ::curl_easy_setopt(curl, CURLOPT_URL, upload_as.c_str());
  401. // now specify which file to upload
  402. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  403. // and give the size of the upload (optional)
  404. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(filelen));
  405. // and give curl the buffer for errors
  406. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  407. // specify handler for output
  408. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  409. cmCTestSubmitHandlerWriteMemoryCallback);
  410. ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  411. cmCTestSubmitHandlerCurlDebugCallback);
  412. /* we pass our 'chunk' struct to the callback function */
  413. cmCTestSubmitHandlerVectorOfChar chunk;
  414. cmCTestSubmitHandlerVectorOfChar chunkDebug;
  415. ::curl_easy_setopt(curl, CURLOPT_FILE, (void*)&chunk);
  416. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void*)&chunkDebug);
  417. // Now run off and do what you've been told!
  418. res = ::curl_easy_perform(curl);
  419. if (cmSystemTools::IsOn(this->GetOption("InternalTest")) &&
  420. cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  421. this->CTest->GetCDashVersion().c_str(),
  422. "1.7")) {
  423. // mock failure output for internal test case
  424. std::string mock_output =
  425. "<cdash version=\"1.7.0\">\n"
  426. " <status>ERROR</status>\n"
  427. " <message>Checksum failed for file.</message>\n"
  428. "</cdash>\n";
  429. chunk.clear();
  430. chunk.assign(mock_output.begin(), mock_output.end());
  431. }
  432. if (!chunk.empty()) {
  433. cmCTestOptionalLog(this->CTest, DEBUG, "CURL output: ["
  434. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  435. << "]" << std::endl,
  436. this->Quiet);
  437. this->ParseResponse(chunk);
  438. }
  439. if (!chunkDebug.empty()) {
  440. cmCTestOptionalLog(
  441. this->CTest, DEBUG, "CURL debug output: ["
  442. << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
  443. << std::endl,
  444. this->Quiet);
  445. }
  446. // If curl failed for any reason, or checksum fails, wait and retry
  447. //
  448. if (res != CURLE_OK || this->HasErrors) {
  449. std::string retryDelay = this->GetOption("RetryDelay") == CM_NULLPTR
  450. ? ""
  451. : this->GetOption("RetryDelay");
  452. std::string retryCount = this->GetOption("RetryCount") == CM_NULLPTR
  453. ? ""
  454. : this->GetOption("RetryCount");
  455. int delay = retryDelay == ""
  456. ? atoi(this->CTest->GetCTestConfiguration("CTestSubmitRetryDelay")
  457. .c_str())
  458. : atoi(retryDelay.c_str());
  459. int count = retryCount == ""
  460. ? atoi(this->CTest->GetCTestConfiguration("CTestSubmitRetryCount")
  461. .c_str())
  462. : atoi(retryCount.c_str());
  463. for (int i = 0; i < count; i++) {
  464. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  465. " Submit failed, waiting " << delay
  466. << " seconds...\n",
  467. this->Quiet);
  468. double stop = cmSystemTools::GetTime() + delay;
  469. while (cmSystemTools::GetTime() < stop) {
  470. cmSystemTools::Delay(100);
  471. }
  472. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  473. " Retry submission: Attempt "
  474. << (i + 1) << " of " << count << std::endl,
  475. this->Quiet);
  476. ::fclose(ftpfile);
  477. ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  478. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  479. chunk.clear();
  480. chunkDebug.clear();
  481. this->HasErrors = false;
  482. res = ::curl_easy_perform(curl);
  483. if (!chunk.empty()) {
  484. cmCTestOptionalLog(
  485. this->CTest, DEBUG, "CURL output: ["
  486. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  487. << std::endl,
  488. this->Quiet);
  489. this->ParseResponse(chunk);
  490. }
  491. if (res == CURLE_OK && !this->HasErrors) {
  492. break;
  493. }
  494. }
  495. }
  496. fclose(ftpfile);
  497. if (res) {
  498. cmCTestLog(this->CTest, ERROR_MESSAGE, " Error when uploading file: "
  499. << local_file << std::endl);
  500. cmCTestLog(this->CTest, ERROR_MESSAGE,
  501. " Error message was: " << error_buffer << std::endl);
  502. *this->LogFile << " Error when uploading file: " << local_file
  503. << std::endl
  504. << " Error message was: " << error_buffer
  505. << std::endl;
  506. // avoid deref of begin for zero size array
  507. if (!chunk.empty()) {
  508. *this->LogFile << " Curl output was: "
  509. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  510. << std::endl;
  511. cmCTestLog(this->CTest, ERROR_MESSAGE, "CURL output: ["
  512. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  513. << std::endl);
  514. }
  515. ::curl_easy_cleanup(curl);
  516. ::curl_slist_free_all(headers);
  517. ::curl_global_cleanup();
  518. return false;
  519. }
  520. // always cleanup
  521. ::curl_easy_cleanup(curl);
  522. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  523. " Uploaded: " + local_file << std::endl,
  524. this->Quiet);
  525. }
  526. }
  527. ::curl_slist_free_all(headers);
  528. ::curl_global_cleanup();
  529. return true;
  530. }
  531. void cmCTestSubmitHandler::ParseResponse(
  532. cmCTestSubmitHandlerVectorOfChar chunk)
  533. {
  534. std::string output = "";
  535. output.append(chunk.begin(), chunk.end());
  536. if (output.find("<cdash") != output.npos) {
  537. ResponseParser parser;
  538. parser.Parse(output.c_str());
  539. if (parser.Status != ResponseParser::STATUS_OK) {
  540. this->HasErrors = true;
  541. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  542. " Submission failed: " << parser.Message << std::endl);
  543. return;
  544. }
  545. }
  546. output = cmSystemTools::UpperCase(output);
  547. if (output.find("WARNING") != std::string::npos) {
  548. this->HasWarnings = true;
  549. }
  550. if (output.find("ERROR") != std::string::npos) {
  551. this->HasErrors = true;
  552. }
  553. if (this->HasWarnings || this->HasErrors) {
  554. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Server Response:\n"
  555. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "\n");
  556. }
  557. }
  558. bool cmCTestSubmitHandler::TriggerUsingHTTP(const std::set<std::string>& files,
  559. const std::string& remoteprefix,
  560. const std::string& url)
  561. {
  562. CURL* curl;
  563. char error_buffer[1024];
  564. /* In windows, this will init the winsock stuff */
  565. ::curl_global_init(CURL_GLOBAL_ALL);
  566. cmCTest::SetOfStrings::const_iterator file;
  567. for (file = files.begin(); file != files.end(); ++file) {
  568. /* get a curl handle */
  569. curl = curl_easy_init();
  570. if (curl) {
  571. // Using proxy
  572. if (this->HTTPProxyType > 0) {
  573. curl_easy_setopt(curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  574. switch (this->HTTPProxyType) {
  575. case 2:
  576. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  577. break;
  578. case 3:
  579. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  580. break;
  581. default:
  582. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  583. if (!this->HTTPProxyAuth.empty()) {
  584. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,
  585. this->HTTPProxyAuth.c_str());
  586. }
  587. }
  588. }
  589. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  590. // and give curl the buffer for errors
  591. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  592. // specify handler for output
  593. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  594. cmCTestSubmitHandlerWriteMemoryCallback);
  595. ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  596. cmCTestSubmitHandlerCurlDebugCallback);
  597. /* we pass our 'chunk' struct to the callback function */
  598. cmCTestSubmitHandlerVectorOfChar chunk;
  599. cmCTestSubmitHandlerVectorOfChar chunkDebug;
  600. ::curl_easy_setopt(curl, CURLOPT_FILE, (void*)&chunk);
  601. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void*)&chunkDebug);
  602. std::string rfile = remoteprefix + cmSystemTools::GetFilenameName(*file);
  603. std::string ofile = "";
  604. std::string::iterator kk;
  605. for (kk = rfile.begin(); kk < rfile.end(); ++kk) {
  606. char c = *kk;
  607. char hexCh[4] = { 0, 0, 0, 0 };
  608. hexCh[0] = c;
  609. switch (c) {
  610. case '+':
  611. case '?':
  612. case '/':
  613. case '\\':
  614. case '&':
  615. case ' ':
  616. case '=':
  617. case '%':
  618. sprintf(hexCh, "%%%02X", (int)c);
  619. ofile.append(hexCh);
  620. break;
  621. default:
  622. ofile.append(hexCh);
  623. }
  624. }
  625. std::string turl = url +
  626. ((url.find('?') == std::string::npos) ? '?' : '&') + "xmlfile=" +
  627. ofile;
  628. *this->LogFile << "Trigger url: " << turl << std::endl;
  629. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  630. " Trigger url: " << turl << std::endl, this->Quiet);
  631. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  632. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  633. if (curl_easy_perform(curl)) {
  634. cmCTestLog(this->CTest, ERROR_MESSAGE,
  635. " Error when triggering: " << turl << std::endl);
  636. cmCTestLog(this->CTest, ERROR_MESSAGE,
  637. " Error message was: " << error_buffer << std::endl);
  638. *this->LogFile << "\tTriggering failed with error: " << error_buffer
  639. << std::endl
  640. << " Error message was: " << error_buffer
  641. << std::endl;
  642. if (!chunk.empty()) {
  643. *this->LogFile << " Curl output was: "
  644. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  645. << std::endl;
  646. cmCTestLog(this->CTest, ERROR_MESSAGE, "CURL output: ["
  647. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  648. << std::endl);
  649. }
  650. ::curl_easy_cleanup(curl);
  651. ::curl_global_cleanup();
  652. return false;
  653. }
  654. if (!chunk.empty()) {
  655. cmCTestOptionalLog(this->CTest, DEBUG, "CURL output: ["
  656. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  657. << "]" << std::endl,
  658. this->Quiet);
  659. }
  660. if (!chunkDebug.empty()) {
  661. cmCTestOptionalLog(
  662. this->CTest, DEBUG, "CURL debug output: ["
  663. << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
  664. << std::endl,
  665. this->Quiet);
  666. }
  667. // always cleanup
  668. ::curl_easy_cleanup(curl);
  669. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
  670. this->Quiet);
  671. }
  672. }
  673. ::curl_global_cleanup();
  674. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  675. " Dart server triggered..." << std::endl, this->Quiet);
  676. return true;
  677. }
  678. bool cmCTestSubmitHandler::SubmitUsingSCP(const std::string& scp_command,
  679. const std::string& localprefix,
  680. const std::set<std::string>& files,
  681. const std::string& remoteprefix,
  682. const std::string& url)
  683. {
  684. if (scp_command.empty() || localprefix.empty() || files.empty() ||
  685. remoteprefix.empty() || url.empty()) {
  686. return 0;
  687. }
  688. std::vector<const char*> argv;
  689. argv.push_back(scp_command.c_str()); // Scp command
  690. argv.push_back(scp_command.c_str()); // Dummy string for file
  691. argv.push_back(scp_command.c_str()); // Dummy string for remote url
  692. argv.push_back(CM_NULLPTR);
  693. cmsysProcess* cp = cmsysProcess_New();
  694. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  695. // cmsysProcess_SetTimeout(cp, timeout);
  696. int problems = 0;
  697. cmCTest::SetOfStrings::const_iterator file;
  698. for (file = files.begin(); file != files.end(); ++file) {
  699. int retVal;
  700. std::string lfname = localprefix;
  701. cmSystemTools::ConvertToUnixSlashes(lfname);
  702. lfname += "/" + *file;
  703. lfname = cmSystemTools::ConvertToOutputPath(lfname.c_str());
  704. argv[1] = lfname.c_str();
  705. std::string rfname = url + "/" + remoteprefix + *file;
  706. argv[2] = rfname.c_str();
  707. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Execute \""
  708. << argv[0] << "\" \"" << argv[1] << "\" \"" << argv[2]
  709. << "\"" << std::endl,
  710. this->Quiet);
  711. *this->LogFile << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
  712. << argv[2] << "\"" << std::endl;
  713. cmsysProcess_SetCommand(cp, &*argv.begin());
  714. cmsysProcess_Execute(cp);
  715. char* data;
  716. int length;
  717. while (cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR)) {
  718. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  719. cmCTestLogWrite(data, length), this->Quiet);
  720. }
  721. cmsysProcess_WaitForExit(cp, CM_NULLPTR);
  722. int result = cmsysProcess_GetState(cp);
  723. if (result == cmsysProcess_State_Exited) {
  724. retVal = cmsysProcess_GetExitValue(cp);
  725. if (retVal != 0) {
  726. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  727. "\tSCP returned: " << retVal << std::endl,
  728. this->Quiet);
  729. *this->LogFile << "\tSCP returned: " << retVal << std::endl;
  730. problems++;
  731. }
  732. } else if (result == cmsysProcess_State_Exception) {
  733. retVal = cmsysProcess_GetExitException(cp);
  734. cmCTestLog(this->CTest, ERROR_MESSAGE,
  735. "\tThere was an exception: " << retVal << std::endl);
  736. *this->LogFile << "\tThere was an exception: " << retVal << std::endl;
  737. problems++;
  738. } else if (result == cmsysProcess_State_Expired) {
  739. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tThere was a timeout"
  740. << std::endl);
  741. *this->LogFile << "\tThere was a timeout" << std::endl;
  742. problems++;
  743. } else if (result == cmsysProcess_State_Error) {
  744. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tError executing SCP: "
  745. << cmsysProcess_GetErrorString(cp) << std::endl);
  746. *this->LogFile << "\tError executing SCP: "
  747. << cmsysProcess_GetErrorString(cp) << std::endl;
  748. problems++;
  749. }
  750. }
  751. cmsysProcess_Delete(cp);
  752. return problems == 0;
  753. }
  754. bool cmCTestSubmitHandler::SubmitUsingCP(const std::string& localprefix,
  755. const std::set<std::string>& files,
  756. const std::string& remoteprefix,
  757. const std::string& destination)
  758. {
  759. if (localprefix.empty() || files.empty() || remoteprefix.empty() ||
  760. destination.empty()) {
  761. /* clang-format off */
  762. cmCTestLog(this->CTest, ERROR_MESSAGE,
  763. "Missing arguments for submit via cp:\n"
  764. << "\tlocalprefix: " << localprefix << "\n"
  765. << "\tNumber of files: " << files.size() << "\n"
  766. << "\tremoteprefix: " << remoteprefix << "\n"
  767. << "\tdestination: " << destination << std::endl);
  768. /* clang-format on */
  769. return 0;
  770. }
  771. cmCTest::SetOfStrings::const_iterator file;
  772. for (file = files.begin(); file != files.end(); ++file) {
  773. std::string lfname = localprefix;
  774. cmSystemTools::ConvertToUnixSlashes(lfname);
  775. lfname += "/" + *file;
  776. std::string rfname = destination + "/" + remoteprefix + *file;
  777. cmSystemTools::CopyFileAlways(lfname, rfname);
  778. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " Copy file: "
  779. << lfname << " to " << rfname << std::endl,
  780. this->Quiet);
  781. }
  782. std::string tagDoneFile = destination + "/" + remoteprefix + "DONE";
  783. cmSystemTools::Touch(tagDoneFile, true);
  784. return true;
  785. }
  786. #if defined(CTEST_USE_XMLRPC)
  787. bool cmCTestSubmitHandler::SubmitUsingXMLRPC(
  788. const std::string& localprefix, const std::set<std::string>& files,
  789. const std::string& remoteprefix, const std::string& url)
  790. {
  791. xmlrpc_env env;
  792. char ctestString[] = "CTest";
  793. std::string ctestVersionString = cmVersion::GetCMakeVersion();
  794. char* ctestVersion = const_cast<char*>(ctestVersionString.c_str());
  795. std::string realURL = url + "/" + remoteprefix + "/Command/";
  796. /* Start up our XML-RPC client library. */
  797. xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, ctestString, ctestVersion);
  798. /* Initialize our error-handling environment. */
  799. xmlrpc_env_init(&env);
  800. /* Call the famous server at UserLand. */
  801. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Submitting to: "
  802. << realURL << " (" << remoteprefix << ")" << std::endl,
  803. this->Quiet);
  804. cmCTest::SetOfStrings::const_iterator file;
  805. for (file = files.begin(); file != files.end(); ++file) {
  806. xmlrpc_value* result;
  807. std::string local_file = *file;
  808. if (!cmSystemTools::FileExists(local_file.c_str())) {
  809. local_file = localprefix + "/" + *file;
  810. }
  811. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  812. " Submit file: " << local_file << std::endl,
  813. this->Quiet);
  814. struct stat st;
  815. if (::stat(local_file.c_str(), &st)) {
  816. cmCTestLog(this->CTest, ERROR_MESSAGE,
  817. " Cannot find file: " << local_file << std::endl);
  818. return false;
  819. }
  820. // off_t can be bigger than size_t. fread takes size_t.
  821. // make sure the file is not too big.
  822. if (static_cast<off_t>(static_cast<size_t>(st.st_size)) !=
  823. static_cast<off_t>(st.st_size)) {
  824. cmCTestLog(this->CTest, ERROR_MESSAGE, " File too big: " << local_file
  825. << std::endl);
  826. return false;
  827. }
  828. size_t fileSize = static_cast<size_t>(st.st_size);
  829. FILE* fp = cmsys::SystemTools::Fopen(local_file.c_str(), "rb");
  830. if (!fp) {
  831. cmCTestLog(this->CTest, ERROR_MESSAGE,
  832. " Cannot open file: " << local_file << std::endl);
  833. return false;
  834. }
  835. unsigned char* fileBuffer = new unsigned char[fileSize];
  836. if (fread(fileBuffer, 1, fileSize, fp) != fileSize) {
  837. delete[] fileBuffer;
  838. fclose(fp);
  839. cmCTestLog(this->CTest, ERROR_MESSAGE,
  840. " Cannot read file: " << local_file << std::endl);
  841. return false;
  842. }
  843. fclose(fp);
  844. char remoteCommand[] = "Submit.put";
  845. char* pRealURL = const_cast<char*>(realURL.c_str());
  846. result = xmlrpc_client_call(&env, pRealURL, remoteCommand, "(6)",
  847. fileBuffer, (xmlrpc_int32)fileSize);
  848. delete[] fileBuffer;
  849. if (env.fault_occurred) {
  850. cmCTestLog(this->CTest, ERROR_MESSAGE, " Submission problem: "
  851. << env.fault_string << " (" << env.fault_code << ")"
  852. << std::endl);
  853. xmlrpc_env_clean(&env);
  854. xmlrpc_client_cleanup();
  855. return false;
  856. }
  857. /* Dispose of our result value. */
  858. xmlrpc_DECREF(result);
  859. }
  860. /* Clean up our error-handling environment. */
  861. xmlrpc_env_clean(&env);
  862. /* Shutdown our XML-RPC client library. */
  863. xmlrpc_client_cleanup();
  864. return true;
  865. }
  866. #else
  867. bool cmCTestSubmitHandler::SubmitUsingXMLRPC(
  868. std::string const& /*unused*/, std::set<std::string> const& /*unused*/,
  869. std::string const& /*unused*/, std::string const& /*unused*/)
  870. {
  871. return false;
  872. }
  873. #endif
  874. void cmCTestSubmitHandler::ConstructCDashURL(std::string& dropMethod,
  875. std::string& url)
  876. {
  877. dropMethod = this->CTest->GetCTestConfiguration("DropMethod");
  878. url = dropMethod;
  879. url += "://";
  880. if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
  881. url += this->CTest->GetCTestConfiguration("DropSiteUser");
  882. cmCTestOptionalLog(
  883. this->CTest, HANDLER_OUTPUT,
  884. this->CTest->GetCTestConfiguration("DropSiteUser").c_str(), this->Quiet);
  885. if (!this->CTest->GetCTestConfiguration("DropSitePassword").empty()) {
  886. url += ":" + this->CTest->GetCTestConfiguration("DropSitePassword");
  887. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, ":******", this->Quiet);
  888. }
  889. url += "@";
  890. }
  891. url += this->CTest->GetCTestConfiguration("DropSite") +
  892. this->CTest->GetCTestConfiguration("DropLocation");
  893. }
  894. int cmCTestSubmitHandler::HandleCDashUploadFile(std::string const& file,
  895. std::string const& typeString)
  896. {
  897. if (file.empty()) {
  898. cmCTestLog(this->CTest, ERROR_MESSAGE, "Upload file not specified\n");
  899. return -1;
  900. }
  901. if (!cmSystemTools::FileExists(file)) {
  902. cmCTestLog(this->CTest, ERROR_MESSAGE, "Upload file not found: '"
  903. << file << "'\n");
  904. return -1;
  905. }
  906. cmCTestCurl curl(this->CTest);
  907. std::string curlopt(this->CTest->GetCTestConfiguration("CurlOptions"));
  908. std::vector<std::string> args;
  909. cmSystemTools::ExpandListArgument(curlopt, args);
  910. curl.SetCurlOptions(args);
  911. curl.SetTimeOutSeconds(SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
  912. std::string dropMethod;
  913. std::string url;
  914. this->ConstructCDashURL(dropMethod, url);
  915. std::string::size_type pos = url.find("submit.php?");
  916. url = url.substr(0, pos + 10);
  917. if (!(dropMethod == "http" || dropMethod == "https")) {
  918. cmCTestLog(this->CTest, ERROR_MESSAGE,
  919. "Only http and https are supported for CDASH_UPLOAD\n");
  920. return -1;
  921. }
  922. char md5sum[33];
  923. md5sum[32] = 0;
  924. cmSystemTools::ComputeFileMD5(file, md5sum);
  925. // 1. request the buildid and check to see if the file
  926. // has already been uploaded
  927. // TODO I added support for subproject. You would need to add
  928. // a "&subproject=subprojectname" to the first POST.
  929. cmCTestScriptHandler* ch =
  930. static_cast<cmCTestScriptHandler*>(this->CTest->GetHandler("script"));
  931. cmake* cm = ch->GetCMake();
  932. const char* subproject = cm->GetState()->GetGlobalProperty("SubProject");
  933. // TODO: Encode values for a URL instead of trusting caller.
  934. std::ostringstream str;
  935. str << "project="
  936. << curl.Escape(this->CTest->GetCTestConfiguration("ProjectName")) << "&";
  937. if (subproject) {
  938. str << "subproject=" << curl.Escape(subproject) << "&";
  939. }
  940. str << "stamp=" << curl.Escape(this->CTest->GetCurrentTag()) << "-"
  941. << curl.Escape(this->CTest->GetTestModelString()) << "&"
  942. << "model=" << curl.Escape(this->CTest->GetTestModelString()) << "&"
  943. << "build="
  944. << curl.Escape(this->CTest->GetCTestConfiguration("BuildName")) << "&"
  945. << "site=" << curl.Escape(this->CTest->GetCTestConfiguration("Site"))
  946. << "&"
  947. << "track=" << curl.Escape(this->CTest->GetTestModelString()) << "&"
  948. << "starttime=" << (int)cmSystemTools::GetTime() << "&"
  949. << "endtime=" << (int)cmSystemTools::GetTime() << "&"
  950. << "datafilesmd5[0]=" << md5sum << "&"
  951. << "type=" << curl.Escape(typeString);
  952. std::string fields = str.str();
  953. cmCTestOptionalLog(this->CTest, DEBUG,
  954. "fields: " << fields << "\nurl:" << url
  955. << "\nfile: " << file << "\n",
  956. this->Quiet);
  957. std::string response;
  958. if (!curl.HttpRequest(url, fields, response)) {
  959. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error in HttpRequest\n"
  960. << response);
  961. return -1;
  962. }
  963. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  964. "Request upload response: [" << response << "]\n",
  965. this->Quiet);
  966. Json::Value json;
  967. Json::Reader reader;
  968. if (!reader.parse(response, json)) {
  969. cmCTestLog(this->CTest, ERROR_MESSAGE, "error parsing json string ["
  970. << response << "]\n"
  971. << reader.getFormattedErrorMessages() << "\n");
  972. return -1;
  973. }
  974. if (json["status"].asInt() != 0) {
  975. cmCTestLog(this->CTest, ERROR_MESSAGE,
  976. "Bad status returned from CDash: " << json["status"].asInt());
  977. return -1;
  978. }
  979. if (json["datafilesmd5"].isArray()) {
  980. int datares = json["datafilesmd5"][0].asInt();
  981. if (datares == 1) {
  982. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  983. "File already exists on CDash, skip upload " << file
  984. << "\n",
  985. this->Quiet);
  986. return 0;
  987. }
  988. } else {
  989. cmCTestLog(this->CTest, ERROR_MESSAGE,
  990. "bad datafilesmd5 value in response " << response << "\n");
  991. return -1;
  992. }
  993. std::string upload_as = cmSystemTools::GetFilenameName(file);
  994. std::ostringstream fstr;
  995. fstr << "type=" << curl.Escape(typeString) << "&"
  996. << "md5=" << md5sum << "&"
  997. << "filename=" << curl.Escape(upload_as) << "&"
  998. << "buildid=" << json["buildid"].asString();
  999. if (!curl.UploadFile(file, url, fstr.str(), response)) {
  1000. cmCTestLog(this->CTest, ERROR_MESSAGE, "error uploading to CDash. "
  1001. << file << " " << url << " " << fstr.str());
  1002. return -1;
  1003. }
  1004. if (!reader.parse(response, json)) {
  1005. cmCTestLog(this->CTest, ERROR_MESSAGE, "error parsing json string ["
  1006. << response << "]\n"
  1007. << reader.getFormattedErrorMessages() << "\n");
  1008. return -1;
  1009. }
  1010. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1011. "Upload file response: [" << response << "]\n",
  1012. this->Quiet);
  1013. return 0;
  1014. }
  1015. int cmCTestSubmitHandler::ProcessHandler()
  1016. {
  1017. const char* cdashUploadFile = this->GetOption("CDashUploadFile");
  1018. const char* cdashUploadType = this->GetOption("CDashUploadType");
  1019. if (cdashUploadFile && cdashUploadType) {
  1020. return this->HandleCDashUploadFile(cdashUploadFile, cdashUploadType);
  1021. }
  1022. std::string iscdash = this->CTest->GetCTestConfiguration("IsCDash");
  1023. // cdash does not need to trigger so just return true
  1024. if (!iscdash.empty()) {
  1025. this->CDash = true;
  1026. }
  1027. const std::string& buildDirectory =
  1028. this->CTest->GetCTestConfiguration("BuildDirectory");
  1029. if (buildDirectory.empty()) {
  1030. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1031. "Cannot find BuildDirectory key in the DartConfiguration.tcl"
  1032. << std::endl);
  1033. return -1;
  1034. }
  1035. if (getenv("HTTP_PROXY")) {
  1036. this->HTTPProxyType = 1;
  1037. this->HTTPProxy = getenv("HTTP_PROXY");
  1038. if (getenv("HTTP_PROXY_PORT")) {
  1039. this->HTTPProxy += ":";
  1040. this->HTTPProxy += getenv("HTTP_PROXY_PORT");
  1041. }
  1042. if (getenv("HTTP_PROXY_TYPE")) {
  1043. std::string type = getenv("HTTP_PROXY_TYPE");
  1044. // HTTP/SOCKS4/SOCKS5
  1045. if (type == "HTTP") {
  1046. this->HTTPProxyType = 1;
  1047. } else if (type == "SOCKS4") {
  1048. this->HTTPProxyType = 2;
  1049. } else if (type == "SOCKS5") {
  1050. this->HTTPProxyType = 3;
  1051. }
  1052. }
  1053. if (getenv("HTTP_PROXY_USER")) {
  1054. this->HTTPProxyAuth = getenv("HTTP_PROXY_USER");
  1055. }
  1056. if (getenv("HTTP_PROXY_PASSWD")) {
  1057. this->HTTPProxyAuth += ":";
  1058. this->HTTPProxyAuth += getenv("HTTP_PROXY_PASSWD");
  1059. }
  1060. }
  1061. if (getenv("FTP_PROXY")) {
  1062. this->FTPProxyType = 1;
  1063. this->FTPProxy = getenv("FTP_PROXY");
  1064. if (getenv("FTP_PROXY_PORT")) {
  1065. this->FTPProxy += ":";
  1066. this->FTPProxy += getenv("FTP_PROXY_PORT");
  1067. }
  1068. if (getenv("FTP_PROXY_TYPE")) {
  1069. std::string type = getenv("FTP_PROXY_TYPE");
  1070. // HTTP/SOCKS4/SOCKS5
  1071. if (type == "HTTP") {
  1072. this->FTPProxyType = 1;
  1073. } else if (type == "SOCKS4") {
  1074. this->FTPProxyType = 2;
  1075. } else if (type == "SOCKS5") {
  1076. this->FTPProxyType = 3;
  1077. }
  1078. }
  1079. }
  1080. if (!this->HTTPProxy.empty()) {
  1081. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1082. " Use HTTP Proxy: " << this->HTTPProxy << std::endl,
  1083. this->Quiet);
  1084. }
  1085. if (!this->FTPProxy.empty()) {
  1086. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1087. " Use FTP Proxy: " << this->FTPProxy << std::endl,
  1088. this->Quiet);
  1089. }
  1090. cmGeneratedFileStream ofs;
  1091. this->StartLogFile("Submit", ofs);
  1092. cmCTest::SetOfStrings files;
  1093. std::string prefix = this->GetSubmitResultsPrefix();
  1094. if (!this->Files.empty()) {
  1095. // Submit the explicitly selected files:
  1096. //
  1097. files.insert(this->Files.begin(), this->Files.end());
  1098. }
  1099. // Add to the list of files to submit from any selected, existing parts:
  1100. //
  1101. // TODO:
  1102. // Check if test is enabled
  1103. this->CTest->AddIfExists(cmCTest::PartUpdate, "Update.xml");
  1104. this->CTest->AddIfExists(cmCTest::PartConfigure, "Configure.xml");
  1105. this->CTest->AddIfExists(cmCTest::PartBuild, "Build.xml");
  1106. this->CTest->AddIfExists(cmCTest::PartTest, "Test.xml");
  1107. if (this->CTest->AddIfExists(cmCTest::PartCoverage, "Coverage.xml")) {
  1108. std::vector<std::string> gfiles;
  1109. std::string gpath =
  1110. buildDirectory + "/Testing/" + this->CTest->GetCurrentTag();
  1111. std::string::size_type glen = gpath.size() + 1;
  1112. gpath = gpath + "/CoverageLog*";
  1113. cmCTestOptionalLog(this->CTest, DEBUG,
  1114. "Globbing for: " << gpath << std::endl, this->Quiet);
  1115. if (cmSystemTools::SimpleGlob(gpath, gfiles, 1)) {
  1116. size_t cc;
  1117. for (cc = 0; cc < gfiles.size(); cc++) {
  1118. gfiles[cc] = gfiles[cc].substr(glen);
  1119. cmCTestOptionalLog(this->CTest, DEBUG,
  1120. "Glob file: " << gfiles[cc] << std::endl,
  1121. this->Quiet);
  1122. this->CTest->AddSubmitFile(cmCTest::PartCoverage, gfiles[cc].c_str());
  1123. }
  1124. } else {
  1125. cmCTestLog(this->CTest, ERROR_MESSAGE, "Problem globbing" << std::endl);
  1126. }
  1127. }
  1128. this->CTest->AddIfExists(cmCTest::PartMemCheck, "DynamicAnalysis.xml");
  1129. this->CTest->AddIfExists(cmCTest::PartMemCheck, "Purify.xml");
  1130. this->CTest->AddIfExists(cmCTest::PartNotes, "Notes.xml");
  1131. this->CTest->AddIfExists(cmCTest::PartUpload, "Upload.xml");
  1132. // Query parts for files to submit.
  1133. for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
  1134. p = cmCTest::Part(p + 1)) {
  1135. // Skip parts we are not submitting.
  1136. if (!this->SubmitPart[p]) {
  1137. continue;
  1138. }
  1139. // Submit files from this part.
  1140. std::vector<std::string> const& pfiles = this->CTest->GetSubmitFiles(p);
  1141. files.insert(pfiles.begin(), pfiles.end());
  1142. }
  1143. if (ofs) {
  1144. ofs << "Upload files:" << std::endl;
  1145. int cnt = 0;
  1146. cmCTest::SetOfStrings::iterator it;
  1147. for (it = files.begin(); it != files.end(); ++it) {
  1148. ofs << cnt << "\t" << *it << std::endl;
  1149. cnt++;
  1150. }
  1151. }
  1152. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "Submit files (using "
  1153. << this->CTest->GetCTestConfiguration("DropMethod")
  1154. << ")" << std::endl,
  1155. this->Quiet);
  1156. const char* specificTrack = this->CTest->GetSpecificTrack();
  1157. if (specificTrack) {
  1158. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1159. " Send to track: " << specificTrack << std::endl,
  1160. this->Quiet);
  1161. }
  1162. this->SetLogFile(&ofs);
  1163. std::string dropMethod(this->CTest->GetCTestConfiguration("DropMethod"));
  1164. if (dropMethod == "" || dropMethod == "ftp") {
  1165. ofs << "Using drop method: FTP" << std::endl;
  1166. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1167. " Using FTP submit method" << std::endl
  1168. << " Drop site: ftp://",
  1169. this->Quiet);
  1170. std::string url = "ftp://";
  1171. url += cmCTest::MakeURLSafe(
  1172. this->CTest->GetCTestConfiguration("DropSiteUser")) +
  1173. ":" + cmCTest::MakeURLSafe(
  1174. this->CTest->GetCTestConfiguration("DropSitePassword")) +
  1175. "@" + this->CTest->GetCTestConfiguration("DropSite") +
  1176. cmCTest::MakeURLSafe(this->CTest->GetCTestConfiguration("DropLocation"));
  1177. if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
  1178. cmCTestOptionalLog(
  1179. this->CTest, HANDLER_OUTPUT,
  1180. this->CTest->GetCTestConfiguration("DropSiteUser").c_str(),
  1181. this->Quiet);
  1182. if (!this->CTest->GetCTestConfiguration("DropSitePassword").empty()) {
  1183. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, ":******",
  1184. this->Quiet);
  1185. }
  1186. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "@", this->Quiet);
  1187. }
  1188. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1189. this->CTest->GetCTestConfiguration("DropSite")
  1190. << this->CTest->GetCTestConfiguration("DropLocation")
  1191. << std::endl,
  1192. this->Quiet);
  1193. if (!this->SubmitUsingFTP(buildDirectory + "/Testing/" +
  1194. this->CTest->GetCurrentTag(),
  1195. files, prefix, url)) {
  1196. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1197. " Problems when submitting via FTP" << std::endl);
  1198. ofs << " Problems when submitting via FTP" << std::endl;
  1199. return -1;
  1200. }
  1201. if (!this->CDash) {
  1202. cmCTestOptionalLog(
  1203. this->CTest, HANDLER_OUTPUT, " Using HTTP trigger method"
  1204. << std::endl
  1205. << " Trigger site: "
  1206. << this->CTest->GetCTestConfiguration("TriggerSite") << std::endl,
  1207. this->Quiet);
  1208. if (!this->TriggerUsingHTTP(
  1209. files, prefix,
  1210. this->CTest->GetCTestConfiguration("TriggerSite"))) {
  1211. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1212. " Problems when triggering via HTTP" << std::endl);
  1213. ofs << " Problems when triggering via HTTP" << std::endl;
  1214. return -1;
  1215. }
  1216. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1217. " Submission successful" << std::endl, this->Quiet);
  1218. ofs << " Submission successful" << std::endl;
  1219. return 0;
  1220. }
  1221. } else if (dropMethod == "http" || dropMethod == "https") {
  1222. std::string url = dropMethod;
  1223. url += "://";
  1224. ofs << "Using drop method: " << dropMethod << std::endl;
  1225. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1226. " Using HTTP submit method" << std::endl
  1227. << " Drop site:" << url,
  1228. this->Quiet);
  1229. if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
  1230. url += this->CTest->GetCTestConfiguration("DropSiteUser");
  1231. cmCTestOptionalLog(
  1232. this->CTest, HANDLER_OUTPUT,
  1233. this->CTest->GetCTestConfiguration("DropSiteUser").c_str(),
  1234. this->Quiet);
  1235. if (!this->CTest->GetCTestConfiguration("DropSitePassword").empty()) {
  1236. url += ":" + this->CTest->GetCTestConfiguration("DropSitePassword");
  1237. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, ":******",
  1238. this->Quiet);
  1239. }
  1240. url += "@";
  1241. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "@", this->Quiet);
  1242. }
  1243. url += this->CTest->GetCTestConfiguration("DropSite") +
  1244. this->CTest->GetCTestConfiguration("DropLocation");
  1245. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1246. this->CTest->GetCTestConfiguration("DropSite")
  1247. << this->CTest->GetCTestConfiguration("DropLocation")
  1248. << std::endl,
  1249. this->Quiet);
  1250. if (!this->SubmitUsingHTTP(buildDirectory + "/Testing/" +
  1251. this->CTest->GetCurrentTag(),
  1252. files, prefix, url)) {
  1253. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1254. " Problems when submitting via HTTP" << std::endl);
  1255. ofs << " Problems when submitting via HTTP" << std::endl;
  1256. return -1;
  1257. }
  1258. if (!this->CDash) {
  1259. cmCTestOptionalLog(
  1260. this->CTest, HANDLER_OUTPUT, " Using HTTP trigger method"
  1261. << std::endl
  1262. << " Trigger site: "
  1263. << this->CTest->GetCTestConfiguration("TriggerSite") << std::endl,
  1264. this->Quiet);
  1265. if (!this->TriggerUsingHTTP(
  1266. files, prefix,
  1267. this->CTest->GetCTestConfiguration("TriggerSite"))) {
  1268. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1269. " Problems when triggering via HTTP" << std::endl);
  1270. ofs << " Problems when triggering via HTTP" << std::endl;
  1271. return -1;
  1272. }
  1273. }
  1274. if (this->HasErrors) {
  1275. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Errors occurred during "
  1276. "submission."
  1277. << std::endl);
  1278. ofs << " Errors occurred during submission. " << std::endl;
  1279. } else {
  1280. cmCTestOptionalLog(
  1281. this->CTest, HANDLER_OUTPUT, " Submission successful"
  1282. << (this->HasWarnings ? ", with warnings." : "") << std::endl,
  1283. this->Quiet);
  1284. ofs << " Submission successful"
  1285. << (this->HasWarnings ? ", with warnings." : "") << std::endl;
  1286. }
  1287. return 0;
  1288. } else if (dropMethod == "xmlrpc") {
  1289. #if defined(CTEST_USE_XMLRPC)
  1290. ofs << "Using drop method: XML-RPC" << std::endl;
  1291. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1292. " Using XML-RPC submit method" << std::endl,
  1293. this->Quiet);
  1294. std::string url = this->CTest->GetCTestConfiguration("DropSite");
  1295. prefix = this->CTest->GetCTestConfiguration("DropLocation");
  1296. if (!this->SubmitUsingXMLRPC(buildDirectory + "/Testing/" +
  1297. this->CTest->GetCurrentTag(),
  1298. files, prefix, url)) {
  1299. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1300. " Problems when submitting via XML-RPC" << std::endl);
  1301. ofs << " Problems when submitting via XML-RPC" << std::endl;
  1302. return -1;
  1303. }
  1304. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1305. " Submission successful" << std::endl, this->Quiet);
  1306. ofs << " Submission successful" << std::endl;
  1307. return 0;
  1308. #else
  1309. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1310. " Submission method \"xmlrpc\" not compiled into CTest!"
  1311. << std::endl);
  1312. return -1;
  1313. #endif
  1314. } else if (dropMethod == "scp") {
  1315. std::string url;
  1316. std::string oldWorkingDirectory;
  1317. if (!this->CTest->GetCTestConfiguration("DropSiteUser").empty()) {
  1318. url += this->CTest->GetCTestConfiguration("DropSiteUser") + "@";
  1319. }
  1320. url += this->CTest->GetCTestConfiguration("DropSite") + ":" +
  1321. this->CTest->GetCTestConfiguration("DropLocation");
  1322. // change to the build directory so that we can uses a relative path
  1323. // on windows since scp dosn't support "c:" a drive in the path
  1324. oldWorkingDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  1325. cmSystemTools::ChangeDirectory(buildDirectory);
  1326. if (!this->SubmitUsingSCP(this->CTest->GetCTestConfiguration("ScpCommand"),
  1327. "Testing/" + this->CTest->GetCurrentTag(), files,
  1328. prefix, url)) {
  1329. cmSystemTools::ChangeDirectory(oldWorkingDirectory);
  1330. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1331. " Problems when submitting via SCP" << std::endl);
  1332. ofs << " Problems when submitting via SCP" << std::endl;
  1333. return -1;
  1334. }
  1335. cmSystemTools::ChangeDirectory(oldWorkingDirectory);
  1336. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1337. " Submission successful" << std::endl, this->Quiet);
  1338. ofs << " Submission successful" << std::endl;
  1339. return 0;
  1340. } else if (dropMethod == "cp") {
  1341. std::string location = this->CTest->GetCTestConfiguration("DropLocation");
  1342. // change to the build directory so that we can uses a relative path
  1343. // on windows since scp dosn't support "c:" a drive in the path
  1344. std::string oldWorkingDirectory =
  1345. cmSystemTools::GetCurrentWorkingDirectory();
  1346. cmSystemTools::ChangeDirectory(buildDirectory);
  1347. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1348. " Change directory: " << buildDirectory << std::endl,
  1349. this->Quiet);
  1350. if (!this->SubmitUsingCP("Testing/" + this->CTest->GetCurrentTag(), files,
  1351. prefix, location)) {
  1352. cmSystemTools::ChangeDirectory(oldWorkingDirectory);
  1353. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1354. " Problems when submitting via CP" << std::endl);
  1355. ofs << " Problems when submitting via cp" << std::endl;
  1356. return -1;
  1357. }
  1358. cmSystemTools::ChangeDirectory(oldWorkingDirectory);
  1359. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  1360. " Submission successful" << std::endl, this->Quiet);
  1361. ofs << " Submission successful" << std::endl;
  1362. return 0;
  1363. }
  1364. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown submission method: \""
  1365. << dropMethod << "\"" << std::endl);
  1366. return -1;
  1367. }
  1368. std::string cmCTestSubmitHandler::GetSubmitResultsPrefix()
  1369. {
  1370. std::string buildname =
  1371. cmCTest::SafeBuildIdField(this->CTest->GetCTestConfiguration("BuildName"));
  1372. std::string name = this->CTest->GetCTestConfiguration("Site") + "___" +
  1373. buildname + "___" + this->CTest->GetCurrentTag() + "-" +
  1374. this->CTest->GetTestModelString() + "___XML___";
  1375. return name;
  1376. }
  1377. void cmCTestSubmitHandler::SelectParts(std::set<cmCTest::Part> const& parts)
  1378. {
  1379. // Check whether each part is selected.
  1380. for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
  1381. p = cmCTest::Part(p + 1)) {
  1382. this->SubmitPart[p] =
  1383. (std::set<cmCTest::Part>::const_iterator(parts.find(p)) != parts.end());
  1384. }
  1385. }
  1386. void cmCTestSubmitHandler::SelectFiles(cmCTest::SetOfStrings const& files)
  1387. {
  1388. this->Files.insert(files.begin(), files.end());
  1389. }