MetalinkParserController.cc 10 KB

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