MetalinkParserController.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "MetalinkParserController.h"
  36. #include <algorithm>
  37. #include "Metalinker.h"
  38. #include "MetalinkEntry.h"
  39. #include "MetalinkResource.h"
  40. #include "MetalinkMetaurl.h"
  41. #include "FileEntry.h"
  42. #include "a2functional.h"
  43. #include "A2STR.h"
  44. #ifdef ENABLE_MESSAGE_DIGEST
  45. # include "Checksum.h"
  46. # include "ChunkChecksum.h"
  47. # include "messageDigest.h"
  48. #endif // ENABLE_MESSAGE_DIGEST
  49. #include "Signature.h"
  50. #include "util.h"
  51. namespace aria2 {
  52. MetalinkParserController::MetalinkParserController():
  53. _metalinker(new Metalinker())
  54. {}
  55. MetalinkParserController::~MetalinkParserController() {}
  56. void MetalinkParserController::newEntryTransaction()
  57. {
  58. _tEntry.reset(new MetalinkEntry());
  59. _tResource.reset();
  60. _tMetaurl.reset();
  61. #ifdef ENABLE_MESSAGE_DIGEST
  62. _tChecksum.reset();
  63. _tChunkChecksumV4.reset();
  64. _tChunkChecksum.reset();
  65. #endif // ENABLE_MESSAGE_DIGEST
  66. }
  67. void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
  68. {
  69. if(_tEntry.isNull()) {
  70. return;
  71. }
  72. if(_tEntry->file.isNull()) {
  73. _tEntry->file.reset(new FileEntry(util::escapePath(filename), 0, 0));
  74. } else {
  75. _tEntry->file->setPath(util::escapePath(filename));
  76. }
  77. }
  78. void MetalinkParserController::setFileLengthOfEntry(uint64_t length)
  79. {
  80. if(_tEntry.isNull()) {
  81. return;
  82. }
  83. if(_tEntry->file.isNull()) {
  84. return;
  85. }
  86. _tEntry->file->setLength(length);
  87. _tEntry->sizeKnown = true;
  88. }
  89. void MetalinkParserController::setVersionOfEntry(const std::string& version)
  90. {
  91. if(_tEntry.isNull()) {
  92. return;
  93. }
  94. _tEntry->version = version;
  95. }
  96. void MetalinkParserController::setLanguageOfEntry(const std::string& language)
  97. {
  98. if(_tEntry.isNull()) {
  99. return;
  100. }
  101. _tEntry->languages.push_back(language);
  102. }
  103. void MetalinkParserController::setOSOfEntry(const std::string& os)
  104. {
  105. if(_tEntry.isNull()) {
  106. return;
  107. }
  108. _tEntry->oses.push_back(os);
  109. }
  110. void MetalinkParserController::setMaxConnectionsOfEntry(int maxConnections)
  111. {
  112. if(_tEntry.isNull()) {
  113. return;
  114. }
  115. _tEntry->maxConnections = maxConnections;
  116. }
  117. void MetalinkParserController::commitEntryTransaction()
  118. {
  119. if(_tEntry.isNull()) {
  120. return;
  121. }
  122. commitResourceTransaction();
  123. commitMetaurlTransaction();
  124. commitChecksumTransaction();
  125. commitChunkChecksumTransactionV4();
  126. commitChunkChecksumTransaction();
  127. commitSignatureTransaction();
  128. _metalinker->entries.push_back(_tEntry);
  129. _tEntry.reset();
  130. }
  131. void MetalinkParserController::cancelEntryTransaction()
  132. {
  133. cancelResourceTransaction();
  134. cancelMetaurlTransaction();
  135. cancelChecksumTransaction();
  136. cancelChunkChecksumTransactionV4();
  137. cancelChunkChecksumTransaction();
  138. cancelSignatureTransaction();
  139. _tEntry.reset();
  140. }
  141. void MetalinkParserController::newResourceTransaction()
  142. {
  143. if(_tEntry.isNull()) {
  144. return;
  145. }
  146. _tResource.reset(new MetalinkResource());
  147. }
  148. void MetalinkParserController::setURLOfResource(const std::string& url)
  149. {
  150. if(_tResource.isNull()) {
  151. return;
  152. }
  153. _tResource->url = url;
  154. // Metalink4Spec
  155. if(_tResource->type == MetalinkResource::TYPE_UNKNOWN) {
  156. // guess from URI sheme
  157. std::string::size_type pos = url.find("://");
  158. if(pos != std::string::npos) {
  159. setTypeOfResource(url.substr(0, pos));
  160. }
  161. }
  162. }
  163. void MetalinkParserController::setTypeOfResource(const std::string& type)
  164. {
  165. if(_tResource.isNull()) {
  166. return;
  167. }
  168. if(type == MetalinkResource::FTP) {
  169. _tResource->type = MetalinkResource::TYPE_FTP;
  170. } else if(type == MetalinkResource::HTTP) {
  171. _tResource->type = MetalinkResource::TYPE_HTTP;
  172. } else if(type == MetalinkResource::HTTPS) {
  173. _tResource->type = MetalinkResource::TYPE_HTTPS;
  174. } else if(type == MetalinkResource::BITTORRENT) {
  175. _tResource->type = MetalinkResource::TYPE_BITTORRENT;
  176. } else if(type == MetalinkResource::TORRENT) { // Metalink4Spec
  177. _tResource->type = MetalinkResource::TYPE_BITTORRENT;
  178. } else {
  179. _tResource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  180. }
  181. }
  182. void MetalinkParserController::setLocationOfResource(const std::string& location)
  183. {
  184. if(_tResource.isNull()) {
  185. return;
  186. }
  187. _tResource->location = location;
  188. }
  189. void MetalinkParserController::setPriorityOfResource(int priority)
  190. {
  191. if(_tResource.isNull()) {
  192. return;
  193. }
  194. _tResource->priority = priority;
  195. }
  196. void MetalinkParserController::setMaxConnectionsOfResource(int maxConnections)
  197. {
  198. if(_tResource.isNull()) {
  199. return;
  200. }
  201. _tResource->maxConnections = maxConnections;
  202. }
  203. void MetalinkParserController::commitResourceTransaction()
  204. {
  205. if(_tResource.isNull()) {
  206. return;
  207. }
  208. #ifdef ENABLE_BITTORRENT
  209. if(_tResource->type == MetalinkResource::TYPE_BITTORRENT) {
  210. SharedHandle<MetalinkMetaurl> metaurl(new MetalinkMetaurl());
  211. metaurl->url = _tResource->url;
  212. metaurl->priority = _tResource->priority;
  213. metaurl->mediatype = MetalinkMetaurl::MEDIATYPE_TORRENT;
  214. _tEntry->metaurls.push_back(metaurl);
  215. } else {
  216. _tEntry->resources.push_back(_tResource);
  217. }
  218. #else // !ENABLE_BITTORRENT
  219. _tEntry->resources.push_back(_tResource);
  220. #endif // !ENABLE_BITTORRENT
  221. _tResource.reset();
  222. }
  223. void MetalinkParserController::cancelResourceTransaction()
  224. {
  225. _tResource.reset();
  226. }
  227. void MetalinkParserController::newChecksumTransaction()
  228. {
  229. #ifdef ENABLE_MESSAGE_DIGEST
  230. if(_tEntry.isNull()) {
  231. return;
  232. }
  233. _tChecksum.reset(new Checksum());
  234. #endif // ENABLE_MESSAGE_DIGEST
  235. }
  236. void MetalinkParserController::setTypeOfChecksum(const std::string& type)
  237. {
  238. #ifdef ENABLE_MESSAGE_DIGEST
  239. if(_tChecksum.isNull()) {
  240. return;
  241. }
  242. std::string calgo = MessageDigestContext::getCanonicalAlgo(type);
  243. if(MessageDigestContext::supports(calgo)) {
  244. _tChecksum->setAlgo(calgo);
  245. } else {
  246. cancelChecksumTransaction();
  247. }
  248. #endif // ENABLE_MESSAGE_DIGEST
  249. }
  250. void MetalinkParserController::setHashOfChecksum(const std::string& md)
  251. {
  252. #ifdef ENABLE_MESSAGE_DIGEST
  253. if(_tChecksum.isNull()) {
  254. return;
  255. }
  256. if(MessageDigestContext::isValidHash(_tChecksum->getAlgo(), md)) {
  257. _tChecksum->setMessageDigest(md);
  258. } else {
  259. cancelChecksumTransaction();
  260. }
  261. #endif // ENABLE_MESSAGE_DIGEST
  262. }
  263. void MetalinkParserController::commitChecksumTransaction()
  264. {
  265. #ifdef ENABLE_MESSAGE_DIGEST
  266. if(_tChecksum.isNull()) {
  267. return;
  268. }
  269. if(_tEntry->checksum.isNull() ||
  270. MessageDigestContext::isStronger(_tChecksum->getAlgo(),
  271. _tEntry->checksum->getAlgo())) {
  272. _tEntry->checksum = _tChecksum;
  273. }
  274. _tChecksum.reset();
  275. #endif // ENABLE_MESSAGE_DIGEST
  276. }
  277. void MetalinkParserController::cancelChecksumTransaction()
  278. {
  279. #ifdef ENABLE_MESSAGE_DIGEST
  280. _tChecksum.reset();
  281. #endif // ENABLE_MESSAGE_DIGEST
  282. }
  283. void MetalinkParserController::newChunkChecksumTransactionV4()
  284. {
  285. #ifdef ENABLE_MESSAGE_DIGEST
  286. if(_tEntry.isNull()) {
  287. return;
  288. }
  289. _tChunkChecksumV4.reset(new ChunkChecksum());
  290. _tempChunkChecksumsV4.clear();
  291. #endif // ENABLE_MESSAGE_DIGEST
  292. }
  293. void MetalinkParserController::setTypeOfChunkChecksumV4(const std::string& type)
  294. {
  295. #ifdef ENABLE_MESSAGE_DIGEST
  296. if(_tChunkChecksumV4.isNull()) {
  297. return;
  298. }
  299. std::string calgo = MessageDigestContext::getCanonicalAlgo(type);
  300. if(MessageDigestContext::supports(calgo)) {
  301. _tChunkChecksumV4->setAlgo(calgo);
  302. } else {
  303. cancelChunkChecksumTransactionV4();
  304. }
  305. #endif // ENABLE_MESSAGE_DIGEST
  306. }
  307. void MetalinkParserController::setLengthOfChunkChecksumV4(size_t length)
  308. {
  309. #ifdef ENABLE_MESSAGE_DIGEST
  310. if(_tChunkChecksumV4.isNull()) {
  311. return;
  312. }
  313. if(length > 0) {
  314. _tChunkChecksumV4->setChecksumLength(length);
  315. } else {
  316. cancelChunkChecksumTransactionV4();
  317. }
  318. #endif // ENABLE_MESSAGE_DIGEST
  319. }
  320. void MetalinkParserController::addHashOfChunkChecksumV4(const std::string& md)
  321. {
  322. #ifdef ENABLE_MESSAGE_DIGEST
  323. if(_tChunkChecksumV4.isNull()) {
  324. return;
  325. }
  326. if(MessageDigestContext::isValidHash(_tChunkChecksumV4->getAlgo(), md)) {
  327. _tempChunkChecksumsV4.push_back(md);
  328. } else {
  329. cancelChunkChecksumTransactionV4();
  330. }
  331. #endif // ENABLE_MESSAGE_DIGEST
  332. }
  333. void MetalinkParserController::commitChunkChecksumTransactionV4()
  334. {
  335. #ifdef ENABLE_MESSAGE_DIGEST
  336. if(_tChunkChecksumV4.isNull()) {
  337. return;
  338. }
  339. if(_tEntry->chunkChecksum.isNull() ||
  340. MessageDigestContext::isStronger(_tChunkChecksumV4->getAlgo(),
  341. _tEntry->chunkChecksum->getAlgo())) {
  342. std::vector<std::string> checksums(_tempChunkChecksumsV4.begin(),
  343. _tempChunkChecksumsV4.end());
  344. _tChunkChecksumV4->setChecksums(checksums);
  345. _tEntry->chunkChecksum = _tChunkChecksumV4;
  346. }
  347. _tChunkChecksumV4.reset();
  348. #endif // ENABLE_MESSAGE_DIGEST
  349. }
  350. void MetalinkParserController::cancelChunkChecksumTransactionV4()
  351. {
  352. #ifdef ENABLE_MESSAGE_DIGEST
  353. _tChunkChecksumV4.reset();
  354. #endif // ENABLE_MESSAGE_DIGEST
  355. }
  356. void MetalinkParserController::newChunkChecksumTransaction()
  357. {
  358. #ifdef ENABLE_MESSAGE_DIGEST
  359. if(_tEntry.isNull()) {
  360. return;
  361. }
  362. _tChunkChecksum.reset(new ChunkChecksum());
  363. _tempChunkChecksums.clear();
  364. #endif // ENABLE_MESSAGE_DIGEST
  365. }
  366. void MetalinkParserController::setTypeOfChunkChecksum(const std::string& type)
  367. {
  368. #ifdef ENABLE_MESSAGE_DIGEST
  369. if(_tChunkChecksum.isNull()) {
  370. return;
  371. }
  372. std::string calgo = MessageDigestContext::getCanonicalAlgo(type);
  373. if(MessageDigestContext::supports(calgo)) {
  374. _tChunkChecksum->setAlgo(calgo);
  375. } else {
  376. cancelChunkChecksumTransaction();
  377. }
  378. #endif // ENABLE_MESSAGE_DIGEST
  379. }
  380. void MetalinkParserController::setLengthOfChunkChecksum(size_t length)
  381. {
  382. #ifdef ENABLE_MESSAGE_DIGEST
  383. if(_tChunkChecksum.isNull()) {
  384. return;
  385. }
  386. if(length > 0) {
  387. _tChunkChecksum->setChecksumLength(length);
  388. } else {
  389. cancelChunkChecksumTransaction();
  390. }
  391. #endif // ENABLE_MESSAGE_DIGEST
  392. }
  393. void MetalinkParserController::addHashOfChunkChecksum(size_t order, const std::string& md)
  394. {
  395. #ifdef ENABLE_MESSAGE_DIGEST
  396. if(_tChunkChecksum.isNull()) {
  397. return;
  398. }
  399. if(MessageDigestContext::isValidHash(_tChunkChecksum->getAlgo(), md)) {
  400. _tempChunkChecksums.push_back(std::make_pair(order, md));
  401. } else {
  402. cancelChunkChecksumTransaction();
  403. }
  404. #endif // ENABLE_MESSAGE_DIGEST
  405. }
  406. void MetalinkParserController::createNewHashOfChunkChecksum(size_t order)
  407. {
  408. #ifdef ENABLE_MESSAGE_DIGEST
  409. if(_tChunkChecksum.isNull()) {
  410. return;
  411. }
  412. _tempHashPair.first = order;
  413. #endif // ENABLE_MESSAGE_DIGEST
  414. }
  415. void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string& md)
  416. {
  417. #ifdef ENABLE_MESSAGE_DIGEST
  418. if(_tChunkChecksum.isNull()) {
  419. return;
  420. }
  421. if(MessageDigestContext::isValidHash(_tChunkChecksum->getAlgo(), md)) {
  422. _tempHashPair.second = md;
  423. } else {
  424. cancelChunkChecksumTransaction();
  425. }
  426. #endif // ENABLE_MESSAGE_DIGEST
  427. }
  428. void MetalinkParserController::addHashOfChunkChecksum()
  429. {
  430. #ifdef ENABLE_MESSAGE_DIGEST
  431. if(_tChunkChecksum.isNull()) {
  432. return;
  433. }
  434. _tempChunkChecksums.push_back(_tempHashPair);
  435. #endif // ENABLE_MESSAGE_DIGEST
  436. }
  437. void MetalinkParserController::commitChunkChecksumTransaction()
  438. {
  439. #ifdef ENABLE_MESSAGE_DIGEST
  440. if(_tChunkChecksum.isNull()) {
  441. return;
  442. }
  443. if(_tEntry->chunkChecksum.isNull() ||
  444. MessageDigestContext::isStronger(_tChunkChecksum->getAlgo(),
  445. _tEntry->chunkChecksum->getAlgo())) {
  446. std::sort(_tempChunkChecksums.begin(), _tempChunkChecksums.end(),
  447. Ascend1st<std::pair<size_t, std::string> >());
  448. std::vector<std::string> checksums;
  449. std::transform(_tempChunkChecksums.begin(), _tempChunkChecksums.end(),
  450. std::back_inserter(checksums),
  451. select2nd<std::pair<size_t, std::string> >());
  452. _tChunkChecksum->setChecksums(checksums);
  453. _tEntry->chunkChecksum = _tChunkChecksum;
  454. }
  455. _tChunkChecksum.reset();
  456. #endif // ENABLE_MESSAGE_DIGEST
  457. }
  458. void MetalinkParserController::cancelChunkChecksumTransaction()
  459. {
  460. #ifdef ENABLE_MESSAGE_DIGEST
  461. _tChunkChecksum.reset();
  462. #endif // ENABLE_MESSAGE_DIGEST
  463. }
  464. void MetalinkParserController::newSignatureTransaction()
  465. {
  466. if(_tEntry.isNull()) {
  467. return;
  468. }
  469. _tSignature.reset(new Signature());
  470. }
  471. void MetalinkParserController::setTypeOfSignature(const std::string& type)
  472. {
  473. if(_tSignature.isNull()) {
  474. return;
  475. }
  476. _tSignature->setType(type);
  477. }
  478. void MetalinkParserController::setFileOfSignature(const std::string& file)
  479. {
  480. if(_tSignature.isNull()) {
  481. return;
  482. }
  483. _tSignature->setFile(file);
  484. }
  485. void MetalinkParserController::setBodyOfSignature(const std::string& body)
  486. {
  487. if(_tSignature.isNull()) {
  488. return;
  489. }
  490. _tSignature->setBody(body);
  491. }
  492. void MetalinkParserController::commitSignatureTransaction()
  493. {
  494. if(_tSignature.isNull()) {
  495. return;
  496. }
  497. _tEntry->setSignature(_tSignature);
  498. _tSignature.reset();
  499. }
  500. void MetalinkParserController::cancelSignatureTransaction()
  501. {
  502. _tSignature.reset();
  503. }
  504. void MetalinkParserController::newMetaurlTransaction()
  505. {
  506. if(_tEntry.isNull()) {
  507. return;
  508. }
  509. _tMetaurl.reset(new MetalinkMetaurl());
  510. }
  511. void MetalinkParserController::setURLOfMetaurl(const std::string& url)
  512. {
  513. if(_tMetaurl.isNull()) {
  514. return;
  515. }
  516. _tMetaurl->url = url;
  517. }
  518. void MetalinkParserController::setMediatypeOfMetaurl
  519. (const std::string& mediatype)
  520. {
  521. if(_tMetaurl.isNull()) {
  522. return;
  523. }
  524. _tMetaurl->mediatype = mediatype;
  525. }
  526. void MetalinkParserController::setPriorityOfMetaurl(int priority)
  527. {
  528. if(_tMetaurl.isNull()) {
  529. return;
  530. }
  531. _tMetaurl->priority = priority;
  532. }
  533. void MetalinkParserController::setNameOfMetaurl(const std::string& name)
  534. {
  535. if(_tMetaurl.isNull()) {
  536. return;
  537. }
  538. _tMetaurl->name = name;
  539. }
  540. void MetalinkParserController::commitMetaurlTransaction()
  541. {
  542. if(_tMetaurl.isNull()) {
  543. return;
  544. }
  545. #ifdef ENABLE_BITTORRENT
  546. if(_tMetaurl->mediatype == MetalinkMetaurl::MEDIATYPE_TORRENT) {
  547. _tEntry->metaurls.push_back(_tMetaurl);
  548. }
  549. #endif // ENABLE_BITTORRENT
  550. _tMetaurl.reset();
  551. }
  552. void MetalinkParserController::cancelMetaurlTransaction()
  553. {
  554. _tMetaurl.reset();
  555. }
  556. } // namespace aria2