ExpatMetalinkProcessor.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "ExpatMetalinkProcessor.h"
  36. #include <iostream>
  37. #include <fstream>
  38. #include "DefaultDiskWriter.h"
  39. #include "MetalinkParserStateMachine.h"
  40. #include "Metalinker.h"
  41. #include "MetalinkEntry.h"
  42. #include "util.h"
  43. #include "message.h"
  44. #include "DlAbortEx.h"
  45. #include "MetalinkParserState.h"
  46. #include "A2STR.h"
  47. #include "error_code.h"
  48. namespace aria2 {
  49. namespace {
  50. class SessionData {
  51. public:
  52. SharedHandle<MetalinkParserStateMachine> stm_;
  53. std::deque<std::string> charactersStack_;
  54. SessionData(const SharedHandle<MetalinkParserStateMachine>& stm):stm_(stm) {}
  55. };
  56. } // namespace
  57. namespace {
  58. void splitNsName
  59. (std::string& localname, std::string& prefix, std::string& nsUri,
  60. const std::string& nsName)
  61. {
  62. std::pair<std::string, std::string> nsNamePair;
  63. util::divide(nsNamePair, nsName, '\t');
  64. if(nsNamePair.second.empty()) {
  65. localname = nsNamePair.first;
  66. } else {
  67. nsUri = nsNamePair.first;
  68. localname = nsNamePair.second;
  69. }
  70. }
  71. } // namespace
  72. namespace {
  73. void mlStartElement(void* userData, const char* nsName, const char** attrs)
  74. {
  75. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  76. std::vector<XmlAttr> xmlAttrs;
  77. if(attrs) {
  78. const char** p = attrs;
  79. while(*p != 0) {
  80. std::string attrNsName = *p++;
  81. if(*p == 0) {
  82. break;
  83. }
  84. std::string value = *p++;
  85. std::pair<std::string, std::string> nsNamePair;
  86. util::divide(nsNamePair, attrNsName, '\t');
  87. XmlAttr xa;
  88. if(nsNamePair.second.empty()) {
  89. xa.localname = nsNamePair.first;
  90. } else {
  91. xa.nsUri = nsNamePair.first;
  92. xa.localname = nsNamePair.second;
  93. }
  94. xa.value = value;
  95. xmlAttrs.push_back(xa);
  96. }
  97. }
  98. std::string localname;
  99. std::string prefix;
  100. std::string nsUri;
  101. splitNsName(localname, prefix, nsUri, nsName);
  102. sd->stm_->beginElement(localname, prefix, nsUri, xmlAttrs);
  103. if(sd->stm_->needsCharactersBuffering()) {
  104. sd->charactersStack_.push_front(A2STR::NIL);
  105. }
  106. }
  107. } // namespace
  108. namespace {
  109. void mlEndElement(void* userData, const char* nsName)
  110. {
  111. std::string localname;
  112. std::string prefix;
  113. std::string nsUri;
  114. splitNsName(localname, prefix, nsUri, nsName);
  115. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  116. std::string characters;
  117. if(sd->stm_->needsCharactersBuffering()) {
  118. characters = sd->charactersStack_.front();
  119. sd->charactersStack_.pop_front();
  120. }
  121. sd->stm_->endElement(localname, prefix, nsUri, characters);
  122. }
  123. } // namespace
  124. namespace {
  125. void mlCharacters(void* userData, const char* ch, int len)
  126. {
  127. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  128. if(sd->stm_->needsCharactersBuffering()) {
  129. sd->charactersStack_.front() += std::string(&ch[0], &ch[len]);
  130. }
  131. }
  132. } // namespace
  133. namespace {
  134. XML_Parser createParser(const SharedHandle<SessionData>& sessionData)
  135. {
  136. XML_Parser parser = XML_ParserCreateNS(0, static_cast<const XML_Char>('\t'));
  137. XML_SetUserData(parser, sessionData.get());
  138. XML_SetElementHandler(parser, &mlStartElement, &mlEndElement);
  139. XML_SetCharacterDataHandler(parser, &mlCharacters);
  140. return parser;
  141. }
  142. } // namespace
  143. namespace {
  144. void checkError(XML_Parser parser)
  145. {
  146. if(XML_Parse(parser, 0, 0, 1) == XML_STATUS_ERROR) {
  147. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  148. error_code::METALINK_PARSE_ERROR);
  149. }
  150. SessionData* sessionData =
  151. reinterpret_cast<SessionData*>(XML_GetUserData(parser));
  152. const SharedHandle<MetalinkParserStateMachine>& stm = sessionData->stm_;
  153. if(!stm->finished()) {
  154. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  155. error_code::METALINK_PARSE_ERROR);
  156. }
  157. if(!stm->getErrors().empty()) {
  158. throw DL_ABORT_EX2(stm->getErrorString(),
  159. error_code::METALINK_PARSE_ERROR);
  160. }
  161. }
  162. } // namespace
  163. SharedHandle<Metalinker>
  164. MetalinkProcessor::parseFile
  165. (const std::string& filename,
  166. const std::string& baseUri)
  167. {
  168. if(filename == DEV_STDIN) {
  169. return parseFile(std::cin);
  170. } else {
  171. std::ifstream infile(filename.c_str(), std::ios::binary);
  172. return parseFile(infile, baseUri);
  173. }
  174. }
  175. SharedHandle<Metalinker>
  176. MetalinkProcessor::parseFile
  177. (std::istream& stream,
  178. const std::string& baseUri)
  179. {
  180. stm_.reset(new MetalinkParserStateMachine());
  181. stm_->setBaseUri(baseUri);
  182. char buf[4096];
  183. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  184. XML_Parser parser = createParser(sessionData);
  185. auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
  186. while(stream) {
  187. stream.read(buf, sizeof(buf));
  188. if(XML_Parse(parser, buf, stream.gcount(), 0) == XML_STATUS_ERROR) {
  189. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  190. error_code::METALINK_PARSE_ERROR);
  191. }
  192. }
  193. if(stream.bad()) {
  194. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  195. error_code::METALINK_PARSE_ERROR);
  196. }
  197. checkError(parser);
  198. return stm_->getResult();
  199. }
  200. SharedHandle<Metalinker>
  201. MetalinkProcessor::parseFromBinaryStream
  202. (const SharedHandle<BinaryStream>& binaryStream,
  203. const std::string& baseUri)
  204. {
  205. stm_.reset(new MetalinkParserStateMachine());
  206. stm_->setBaseUri(baseUri);
  207. ssize_t bufSize = 4096;
  208. unsigned char buf[bufSize];
  209. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  210. XML_Parser parser = createParser(sessionData);
  211. auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
  212. off_t readOffset = 0;
  213. while(1) {
  214. ssize_t res = binaryStream->readData(buf, bufSize, readOffset);
  215. if(res == 0) {
  216. break;
  217. }
  218. if(XML_Parse(parser, reinterpret_cast<const char*>(buf), res, 0) ==
  219. XML_STATUS_ERROR) {
  220. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  221. error_code::METALINK_PARSE_ERROR);
  222. }
  223. readOffset += res;
  224. }
  225. checkError(parser);
  226. return stm_->getResult();
  227. }
  228. } // namespace aria2