MetalinkParserController.cc 14 KB

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