MetalinkParserController.cc 10 KB

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