MetalinkParserStateV3Impl.cc 12 KB

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