MetalinkParserController.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #ifdef ENABLE_MESSAGE_DIGEST
  40. # include "Checksum.h"
  41. # include "ChunkChecksum.h"
  42. #endif // ENABLE_MESSAGE_DIGEST
  43. MetalinkParserController::MetalinkParserController():
  44. _metalinker(new Metalinker()),
  45. _tEntry(0),
  46. _tResource(0)
  47. #ifdef ENABLE_MESSAGE_DIGEST
  48. ,
  49. _tChecksum(0),
  50. _tChunkChecksum(0)
  51. #endif // ENABLE_MESSAGE_DIGEST
  52. {}
  53. MetalinkParserController::~MetalinkParserController() {}
  54. MetalinkerHandle MetalinkParserController::getResult() const
  55. {
  56. return _metalinker;
  57. }
  58. void MetalinkParserController::newEntryTransaction()
  59. {
  60. _tEntry = new MetalinkEntry();
  61. _tResource = 0;
  62. #ifdef ENABLE_MESSAGE_DIGEST
  63. _tChecksum = 0;
  64. _tChunkChecksum = 0;
  65. #endif // ENABLE_MESSAGE_DIGEST
  66. }
  67. void MetalinkParserController::setFileNameOfEntry(const string& filename)
  68. {
  69. if(_tEntry.isNull()) {
  70. return;
  71. }
  72. if(_tEntry->file.isNull()) {
  73. _tEntry->file = new FileEntry(filename, 0, 0);
  74. } else {
  75. _tEntry->file->setPath(filename);
  76. }
  77. }
  78. void MetalinkParserController::setFileLengthOfEntry(int64_t length)
  79. {
  80. if(_tEntry.isNull()) {
  81. return;
  82. }
  83. if(_tEntry->file.isNull()) {
  84. _tEntry->file = new FileEntry("", length, 0);
  85. } else {
  86. _tEntry->file->setLength(length);
  87. }
  88. }
  89. void MetalinkParserController::setVersionOfEntry(const string& version)
  90. {
  91. if(_tEntry.isNull()) {
  92. return;
  93. }
  94. _tEntry->version = version;
  95. }
  96. void MetalinkParserController::setLanguageOfEntry(const string& language)
  97. {
  98. if(_tEntry.isNull()) {
  99. return;
  100. }
  101. _tEntry->language = language;
  102. }
  103. void MetalinkParserController::setOSOfEntry(const string& os)
  104. {
  105. if(_tEntry.isNull()) {
  106. return;
  107. }
  108. _tEntry->os = os;
  109. }
  110. void MetalinkParserController::setMaxConnectionsOfEntry(int32_t 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 = 0;
  127. }
  128. void MetalinkParserController::cancelEntryTransaction()
  129. {
  130. cancelResourceTransaction();
  131. cancelChecksumTransaction();
  132. cancelChunkChecksumTransaction();
  133. _tEntry = 0;
  134. }
  135. void MetalinkParserController::newResourceTransaction()
  136. {
  137. if(_tEntry.isNull()) {
  138. return;
  139. }
  140. _tResource = new MetalinkResource();
  141. }
  142. void MetalinkParserController::setURLOfResource(const string& url)
  143. {
  144. if(_tResource.isNull()) {
  145. return;
  146. }
  147. _tResource->url = url;
  148. }
  149. void MetalinkParserController::setTypeOfResource(const string& type)
  150. {
  151. if(_tResource.isNull()) {
  152. return;
  153. }
  154. if(type == "ftp") {
  155. _tResource->type = MetalinkResource::TYPE_FTP;
  156. } else if(type == "http") {
  157. _tResource->type = MetalinkResource::TYPE_HTTP;
  158. } else if(type == "https") {
  159. _tResource->type = MetalinkResource::TYPE_HTTPS;
  160. } else if(type == "bittorrent") {
  161. _tResource->type = MetalinkResource::TYPE_BITTORRENT;
  162. } else {
  163. _tResource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  164. }
  165. }
  166. void MetalinkParserController::setLocationOfResource(const string& location)
  167. {
  168. if(_tResource.isNull()) {
  169. return;
  170. }
  171. _tResource->location = location;
  172. }
  173. void MetalinkParserController::setPreferenceOfResource(int32_t preference)
  174. {
  175. if(_tResource.isNull()) {
  176. return;
  177. }
  178. _tResource->preference = preference;
  179. }
  180. void MetalinkParserController::setMaxConnectionsOfResource(int32_t 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 = 0;
  194. }
  195. void MetalinkParserController::cancelResourceTransaction()
  196. {
  197. _tResource = 0;
  198. }
  199. void MetalinkParserController::newChecksumTransaction()
  200. {
  201. #ifdef ENABLE_MESSAGE_DIGEST
  202. if(_tEntry.isNull()) {
  203. return;
  204. }
  205. _tChecksum = new Checksum();
  206. #endif // ENABLE_MESSAGE_DIGEST
  207. }
  208. void MetalinkParserController::setTypeOfChecksum(const 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 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() || _tEntry->checksum->getAlgo() != "sha1") {
  237. _tEntry->checksum = _tChecksum;
  238. }
  239. _tChecksum = 0;
  240. #endif // ENABLE_MESSAGE_DIGEST
  241. }
  242. void MetalinkParserController::cancelChecksumTransaction()
  243. {
  244. #ifdef ENABLE_MESSAGE_DIGEST
  245. _tChecksum = 0;
  246. #endif // ENABLE_MESSAGE_DIGEST
  247. }
  248. void MetalinkParserController::newChunkChecksumTransaction()
  249. {
  250. #ifdef ENABLE_MESSAGE_DIGEST
  251. if(_tEntry.isNull()) {
  252. return;
  253. }
  254. _tChunkChecksum = new ChunkChecksum();
  255. _tempChunkChecksums.clear();
  256. #endif // ENABLE_MESSAGE_DIGEST
  257. }
  258. void MetalinkParserController::setTypeOfChunkChecksum(const string& type)
  259. {
  260. #ifdef ENABLE_MESSAGE_DIGEST
  261. if(_tChunkChecksum.isNull()) {
  262. return;
  263. }
  264. if(MessageDigestContext::supports(type)) {
  265. _tChunkChecksum->setAlgo(type);
  266. } else {
  267. cancelChunkChecksumTransaction();
  268. }
  269. #endif // ENABLE_MESSAGE_DIGEST
  270. }
  271. void MetalinkParserController::setLengthOfChunkChecksum(int32_t length)
  272. {
  273. #ifdef ENABLE_MESSAGE_DIGEST
  274. if(_tChunkChecksum.isNull()) {
  275. return;
  276. }
  277. if(length > 0) {
  278. _tChunkChecksum->setChecksumLength(length);
  279. } else {
  280. cancelChunkChecksumTransaction();
  281. }
  282. #endif // ENABLE_MESSAGE_DIGEST
  283. }
  284. void MetalinkParserController::addHashOfChunkChecksum(int32_t order, const string& md)
  285. {
  286. #ifdef ENABLE_MESSAGE_DIGEST
  287. if(_tChunkChecksum.isNull()) {
  288. return;
  289. }
  290. _tempChunkChecksums.push_back(pair<int32_t, string>(order, md));
  291. #endif // ENABLE_MESSAGE_DIGEST
  292. }
  293. void MetalinkParserController::createNewHashOfChunkChecksum(int32_t order)
  294. {
  295. #ifdef ENABLE_MESSAGE_DIGEST
  296. if(_tChunkChecksum.isNull()) {
  297. return;
  298. }
  299. _tempHashPair.first = order;
  300. #endif // ENABLE_MESSAGE_DIGEST
  301. }
  302. void MetalinkParserController::setMessageDigestOfChunkChecksum(const string& md)
  303. {
  304. #ifdef ENABLE_MESSAGE_DIGEST
  305. if(_tChunkChecksum.isNull()) {
  306. return;
  307. }
  308. _tempHashPair.second = md;
  309. #endif // ENABLE_MESSAGE_DIGEST
  310. }
  311. void MetalinkParserController::addHashOfChunkChecksum()
  312. {
  313. #ifdef ENABLE_MESSAGE_DIGEST
  314. if(_tChunkChecksum.isNull()) {
  315. return;
  316. }
  317. _tempChunkChecksums.push_back(_tempHashPair);
  318. #endif // ENABLE_MESSAGE_DIGEST
  319. }
  320. bool firstAsc(const pair<int32_t, string>& p1, const pair<int32_t, string>& p2)
  321. {
  322. return p1.first < p2.first;
  323. }
  324. class GetSecond
  325. {
  326. private:
  327. Strings& ss;
  328. public:
  329. GetSecond(Strings& s):ss(s) {}
  330. void operator()(const pair<int32_t, string>& p)
  331. {
  332. ss.push_back(p.second);
  333. }
  334. };
  335. void MetalinkParserController::commitChunkChecksumTransaction()
  336. {
  337. #ifdef ENABLE_MESSAGE_DIGEST
  338. if(_tChunkChecksum.isNull()) {
  339. return;
  340. }
  341. if(_tEntry->chunkChecksum.isNull() || _tEntry->chunkChecksum->getAlgo() != "sha1") {
  342. sort(_tempChunkChecksums.begin(), _tempChunkChecksums.end(), firstAsc);
  343. Strings checksums;
  344. for_each(_tempChunkChecksums.begin(), _tempChunkChecksums.end(), GetSecond(checksums));
  345. _tChunkChecksum->setChecksums(checksums);
  346. _tEntry->chunkChecksum = _tChunkChecksum;
  347. }
  348. _tChunkChecksum = 0;
  349. #endif // ENABLE_MESSAGE_DIGEST
  350. }
  351. void MetalinkParserController::cancelChunkChecksumTransaction()
  352. {
  353. #ifdef ENABLE_MESSAGE_DIGEST
  354. _tChunkChecksum = 0;
  355. #endif // ENABLE_MESSAGE_DIGEST
  356. }