MetalinkParserController.cc 9.2 KB

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