Metalink2RequestGroup.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "Metalink2RequestGroup.h"
  36. #include "RequestGroup.h"
  37. #include "Option.h"
  38. #include "LogFactory.h"
  39. #include "prefs.h"
  40. #include "Util.h"
  41. #include "message.h"
  42. #include "SingleFileDownloadContext.h"
  43. #include "MetalinkHelper.h"
  44. #include "BinaryStream.h"
  45. #include "MemoryBufferPreDownloadHandler.h"
  46. #include "TrueRequestGroupCriteria.h"
  47. #include "MetalinkEntry.h"
  48. #ifdef ENABLE_BITTORRENT
  49. # include "BtDependency.h"
  50. #endif // ENABLE_BITTORRENT
  51. Metalink2RequestGroup::Metalink2RequestGroup(const Option* option):_option(option), _logger(LogFactory::getInstance()) {}
  52. Metalink2RequestGroup::~Metalink2RequestGroup() {}
  53. class AccumulateNonP2PUrl {
  54. private:
  55. Strings* urlsPtr;
  56. int32_t split;
  57. public:
  58. AccumulateNonP2PUrl(Strings* urlsPtr, int32_t split)
  59. :urlsPtr(urlsPtr),
  60. split(split) {}
  61. void operator()(const MetalinkResourceHandle& resource) {
  62. int32_t maxConnections;
  63. if(resource->maxConnections < 0) {
  64. maxConnections = split;
  65. } else {
  66. maxConnections = min<int32_t>(resource->maxConnections, split);
  67. }
  68. switch(resource->type) {
  69. case MetalinkResource::TYPE_HTTP:
  70. case MetalinkResource::TYPE_HTTPS:
  71. case MetalinkResource::TYPE_FTP:
  72. for(int32_t s = 1; s <= maxConnections; s++) {
  73. urlsPtr->push_back(resource->url);
  74. }
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. };
  81. class FindBitTorrentUrl {
  82. public:
  83. FindBitTorrentUrl() {}
  84. bool operator()(const MetalinkResourceHandle& resource) {
  85. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. };
  92. RequestGroups Metalink2RequestGroup::generate(const string& metalinkFile)
  93. {
  94. MetalinkEntries entries = MetalinkHelper::parseAndQuery(metalinkFile,
  95. _option);
  96. return createRequestGroup(entries);
  97. }
  98. RequestGroups Metalink2RequestGroup::generate(const BinaryStreamHandle& binaryStream)
  99. {
  100. MetalinkEntries entries = MetalinkHelper::parseAndQuery(binaryStream,
  101. _option);
  102. return createRequestGroup(entries);
  103. }
  104. RequestGroups Metalink2RequestGroup::createRequestGroup(MetalinkEntries entries)
  105. {
  106. if(entries.size() == 0) {
  107. _logger->notice(EX_NO_RESULT_WITH_YOUR_PREFS);
  108. return RequestGroups();
  109. }
  110. Integers selectIndexes = Util::parseIntRange(_option->get(PREF_SELECT_FILE)).flush();
  111. bool useIndex;
  112. if(selectIndexes.size()) {
  113. useIndex = true;
  114. } else {
  115. useIndex = false;
  116. }
  117. RequestGroups groups;
  118. int32_t count = 0;
  119. for(MetalinkEntries::iterator itr = entries.begin(); itr != entries.end();
  120. itr++, ++count) {
  121. MetalinkEntryHandle& entry = *itr;
  122. if(_option->defined(PREF_METALINK_LOCATION)) {
  123. Strings locations;
  124. Util::slice(locations, _option->get(PREF_METALINK_LOCATION), ',', true);
  125. entry->setLocationPreference(locations, 100);
  126. }
  127. if(_option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) {
  128. entry->setProtocolPreference(_option->get(PREF_METALINK_PREFERRED_PROTOCOL), 100);
  129. }
  130. if(useIndex) {
  131. if(find(selectIndexes.begin(), selectIndexes.end(), count+1) == selectIndexes.end()) {
  132. continue;
  133. }
  134. }
  135. entry->dropUnsupportedResource();
  136. if(entry->resources.size() == 0) {
  137. continue;
  138. }
  139. _logger->info(MSG_METALINK_QUEUEING, entry->getPath().c_str());
  140. MetalinkResources::iterator itr = find_if(entry->resources.begin(),
  141. entry->resources.end(),
  142. FindBitTorrentUrl());
  143. #ifdef ENABLE_BITTORRENT
  144. RequestGroupHandle torrentRg = 0;
  145. // there is torrent entry
  146. if(itr != entry->resources.end()) {
  147. Strings uris;
  148. uris.push_back((*itr)->url);
  149. torrentRg = new RequestGroup(_option, uris);
  150. SingleFileDownloadContextHandle dctx =
  151. new SingleFileDownloadContext(_option->getAsInt(PREF_SEGMENT_SIZE),
  152. 0,
  153. "");
  154. //dctx->setDir(_option->get(PREF_DIR));
  155. torrentRg->setDownloadContext(dctx);
  156. torrentRg->clearPreDowloadHandler();
  157. torrentRg->clearPostDowloadHandler();
  158. // make it in-memory download
  159. PreDownloadHandlerHandle preh = new MemoryBufferPreDownloadHandler();
  160. preh->setCriteria(new TrueRequestGroupCriteria());
  161. torrentRg->addPreDownloadHandler(preh);
  162. groups.push_back(torrentRg);
  163. }
  164. #endif // ENABLE_BITTORRENT
  165. entry->reorderResourcesByPreference();
  166. Strings uris;
  167. for_each(entry->resources.begin(), entry->resources.end(),
  168. AccumulateNonP2PUrl(&uris, _option->getAsInt(PREF_SPLIT)));
  169. RequestGroupHandle rg = new RequestGroup(_option, uris);
  170. // If piece hash is specified in the metalink,
  171. // make segment size equal to piece hash size.
  172. int32_t pieceLength;
  173. #ifdef ENABLE_MESSAGE_DIGEST
  174. if(entry->chunkChecksum.isNull()) {
  175. pieceLength = _option->getAsInt(PREF_SEGMENT_SIZE);
  176. } else {
  177. pieceLength = entry->chunkChecksum->getChecksumLength();
  178. }
  179. #else
  180. pieceLength = _option->getAsInt(PREF_SEGMENT_SIZE);
  181. #endif // ENABLE_MESSAGE_DIGEST
  182. SingleFileDownloadContextHandle dctx =
  183. new SingleFileDownloadContext(pieceLength,
  184. entry->getLength(),
  185. "",
  186. entry->file->getPath());
  187. dctx->setDir(_option->get(PREF_DIR));
  188. #ifdef ENABLE_MESSAGE_DIGEST
  189. if(entry->chunkChecksum.isNull()) {
  190. if(!entry->checksum.isNull()) {
  191. dctx->setChecksum(entry->checksum->getMessageDigest());
  192. dctx->setChecksumHashAlgo(entry->checksum->getAlgo());
  193. }
  194. } else {
  195. dctx->setPieceHashes(entry->chunkChecksum->getChecksums());
  196. dctx->setPieceHashAlgo(entry->chunkChecksum->getAlgo());
  197. }
  198. #endif // ENABLE_MESSAGE_DIGEST
  199. rg->setDownloadContext(dctx);
  200. rg->setNumConcurrentCommand(entry->maxConnections < 0 ?
  201. _option->getAsInt(PREF_METALINK_SERVERS) :
  202. min<int32_t>(_option->getAsInt(PREF_METALINK_SERVERS), entry->maxConnections));
  203. // In metalink, multi connection to a single host is not allowed.
  204. rg->setSingleHostMultiConnectionEnabled(false);
  205. #ifdef ENABLE_BITTORRENT
  206. // Inject depenency between rg and torrentRg here if torrentRg.isNull() == false
  207. if(!torrentRg.isNull()) {
  208. rg->dependsOn(new BtDependency(rg, torrentRg, _option));
  209. }
  210. #endif // ENABLE_BITTORRENT
  211. groups.push_back(rg);
  212. }
  213. return groups;
  214. }