ExpatMetalinkProcessor.cc 6.5 KB

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