Xml2MetalinkProcessor.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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(uint32_t index = 1; 1; index++) {
  74. MetalinkEntryHandle entry = getEntry(xpath+"["+Util::uitos(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. FileEntryHandle fileEntry = new FileEntry(filename, 0, 0);
  94. string sizeStr = Util::trim(xpathContent(xpath+"/m:size"));
  95. if(sizeStr == "") {
  96. fileEntry->setLength(0);
  97. } else {
  98. fileEntry->setLength(strtoll(sizeStr.c_str(), 0, 10));
  99. }
  100. entry->file = fileEntry;
  101. entry->version = Util::trim(xpathContent(xpath+"/m:version"));
  102. entry->language = Util::trim(xpathContent(xpath+"/m:language"));
  103. entry->os = Util::trim(xpathContent(xpath+"/m:os"));
  104. #ifdef ENABLE_MESSAGE_DIGEST
  105. xmlXPathObjectPtr hashPathObj = xpathEvaluation(xpath+"/m:verification/m:hash");
  106. if(hashPathObj) {
  107. xmlNodeSetPtr nodeSet = hashPathObj->nodesetval;
  108. for(int32_t i = 0; i < nodeSet->nodeNr; ++i) {
  109. xmlNodePtr node = nodeSet->nodeTab[i];
  110. string algo = Util::trim(xmlAttribute(node, "type"));
  111. if(MessageDigestContext::supports(algo)) {
  112. entry->checksum = new Checksum(algo, Util::trim(xmlContent(node)));
  113. break;
  114. }
  115. }
  116. }
  117. xmlXPathFreeObject(hashPathObj);
  118. string piecesPath = xpath+"/m:verification/m:pieces";
  119. xmlXPathObjectPtr pieceHashPathObj = xpathEvaluation(piecesPath);
  120. if(pieceHashPathObj) {
  121. xmlNodeSetPtr nodeSet = pieceHashPathObj->nodesetval;
  122. for(int32_t i = 0; i < nodeSet->nodeNr; ++i) {
  123. xmlNodePtr node = nodeSet->nodeTab[i];
  124. string algo = Util::trim(xmlAttribute(node, "type"));
  125. if(MessageDigestContext::supports(algo)) {
  126. entry->chunkChecksum = getPieceHash(piecesPath+"[@type=\""+algo+"\"]",
  127. entry->getLength());
  128. break;
  129. }
  130. }
  131. }
  132. xmlXPathFreeObject(pieceHashPathObj);
  133. #endif // ENABLE_MESSAGE_DIGEST
  134. string resourcesPath = xpath+"/m:resources[@maxconnections]";
  135. xmlXPathObjectPtr resourcesPathObj = xpathEvaluation(resourcesPath);
  136. if(resourcesPathObj) {
  137. xmlNodeSetPtr nodeSet = resourcesPathObj->nodesetval;
  138. xmlNodePtr node = nodeSet->nodeTab[0];
  139. int32_t maxConnections = strtol(Util::trim(xmlAttribute(node, "maxconnections")).c_str(), 0, 10);
  140. entry->maxConnections = maxConnections;
  141. }
  142. xmlXPathFreeObject(resourcesPathObj);
  143. for(uint32_t index = 1; 1; index++) {
  144. MetalinkResourceHandle resource(getResource(xpath+"/m:resources/m:url["+Util::uitos(index)+"]"));
  145. if(!resource.get()) {
  146. break;
  147. } else {
  148. entry->resources.push_back(resource);
  149. }
  150. }
  151. return entry;
  152. }
  153. #ifdef ENABLE_MESSAGE_DIGEST
  154. ChunkChecksumHandle Xml2MetalinkProcessor::getPieceHash(const string& xpath,
  155. int64_t totalSize)
  156. {
  157. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  158. if(!result) {
  159. return 0;
  160. }
  161. xmlNodeSetPtr nodeSet = result->nodesetval;
  162. xmlNodePtr node = nodeSet->nodeTab[0];
  163. int64_t checksumLength = STRTOLL(Util::trim(xmlAttribute(node, "length")).c_str());
  164. string algoString = Util::trim(xmlAttribute(node, "type"));
  165. xmlXPathFreeObject(result);
  166. if(!MessageDigestContext::supports(algoString)) {
  167. // unknown checksum type
  168. return 0;
  169. }
  170. Strings checksums;
  171. uint64_t numPiece = (totalSize+checksumLength-1)/checksumLength;
  172. for(uint64_t i = 0; i < numPiece; ++i) {
  173. string pieceHash = Util::trim(xpathContent(xpath+"/m:hash[@piece=\""+Util::ullitos(i)+"\"]"));
  174. if(pieceHash == "") {
  175. throw new DlAbortEx("Piece hash missing. index=%s", Util::ullitos(i).c_str());
  176. }
  177. checksums.push_back(pieceHash);
  178. }
  179. return new ChunkChecksum(algoString, checksums, checksumLength);
  180. }
  181. #endif // ENABLE_MESSAGE_DIGEST
  182. MetalinkResourceHandle Xml2MetalinkProcessor::getResource(const string& xpath) {
  183. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  184. if(!result) {
  185. return 0;
  186. }
  187. MetalinkResourceHandle resource(new MetalinkResource());
  188. xmlNodeSetPtr nodeSet = result->nodesetval;
  189. xmlNodePtr node = nodeSet->nodeTab[0];
  190. string type = Util::trim(xmlAttribute(node, "type"));
  191. if(type == "ftp") {
  192. resource->type = MetalinkResource::TYPE_FTP;
  193. } else if(type == "http") {
  194. resource->type = MetalinkResource::TYPE_HTTP;
  195. } else if(type == "https") {
  196. resource->type = MetalinkResource::TYPE_HTTPS;
  197. } else if(type == "bittorrent") {
  198. resource->type = MetalinkResource::TYPE_BITTORRENT;
  199. } else {
  200. resource->type = MetalinkResource::TYPE_NOT_SUPPORTED;
  201. }
  202. string pref = Util::trim(xmlAttribute(node, "preference"));
  203. if(pref.empty()) {
  204. resource->preference = 100;
  205. } else {
  206. resource->preference = STRTOLL(pref.c_str());
  207. }
  208. resource->location = Util::trim(xmlAttribute(node, "location"));
  209. resource->url = Util::trim(xmlContent(node));
  210. {
  211. string cnn = Util::trim(xmlAttribute(node, "maxconnections"));
  212. if(!cnn.empty()) {
  213. resource->maxConnections = strtol(cnn.c_str(), 0, 10);
  214. }
  215. }
  216. xmlXPathFreeObject(result);
  217. return resource;
  218. }
  219. xmlXPathObjectPtr Xml2MetalinkProcessor::xpathEvaluation(const string& xpath) {
  220. xmlXPathObjectPtr result = xmlXPathEvalExpression((xmlChar*)xpath.c_str(),
  221. context);
  222. if(!result) {
  223. throw new DlAbortEx("Cannot evaluate xpath %s", xpath.c_str());
  224. }
  225. if(xmlXPathNodeSetIsEmpty(result->nodesetval)) {
  226. xmlXPathFreeObject(result);
  227. return 0;
  228. }
  229. return result;
  230. }
  231. string Xml2MetalinkProcessor::xmlAttribute(xmlNodePtr node, const string& attrName) {
  232. xmlChar* temp = xmlGetNoNsProp(node, (xmlChar*)attrName.c_str());
  233. if(!temp) {
  234. return "";
  235. } else {
  236. string attr = (char*)temp;
  237. xmlFree(temp);
  238. return attr;
  239. }
  240. }
  241. string Xml2MetalinkProcessor::xmlContent(xmlNodePtr node) {
  242. xmlChar* temp = xmlNodeGetContent(node);
  243. if(!temp) {
  244. return "";
  245. } else {
  246. string content = (char*)temp;
  247. xmlFree(temp);
  248. return content;
  249. }
  250. }
  251. string Xml2MetalinkProcessor::xpathContent(const string& xpath) {
  252. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  253. if(!result) {
  254. return "";
  255. }
  256. xmlNodeSetPtr nodeSet = result->nodesetval;
  257. xmlNodePtr node = nodeSet->nodeTab[0]->children;
  258. string content = (char*)node->content;
  259. xmlXPathFreeObject(result);
  260. return content;
  261. }
  262. bool Xml2MetalinkProcessor::xpathExists(const string& xpath) {
  263. xmlXPathObjectPtr result = xpathEvaluation(xpath);
  264. bool retval = true;
  265. if(!result) {
  266. retval = false;
  267. }
  268. xmlXPathFreeObject(result);
  269. return retval;
  270. }