MetalinkParserController.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #include "uri.h"
  45. #include "Signature.h"
  46. #include "util.h"
  47. #ifdef ENABLE_MESSAGE_DIGEST
  48. # include "Checksum.h"
  49. # include "ChunkChecksum.h"
  50. # include "MessageDigest.h"
  51. #endif // ENABLE_MESSAGE_DIGEST
  52. #ifdef ENABLE_BITTORRENT
  53. # include "magnet.h"
  54. #endif // ENABLE_BITTORRENT
  55. namespace aria2 {
  56. MetalinkParserController::MetalinkParserController():
  57. metalinker_(new Metalinker())
  58. {}
  59. MetalinkParserController::~MetalinkParserController() {}
  60. void MetalinkParserController::newEntryTransaction()
  61. {
  62. tEntry_.reset(new MetalinkEntry());
  63. tResource_.reset();
  64. tMetaurl_.reset();
  65. #ifdef ENABLE_MESSAGE_DIGEST
  66. tChecksum_.reset();
  67. tChunkChecksumV4_.reset();
  68. tChunkChecksum_.reset();
  69. #endif // ENABLE_MESSAGE_DIGEST
  70. }
  71. void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
  72. {
  73. if(!tEntry_) {
  74. return;
  75. }
  76. if(!tEntry_->file) {
  77. tEntry_->file.reset(new FileEntry(util::escapePath(filename), 0, 0));
  78. } else {
  79. tEntry_->file->setPath(util::escapePath(filename));
  80. }
  81. }
  82. void MetalinkParserController::setFileLengthOfEntry(uint64_t length)
  83. {
  84. if(!tEntry_) {
  85. return;
  86. }
  87. if(!tEntry_->file) {
  88. return;
  89. }
  90. tEntry_->file->setLength(length);
  91. tEntry_->sizeKnown = true;
  92. }
  93. void MetalinkParserController::setVersionOfEntry(const std::string& version)
  94. {
  95. if(!tEntry_) {
  96. return;
  97. }
  98. tEntry_->version = version;
  99. }
  100. void MetalinkParserController::setLanguageOfEntry(const std::string& language)
  101. {
  102. if(!tEntry_) {
  103. return;
  104. }
  105. tEntry_->languages.push_back(language);
  106. }
  107. void MetalinkParserController::setOSOfEntry(const std::string& os)
  108. {
  109. if(!tEntry_) {
  110. return;
  111. }
  112. tEntry_->oses.push_back(os);
  113. }
  114. void MetalinkParserController::setMaxConnectionsOfEntry(int maxConnections)
  115. {
  116. if(!tEntry_) {
  117. return;
  118. }
  119. tEntry_->maxConnections = maxConnections;
  120. }
  121. void MetalinkParserController::commitEntryTransaction()
  122. {
  123. if(!tEntry_) {
  124. return;
  125. }
  126. commitResourceTransaction();
  127. commitMetaurlTransaction();
  128. commitChecksumTransaction();
  129. commitChunkChecksumTransactionV4();
  130. commitChunkChecksumTransaction();
  131. commitSignatureTransaction();
  132. metalinker_->addEntry(tEntry_);
  133. tEntry_.reset();
  134. }
  135. void MetalinkParserController::cancelEntryTransaction()
  136. {
  137. cancelResourceTransaction();
  138. cancelMetaurlTransaction();
  139. cancelChecksumTransaction();
  140. cancelChunkChecksumTransactionV4();
  141. cancelChunkChecksumTransaction();
  142. cancelSignatureTransaction();
  143. tEntry_.reset();
  144. }
  145. void MetalinkParserController::newResourceTransaction()
  146. {
  147. if(!tEntry_) {
  148. return;
  149. }
  150. tResource_.reset(new MetalinkResource());
  151. }
  152. void MetalinkParserController::setURLOfResource(const std::string& url)
  153. {
  154. if(!tResource_) {
  155. return;
  156. }
  157. std::string u = uri::joinUri(baseUri_, url);
  158. uri::UriStruct us;
  159. if(uri::parse(us, u)) {
  160. tResource_->url = u;
  161. if(tResource_->type == MetalinkResource::TYPE_UNKNOWN) {
  162. setTypeOfResource(us.protocol);
  163. }
  164. } else {
  165. tResource_->url = url;
  166. }
  167. }
  168. void MetalinkParserController::setTypeOfResource(const std::string& type)
  169. {
  170. if(!tResource_) {
  171. return;
  172. }
  173. if(type == MetalinkResource::FTP) {
  174. tResource_->type = MetalinkResource::TYPE_FTP;
  175. } else if(type == MetalinkResource::HTTP) {
  176. tResource_->type = MetalinkResource::TYPE_HTTP;
  177. } else if(type == MetalinkResource::HTTPS) {
  178. tResource_->type = MetalinkResource::TYPE_HTTPS;
  179. } else if(type == MetalinkResource::BITTORRENT) {
  180. tResource_->type = MetalinkResource::TYPE_BITTORRENT;
  181. } else if(type == MetalinkResource::TORRENT) { // Metalink4Spec
  182. tResource_->type = MetalinkResource::TYPE_BITTORRENT;
  183. } else {
  184. tResource_->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  185. }
  186. }
  187. void MetalinkParserController::setLocationOfResource(const std::string& location)
  188. {
  189. if(!tResource_) {
  190. return;
  191. }
  192. tResource_->location = location;
  193. }
  194. void MetalinkParserController::setPriorityOfResource(int priority)
  195. {
  196. if(!tResource_) {
  197. return;
  198. }
  199. tResource_->priority = priority;
  200. }
  201. void MetalinkParserController::setMaxConnectionsOfResource(int maxConnections)
  202. {
  203. if(!tResource_) {
  204. return;
  205. }
  206. tResource_->maxConnections = maxConnections;
  207. }
  208. void MetalinkParserController::commitResourceTransaction()
  209. {
  210. if(!tResource_) {
  211. return;
  212. }
  213. #ifdef ENABLE_BITTORRENT
  214. if(tResource_->type == MetalinkResource::TYPE_BITTORRENT) {
  215. SharedHandle<MetalinkMetaurl> metaurl(new MetalinkMetaurl());
  216. metaurl->url = tResource_->url;
  217. metaurl->priority = tResource_->priority;
  218. metaurl->mediatype = MetalinkMetaurl::MEDIATYPE_TORRENT;
  219. tEntry_->metaurls.push_back(metaurl);
  220. } else {
  221. tEntry_->resources.push_back(tResource_);
  222. }
  223. #else // !ENABLE_BITTORRENT
  224. tEntry_->resources.push_back(tResource_);
  225. #endif // !ENABLE_BITTORRENT
  226. tResource_.reset();
  227. }
  228. void MetalinkParserController::cancelResourceTransaction()
  229. {
  230. tResource_.reset();
  231. }
  232. void MetalinkParserController::newChecksumTransaction()
  233. {
  234. #ifdef ENABLE_MESSAGE_DIGEST
  235. if(!tEntry_) {
  236. return;
  237. }
  238. tChecksum_.reset(new Checksum());
  239. #endif // ENABLE_MESSAGE_DIGEST
  240. }
  241. void MetalinkParserController::setTypeOfChecksum(const std::string& type)
  242. {
  243. #ifdef ENABLE_MESSAGE_DIGEST
  244. if(!tChecksum_) {
  245. return;
  246. }
  247. std::string calgo = MessageDigest::getCanonicalHashType(type);
  248. if(MessageDigest::supports(calgo)) {
  249. tChecksum_->setAlgo(calgo);
  250. } else {
  251. cancelChecksumTransaction();
  252. }
  253. #endif // ENABLE_MESSAGE_DIGEST
  254. }
  255. void MetalinkParserController::setHashOfChecksum(const std::string& md)
  256. {
  257. #ifdef ENABLE_MESSAGE_DIGEST
  258. if(!tChecksum_) {
  259. return;
  260. }
  261. if(MessageDigest::isValidHash(tChecksum_->getAlgo(), md)) {
  262. tChecksum_->setMessageDigest(md);
  263. } else {
  264. cancelChecksumTransaction();
  265. }
  266. #endif // ENABLE_MESSAGE_DIGEST
  267. }
  268. void MetalinkParserController::commitChecksumTransaction()
  269. {
  270. #ifdef ENABLE_MESSAGE_DIGEST
  271. if(!tChecksum_) {
  272. return;
  273. }
  274. if(!tEntry_->checksum ||
  275. MessageDigest::isStronger(tChecksum_->getAlgo(),
  276. tEntry_->checksum->getAlgo())) {
  277. tEntry_->checksum = tChecksum_;
  278. }
  279. tChecksum_.reset();
  280. #endif // ENABLE_MESSAGE_DIGEST
  281. }
  282. void MetalinkParserController::cancelChecksumTransaction()
  283. {
  284. #ifdef ENABLE_MESSAGE_DIGEST
  285. tChecksum_.reset();
  286. #endif // ENABLE_MESSAGE_DIGEST
  287. }
  288. void MetalinkParserController::newChunkChecksumTransactionV4()
  289. {
  290. #ifdef ENABLE_MESSAGE_DIGEST
  291. if(!tEntry_) {
  292. return;
  293. }
  294. tChunkChecksumV4_.reset(new ChunkChecksum());
  295. tempChunkChecksumsV4_.clear();
  296. #endif // ENABLE_MESSAGE_DIGEST
  297. }
  298. void MetalinkParserController::setTypeOfChunkChecksumV4(const std::string& type)
  299. {
  300. #ifdef ENABLE_MESSAGE_DIGEST
  301. if(!tChunkChecksumV4_) {
  302. return;
  303. }
  304. std::string calgo = MessageDigest::getCanonicalHashType(type);
  305. if(MessageDigest::supports(calgo)) {
  306. tChunkChecksumV4_->setAlgo(calgo);
  307. } else {
  308. cancelChunkChecksumTransactionV4();
  309. }
  310. #endif // ENABLE_MESSAGE_DIGEST
  311. }
  312. void MetalinkParserController::setLengthOfChunkChecksumV4(size_t length)
  313. {
  314. #ifdef ENABLE_MESSAGE_DIGEST
  315. if(!tChunkChecksumV4_) {
  316. return;
  317. }
  318. if(length > 0) {
  319. tChunkChecksumV4_->setChecksumLength(length);
  320. } else {
  321. cancelChunkChecksumTransactionV4();
  322. }
  323. #endif // ENABLE_MESSAGE_DIGEST
  324. }
  325. void MetalinkParserController::addHashOfChunkChecksumV4(const std::string& md)
  326. {
  327. #ifdef ENABLE_MESSAGE_DIGEST
  328. if(!tChunkChecksumV4_) {
  329. return;
  330. }
  331. if(MessageDigest::isValidHash(tChunkChecksumV4_->getAlgo(), md)) {
  332. tempChunkChecksumsV4_.push_back(md);
  333. } else {
  334. cancelChunkChecksumTransactionV4();
  335. }
  336. #endif // ENABLE_MESSAGE_DIGEST
  337. }
  338. void MetalinkParserController::commitChunkChecksumTransactionV4()
  339. {
  340. #ifdef ENABLE_MESSAGE_DIGEST
  341. if(!tChunkChecksumV4_) {
  342. return;
  343. }
  344. if(!tEntry_->chunkChecksum ||
  345. MessageDigest::isStronger(tChunkChecksumV4_->getAlgo(),
  346. tEntry_->chunkChecksum->getAlgo())) {
  347. std::vector<std::string> checksums(tempChunkChecksumsV4_.begin(),
  348. tempChunkChecksumsV4_.end());
  349. tChunkChecksumV4_->setChecksums(checksums);
  350. tEntry_->chunkChecksum = tChunkChecksumV4_;
  351. }
  352. tChunkChecksumV4_.reset();
  353. #endif // ENABLE_MESSAGE_DIGEST
  354. }
  355. void MetalinkParserController::cancelChunkChecksumTransactionV4()
  356. {
  357. #ifdef ENABLE_MESSAGE_DIGEST
  358. tChunkChecksumV4_.reset();
  359. #endif // ENABLE_MESSAGE_DIGEST
  360. }
  361. void MetalinkParserController::newChunkChecksumTransaction()
  362. {
  363. #ifdef ENABLE_MESSAGE_DIGEST
  364. if(!tEntry_) {
  365. return;
  366. }
  367. tChunkChecksum_.reset(new ChunkChecksum());
  368. tempChunkChecksums_.clear();
  369. #endif // ENABLE_MESSAGE_DIGEST
  370. }
  371. void MetalinkParserController::setTypeOfChunkChecksum(const std::string& type)
  372. {
  373. #ifdef ENABLE_MESSAGE_DIGEST
  374. if(!tChunkChecksum_) {
  375. return;
  376. }
  377. std::string calgo = MessageDigest::getCanonicalHashType(type);
  378. if(MessageDigest::supports(calgo)) {
  379. tChunkChecksum_->setAlgo(calgo);
  380. } else {
  381. cancelChunkChecksumTransaction();
  382. }
  383. #endif // ENABLE_MESSAGE_DIGEST
  384. }
  385. void MetalinkParserController::setLengthOfChunkChecksum(size_t length)
  386. {
  387. #ifdef ENABLE_MESSAGE_DIGEST
  388. if(!tChunkChecksum_) {
  389. return;
  390. }
  391. if(length > 0) {
  392. tChunkChecksum_->setChecksumLength(length);
  393. } else {
  394. cancelChunkChecksumTransaction();
  395. }
  396. #endif // ENABLE_MESSAGE_DIGEST
  397. }
  398. void MetalinkParserController::addHashOfChunkChecksum(size_t order, const std::string& md)
  399. {
  400. #ifdef ENABLE_MESSAGE_DIGEST
  401. if(!tChunkChecksum_) {
  402. return;
  403. }
  404. if(MessageDigest::isValidHash(tChunkChecksum_->getAlgo(), md)) {
  405. tempChunkChecksums_.push_back(std::make_pair(order, md));
  406. } else {
  407. cancelChunkChecksumTransaction();
  408. }
  409. #endif // ENABLE_MESSAGE_DIGEST
  410. }
  411. void MetalinkParserController::createNewHashOfChunkChecksum(size_t order)
  412. {
  413. #ifdef ENABLE_MESSAGE_DIGEST
  414. if(!tChunkChecksum_) {
  415. return;
  416. }
  417. tempHashPair_.first = order;
  418. #endif // ENABLE_MESSAGE_DIGEST
  419. }
  420. void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string& md)
  421. {
  422. #ifdef ENABLE_MESSAGE_DIGEST
  423. if(!tChunkChecksum_) {
  424. return;
  425. }
  426. if(MessageDigest::isValidHash(tChunkChecksum_->getAlgo(), md)) {
  427. tempHashPair_.second = md;
  428. } else {
  429. cancelChunkChecksumTransaction();
  430. }
  431. #endif // ENABLE_MESSAGE_DIGEST
  432. }
  433. void MetalinkParserController::addHashOfChunkChecksum()
  434. {
  435. #ifdef ENABLE_MESSAGE_DIGEST
  436. if(!tChunkChecksum_) {
  437. return;
  438. }
  439. tempChunkChecksums_.push_back(tempHashPair_);
  440. #endif // ENABLE_MESSAGE_DIGEST
  441. }
  442. void MetalinkParserController::commitChunkChecksumTransaction()
  443. {
  444. #ifdef ENABLE_MESSAGE_DIGEST
  445. if(!tChunkChecksum_) {
  446. return;
  447. }
  448. if(!tEntry_->chunkChecksum ||
  449. MessageDigest::isStronger(tChunkChecksum_->getAlgo(),
  450. tEntry_->chunkChecksum->getAlgo())) {
  451. std::sort(tempChunkChecksums_.begin(), tempChunkChecksums_.end(),
  452. Ascend1st<std::pair<size_t, std::string> >());
  453. std::vector<std::string> checksums;
  454. std::transform(tempChunkChecksums_.begin(), tempChunkChecksums_.end(),
  455. std::back_inserter(checksums),
  456. select2nd<std::pair<size_t, std::string> >());
  457. tChunkChecksum_->setChecksums(checksums);
  458. tEntry_->chunkChecksum = tChunkChecksum_;
  459. }
  460. tChunkChecksum_.reset();
  461. #endif // ENABLE_MESSAGE_DIGEST
  462. }
  463. void MetalinkParserController::cancelChunkChecksumTransaction()
  464. {
  465. #ifdef ENABLE_MESSAGE_DIGEST
  466. tChunkChecksum_.reset();
  467. #endif // ENABLE_MESSAGE_DIGEST
  468. }
  469. void MetalinkParserController::newSignatureTransaction()
  470. {
  471. if(!tEntry_) {
  472. return;
  473. }
  474. tSignature_.reset(new Signature());
  475. }
  476. void MetalinkParserController::setTypeOfSignature(const std::string& type)
  477. {
  478. if(!tSignature_) {
  479. return;
  480. }
  481. tSignature_->setType(type);
  482. }
  483. void MetalinkParserController::setFileOfSignature(const std::string& file)
  484. {
  485. if(!tSignature_) {
  486. return;
  487. }
  488. tSignature_->setFile(file);
  489. }
  490. void MetalinkParserController::setBodyOfSignature(const std::string& body)
  491. {
  492. if(!tSignature_) {
  493. return;
  494. }
  495. tSignature_->setBody(body);
  496. }
  497. void MetalinkParserController::commitSignatureTransaction()
  498. {
  499. if(!tSignature_) {
  500. return;
  501. }
  502. tEntry_->setSignature(tSignature_);
  503. tSignature_.reset();
  504. }
  505. void MetalinkParserController::cancelSignatureTransaction()
  506. {
  507. tSignature_.reset();
  508. }
  509. void MetalinkParserController::newMetaurlTransaction()
  510. {
  511. if(!tEntry_) {
  512. return;
  513. }
  514. tMetaurl_.reset(new MetalinkMetaurl());
  515. }
  516. void MetalinkParserController::setURLOfMetaurl(const std::string& url)
  517. {
  518. if(!tMetaurl_) {
  519. return;
  520. }
  521. #ifdef ENABLE_BITTORRENT
  522. if(magnet::parse(url)) {
  523. tMetaurl_->url = url;
  524. } else
  525. #endif // ENABLE_BITTORRENT
  526. {
  527. std::string u = uri::joinUri(baseUri_, url);
  528. uri::UriStruct us;
  529. if(uri::parse(us, u)) {
  530. tMetaurl_->url = u;
  531. } else {
  532. tMetaurl_->url = url;
  533. }
  534. }
  535. }
  536. void MetalinkParserController::setMediatypeOfMetaurl
  537. (const std::string& mediatype)
  538. {
  539. if(!tMetaurl_) {
  540. return;
  541. }
  542. tMetaurl_->mediatype = mediatype;
  543. }
  544. void MetalinkParserController::setPriorityOfMetaurl(int priority)
  545. {
  546. if(!tMetaurl_) {
  547. return;
  548. }
  549. tMetaurl_->priority = priority;
  550. }
  551. void MetalinkParserController::setNameOfMetaurl(const std::string& name)
  552. {
  553. if(!tMetaurl_) {
  554. return;
  555. }
  556. tMetaurl_->name = name;
  557. }
  558. void MetalinkParserController::commitMetaurlTransaction()
  559. {
  560. if(!tMetaurl_) {
  561. return;
  562. }
  563. #ifdef ENABLE_BITTORRENT
  564. if(tMetaurl_->mediatype == MetalinkMetaurl::MEDIATYPE_TORRENT) {
  565. tEntry_->metaurls.push_back(tMetaurl_);
  566. }
  567. #endif // ENABLE_BITTORRENT
  568. tMetaurl_.reset();
  569. }
  570. void MetalinkParserController::cancelMetaurlTransaction()
  571. {
  572. tMetaurl_.reset();
  573. }
  574. } // namespace aria2