XML2SAXMetalinkProcessor.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // Old libxml2(at least 2.7.6, Ubuntu 10.04LTS) does not read stdin
  167. // when "/dev/stdin" is passed as filename while 2.7.7 does. So we
  168. // convert DEV_STDIN to "-" for compatibility.
  169. std::string nfilename;
  170. if(filename == DEV_STDIN) {
  171. nfilename = "-";
  172. } else {
  173. nfilename = filename;
  174. }
  175. int retval = xmlSAXUserParseFile(&mySAXHandler, sessionData.get(),
  176. nfilename.c_str());
  177. if(retval != 0) {
  178. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  179. }
  180. if(!stm_->finished()) {
  181. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  182. }
  183. if(!stm_->getErrors().empty()) {
  184. throw DL_ABORT_EX(stm_->getErrorString());
  185. }
  186. return stm_->getResult();
  187. }
  188. SharedHandle<Metalinker>
  189. MetalinkProcessor::parseFromBinaryStream(const SharedHandle<BinaryStream>& binaryStream)
  190. {
  191. stm_.reset(new MetalinkParserStateMachine());
  192. size_t bufSize = 4096;
  193. unsigned char buf[bufSize];
  194. ssize_t res = binaryStream->readData(buf, 4, 0);
  195. if(res != 4) {
  196. throw DL_ABORT_EX("Too small data for parsing XML.");
  197. }
  198. SharedHandle<SessionData> sessionData(new SessionData(stm_));
  199. xmlParserCtxtPtr ctx = xmlCreatePushParserCtxt
  200. (&mySAXHandler, sessionData.get(),
  201. reinterpret_cast<const char*>(buf), res, 0);
  202. auto_delete<xmlParserCtxtPtr> deleter(ctx, xmlFreeParserCtxt);
  203. off_t readOffset = res;
  204. while(1) {
  205. ssize_t res = binaryStream->readData(buf, bufSize, readOffset);
  206. if(res == 0) {
  207. break;
  208. }
  209. if(xmlParseChunk(ctx, reinterpret_cast<const char*>(buf), res, 0) != 0) {
  210. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  211. }
  212. readOffset += res;
  213. }
  214. xmlParseChunk(ctx, reinterpret_cast<const char*>(buf), 0, 1);
  215. if(!stm_->finished()) {
  216. throw DL_ABORT_EX(MSG_CANNOT_PARSE_METALINK);
  217. }
  218. if(!stm_->getErrors().empty()) {
  219. throw DL_ABORT_EX(stm_->getErrorString());
  220. }
  221. return stm_->getResult();
  222. }
  223. } // namespace aria2