cmCTestSubmitHandler.cxx 56 KB

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