MetalinkParserController.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 "FileEntry.h"
  41. #include "a2functional.h"
  42. #include "A2STR.h"
  43. #ifdef ENABLE_MESSAGE_DIGEST
  44. # include "Checksum.h"
  45. # include "ChunkChecksum.h"
  46. # include "messageDigest.h"
  47. #endif // ENABLE_MESSAGE_DIGEST
  48. #include "Signature.h"
  49. #include "Util.h"
  50. namespace aria2 {
  51. const std::string MetalinkParserController::SHA1("sha1");
  52. MetalinkParserController::MetalinkParserController():
  53. _metalinker(new Metalinker())
  54. {}
  55. MetalinkParserController::~MetalinkParserController() {}
  56. SharedHandle<Metalinker> MetalinkParserController::getResult() const
  57. {
  58. return _metalinker;
  59. }
  60. void MetalinkParserController::newEntryTransaction()
  61. {
  62. _tEntry.reset(new MetalinkEntry());
  63. _tResource.reset();
  64. #ifdef ENABLE_MESSAGE_DIGEST
  65. _tChecksum.reset();
  66. _tChunkChecksum.reset();
  67. #endif // ENABLE_MESSAGE_DIGEST
  68. }
  69. void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
  70. {
  71. if(_tEntry.isNull()) {
  72. return;
  73. }
  74. std::deque<std::string> elements;
  75. Util::slice(elements, filename, '/');
  76. std::string path = Util::joinPath(elements.begin(), elements.end());
  77. if(_tEntry->file.isNull()) {
  78. _tEntry->file.reset(new FileEntry(path, 0, 0));
  79. } else {
  80. _tEntry->file->setPath(path);
  81. }
  82. }
  83. void MetalinkParserController::setFileLengthOfEntry(uint64_t length)
  84. {
  85. if(_tEntry.isNull()) {
  86. return;
  87. }
  88. if(_tEntry->file.isNull()) {
  89. _tEntry->file.reset(new FileEntry(A2STR::NIL, length, 0));
  90. } else {
  91. _tEntry->file->setLength(length);
  92. }
  93. }
  94. void MetalinkParserController::setVersionOfEntry(const std::string& version)
  95. {
  96. if(_tEntry.isNull()) {
  97. return;
  98. }
  99. _tEntry->version = version;
  100. }
  101. void MetalinkParserController::setLanguageOfEntry(const std::string& language)
  102. {
  103. if(_tEntry.isNull()) {
  104. return;
  105. }
  106. _tEntry->language = language;
  107. }
  108. void MetalinkParserController::setOSOfEntry(const std::string& os)
  109. {
  110. if(_tEntry.isNull()) {
  111. return;
  112. }
  113. _tEntry->os = os;
  114. }
  115. void MetalinkParserController::setMaxConnectionsOfEntry(int maxConnections)
  116. {
  117. if(_tEntry.isNull()) {
  118. return;
  119. }
  120. _tEntry->maxConnections = maxConnections;
  121. }
  122. void MetalinkParserController::commitEntryTransaction()
  123. {
  124. if(_tEntry.isNull()) {
  125. return;
  126. }
  127. commitResourceTransaction();
  128. commitChecksumTransaction();
  129. commitChunkChecksumTransaction();
  130. commitSignatureTransaction();
  131. _metalinker->entries.push_back(_tEntry);
  132. _tEntry.reset();
  133. }
  134. void MetalinkParserController::cancelEntryTransaction()
  135. {
  136. cancelResourceTransaction();
  137. cancelChecksumTransaction();
  138. cancelChunkChecksumTransaction();
  139. cancelSignatureTransaction();
  140. _tEntry.reset();
  141. }
  142. void MetalinkParserController::newResourceTransaction()
  143. {
  144. if(_tEntry.isNull()) {
  145. return;
  146. }
  147. _tResource.reset(new MetalinkResource());
  148. }
  149. void MetalinkParserController::setURLOfResource(const std::string& url)
  150. {
  151. if(_tResource.isNull()) {
  152. return;
  153. }
  154. _tResource->url = url;
  155. }
  156. void MetalinkParserController::setTypeOfResource(const std::string& type)
  157. {
  158. if(_tResource.isNull()) {
  159. return;
  160. }
  161. if(type == MetalinkResource::FTP) {
  162. _tResource->type = MetalinkResource::TYPE_FTP;
  163. } else if(type == MetalinkResource::HTTP) {
  164. _tResource->type = MetalinkResource::TYPE_HTTP;
  165. } else if(type == MetalinkResource::HTTPS) {
  166. _tResource->type = MetalinkResource::TYPE_HTTPS;
  167. } else if(type == MetalinkResource::BITTORRENT) {
  168. _tResource->type = MetalinkResource::TYPE_BITTORRENT;
  169. } else {
  170. _tResource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  171. }
  172. }
  173. void MetalinkParserController::setLocationOfResource(const std::string& location)
  174. {
  175. if(_tResource.isNull()) {
  176. return;
  177. }
  178. _tResource->location = location;
  179. }
  180. void MetalinkParserController::setPreferenceOfResource(int preference)
  181. {
  182. if(_tResource.isNull()) {
  183. return;
  184. }
  185. _tResource->preference = preference;
  186. }
  187. void MetalinkParserController::setMaxConnectionsOfResource(int maxConnections)
  188. {
  189. if(_tResource.isNull()) {
  190. return;
  191. }
  192. _tResource->maxConnections = maxConnections;
  193. }
  194. void MetalinkParserController::commitResourceTransaction()
  195. {
  196. if(_tResource.isNull()) {
  197. return;
  198. }
  199. _tEntry->resources.push_back(_tResource);
  200. _tResource.reset();
  201. }
  202. void MetalinkParserController::cancelResourceTransaction()
  203. {
  204. _tResource.reset();
  205. }
  206. void MetalinkParserController::newChecksumTransaction()
  207. {
  208. #ifdef ENABLE_MESSAGE_DIGEST
  209. if(_tEntry.isNull()) {
  210. return;
  211. }
  212. _tChecksum.reset(new Checksum());
  213. #endif // ENABLE_MESSAGE_DIGEST
  214. }
  215. void MetalinkParserController::setTypeOfChecksum(const std::string& type)
  216. {
  217. #ifdef ENABLE_MESSAGE_DIGEST
  218. if(_tChecksum.isNull()) {
  219. return;
  220. }
  221. if(MessageDigestContext::supports(type)) {
  222. _tChecksum->setAlgo(type);
  223. } else {
  224. cancelChecksumTransaction();
  225. }
  226. #endif // ENABLE_MESSAGE_DIGEST
  227. }
  228. void MetalinkParserController::setHashOfChecksum(const std::string& md)
  229. {
  230. #ifdef ENABLE_MESSAGE_DIGEST
  231. if(_tChecksum.isNull()) {
  232. return;
  233. }
  234. _tChecksum->setMessageDigest(md);
  235. #endif // ENABLE_MESSAGE_DIGEST
  236. }
  237. void MetalinkParserController::commitChecksumTransaction()
  238. {
  239. #ifdef ENABLE_MESSAGE_DIGEST
  240. if(_tChecksum.isNull()) {
  241. return;
  242. }
  243. if(_tEntry->checksum.isNull() ||
  244. _tEntry->checksum->getAlgo() != MetalinkParserController::SHA1) {
  245. _tEntry->checksum = _tChecksum;
  246. }
  247. _tChecksum.reset();
  248. #endif // ENABLE_MESSAGE_DIGEST
  249. }
  250. void MetalinkParserController::cancelChecksumTransaction()
  251. {
  252. #ifdef ENABLE_MESSAGE_DIGEST
  253. _tChecksum.reset();
  254. #endif // ENABLE_MESSAGE_DIGEST
  255. }
  256. void MetalinkParserController::newChunkChecksumTransaction()
  257. {
  258. #ifdef ENABLE_MESSAGE_DIGEST
  259. if(_tEntry.isNull()) {
  260. return;
  261. }
  262. _tChunkChecksum.reset(new ChunkChecksum());
  263. _tempChunkChecksums.clear();
  264. #endif // ENABLE_MESSAGE_DIGEST
  265. }
  266. void MetalinkParserController::setTypeOfChunkChecksum(const std::string& type)
  267. {
  268. #ifdef ENABLE_MESSAGE_DIGEST
  269. if(_tChunkChecksum.isNull()) {
  270. return;
  271. }
  272. if(MessageDigestContext::supports(type)) {
  273. _tChunkChecksum->setAlgo(type);
  274. } else {
  275. cancelChunkChecksumTransaction();
  276. }
  277. #endif // ENABLE_MESSAGE_DIGEST
  278. }
  279. void MetalinkParserController::setLengthOfChunkChecksum(size_t length)
  280. {
  281. #ifdef ENABLE_MESSAGE_DIGEST
  282. if(_tChunkChecksum.isNull()) {
  283. return;
  284. }
  285. if(length > 0) {
  286. _tChunkChecksum->setChecksumLength(length);
  287. } else {
  288. cancelChunkChecksumTransaction();
  289. }
  290. #endif // ENABLE_MESSAGE_DIGEST
  291. }
  292. void MetalinkParserController::addHashOfChunkChecksum(size_t order, const std::string& md)
  293. {
  294. #ifdef ENABLE_MESSAGE_DIGEST
  295. if(_tChunkChecksum.isNull()) {
  296. return;
  297. }
  298. _tempChunkChecksums.push_back(std::pair<size_t, std::string>(order, md));
  299. #endif // ENABLE_MESSAGE_DIGEST
  300. }
  301. void MetalinkParserController::createNewHashOfChunkChecksum(size_t order)
  302. {
  303. #ifdef ENABLE_MESSAGE_DIGEST
  304. if(_tChunkChecksum.isNull()) {
  305. return;
  306. }
  307. _tempHashPair.first = order;
  308. #endif // ENABLE_MESSAGE_DIGEST
  309. }
  310. void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string& md)
  311. {
  312. #ifdef ENABLE_MESSAGE_DIGEST
  313. if(_tChunkChecksum.isNull()) {
  314. return;
  315. }
  316. _tempHashPair.second = md;
  317. #endif // ENABLE_MESSAGE_DIGEST
  318. }
  319. void MetalinkParserController::addHashOfChunkChecksum()
  320. {
  321. #ifdef ENABLE_MESSAGE_DIGEST
  322. if(_tChunkChecksum.isNull()) {
  323. return;
  324. }
  325. _tempChunkChecksums.push_back(_tempHashPair);
  326. #endif // ENABLE_MESSAGE_DIGEST
  327. }
  328. void MetalinkParserController::commitChunkChecksumTransaction()
  329. {
  330. #ifdef ENABLE_MESSAGE_DIGEST
  331. if(_tChunkChecksum.isNull()) {
  332. return;
  333. }
  334. if(_tEntry->chunkChecksum.isNull() ||
  335. _tEntry->chunkChecksum->getAlgo() != MetalinkParserController::SHA1) {
  336. std::sort(_tempChunkChecksums.begin(), _tempChunkChecksums.end(), Ascend1st<std::pair<size_t, std::string> >());
  337. std::deque<std::string> checksums;
  338. std::transform(_tempChunkChecksums.begin(), _tempChunkChecksums.end(),
  339. std::back_inserter(checksums), select2nd<std::pair<size_t, std::string> >());
  340. _tChunkChecksum->setChecksums(checksums);
  341. _tEntry->chunkChecksum = _tChunkChecksum;
  342. }
  343. _tChunkChecksum.reset();
  344. #endif // ENABLE_MESSAGE_DIGEST
  345. }
  346. void MetalinkParserController::cancelChunkChecksumTransaction()
  347. {
  348. #ifdef ENABLE_MESSAGE_DIGEST
  349. _tChunkChecksum.reset();
  350. #endif // ENABLE_MESSAGE_DIGEST
  351. }
  352. void MetalinkParserController::newSignatureTransaction()
  353. {
  354. if(_tEntry.isNull()) {
  355. return;
  356. }
  357. _tSignature.reset(new Signature());
  358. }
  359. void MetalinkParserController::setTypeOfSignature(const std::string& type)
  360. {
  361. if(_tSignature.isNull()) {
  362. return;
  363. }
  364. _tSignature->setType(type);
  365. }
  366. void MetalinkParserController::setFileOfSignature(const std::string& file)
  367. {
  368. if(_tSignature.isNull()) {
  369. return;
  370. }
  371. _tSignature->setFile(file);
  372. }
  373. void MetalinkParserController::setBodyOfSignature(const std::string& body)
  374. {
  375. if(_tSignature.isNull()) {
  376. return;
  377. }
  378. _tSignature->setBody(body);
  379. }
  380. void MetalinkParserController::commitSignatureTransaction()
  381. {
  382. if(_tSignature.isNull()) {
  383. return;
  384. }
  385. _tEntry->setSignature(_tSignature);
  386. _tSignature.reset();
  387. }
  388. void MetalinkParserController::cancelSignatureTransaction()
  389. {
  390. _tSignature.reset();
  391. }
  392. } // namespace aria2