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*, curl_infotype,
  99. char* chPtr, size_t size,
  100. void* data)
  101. {
  102. cmCTestSubmitHandlerVectorOfChar* vec =
  103. static_cast<cmCTestSubmitHandlerVectorOfChar*>(data);
  104. vec->insert(vec->end(), chPtr, chPtr + size);
  105. return size;
  106. }
  107. cmCTestSubmitHandler::cmCTestSubmitHandler()
  108. : HTTPProxy()
  109. , FTPProxy()
  110. {
  111. this->Initialize();
  112. }
  113. void cmCTestSubmitHandler::Initialize()
  114. {
  115. // We submit all available parts by default.
  116. for (cmCTest::Part p = cmCTest::PartStart; p != cmCTest::PartCount;
  117. p = cmCTest::Part(p + 1)) {
  118. this->SubmitPart[p] = true;
  119. }
  120. this->CDash = false;
  121. this->HasWarnings = false;
  122. this->HasErrors = false;
  123. this->Superclass::Initialize();
  124. this->HTTPProxy = "";
  125. this->HTTPProxyType = 0;
  126. this->HTTPProxyAuth = "";
  127. this->FTPProxy = "";
  128. this->FTPProxyType = 0;
  129. this->LogFile = CM_NULLPTR;
  130. this->Files.clear();
  131. }
  132. bool cmCTestSubmitHandler::SubmitUsingFTP(const std::string& localprefix,
  133. const std::set<std::string>& files,
  134. const std::string& remoteprefix,
  135. const std::string& url)
  136. {
  137. CURL* curl;
  138. CURLcode res;
  139. FILE* ftpfile;
  140. char error_buffer[1024];
  141. /* In windows, this will init the winsock stuff */
  142. ::curl_global_init(CURL_GLOBAL_ALL);
  143. cmCTest::SetOfStrings::const_iterator file;
  144. for (file = files.begin(); file != files.end(); ++file) {
  145. /* get a curl handle */
  146. curl = curl_easy_init();
  147. if (curl) {
  148. // Using proxy
  149. if (this->FTPProxyType > 0) {
  150. curl_easy_setopt(curl, CURLOPT_PROXY, this->FTPProxy.c_str());
  151. switch (this->FTPProxyType) {
  152. case 2:
  153. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  154. break;
  155. case 3:
  156. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  157. break;
  158. default:
  159. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  160. }
  161. }
  162. // enable uploading
  163. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  164. // if there is little to no activity for too long stop submitting
  165. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  166. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,
  167. SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
  168. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  169. std::string local_file = *file;
  170. if (!cmSystemTools::FileExists(local_file.c_str())) {
  171. local_file = localprefix + "/" + *file;
  172. }
  173. std::string upload_as =
  174. url + "/" + remoteprefix + cmSystemTools::GetFilenameName(*file);
  175. if (!cmSystemTools::FileExists(local_file.c_str())) {
  176. cmCTestLog(this->CTest, ERROR_MESSAGE,
  177. " Cannot find file: " << local_file << std::endl);
  178. ::curl_easy_cleanup(curl);
  179. ::curl_global_cleanup();
  180. return false;
  181. }
  182. unsigned long filelen = cmSystemTools::FileLength(local_file);
  183. ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  184. *this->LogFile << "\tUpload file: " << local_file << " to " << upload_as
  185. << std::endl;
  186. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  187. " Upload file: " << local_file << " to "
  188. << upload_as << std::endl,
  189. this->Quiet);
  190. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  191. // specify target
  192. ::curl_easy_setopt(curl, CURLOPT_URL, upload_as.c_str());
  193. // now specify which file to upload
  194. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  195. // and give the size of the upload (optional)
  196. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(filelen));
  197. // and give curl the buffer for errors
  198. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  199. // specify handler for output
  200. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  201. cmCTestSubmitHandlerWriteMemoryCallback);
  202. ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  203. cmCTestSubmitHandlerCurlDebugCallback);
  204. /* we pass our 'chunk' struct to the callback function */
  205. cmCTestSubmitHandlerVectorOfChar chunk;
  206. cmCTestSubmitHandlerVectorOfChar chunkDebug;
  207. ::curl_easy_setopt(curl, CURLOPT_FILE, (void*)&chunk);
  208. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void*)&chunkDebug);
  209. // Now run off and do what you've been told!
  210. res = ::curl_easy_perform(curl);
  211. if (!chunk.empty()) {
  212. cmCTestOptionalLog(this->CTest, DEBUG, "CURL output: ["
  213. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  214. << "]" << std::endl,
  215. this->Quiet);
  216. }
  217. if (!chunkDebug.empty()) {
  218. cmCTestOptionalLog(
  219. this->CTest, DEBUG, "CURL debug output: ["
  220. << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
  221. << std::endl,
  222. this->Quiet);
  223. }
  224. fclose(ftpfile);
  225. if (res) {
  226. cmCTestLog(this->CTest, ERROR_MESSAGE, " Error when uploading file: "
  227. << local_file << std::endl);
  228. cmCTestLog(this->CTest, ERROR_MESSAGE,
  229. " Error message was: " << error_buffer << std::endl);
  230. *this->LogFile << " Error when uploading file: " << local_file
  231. << std::endl
  232. << " Error message was: " << error_buffer << std::endl
  233. << " Curl output was: ";
  234. // avoid dereference of empty vector
  235. if (!chunk.empty()) {
  236. *this->LogFile << cmCTestLogWrite(&*chunk.begin(), chunk.size());
  237. cmCTestLog(this->CTest, ERROR_MESSAGE, "CURL output: ["
  238. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  239. << std::endl);
  240. }
  241. *this->LogFile << std::endl;
  242. ::curl_easy_cleanup(curl);
  243. ::curl_global_cleanup();
  244. return false;
  245. }
  246. // always cleanup
  247. ::curl_easy_cleanup(curl);
  248. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  249. " Uploaded: " + local_file << std::endl,
  250. this->Quiet);
  251. }
  252. }
  253. ::curl_global_cleanup();
  254. return true;
  255. }
  256. // Uploading files is simpler
  257. bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix,
  258. const std::set<std::string>& files,
  259. const std::string& remoteprefix,
  260. const std::string& url)
  261. {
  262. CURL* curl;
  263. CURLcode res;
  264. FILE* ftpfile;
  265. char error_buffer[1024];
  266. struct curl_slist* headers =
  267. ::curl_slist_append(CM_NULLPTR, "Content-Type: text/xml");
  268. /* In windows, this will init the winsock stuff */
  269. ::curl_global_init(CURL_GLOBAL_ALL);
  270. std::string dropMethod(this->CTest->GetCTestConfiguration("DropMethod"));
  271. std::string curlopt(this->CTest->GetCTestConfiguration("CurlOptions"));
  272. std::vector<std::string> args;
  273. cmSystemTools::ExpandListArgument(curlopt, args);
  274. bool verifyPeerOff = false;
  275. bool verifyHostOff = false;
  276. for (std::vector<std::string>::iterator i = args.begin(); i != args.end();
  277. ++i) {
  278. if (*i == "CURLOPT_SSL_VERIFYPEER_OFF") {
  279. verifyPeerOff = true;
  280. }
  281. if (*i == "CURLOPT_SSL_VERIFYHOST_OFF") {
  282. verifyHostOff = true;
  283. }
  284. }
  285. std::string::size_type kk;
  286. cmCTest::SetOfStrings::const_iterator file;
  287. for (file = files.begin(); file != files.end(); ++file) {
  288. /* get a curl handle */
  289. curl = curl_easy_init();
  290. if (curl) {
  291. cmCurlSetCAInfo(curl);
  292. if (verifyPeerOff) {
  293. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  294. " Set CURLOPT_SSL_VERIFYPEER to off\n",
  295. this->Quiet);
  296. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  297. }
  298. if (verifyHostOff) {
  299. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  300. " Set CURLOPT_SSL_VERIFYHOST to off\n",
  301. this->Quiet);
  302. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  303. }
  304. // Using proxy
  305. if (this->HTTPProxyType > 0) {
  306. curl_easy_setopt(curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  307. switch (this->HTTPProxyType) {
  308. case 2:
  309. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  310. break;
  311. case 3:
  312. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  313. break;
  314. default:
  315. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  316. if (!this->HTTPProxyAuth.empty()) {
  317. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,
  318. this->HTTPProxyAuth.c_str());
  319. }
  320. }
  321. }
  322. if (this->CTest->ShouldUseHTTP10()) {
  323. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  324. }
  325. // enable HTTP ERROR parsing
  326. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  327. /* enable uploading */
  328. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  329. // if there is little to no activity for too long stop submitting
  330. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  331. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME,
  332. SUBMIT_TIMEOUT_IN_SECONDS_DEFAULT);
  333. /* HTTP PUT please */
  334. ::curl_easy_setopt(curl, CURLOPT_PUT, 1);
  335. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  336. // Be sure to set Content-Type to satisfy fussy modsecurity rules
  337. ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  338. std::string local_file = *file;
  339. if (!cmSystemTools::FileExists(local_file.c_str())) {
  340. local_file = localprefix + "/" + *file;
  341. }
  342. std::string remote_file =
  343. remoteprefix + cmSystemTools::GetFilenameName(*file);
  344. *this->LogFile << "\tUpload file: " << local_file << " to "
  345. << remote_file << std::endl;
  346. std::string ofile = "";
  347. for (kk = 0; kk < remote_file.size(); kk++) {
  348. char c = remote_file[kk];
  349. char hexCh[4] = { 0, 0, 0, 0 };
  350. hexCh[0] = c;
  351. switch (c) {
  352. case '+':
  353. case '?':
  354. case '/':
  355. case '\\':
  356. case '&':
  357. case ' ':
  358. case '=':
  359. case '%':
  360. sprintf(hexCh, "%%%02X", (int)c);
  361. ofile.append(hexCh);
  362. break;
  363. default:
  364. ofile.append(hexCh);
  365. }
  366. }
  367. std::string upload_as = url +
  368. ((url.find('?') == std::string::npos) ? '?' : '&') + "FileName=" +
  369. ofile;
  370. upload_as += "&MD5=";
  371. if (cmSystemTools::IsOn(this->GetOption("InternalTest"))) {
  372. upload_as += "bad_md5sum";
  373. } else {
  374. char md5[33];
  375. cmSystemTools::ComputeFileMD5(local_file, md5);
  376. md5[32] = 0;
  377. upload_as += md5;
  378. }
  379. if (!cmSystemTools::FileExists(local_file.c_str())) {
  380. cmCTestLog(this->CTest, ERROR_MESSAGE,
  381. " Cannot find file: " << local_file << std::endl);
  382. ::curl_easy_cleanup(curl);
  383. ::curl_slist_free_all(headers);
  384. ::curl_global_cleanup();
  385. return false;
  386. }
  387. unsigned long filelen = cmSystemTools::FileLength(local_file);
  388. ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  389. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  390. " Upload file: " << local_file << " to "
  391. << upload_as << " Size: "
  392. << filelen << std::endl,
  393. this->Quiet);
  394. // specify target
  395. ::curl_easy_setopt(curl, CURLOPT_URL, upload_as.c_str());
  396. // now specify which file to upload
  397. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  398. // and give the size of the upload (optional)
  399. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(filelen));
  400. // and give curl the buffer for errors
  401. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  402. // specify handler for output
  403. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  404. cmCTestSubmitHandlerWriteMemoryCallback);
  405. ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  406. cmCTestSubmitHandlerCurlDebugCallback);
  407. /* we pass our 'chunk' struct to the callback function */
  408. cmCTestSubmitHandlerVectorOfChar chunk;
  409. cmCTestSubmitHandlerVectorOfChar chunkDebug;
  410. ::curl_easy_setopt(curl, CURLOPT_FILE, (void*)&chunk);
  411. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void*)&chunkDebug);
  412. // Now run off and do what you've been told!
  413. res = ::curl_easy_perform(curl);
  414. if (cmSystemTools::IsOn(this->GetOption("InternalTest")) &&
  415. cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  416. this->CTest->GetCDashVersion().c_str(),
  417. "1.7")) {
  418. // mock failure output for internal test case
  419. std::string mock_output =
  420. "<cdash version=\"1.7.0\">\n"
  421. " <status>ERROR</status>\n"
  422. " <message>Checksum failed for file.</message>\n"
  423. "</cdash>\n";
  424. chunk.clear();
  425. chunk.assign(mock_output.begin(), mock_output.end());
  426. }
  427. if (!chunk.empty()) {
  428. cmCTestOptionalLog(this->CTest, DEBUG, "CURL output: ["
  429. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  430. << "]" << std::endl,
  431. this->Quiet);
  432. this->ParseResponse(chunk);
  433. }
  434. if (!chunkDebug.empty()) {
  435. cmCTestOptionalLog(
  436. this->CTest, DEBUG, "CURL debug output: ["
  437. << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
  438. << std::endl,
  439. this->Quiet);
  440. }
  441. // If curl failed for any reason, or checksum fails, wait and retry
  442. //
  443. if (res != CURLE_OK || this->HasErrors) {
  444. std::string retryDelay = this->GetOption("RetryDelay") == CM_NULLPTR
  445. ? ""
  446. : this->GetOption("RetryDelay");
  447. std::string retryCount = this->GetOption("RetryCount") == CM_NULLPTR
  448. ? ""
  449. : this->GetOption("RetryCount");
  450. int delay = retryDelay == ""
  451. ? atoi(this->CTest->GetCTestConfiguration("CTestSubmitRetryDelay")
  452. .c_str())
  453. : atoi(retryDelay.c_str());
  454. int count = retryCount == ""
  455. ? atoi(this->CTest->GetCTestConfiguration("CTestSubmitRetryCount")
  456. .c_str())
  457. : atoi(retryCount.c_str());
  458. for (int i = 0; i < count; i++) {
  459. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  460. " Submit failed, waiting " << delay
  461. << " seconds...\n",
  462. this->Quiet);
  463. double stop = cmSystemTools::GetTime() + delay;
  464. while (cmSystemTools::GetTime() < stop) {
  465. cmSystemTools::Delay(100);
  466. }
  467. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  468. " Retry submission: Attempt "
  469. << (i + 1) << " of " << count << std::endl,
  470. this->Quiet);
  471. ::fclose(ftpfile);
  472. ftpfile = cmsys::SystemTools::Fopen(local_file, "rb");
  473. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  474. chunk.clear();
  475. chunkDebug.clear();
  476. this->HasErrors = false;
  477. res = ::curl_easy_perform(curl);
  478. if (!chunk.empty()) {
  479. cmCTestOptionalLog(
  480. this->CTest, DEBUG, "CURL output: ["
  481. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  482. << std::endl,
  483. this->Quiet);
  484. this->ParseResponse(chunk);
  485. }
  486. if (res == CURLE_OK && !this->HasErrors) {
  487. break;
  488. }
  489. }
  490. }
  491. fclose(ftpfile);
  492. if (res) {
  493. cmCTestLog(this->CTest, ERROR_MESSAGE, " Error when uploading file: "
  494. << local_file << std::endl);
  495. cmCTestLog(this->CTest, ERROR_MESSAGE,
  496. " Error message was: " << error_buffer << std::endl);
  497. *this->LogFile << " Error when uploading file: " << local_file
  498. << std::endl
  499. << " Error message was: " << error_buffer
  500. << std::endl;
  501. // avoid deref of begin for zero size array
  502. if (!chunk.empty()) {
  503. *this->LogFile << " Curl output was: "
  504. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  505. << std::endl;
  506. cmCTestLog(this->CTest, ERROR_MESSAGE, "CURL output: ["
  507. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  508. << std::endl);
  509. }
  510. ::curl_easy_cleanup(curl);
  511. ::curl_slist_free_all(headers);
  512. ::curl_global_cleanup();
  513. return false;
  514. }
  515. // always cleanup
  516. ::curl_easy_cleanup(curl);
  517. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  518. " Uploaded: " + local_file << std::endl,
  519. this->Quiet);
  520. }
  521. }
  522. ::curl_slist_free_all(headers);
  523. ::curl_global_cleanup();
  524. return true;
  525. }
  526. void cmCTestSubmitHandler::ParseResponse(
  527. cmCTestSubmitHandlerVectorOfChar chunk)
  528. {
  529. std::string output = "";
  530. output.append(chunk.begin(), chunk.end());
  531. if (output.find("<cdash") != output.npos) {
  532. ResponseParser parser;
  533. parser.Parse(output.c_str());
  534. if (parser.Status != ResponseParser::STATUS_OK) {
  535. this->HasErrors = true;
  536. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  537. " Submission failed: " << parser.Message << std::endl);
  538. return;
  539. }
  540. }
  541. output = cmSystemTools::UpperCase(output);
  542. if (output.find("WARNING") != std::string::npos) {
  543. this->HasWarnings = true;
  544. }
  545. if (output.find("ERROR") != std::string::npos) {
  546. this->HasErrors = true;
  547. }
  548. if (this->HasWarnings || this->HasErrors) {
  549. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Server Response:\n"
  550. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "\n");
  551. }
  552. }
  553. bool cmCTestSubmitHandler::TriggerUsingHTTP(const std::set<std::string>& files,
  554. const std::string& remoteprefix,
  555. const std::string& url)
  556. {
  557. CURL* curl;
  558. char error_buffer[1024];
  559. /* In windows, this will init the winsock stuff */
  560. ::curl_global_init(CURL_GLOBAL_ALL);
  561. cmCTest::SetOfStrings::const_iterator file;
  562. for (file = files.begin(); file != files.end(); ++file) {
  563. /* get a curl handle */
  564. curl = curl_easy_init();
  565. if (curl) {
  566. // Using proxy
  567. if (this->HTTPProxyType > 0) {
  568. curl_easy_setopt(curl, CURLOPT_PROXY, this->HTTPProxy.c_str());
  569. switch (this->HTTPProxyType) {
  570. case 2:
  571. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  572. break;
  573. case 3:
  574. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  575. break;
  576. default:
  577. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  578. if (!this->HTTPProxyAuth.empty()) {
  579. curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD,
  580. this->HTTPProxyAuth.c_str());
  581. }
  582. }
  583. }
  584. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  585. // and give curl the buffer for errors
  586. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  587. // specify handler for output
  588. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  589. cmCTestSubmitHandlerWriteMemoryCallback);
  590. ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  591. cmCTestSubmitHandlerCurlDebugCallback);
  592. /* we pass our 'chunk' struct to the callback function */
  593. cmCTestSubmitHandlerVectorOfChar chunk;
  594. cmCTestSubmitHandlerVectorOfChar chunkDebug;
  595. ::curl_easy_setopt(curl, CURLOPT_FILE, (void*)&chunk);
  596. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void*)&chunkDebug);
  597. std::string rfile = remoteprefix + cmSystemTools::GetFilenameName(*file);
  598. std::string ofile = "";
  599. std::string::iterator kk;
  600. for (kk = rfile.begin(); kk < rfile.end(); ++kk) {
  601. char c = *kk;
  602. char hexCh[4] = { 0, 0, 0, 0 };
  603. hexCh[0] = c;
  604. switch (c) {
  605. case '+':
  606. case '?':
  607. case '/':
  608. case '\\':
  609. case '&':
  610. case ' ':
  611. case '=':
  612. case '%':
  613. sprintf(hexCh, "%%%02X", (int)c);
  614. ofile.append(hexCh);
  615. break;
  616. default:
  617. ofile.append(hexCh);
  618. }
  619. }
  620. std::string turl = url +
  621. ((url.find('?') == std::string::npos) ? '?' : '&') + "xmlfile=" +
  622. ofile;
  623. *this->LogFile << "Trigger url: " << turl << std::endl;
  624. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  625. " Trigger url: " << turl << std::endl, this->Quiet);
  626. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  627. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  628. if (curl_easy_perform(curl)) {
  629. cmCTestLog(this->CTest, ERROR_MESSAGE,
  630. " Error when triggering: " << turl << std::endl);
  631. cmCTestLog(this->CTest, ERROR_MESSAGE,
  632. " Error message was: " << error_buffer << std::endl);
  633. *this->LogFile << "\tTriggering failed with error: " << error_buffer
  634. << std::endl
  635. << " Error message was: " << error_buffer
  636. << std::endl;
  637. if (!chunk.empty()) {
  638. *this->LogFile << " Curl output was: "
  639. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  640. << std::endl;
  641. cmCTestLog(this->CTest, ERROR_MESSAGE, "CURL output: ["
  642. << cmCTestLogWrite(&*chunk.begin(), chunk.size()) << "]"
  643. << std::endl);
  644. }
  645. ::curl_easy_cleanup(curl);
  646. ::curl_global_cleanup();
  647. return false;
  648. }
  649. if (!chunk.empty()) {
  650. cmCTestOptionalLog(this->CTest, DEBUG, "CURL output: ["
  651. << cmCTestLogWrite(&*chunk.begin(), chunk.size())
  652. << "]" << std::endl,
  653. this->Quiet);
  654. }
  655. if (!chunkDebug.empty()) {
  656. cmCTestOptionalLog(
  657. this->CTest, DEBUG, "CURL debug output: ["
  658. << cmCTestLogWrite(&*chunkDebug.begin(), chunkDebug.size()) << "]"
  659. << std::endl,
  660. this->Quiet);
  661. }
  662. // always cleanup
  663. ::curl_easy_cleanup(curl);
  664. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
  665. this->Quiet);
  666. }
  667. }
  668. ::curl_global_cleanup();
  669. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  670. " Dart server triggered..." << std::endl, this->Quiet);
  671. return true;
  672. }
  673. bool cmCTestSubmitHandler::SubmitUsingSCP(const std::string& scp_command,
  674. const std::string& localprefix,
  675. const std::set<std::string>& files,
  676. const std::string& remoteprefix,
  677. const std::string& url)
  678. {
  679. if (scp_command.empty() || localprefix.empty() || files.empty() ||
  680. remoteprefix.empty() || url.empty()) {
  681. return 0;
  682. }
  683. std::vector<const char*> argv;
  684. argv.push_back(scp_command.c_str()); // Scp command
  685. argv.push_back(scp_command.c_str()); // Dummy string for file
  686. argv.push_back(scp_command.c_str()); // Dummy string for remote url
  687. argv.push_back(CM_NULLPTR);
  688. cmsysProcess* cp = cmsysProcess_New();
  689. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  690. // cmsysProcess_SetTimeout(cp, timeout);
  691. int problems = 0;
  692. cmCTest::SetOfStrings::const_iterator file;
  693. for (file = files.begin(); file != files.end(); ++file) {
  694. int retVal;
  695. std::string lfname = localprefix;
  696. cmSystemTools::ConvertToUnixSlashes(lfname);
  697. lfname += "/" + *file;
  698. lfname = cmSystemTools::ConvertToOutputPath(lfname.c_str());
  699. argv[1] = lfname.c_str();
  700. std::string rfname = url + "/" + remoteprefix + *file;
  701. argv[2] = rfname.c_str();
  702. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Execute \""
  703. << argv[0] << "\" \"" << argv[1] << "\" \"" << argv[2]
  704. << "\"" << std::endl,
  705. this->Quiet);
  706. *this->LogFile << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
  707. << argv[2] << "\"" << std::endl;
  708. cmsysProcess_SetCommand(cp, &*argv.begin());
  709. cmsysProcess_Execute(cp);
  710. char* data;
  711. int length;
  712. while (cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR)) {
  713. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  714. cmCTestLogWrite(data, length), this->Quiet);
  715. }
  716. cmsysProcess_WaitForExit(cp, CM_NULLPTR);
  717. int result = cmsysProcess_GetState(cp);
  718. if (result == cmsysProcess_State_Exited) {
  719. retVal = cmsysProcess_GetExitValue(cp);
  720. if (retVal != 0) {
  721. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  722. "\tSCP returned: " << retVal << std::endl,
  723. this->Quiet);
  724. *this->LogFile << "\tSCP returned: " << retVal << std::endl;
  725. problems++;
  726. }
  727. } else if (result == cmsysProcess_State_Exception) {
  728. retVal = cmsysProcess_GetExitException(cp);
  729. cmCTestLog(this->CTest, ERROR_MESSAGE,
  730. "\tThere was an exception: " << retVal << std::endl);
  731. *this->LogFile << "\tThere was an exception: " << retVal << std::endl;
  732. problems++;
  733. } else if (result == cmsysProcess_State_Expired) {
  734. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tThere was a timeout"
  735. << std::endl);
  736. *this->LogFile << "\tThere was a timeout" << std::endl;
  737. problems++;
  738. } else if (result == cmsysProcess_State_Error) {
  739. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tError executing SCP: "
  740. << cmsysProcess_GetErrorString(cp) << std::endl);
  741. *this->LogFile << "\tError executing SCP: "
  742. << cmsysProcess_GetErrorString(cp) << std::endl;
  743. problems++;
  744. }
  745. }
  746. cmsysProcess_Delete(cp);
  747. return problems == 0;
  748. }
  749. bool cmCTestSubmitHandler::SubmitUsingCP(const std::string& localprefix,
  750. const std::set<std::string>& files,
  751. const std::string& remoteprefix,
  752. const std::string& destination)
  753. {
  754. if (localprefix.empty() || files.empty() || remoteprefix.empty() ||
  755. destination.empty()) {
  756. /* clang-format off */
  757. cmCTestLog(this->CTest, ERROR_MESSAGE,
  758. "Missing arguments for submit via cp:\n"
  759. << "\tlocalprefix: " << localprefix << "\n"
  760. << "\tNumber of files: " << files.size() << "\n"
  761. << "\tremoteprefix: " << remoteprefix << "\n"
  762. << "\tdestination: " << destination << std::endl);
  763. /* clang-format on */
  764. return 0;
  765. }
  766. cmCTest::SetOfStrings::const_iterator file;
  767. for (file = files.begin(); file != files.end(); ++file) {
  768. std::string lfname = localprefix;
  769. cmSystemTools::ConvertToUnixSlashes(lfname);
  770. lfname += "/" + *file;
  771. std::string rfname = destination + "/" + remoteprefix + *file;
  772. cmSystemTools::CopyFileAlways(lfname, rfname);
  773. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " Copy file: "
  774. << lfname << " to " << rfname << std::endl,
  775. this->Quiet);
  776. }
  777. std::string tagDoneFile = destination + "/" + remoteprefix + "DONE";
  778. cmSystemTools::Touch(tagDoneFile, true);
  779. return true;
  780. }
  781. #if defined(CTEST_USE_XMLRPC)
  782. bool cmCTestSubmitHandler::SubmitUsingXMLRPC(
  783. const std::string& localprefix, const std::set<std::string>& files,
  784. const std::string& remoteprefix, const std::string& url)
  785. {
  786. xmlrpc_env env;
  787. char ctestString[] = "CTest";
  788. std::string ctestVersionString = cmVersion::GetCMakeVersion();
  789. char* ctestVersion = const_cast<char*>(ctestVersionString.c_str());
  790. std::string realURL = url + "/" + remoteprefix + "/Command/";
  791. /* Start up our XML-RPC client library. */
  792. xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, ctestString, ctestVersion);
  793. /* Initialize our error-handling environment. */
  794. xmlrpc_env_init(&env);
  795. /* Call the famous server at UserLand. */
  796. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Submitting to: "
  797. << realURL << " (" << remoteprefix << ")" << std::endl,
  798. this->Quiet);
  799. cmCTest::SetOfStrings::const_iterator file;
  800. for (file = files.begin(); file != files.end(); ++file) {
  801. xmlrpc_value* result;
  802. std::string local_file = *file;
  803. if (!cmSystemTools::FileExists(local_file.c_str())) {
  804. local_file = localprefix + "/" + *file;
  805. }
  806. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  807. " Submit file: " << local_file << std::endl,
  808. this->Quiet);
  809. struct stat st;
  810. if (::stat(local_file.c_str(), &st)) {
  811. cmCTestLog(this->CTest, ERROR_MESSAGE,
  812. " Cannot find file: " << local_file << std::endl);
  813. return false;
  814. }
  815. // off_t can be bigger than size_t. fread takes size_t.
  816. // make sure the file is not too big.
  817. if (static_cast<off_t>(static_cast<size_t>(st.st_size)) !=
  818. static_cast<off_t>(st.st_size)) {
  819. cmCTestLog(this->CTest, ERROR_MESSAGE, " File too big: " << local_file
  820. << std::endl);
  821. return false;
  822. }
  823. size_t fileSize = static_cast<size_t>(st.st_size);
  824. FILE* fp = cmsys::SystemTools::Fopen(local_file.c_str(), "rb");
  825. if (!fp) {
  826. cmCTestLog(this->CTest, ERROR_MESSAGE,
  827. " Cannot open file: " << local_file << std::endl);
  828. return false;
  829. }
  830. unsigned char* fileBuffer = new unsigned char[fileSize];
  831. if (fread(fileBuffer, 1, fileSize, fp) != fileSize) {
  832. delete[] fileBuffer;
  833. fclose(fp);
  834. cmCTestLog(this->CTest, ERROR_MESSAGE,
  835. " Cannot read file: " << local_file << std::endl);
  836. return false;
  837. }
  838. fclose(fp);
  839. char remoteCommand[] = "Submit.put";
  840. char* pRealURL = const_cast<char*>(realURL.c_str());
  841. result = xmlrpc_client_call(&env, pRealURL, remoteCommand, "(6)",
  842. fileBuffer, (xmlrpc_int32)fileSize);
  843. delete[] fileBuffer;
  844. if (env.fault_occurred) {
  845. cmCTestLog(this->CTest, ERROR_MESSAGE, " Submission problem: "
  846. << env.fault_string << " (" << env.fault_code << ")"
  847. << std::endl);
  848. xmlrpc_env_clean(&env);
  849. xmlrpc_client_cleanup();
  850. return false;
  851. }
  852. /* Dispose of our result value. */
  853. xmlrpc_DECREF(result);
  854. }
  855. /* Clean up our error-handling environment. */
  856. xmlrpc_env_clean(&env);
  857. /* Shutdown our XML-RPC client library. */
  858. xmlrpc_client_cleanup();
  859. return true;
  860. }
  861. #else
  862. bool cmCTestSubmitHandler::SubmitUsingXMLRPC(std::string const&,
  863. std::set<std::string> const&,
  864. std::string const&,
  865. std::string const&)
  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. }