ExpatMetalinkProcessor.cc 6.5 KB

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