MetalinkParserController.cc 15 KB

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