Xml2MetalinkProcessor.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "Xml2MetalinkProcessor.h"
  36. #include "DlAbortEx.h"
  37. #include "Util.h"
  38. #include <libxml/parser.h>
  39. #include <libxml/xpath.h>
  40. #include <libxml/xpathInternals.h>
  41. Xml2MetalinkProcessor::Xml2MetalinkProcessor():doc(0), context(0) {}
  42. Xml2MetalinkProcessor::~Xml2MetalinkProcessor() {
  43. release();
  44. }
  45. void Xml2MetalinkProcessor::release() {
  46. if(context) {
  47. xmlXPathFreeContext(context);
  48. context = 0;
  49. }
  50. if(doc) {
  51. xmlFreeDoc(doc);
  52. doc = 0;
  53. }
  54. }
  55. MetalinkerHandle Xml2MetalinkProcessor::parseFile(const string& filename) {
  56. release();
  57. doc = xmlParseFile(filename.c_str());
  58. if(!doc) {
  59. throw new DlAbortEx("Cannot parse metalink file %s", filename.c_str());
  60. }
  61. context = xmlXPathNewContext(doc);
  62. if(!context) {
  63. throw new DlAbortEx("Cannot create new xpath context");
  64. }
  65. string defaultNamespace = "http://www.metalinker.org/";
  66. if(xmlXPathRegisterNs(context, (xmlChar*)"m",
  67. (xmlChar*)defaultNamespace.c_str()) != 0) {
  68. throw new DlAbortEx("Cannot register namespace %s",
  69. defaultNamespace.c_str());
  70. }
  71. string xpath = "/m:metalink/m:files/m:file";
  72. MetalinkerHandle metalinker(new Metalinker());
  73. for(int index = 1; 1; index++) {
  74. MetalinkEntryHandle entry = getEntry(xpath+"["+Util::itos(index)+"]");
  75. if(!entry.get()) {
  76. break;
  77. } else {
  78. metalinker->entries.push_back(entry);
  79. }
  80. }
  81. return metalinker;
  82. }
  83. MetalinkEntryHandle Xml2MetalinkProcessor::getEntry(const string& xpath) {
  84. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  85. if(!result) {
  86. return 0;
  87. }
  88. xmlNodeSetPtr nodeSet = result->nodesetval;
  89. xmlNodePtr node = nodeSet->nodeTab[0];
  90. string filename = Util::trim(xmlAttribute(node, "name"));
  91. xmlXPathFreeObject(result);
  92. MetalinkEntryHandle entry(new MetalinkEntry());
  93. entry->filename = filename;
  94. string sizeStr = Util::trim(xpathContent(xpath+"/m:size"));
  95. if(sizeStr == "") {
  96. entry->size = 0;
  97. } else {
  98. entry->size = STRTOLL(sizeStr.c_str());
  99. }
  100. entry->version = Util::trim(xpathContent(xpath+"/m:version"));
  101. entry->language = Util::trim(xpathContent(xpath+"/m:language"));
  102. entry->os = Util::trim(xpathContent(xpath+"/m:os"));
  103. #ifdef ENABLE_MESSAGE_DIGEST
  104. string md;
  105. md = Util::toLower(Util::trim(xpathContent(xpath+"/m:verification/m:hash[@type=\"sha1\"]")));
  106. if(md.size() > 0) {
  107. entry->checksum = new Checksum();
  108. entry->checksum->setMessageDigest(md);
  109. entry->checksum->setDigestAlgo(DIGEST_ALGO_SHA1);
  110. } else {
  111. md = Util::toLower(Util::trim(xpathContent(xpath+"/m:verification/m:hash[@type=\"md5\"]")));
  112. if(md.size() > 0) {
  113. entry->checksum = new Checksum();
  114. entry->checksum->setMessageDigest(md);
  115. entry->checksum->setDigestAlgo(DIGEST_ALGO_MD5);
  116. }
  117. }
  118. string piecesPath = xpath+"/m:verification/m:pieces";
  119. string sha1PiecesPath = piecesPath+"[@type=\"sha1\"]";
  120. string md5PiecesPath = piecesPath+"[@type=\"md5\"]";
  121. if(xpathExists(sha1PiecesPath)) {
  122. entry->chunkChecksum = getPieceHash(sha1PiecesPath, entry->size);
  123. } else if(xpathExists(md5PiecesPath)) {
  124. entry->chunkChecksum = getPieceHash(md5PiecesPath, entry->size);
  125. }
  126. #endif // ENABLE_MESSAGE_DIGEST
  127. for(int index = 1; 1; index++) {
  128. MetalinkResourceHandle resource(getResource(xpath+"/m:resources/m:url["+Util::itos(index)+"]"));
  129. if(!resource.get()) {
  130. break;
  131. } else {
  132. entry->resources.push_back(resource);
  133. }
  134. }
  135. return entry;
  136. }
  137. #ifdef ENABLE_MESSAGE_DIGEST
  138. MetalinkChunkChecksumHandle Xml2MetalinkProcessor::getPieceHash(const string& xpath,
  139. int64_t totalSize)
  140. {
  141. MetalinkChunkChecksumHandle chunkChecksum = new MetalinkChunkChecksum();
  142. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  143. if(!result) {
  144. return 0;
  145. }
  146. xmlNodeSetPtr nodeSet = result->nodesetval;
  147. xmlNodePtr node = nodeSet->nodeTab[0];
  148. chunkChecksum->pieceLength = STRTOLL(Util::trim(xmlAttribute(node, "length")).c_str());
  149. string algo = Util::trim(xmlAttribute(node, "type"));
  150. xmlXPathFreeObject(result);
  151. if(algo == "sha1") {
  152. chunkChecksum->digestAlgo = DIGEST_ALGO_SHA1;
  153. } else if(algo == "md5") {
  154. chunkChecksum->digestAlgo = DIGEST_ALGO_MD5;
  155. } else {
  156. // unknown checksum type
  157. chunkChecksum->pieceLength = 0;
  158. return chunkChecksum;
  159. }
  160. int64_t numPiece =
  161. (totalSize+chunkChecksum->pieceLength-1)/chunkChecksum->pieceLength;
  162. for(int64_t i = 0; i < numPiece; ++i) {
  163. string pieceHash = Util::trim(xpathContent(xpath+"/m:hash[@piece=\""+Util::ullitos(i)+"\"]"));
  164. if(pieceHash == "") {
  165. throw new DlAbortEx("Piece hash missing. index=%d", i);
  166. }
  167. chunkChecksum->pieceHashes.push_back(pieceHash);
  168. }
  169. return chunkChecksum;
  170. }
  171. #endif // ENABLE_MESSAGE_DIGEST
  172. MetalinkResourceHandle Xml2MetalinkProcessor::getResource(const string& xpath) {
  173. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  174. if(!result) {
  175. return 0;
  176. }
  177. MetalinkResourceHandle resource(new MetalinkResource());
  178. xmlNodeSetPtr nodeSet = result->nodesetval;
  179. xmlNodePtr node = nodeSet->nodeTab[0];
  180. string type = Util::trim(xmlAttribute(node, "type"));
  181. if(type == "ftp") {
  182. resource->type = MetalinkResource::TYPE_FTP;
  183. } else if(type == "http") {
  184. resource->type = MetalinkResource::TYPE_HTTP;
  185. } else if(type == "https") {
  186. resource->type = MetalinkResource::TYPE_HTTPS;
  187. } else if(type == "bittorrent") {
  188. resource->type = MetalinkResource::TYPE_BITTORRENT;
  189. } else {
  190. resource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  191. }
  192. string pref = Util::trim(xmlAttribute(node, "preference"));
  193. if(pref.empty()) {
  194. resource->preference = 100;
  195. } else {
  196. resource->preference = STRTOLL(pref.c_str());
  197. }
  198. resource->location = Util::trim(xmlAttribute(node, "location"));
  199. resource->url = Util::trim(xmlContent(node));
  200. xmlXPathFreeObject(result);
  201. return resource;
  202. }
  203. xmlXPathObjectPtr Xml2MetalinkProcessor::xpathEvaluation(const string& xpath) {
  204. xmlXPathObjectPtr result = xmlXPathEvalExpression((xmlChar*)xpath.c_str(),
  205. context);
  206. if(!result) {
  207. throw new DlAbortEx("Cannot evaluate xpath %s", xpath.c_str());
  208. }
  209. if(xmlXPathNodeSetIsEmpty(result->nodesetval)) {
  210. xmlXPathFreeObject(result);
  211. return 0;
  212. }
  213. return result;
  214. }
  215. string Xml2MetalinkProcessor::xmlAttribute(xmlNodePtr node, const string& attrName) {
  216. xmlChar* temp = xmlGetNoNsProp(node, (xmlChar*)attrName.c_str());
  217. if(!temp) {
  218. return "";
  219. } else {
  220. string attr = (char*)temp;
  221. xmlFree(temp);
  222. return attr;
  223. }
  224. }
  225. string Xml2MetalinkProcessor::xmlContent(xmlNodePtr node) {
  226. xmlChar* temp = xmlNodeGetContent(node);
  227. if(!temp) {
  228. return "";
  229. } else {
  230. string content = (char*)temp;
  231. xmlFree(temp);
  232. return content;
  233. }
  234. }
  235. string Xml2MetalinkProcessor::xpathContent(const string& xpath) {
  236. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  237. if(!result) {
  238. return "";
  239. }
  240. xmlNodeSetPtr nodeSet = result->nodesetval;
  241. xmlNodePtr node = nodeSet->nodeTab[0]->children;
  242. string content = (char*)node->content;
  243. xmlXPathFreeObject(result);
  244. return content;
  245. }
  246. bool Xml2MetalinkProcessor::xpathExists(const string& xpath) {
  247. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  248. bool retval = true;
  249. if(!result) {
  250. retval = false;
  251. }
  252. xmlXPathFreeObject(result);
  253. return retval;
  254. }