ExpatMetalinkProcessor.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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(const std::string& filename)
  165. {
  166. if(filename == DEV_STDIN) {
  167. return parseFile(std::cin);
  168. } else {
  169. std::ifstream infile(filename.c_str(), std::ios::binary);
  170. return parseFile(infile);
  171. }
  172. }
  173. SharedHandle<Metalinker>
  174. MetalinkProcessor::parseFile(std::istream& stream)
  175. {
  176. stm_.reset(new MetalinkParserStateMachine());
  177. char buf[4096];
  178. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  179. XML_Parser parser = createParser(sessionData);
  180. auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
  181. while(stream) {
  182. stream.read(buf, sizeof(buf));
  183. if(XML_Parse(parser, buf, stream.gcount(), 0) == XML_STATUS_ERROR) {
  184. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  185. error_code::METALINK_PARSE_ERROR);
  186. }
  187. }
  188. if(stream.bad()) {
  189. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  190. error_code::METALINK_PARSE_ERROR);
  191. }
  192. checkError(parser);
  193. return stm_->getResult();
  194. }
  195. SharedHandle<Metalinker>
  196. MetalinkProcessor::parseFromBinaryStream(const SharedHandle<BinaryStream>& binaryStream)
  197. {
  198. stm_.reset(new MetalinkParserStateMachine());
  199. ssize_t bufSize = 4096;
  200. unsigned char buf[bufSize];
  201. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  202. XML_Parser parser = createParser(sessionData);
  203. auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
  204. off_t readOffset = 0;
  205. while(1) {
  206. ssize_t res = binaryStream->readData(buf, bufSize, readOffset);
  207. if(res == 0) {
  208. break;
  209. }
  210. if(XML_Parse(parser, reinterpret_cast<const char*>(buf), res, 0) ==
  211. XML_STATUS_ERROR) {
  212. throw DL_ABORT_EX2(MSG_CANNOT_PARSE_METALINK,
  213. error_code::METALINK_PARSE_ERROR);
  214. }
  215. readOffset += res;
  216. }
  217. checkError(parser);
  218. return stm_->getResult();
  219. }
  220. } // namespace aria2