XML2SAXMetalinkProcessor.cc 7.5 KB

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