cmCTestSubmitHandler.cxx 56 KB

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