XML2SAXMetalinkProcessor.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "XML2SAXMetalinkProcessor.h"
  36. #include <cassert>
  37. #include "BinaryStream.h"
  38. #include "MetalinkParserStateMachine.h"
  39. #include "Metalinker.h"
  40. #include "MetalinkEntry.h"
  41. #include "util.h"
  42. #include "message.h"
  43. #include "DlAbortEx.h"
  44. #include "A2STR.h"
  45. namespace aria2 {
  46. class SessionData {
  47. public:
  48. SharedHandle<MetalinkParserStateMachine> _stm;
  49. std::deque<std::string> _charactersStack;
  50. SessionData(const SharedHandle<MetalinkParserStateMachine>& stm):_stm(stm) {}
  51. };
  52. static void mlStartElement
  53. (void* userData,
  54. const xmlChar* srcLocalname,
  55. const xmlChar* srcPrefix,
  56. const xmlChar* srcNsUri,
  57. int numNamespaces,
  58. const xmlChar **namespaces,
  59. int numAttrs,
  60. int numDefaulted,
  61. const xmlChar **attrs)
  62. {
  63. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  64. std::vector<XmlAttr> xmlAttrs;
  65. size_t index = 0;
  66. for(int attrIndex = 0; attrIndex < numAttrs; ++attrIndex, index += 5) {
  67. XmlAttr xmlAttr;
  68. assert(attrs[index]);
  69. xmlAttr.localname = reinterpret_cast<const char*>(attrs[index]);
  70. if(attrs[index+1]) {
  71. xmlAttr.prefix = reinterpret_cast<const char*>(attrs[index+1]);
  72. }
  73. if(attrs[index+2]) {
  74. xmlAttr.nsUri = reinterpret_cast<const char*>(attrs[index+2]);
  75. }
  76. const char* valueBegin = reinterpret_cast<const char*>(attrs[index+3]);
  77. const char* valueEnd = reinterpret_cast<const char*>(attrs[index+4]);
  78. xmlAttr.value = std::string(valueBegin, valueEnd);
  79. xmlAttrs.push_back(xmlAttr);
  80. }
  81. assert(srcLocalname);
  82. std::string localname = reinterpret_cast<const char*>(srcLocalname);
  83. std::string prefix;
  84. std::string nsUri;
  85. if(srcPrefix) {
  86. prefix = reinterpret_cast<const char*>(srcPrefix);
  87. }
  88. if(srcNsUri) {
  89. nsUri = reinterpret_cast<const char*>(srcNsUri);
  90. }
  91. sd->_stm->beginElement(localname, prefix, nsUri, xmlAttrs);
  92. if(sd->_stm->needsCharactersBuffering()) {
  93. sd->_charactersStack.push_front(A2STR::NIL);
  94. }
  95. }
  96. static void mlEndElement
  97. (void* userData,
  98. const xmlChar* srcLocalname,
  99. const xmlChar* srcPrefix,
  100. const xmlChar* srcNsUri)
  101. {
  102. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  103. std::string characters;
  104. if(sd->_stm->needsCharactersBuffering()) {
  105. characters = sd->_charactersStack.front();
  106. sd->_charactersStack.pop_front();
  107. }
  108. std::string localname = reinterpret_cast<const char*>(srcLocalname);
  109. std::string prefix;
  110. std::string nsUri;
  111. if(srcPrefix) {
  112. prefix = reinterpret_cast<const char*>(srcPrefix);
  113. }
  114. if(srcNsUri) {
  115. nsUri = reinterpret_cast<const char*>(srcNsUri);
  116. }
  117. sd->_stm->endElement(localname, prefix, nsUri, characters);
  118. }
  119. static void mlCharacters(void* userData, const xmlChar* ch, int len)
  120. {
  121. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  122. if(sd->_stm->needsCharactersBuffering()) {
  123. sd->_charactersStack.front() += std::string(&ch[0], &ch[len]);
  124. }
  125. }
  126. static xmlSAXHandler mySAXHandler =
  127. {
  128. 0, // internalSubsetSAXFunc
  129. 0, // isStandaloneSAXFunc
  130. 0, // hasInternalSubsetSAXFunc
  131. 0, // hasExternalSubsetSAXFunc
  132. 0, // resolveEntitySAXFunc
  133. 0, // getEntitySAXFunc
  134. 0, // entityDeclSAXFunc
  135. 0, // notationDeclSAXFunc
  136. 0, // attributeDeclSAXFunc
  137. 0, // elementDeclSAXFunc
  138. 0, // unparsedEntityDeclSAXFunc
  139. 0, // setDocumentLocatorSAXFunc
  140. 0, // startDocumentSAXFunc
  141. 0, // endDocumentSAXFunc
  142. 0, // startElementSAXFunc
  143. 0, // endElementSAXFunc
  144. 0, // referenceSAXFunc
  145. &mlCharacters, // charactersSAXFunc
  146. 0, // ignorableWhitespaceSAXFunc
  147. 0, // processingInstructionSAXFunc
  148. 0, // commentSAXFunc
  149. 0, // warningSAXFunc
  150. 0, // errorSAXFunc
  151. 0, // fatalErrorSAXFunc
  152. 0, // getParameterEntitySAXFunc
  153. 0, // cdataBlockSAXFunc
  154. 0, // externalSubsetSAXFunc
  155. XML_SAX2_MAGIC, // unsigned int initialized
  156. 0, // void * _private
  157. &mlStartElement, // startElementNsSAX2Func
  158. &mlEndElement, // endElementNsSAX2Func
  159. 0, // xmlStructuredErrorFunc
  160. };
  161. SharedHandle<Metalinker>
  162. MetalinkProcessor::parseFile(const std::string& filename)
  163. {
  164. _stm.reset(new MetalinkParserStateMachine());
  165. SharedHandle<SessionData> sessionData(new SessionData(_stm));
  166. int retval = xmlSAXUserParseFile(&mySAXHandler, sessionData.get(),
  167. filename.c_str());
  168. if(retval != 0) {
  169. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  170. }
  171. return _stm->getResult();
  172. }
  173. SharedHandle<Metalinker>
  174. MetalinkProcessor::parseFromBinaryStream(const SharedHandle<BinaryStream>& binaryStream)
  175. {
  176. _stm.reset(new MetalinkParserStateMachine());
  177. size_t bufSize = 4096;
  178. unsigned char buf[bufSize];
  179. ssize_t res = binaryStream->readData(buf, 4, 0);
  180. if(res != 4) {
  181. throw DL_ABORT_EX("Too small data for parsing XML.");
  182. }
  183. SharedHandle<SessionData> sessionData(new SessionData(_stm));
  184. xmlParserCtxtPtr ctx = xmlCreatePushParserCtxt
  185. (&mySAXHandler, sessionData.get(),
  186. reinterpret_cast<const char*>(buf), res, 0);
  187. try {
  188. off_t readOffset = res;
  189. while(1) {
  190. ssize_t res = binaryStream->readData(buf, bufSize, readOffset);
  191. if(res == 0) {
  192. break;
  193. }
  194. if(xmlParseChunk(ctx, reinterpret_cast<const char*>(buf), res, 0) != 0) {
  195. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  196. }
  197. readOffset += res;
  198. }
  199. xmlParseChunk(ctx, reinterpret_cast<const char*>(buf), 0, 1);
  200. } catch(Exception& e) {
  201. xmlFreeParserCtxt(ctx);
  202. throw;
  203. }
  204. xmlFreeParserCtxt(ctx);
  205. if(!_stm->finished()) {
  206. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  207. }
  208. return _stm->getResult();
  209. }
  210. } // namespace aria2