MetalinkParserStateV3Impl.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 "MetalinkParserStateV3Impl.h"
  36. #include "MetalinkParserStateMachine.h"
  37. #include "RecoverableException.h"
  38. #include "MetalinkResource.h"
  39. #include "util.h"
  40. namespace aria2 {
  41. namespace {
  42. const std::string FILE("file");
  43. const std::string FILES("files");
  44. const std::string HASH("hash");
  45. const std::string LANGUAGE("language");
  46. const std::string LENGTH("length");
  47. const std::string LOCATION("location");
  48. const std::string MAXCONNECTIONS("maxconnections");
  49. // Can't use name VERSION because it is used as a macro.
  50. const std::string METALINK_VERSION("version");
  51. const std::string NAME("name");
  52. const std::string OS("os");
  53. const std::string PIECE("piece");
  54. const std::string PIECES("pieces");
  55. const std::string PREFERENCE("preference");
  56. const std::string RESOURCES("resources");
  57. const std::string SIGNATURE("signature");
  58. const std::string SIZE("size");
  59. const std::string TYPE("type");
  60. const std::string URL("url");
  61. const std::string VERIFICATION("verification");
  62. }
  63. const std::string METALINK3_NAMESPACE_URI("http://www.metalinker.org/");
  64. namespace {
  65. class FindAttr {
  66. private:
  67. const std::string& localname_;
  68. public:
  69. FindAttr(const std::string& localname):localname_(localname) {}
  70. bool operator()(const XmlAttr& attr) const
  71. {
  72. return attr.localname == localname_ &&
  73. (attr.nsUri.empty() || attr.nsUri == METALINK3_NAMESPACE_URI);
  74. }
  75. };
  76. }
  77. template<typename Container>
  78. static typename Container::const_iterator findAttr
  79. (const Container& attrs, const std::string& localname)
  80. {
  81. return std::find_if(attrs.begin(), attrs.end(), FindAttr(localname));
  82. }
  83. void MetalinkMetalinkParserState::beginElement
  84. (MetalinkParserStateMachine* stm,
  85. const std::string& localname,
  86. const std::string& prefix,
  87. const std::string& nsUri,
  88. const std::vector<XmlAttr>& attrs)
  89. {
  90. if(nsUri == METALINK3_NAMESPACE_URI && localname == FILES) {
  91. stm->setFilesState();
  92. } else {
  93. stm->setSkipTagState();
  94. }
  95. }
  96. void FilesMetalinkParserState::beginElement
  97. (MetalinkParserStateMachine* stm,
  98. const std::string& localname,
  99. const std::string& prefix,
  100. const std::string& nsUri,
  101. const std::vector<XmlAttr>& attrs)
  102. {
  103. if(nsUri == METALINK3_NAMESPACE_URI && localname == FILE) {
  104. stm->setFileState();
  105. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, NAME);
  106. if(itr != attrs.end()) {
  107. std::string name = util::trim((*itr).value);
  108. if(name.empty() || util::detectDirTraversal(name)) {
  109. return;
  110. }
  111. stm->newEntryTransaction();
  112. stm->setFileNameOfEntry(name);
  113. }
  114. } else {
  115. stm->setSkipTagState();
  116. }
  117. }
  118. void FileMetalinkParserState::beginElement
  119. (MetalinkParserStateMachine* stm,
  120. const std::string& localname,
  121. const std::string& prefix,
  122. const std::string& nsUri,
  123. const std::vector<XmlAttr>& attrs)
  124. {
  125. if(nsUri != METALINK3_NAMESPACE_URI) {
  126. stm->setSkipTagState();
  127. } else if(localname == SIZE) {
  128. stm->setSizeState();
  129. } else if(localname == METALINK_VERSION) {
  130. stm->setVersionState();
  131. } else if(localname == LANGUAGE) {
  132. stm->setLanguageState();
  133. } else if(localname == OS) {
  134. stm->setOSState();
  135. } else if(localname == VERIFICATION) {
  136. stm->setVerificationState();
  137. } else if(localname == RESOURCES) {
  138. stm->setResourcesState();
  139. int maxConnections;
  140. {
  141. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs,MAXCONNECTIONS);
  142. if(itr == attrs.end()) {
  143. maxConnections = -1;
  144. } else {
  145. try {
  146. maxConnections = util::parseInt((*itr).value);
  147. } catch(RecoverableException& e) {
  148. maxConnections = -1;
  149. }
  150. }
  151. }
  152. stm->setMaxConnectionsOfEntry(maxConnections);
  153. } else {
  154. stm->setSkipTagState();
  155. }
  156. }
  157. void FileMetalinkParserState::endElement
  158. (MetalinkParserStateMachine* stm,
  159. const std::string& localname,
  160. const std::string& prefix,
  161. const std::string& nsUri,
  162. const std::string& characters)
  163. {
  164. stm->commitEntryTransaction();
  165. }
  166. void SizeMetalinkParserState::endElement
  167. (MetalinkParserStateMachine* stm,
  168. const std::string& localname,
  169. const std::string& prefix,
  170. const std::string& nsUri,
  171. const std::string& characters)
  172. {
  173. try {
  174. stm->setFileLengthOfEntry(util::parseULLInt(characters));
  175. } catch(RecoverableException& e) {
  176. // current metalink specification doesn't require size element.
  177. }
  178. }
  179. void VersionMetalinkParserState::endElement
  180. (MetalinkParserStateMachine* stm,
  181. const std::string& localname,
  182. const std::string& prefix,
  183. const std::string& nsUri,
  184. const std::string& characters)
  185. {
  186. stm->setVersionOfEntry(util::trim(characters));
  187. }
  188. void LanguageMetalinkParserState::endElement
  189. (MetalinkParserStateMachine* stm,
  190. const std::string& localname,
  191. const std::string& prefix,
  192. const std::string& nsUri,
  193. const std::string& characters)
  194. {
  195. stm->setLanguageOfEntry(util::trim(characters));
  196. }
  197. void OSMetalinkParserState::endElement
  198. (MetalinkParserStateMachine* stm,
  199. const std::string& localname,
  200. const std::string& prefix,
  201. const std::string& nsUri,
  202. const std::string& characters)
  203. {
  204. stm->setOSOfEntry(util::trim(characters));
  205. }
  206. void VerificationMetalinkParserState::beginElement
  207. (MetalinkParserStateMachine* stm,
  208. const std::string& localname,
  209. const std::string& prefix,
  210. const std::string& nsUri,
  211. const std::vector<XmlAttr>& attrs)
  212. {
  213. if(nsUri != METALINK3_NAMESPACE_URI) {
  214. stm->setSkipTagState();
  215. } else
  216. #ifdef ENABLE_MESSAGE_DIGEST
  217. if(localname == HASH) {
  218. stm->setHashState();
  219. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
  220. if(itr == attrs.end()) {
  221. return;
  222. } else {
  223. std::string type = util::trim((*itr).value);
  224. stm->newChecksumTransaction();
  225. stm->setTypeOfChecksum(type);
  226. }
  227. } else if(localname == PIECES) {
  228. stm->setPiecesState();
  229. try {
  230. size_t length;
  231. {
  232. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, LENGTH);
  233. if(itr == attrs.end()) {
  234. return;
  235. } else {
  236. length = util::parseInt((*itr).value);
  237. }
  238. }
  239. std::string type;
  240. {
  241. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
  242. if(itr == attrs.end()) {
  243. return;
  244. } else {
  245. type = util::trim((*itr).value);
  246. }
  247. }
  248. stm->newChunkChecksumTransaction();
  249. stm->setLengthOfChunkChecksum(length);
  250. stm->setTypeOfChunkChecksum(type);
  251. } catch(RecoverableException& e) {
  252. stm->cancelChunkChecksumTransaction();
  253. }
  254. } else
  255. #endif // ENABLE_MESSAGE_DIGEST
  256. if(localname == SIGNATURE) {
  257. stm->setSignatureState();
  258. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
  259. if(itr == attrs.end()) {
  260. return;
  261. } else {
  262. stm->newSignatureTransaction();
  263. stm->setTypeOfSignature(util::trim((*itr).value));
  264. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, FILE);
  265. if(itr != attrs.end()) {
  266. std::string file = util::trim((*itr).value);
  267. if(!util::detectDirTraversal(file)) {
  268. stm->setFileOfSignature(file);
  269. }
  270. }
  271. }
  272. } else {
  273. stm->setSkipTagState();
  274. }
  275. }
  276. void HashMetalinkParserState::endElement
  277. (MetalinkParserStateMachine* stm,
  278. const std::string& localname,
  279. const std::string& prefix,
  280. const std::string& nsUri,
  281. const std::string& characters)
  282. {
  283. stm->setHashOfChecksum(util::trim(characters));
  284. stm->commitChecksumTransaction();
  285. }
  286. void PiecesMetalinkParserState::beginElement
  287. (MetalinkParserStateMachine* stm,
  288. const std::string& localname,
  289. const std::string& prefix,
  290. const std::string& nsUri,
  291. const std::vector<XmlAttr>& attrs)
  292. {
  293. if(nsUri == METALINK3_NAMESPACE_URI && localname == HASH) {
  294. stm->setPieceHashState();
  295. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, PIECE);
  296. if(itr == attrs.end()) {
  297. stm->cancelChunkChecksumTransaction();
  298. } else {
  299. try {
  300. stm->createNewHashOfChunkChecksum(util::parseInt((*itr).value));
  301. } catch(RecoverableException& e) {
  302. stm->cancelChunkChecksumTransaction();
  303. }
  304. }
  305. } else {
  306. stm->setSkipTagState();
  307. }
  308. }
  309. void PiecesMetalinkParserState::endElement
  310. (MetalinkParserStateMachine* stm,
  311. const std::string& localname,
  312. const std::string& prefix,
  313. const std::string& nsUri,
  314. const std::string& characters)
  315. {
  316. stm->commitChunkChecksumTransaction();
  317. }
  318. void PieceHashMetalinkParserState::endElement
  319. (MetalinkParserStateMachine* stm,
  320. const std::string& localname,
  321. const std::string& prefix,
  322. const std::string& nsUri,
  323. const std::string& characters)
  324. {
  325. stm->setMessageDigestOfChunkChecksum(util::trim(characters));
  326. stm->addHashOfChunkChecksum();
  327. }
  328. void SignatureMetalinkParserState::endElement
  329. (MetalinkParserStateMachine* stm,
  330. const std::string& localname,
  331. const std::string& prefix,
  332. const std::string& nsUri,
  333. const std::string& characters)
  334. {
  335. stm->setBodyOfSignature(util::trim(characters));
  336. stm->commitSignatureTransaction();
  337. }
  338. void ResourcesMetalinkParserState::beginElement
  339. (MetalinkParserStateMachine* stm,
  340. const std::string& localname,
  341. const std::string& prefix,
  342. const std::string& nsUri,
  343. const std::vector<XmlAttr>& attrs)
  344. {
  345. if(nsUri != METALINK3_NAMESPACE_URI) {
  346. stm->setSkipTagState();
  347. } else if(localname == URL) {
  348. stm->setURLState();
  349. std::string type;
  350. {
  351. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, TYPE);
  352. if(itr == attrs.end()) {
  353. return;
  354. } else {
  355. type = util::trim((*itr).value);
  356. }
  357. }
  358. std::string location;
  359. {
  360. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, LOCATION);
  361. if(itr != attrs.end()) {
  362. location = util::trim((*itr).value);
  363. }
  364. }
  365. int preference;
  366. {
  367. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs, PREFERENCE);
  368. if(itr == attrs.end()) {
  369. preference = MetalinkResource::getLowestPriority();
  370. } else {
  371. try {
  372. // In Metalink3Spec, highest prefernce value is 100. We
  373. // uses Metalink4Spec priority unit system in which 1 is
  374. // higest.
  375. preference = 101-util::parseInt((*itr).value);
  376. } catch(RecoverableException& e) {
  377. preference = MetalinkResource::getLowestPriority();
  378. }
  379. }
  380. }
  381. int maxConnections;
  382. {
  383. std::vector<XmlAttr>::const_iterator itr = findAttr(attrs,MAXCONNECTIONS);
  384. if(itr == attrs.end()) {
  385. maxConnections = -1;
  386. } else {
  387. try {
  388. maxConnections = util::parseInt((*itr).value);
  389. } catch(RecoverableException& e) {
  390. maxConnections = -1;
  391. }
  392. }
  393. }
  394. stm->newResourceTransaction();
  395. stm->setTypeOfResource(type);
  396. stm->setLocationOfResource(location);
  397. stm->setPriorityOfResource(preference);
  398. stm->setMaxConnectionsOfResource(maxConnections);
  399. } else {
  400. stm->setSkipTagState();
  401. }
  402. }
  403. void URLMetalinkParserState::endElement
  404. (MetalinkParserStateMachine* stm,
  405. const std::string& localname,
  406. const std::string& prefix,
  407. const std::string& nsUri,
  408. const std::string& characters)
  409. {
  410. stm->setURLOfResource(util::trim(characters));
  411. stm->commitResourceTransaction();
  412. }
  413. } // namespace aria2