MetalinkParserController.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. _tEntry(0),
  51. _tResource(0)
  52. #ifdef ENABLE_MESSAGE_DIGEST
  53. ,
  54. _tChecksum(0),
  55. _tChunkChecksum(0)
  56. #endif // ENABLE_MESSAGE_DIGEST
  57. {}
  58. MetalinkParserController::~MetalinkParserController() {}
  59. SharedHandle<Metalinker> MetalinkParserController::getResult() const
  60. {
  61. return _metalinker;
  62. }
  63. void MetalinkParserController::newEntryTransaction()
  64. {
  65. _tEntry = new MetalinkEntry();
  66. _tResource = 0;
  67. #ifdef ENABLE_MESSAGE_DIGEST
  68. _tChecksum = 0;
  69. _tChunkChecksum = 0;
  70. #endif // ENABLE_MESSAGE_DIGEST
  71. }
  72. void MetalinkParserController::setFileNameOfEntry(const std::string& filename)
  73. {
  74. if(_tEntry.isNull()) {
  75. return;
  76. }
  77. if(_tEntry->file.isNull()) {
  78. _tEntry->file = new FileEntry(filename, 0, 0);
  79. } else {
  80. _tEntry->file->setPath(filename);
  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 = new FileEntry("", 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. _metalinker->entries.push_back(_tEntry);
  131. _tEntry = 0;
  132. }
  133. void MetalinkParserController::cancelEntryTransaction()
  134. {
  135. cancelResourceTransaction();
  136. cancelChecksumTransaction();
  137. cancelChunkChecksumTransaction();
  138. _tEntry = 0;
  139. }
  140. void MetalinkParserController::newResourceTransaction()
  141. {
  142. if(_tEntry.isNull()) {
  143. return;
  144. }
  145. _tResource = new MetalinkResource();
  146. }
  147. void MetalinkParserController::setURLOfResource(const std::string& url)
  148. {
  149. if(_tResource.isNull()) {
  150. return;
  151. }
  152. _tResource->url = url;
  153. }
  154. void MetalinkParserController::setTypeOfResource(const std::string& type)
  155. {
  156. if(_tResource.isNull()) {
  157. return;
  158. }
  159. if(type == "ftp") {
  160. _tResource->type = MetalinkResource::TYPE_FTP;
  161. } else if(type == "http") {
  162. _tResource->type = MetalinkResource::TYPE_HTTP;
  163. } else if(type == "https") {
  164. _tResource->type = MetalinkResource::TYPE_HTTPS;
  165. } else if(type == "bittorrent") {
  166. _tResource->type = MetalinkResource::TYPE_BITTORRENT;
  167. } else {
  168. _tResource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  169. }
  170. }
  171. void MetalinkParserController::setLocationOfResource(const std::string& location)
  172. {
  173. if(_tResource.isNull()) {
  174. return;
  175. }
  176. _tResource->location = location;
  177. }
  178. void MetalinkParserController::setPreferenceOfResource(int preference)
  179. {
  180. if(_tResource.isNull()) {
  181. return;
  182. }
  183. _tResource->preference = preference;
  184. }
  185. void MetalinkParserController::setMaxConnectionsOfResource(int maxConnections)
  186. {
  187. if(_tResource.isNull()) {
  188. return;
  189. }
  190. _tResource->maxConnections = maxConnections;
  191. }
  192. void MetalinkParserController::commitResourceTransaction()
  193. {
  194. if(_tResource.isNull()) {
  195. return;
  196. }
  197. _tEntry->resources.push_back(_tResource);
  198. _tResource = 0;
  199. }
  200. void MetalinkParserController::cancelResourceTransaction()
  201. {
  202. _tResource = 0;
  203. }
  204. void MetalinkParserController::newChecksumTransaction()
  205. {
  206. #ifdef ENABLE_MESSAGE_DIGEST
  207. if(_tEntry.isNull()) {
  208. return;
  209. }
  210. _tChecksum = new Checksum();
  211. #endif // ENABLE_MESSAGE_DIGEST
  212. }
  213. void MetalinkParserController::setTypeOfChecksum(const std::string& type)
  214. {
  215. #ifdef ENABLE_MESSAGE_DIGEST
  216. if(_tChecksum.isNull()) {
  217. return;
  218. }
  219. if(MessageDigestContext::supports(type)) {
  220. _tChecksum->setAlgo(type);
  221. } else {
  222. cancelChecksumTransaction();
  223. }
  224. #endif // ENABLE_MESSAGE_DIGEST
  225. }
  226. void MetalinkParserController::setHashOfChecksum(const std::string& md)
  227. {
  228. #ifdef ENABLE_MESSAGE_DIGEST
  229. if(_tChecksum.isNull()) {
  230. return;
  231. }
  232. _tChecksum->setMessageDigest(md);
  233. #endif // ENABLE_MESSAGE_DIGEST
  234. }
  235. void MetalinkParserController::commitChecksumTransaction()
  236. {
  237. #ifdef ENABLE_MESSAGE_DIGEST
  238. if(_tChecksum.isNull()) {
  239. return;
  240. }
  241. if(_tEntry->checksum.isNull() || _tEntry->checksum->getAlgo() != "sha1") {
  242. _tEntry->checksum = _tChecksum;
  243. }
  244. _tChecksum = 0;
  245. #endif // ENABLE_MESSAGE_DIGEST
  246. }
  247. void MetalinkParserController::cancelChecksumTransaction()
  248. {
  249. #ifdef ENABLE_MESSAGE_DIGEST
  250. _tChecksum = 0;
  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 = 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() || _tEntry->chunkChecksum->getAlgo() != "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 = 0;
  340. #endif // ENABLE_MESSAGE_DIGEST
  341. }
  342. void MetalinkParserController::cancelChunkChecksumTransaction()
  343. {
  344. #ifdef ENABLE_MESSAGE_DIGEST
  345. _tChunkChecksum = 0;
  346. #endif // ENABLE_MESSAGE_DIGEST
  347. }
  348. } // namespace aria2