MetalinkParserController.cc 9.4 KB

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