XML2SAXMetalinkProcessor.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. namespace {
  47. class SessionData {
  48. public:
  49. SharedHandle<MetalinkParserStateMachine> stm_;
  50. std::deque<std::string> charactersStack_;
  51. SessionData(const SharedHandle<MetalinkParserStateMachine>& stm):stm_(stm) {}
  52. };
  53. } // namespace
  54. namespace {
  55. void mlStartElement
  56. (void* userData,
  57. const xmlChar* srcLocalname,
  58. const xmlChar* srcPrefix,
  59. const xmlChar* srcNsUri,
  60. int numNamespaces,
  61. const xmlChar **namespaces,
  62. int numAttrs,
  63. int numDefaulted,
  64. const xmlChar **attrs)
  65. {
  66. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  67. std::vector<XmlAttr> xmlAttrs;
  68. size_t index = 0;
  69. for(int attrIndex = 0; attrIndex < numAttrs; ++attrIndex, index += 5) {
  70. XmlAttr xmlAttr;
  71. assert(attrs[index]);
  72. xmlAttr.localname = reinterpret_cast<const char*>(attrs[index]);
  73. if(attrs[index+1]) {
  74. xmlAttr.prefix = reinterpret_cast<const char*>(attrs[index+1]);
  75. }
  76. if(attrs[index+2]) {
  77. xmlAttr.nsUri = reinterpret_cast<const char*>(attrs[index+2]);
  78. }
  79. const char* valueBegin = reinterpret_cast<const char*>(attrs[index+3]);
  80. const char* valueEnd = reinterpret_cast<const char*>(attrs[index+4]);
  81. xmlAttr.value = std::string(valueBegin, valueEnd);
  82. xmlAttrs.push_back(xmlAttr);
  83. }
  84. assert(srcLocalname);
  85. std::string localname = reinterpret_cast<const char*>(srcLocalname);
  86. std::string prefix;
  87. std::string nsUri;
  88. if(srcPrefix) {
  89. prefix = reinterpret_cast<const char*>(srcPrefix);
  90. }
  91. if(srcNsUri) {
  92. nsUri = reinterpret_cast<const char*>(srcNsUri);
  93. }
  94. sd->stm_->beginElement(localname, prefix, nsUri, xmlAttrs);
  95. if(sd->stm_->needsCharactersBuffering()) {
  96. sd->charactersStack_.push_front(A2STR::NIL);
  97. }
  98. }
  99. } // namespace
  100. namespace {
  101. void mlEndElement
  102. (void* userData,
  103. const xmlChar* srcLocalname,
  104. const xmlChar* srcPrefix,
  105. const xmlChar* srcNsUri)
  106. {
  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. std::string localname = reinterpret_cast<const char*>(srcLocalname);
  114. std::string prefix;
  115. std::string nsUri;
  116. if(srcPrefix) {
  117. prefix = reinterpret_cast<const char*>(srcPrefix);
  118. }
  119. if(srcNsUri) {
  120. nsUri = reinterpret_cast<const char*>(srcNsUri);
  121. }
  122. sd->stm_->endElement(localname, prefix, nsUri, characters);
  123. }
  124. } // namespace
  125. namespace {
  126. void mlCharacters(void* userData, const xmlChar* ch, int len)
  127. {
  128. SessionData* sd = reinterpret_cast<SessionData*>(userData);
  129. if(sd->stm_->needsCharactersBuffering()) {
  130. sd->charactersStack_.front() += std::string(&ch[0], &ch[len]);
  131. }
  132. }
  133. } // namespace
  134. namespace {
  135. xmlSAXHandler mySAXHandler =
  136. {
  137. 0, // internalSubsetSAXFunc
  138. 0, // isStandaloneSAXFunc
  139. 0, // hasInternalSubsetSAXFunc
  140. 0, // hasExternalSubsetSAXFunc
  141. 0, // resolveEntitySAXFunc
  142. 0, // getEntitySAXFunc
  143. 0, // entityDeclSAXFunc
  144. 0, // notationDeclSAXFunc
  145. 0, // attributeDeclSAXFunc
  146. 0, // elementDeclSAXFunc
  147. 0, // unparsedEntityDeclSAXFunc
  148. 0, // setDocumentLocatorSAXFunc
  149. 0, // startDocumentSAXFunc
  150. 0, // endDocumentSAXFunc
  151. 0, // startElementSAXFunc
  152. 0, // endElementSAXFunc
  153. 0, // referenceSAXFunc
  154. &mlCharacters, // charactersSAXFunc
  155. 0, // ignorableWhitespaceSAXFunc
  156. 0, // processingInstructionSAXFunc
  157. 0, // commentSAXFunc
  158. 0, // warningSAXFunc
  159. 0, // errorSAXFunc
  160. 0, // fatalErrorSAXFunc
  161. 0, // getParameterEntitySAXFunc
  162. 0, // cdataBlockSAXFunc
  163. 0, // externalSubsetSAXFunc
  164. XML_SAX2_MAGIC, // unsigned int initialized
  165. 0, // void * _private
  166. &mlStartElement, // startElementNsSAX2Func
  167. &mlEndElement, // endElementNsSAX2Func
  168. 0, // xmlStructuredErrorFunc
  169. };
  170. } // namespace
  171. MetalinkProcessor::MetalinkProcessor() {}
  172. MetalinkProcessor::~MetalinkProcessor() {}
  173. SharedHandle<Metalinker>
  174. MetalinkProcessor::parseFile(const std::string& filename)
  175. {
  176. stm_.reset(new MetalinkParserStateMachine());
  177. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  178. // Old libxml2(at least 2.7.6, Ubuntu 10.04LTS) does not read stdin
  179. // when "/dev/stdin" is passed as filename while 2.7.7 does. So we
  180. // convert DEV_STDIN to "-" for compatibility.
  181. std::string nfilename;
  182. if(filename == DEV_STDIN) {
  183. nfilename = "-";
  184. } else {
  185. nfilename = filename;
  186. }
  187. int retval = xmlSAXUserParseFile(&mySAXHandler, sessionData.get(),
  188. nfilename.c_str());
  189. if(retval != 0) {
  190. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  191. }
  192. if(!stm_->finished()) {
  193. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  194. }
  195. if(!stm_->getErrors().empty()) {
  196. throw DL_ABORT_EX(stm_->getErrorString());
  197. }
  198. return stm_->getResult();
  199. }
  200. SharedHandle<Metalinker>
  201. MetalinkProcessor::parseFromBinaryStream(const SharedHandle<BinaryStream>& binaryStream)
  202. {
  203. stm_.reset(new MetalinkParserStateMachine());
  204. size_t bufSize = 4096;
  205. unsigned char buf[bufSize];
  206. ssize_t res = binaryStream->readData(buf, 4, 0);
  207. if(res != 4) {
  208. throw DL_ABORT_EX("Too small data for parsing XML.");
  209. }
  210. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  211. xmlParserCtxtPtr ctx = xmlCreatePushParserCtxt
  212. (&mySAXHandler, sessionData.get(),
  213. reinterpret_cast<const char*>(buf), res, 0);
  214. auto_delete<xmlParserCtxtPtr> deleter(ctx, xmlFreeParserCtxt);
  215. off_t readOffset = res;
  216. while(1) {
  217. ssize_t res = binaryStream->readData(buf, bufSize, readOffset);
  218. if(res == 0) {
  219. break;
  220. }
  221. if(xmlParseChunk(ctx, reinterpret_cast<const char*>(buf), res, 0) != 0) {
  222. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  223. }
  224. readOffset += res;
  225. }
  226. xmlParseChunk(ctx, reinterpret_cast<const char*>(buf), 0, 1);
  227. if(!stm_->finished()) {
  228. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  229. }
  230. if(!stm_->getErrors().empty()) {
  231. throw DL_ABORT_EX(stm_->getErrorString());
  232. }
  233. return stm_->getResult();
  234. }
  235. } // namespace aria2