Metalink2RequestGroup.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <algorithm>
  37. #include "RequestGroup.h"
  38. #include "Option.h"
  39. #include "LogFactory.h"
  40. #include "Logger.h"
  41. #include "prefs.h"
  42. #include "util.h"
  43. #include "message.h"
  44. #include "DownloadContext.h"
  45. #include "MetalinkHelper.h"
  46. #include "BinaryStream.h"
  47. #include "MemoryBufferPreDownloadHandler.h"
  48. #include "TrueRequestGroupCriteria.h"
  49. #include "MetalinkEntry.h"
  50. #include "MetalinkResource.h"
  51. #include "FileEntry.h"
  52. #include "A2STR.h"
  53. #include "a2functional.h"
  54. #ifdef ENABLE_BITTORRENT
  55. # include "BtDependency.h"
  56. # include "download_helper.h"
  57. #endif // ENABLE_BITTORRENT
  58. #ifdef ENABLE_MESSAGE_DIGEST
  59. # include "Checksum.h"
  60. # include "ChunkChecksum.h"
  61. #endif // ENABLE_MESSAGE_DIGEST
  62. namespace aria2 {
  63. Metalink2RequestGroup::Metalink2RequestGroup():
  64. _logger(LogFactory::getInstance()) {}
  65. class AccumulateNonP2PUrl {
  66. private:
  67. std::deque<std::string>& urlsPtr;
  68. public:
  69. AccumulateNonP2PUrl(std::deque<std::string>& urlsPtr)
  70. :urlsPtr(urlsPtr) {}
  71. void operator()(const SharedHandle<MetalinkResource>& resource) {
  72. switch(resource->type) {
  73. case MetalinkResource::TYPE_HTTP:
  74. case MetalinkResource::TYPE_HTTPS:
  75. case MetalinkResource::TYPE_FTP:
  76. urlsPtr.push_back(resource->url);
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. };
  83. class FindBitTorrentUrl {
  84. public:
  85. FindBitTorrentUrl() {}
  86. bool operator()(const SharedHandle<MetalinkResource>& resource) {
  87. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93. };
  94. void
  95. Metalink2RequestGroup::generate(std::deque<SharedHandle<RequestGroup> >& groups,
  96. const std::string& metalinkFile,
  97. const SharedHandle<Option>& option)
  98. {
  99. std::deque<SharedHandle<MetalinkEntry> > entries;
  100. MetalinkHelper::parseAndQuery(entries, metalinkFile, option.get());
  101. createRequestGroup(groups, entries, option);
  102. }
  103. void
  104. Metalink2RequestGroup::generate(std::deque<SharedHandle<RequestGroup> >& groups,
  105. const SharedHandle<BinaryStream>& binaryStream,
  106. const SharedHandle<Option>& option)
  107. {
  108. std::deque<SharedHandle<MetalinkEntry> > entries;
  109. MetalinkHelper::parseAndQuery(entries, binaryStream, option.get());
  110. createRequestGroup(groups, entries, option);
  111. }
  112. void
  113. Metalink2RequestGroup::createRequestGroup
  114. (std::deque<SharedHandle<RequestGroup> >& groups,
  115. std::deque<SharedHandle<MetalinkEntry> > entries,
  116. const SharedHandle<Option>& option)
  117. {
  118. if(entries.size() == 0) {
  119. _logger->notice(EX_NO_RESULT_WITH_YOUR_PREFS);
  120. return;
  121. }
  122. std::deque<int32_t> selectIndexes =
  123. util::parseIntRange(option->get(PREF_SELECT_FILE)).flush();
  124. bool useIndex;
  125. if(selectIndexes.size()) {
  126. useIndex = true;
  127. } else {
  128. useIndex = false;
  129. }
  130. int32_t count = 0;
  131. for(std::deque<SharedHandle<MetalinkEntry> >::iterator itr = entries.begin(); itr != entries.end();
  132. ++itr, ++count) {
  133. SharedHandle<MetalinkEntry>& entry = *itr;
  134. if(option->defined(PREF_METALINK_LOCATION)) {
  135. std::deque<std::string> locations;
  136. util::split(option->get(PREF_METALINK_LOCATION),
  137. std::back_inserter(locations), ",", true);
  138. entry->setLocationPreference(locations, 100);
  139. }
  140. if(option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) {
  141. entry->setProtocolPreference(option->get(PREF_METALINK_PREFERRED_PROTOCOL), 100);
  142. }
  143. if(useIndex) {
  144. if(std::find(selectIndexes.begin(), selectIndexes.end(), count+1) ==
  145. selectIndexes.end()) {
  146. continue;
  147. }
  148. }
  149. entry->dropUnsupportedResource();
  150. if(entry->resources.size() == 0) {
  151. continue;
  152. }
  153. _logger->info(MSG_METALINK_QUEUEING, entry->getPath().c_str());
  154. std::deque<SharedHandle<MetalinkResource> >::iterator itr =
  155. std::find_if(entry->resources.begin(), entry->resources.end(), FindBitTorrentUrl());
  156. #ifdef ENABLE_BITTORRENT
  157. SharedHandle<RequestGroup> torrentRg;
  158. // there is torrent entry
  159. if(itr != entry->resources.end()) {
  160. std::deque<std::string> uris;
  161. uris.push_back((*itr)->url);
  162. {
  163. std::deque<SharedHandle<RequestGroup> > result;
  164. createRequestGroupForUri(result, option, uris,
  165. /* ignoreForceSequential = */true,
  166. /* ignoreLocalPath = */true);
  167. if(!uris.empty()) {
  168. torrentRg = result[0];
  169. }
  170. }
  171. if(!torrentRg.isNull()) {
  172. torrentRg->setNumConcurrentCommand(1);
  173. torrentRg->clearPreDownloadHandler();
  174. torrentRg->clearPostDownloadHandler();
  175. // remove "metalink" from Accept Type list to avoid loop in
  176. // tranparent metalink
  177. torrentRg->removeAcceptType(RequestGroup::ACCEPT_METALINK);
  178. // make it in-memory download
  179. SharedHandle<PreDownloadHandler> preh
  180. (new MemoryBufferPreDownloadHandler());
  181. SharedHandle<RequestGroupCriteria> cri(new TrueRequestGroupCriteria());
  182. preh->setCriteria(cri);
  183. torrentRg->addPreDownloadHandler(preh);
  184. groups.push_back(torrentRg);
  185. }
  186. }
  187. #endif // ENABLE_BITTORRENT
  188. entry->reorderResourcesByPreference();
  189. std::deque<std::string> uris;
  190. std::for_each(entry->resources.begin(), entry->resources.end(),
  191. AccumulateNonP2PUrl(uris));
  192. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  193. // If piece hash is specified in the metalink,
  194. // make segment size equal to piece hash size.
  195. size_t pieceLength;
  196. #ifdef ENABLE_MESSAGE_DIGEST
  197. if(entry->chunkChecksum.isNull()) {
  198. pieceLength = option->getAsInt(PREF_SEGMENT_SIZE);
  199. } else {
  200. pieceLength = entry->chunkChecksum->getChecksumLength();
  201. }
  202. #else
  203. pieceLength = option->getAsInt(PREF_SEGMENT_SIZE);
  204. #endif // ENABLE_MESSAGE_DIGEST
  205. SharedHandle<DownloadContext> dctx
  206. (new DownloadContext
  207. (pieceLength,
  208. entry->getLength(),
  209. util::applyDir(option->get(PREF_DIR), entry->file->getPath())));
  210. dctx->setDir(option->get(PREF_DIR));
  211. dctx->getFirstFileEntry()->setUris(uris);
  212. if(option->getAsBool(PREF_METALINK_ENABLE_UNIQUE_PROTOCOL)) {
  213. dctx->getFirstFileEntry()->disableSingleHostMultiConnection();
  214. }
  215. #ifdef ENABLE_MESSAGE_DIGEST
  216. if(entry->chunkChecksum.isNull()) {
  217. if(!entry->checksum.isNull()) {
  218. dctx->setChecksum(entry->checksum->getMessageDigest());
  219. dctx->setChecksumHashAlgo(entry->checksum->getAlgo());
  220. }
  221. } else {
  222. dctx->setPieceHashes(entry->chunkChecksum->getChecksums().begin(),
  223. entry->chunkChecksum->getChecksums().end());
  224. dctx->setPieceHashAlgo(entry->chunkChecksum->getAlgo());
  225. }
  226. #endif // ENABLE_MESSAGE_DIGEST
  227. dctx->setSignature(entry->getSignature());
  228. rg->setDownloadContext(dctx);
  229. rg->setNumConcurrentCommand
  230. (entry->maxConnections < 0 ?
  231. option->getAsInt(PREF_METALINK_SERVERS) :
  232. std::min(option->getAsInt(PREF_METALINK_SERVERS),
  233. static_cast<int32_t>(entry->maxConnections)));
  234. // remove "metalink" from Accept Type list to avoid loop in tranparent
  235. // metalink
  236. rg->removeAcceptType(RequestGroup::ACCEPT_METALINK);
  237. #ifdef ENABLE_BITTORRENT
  238. // Inject depenency between rg and torrentRg here if torrentRg.isNull() == false
  239. if(!torrentRg.isNull()) {
  240. SharedHandle<Dependency> dep(new BtDependency(rg, torrentRg));
  241. rg->dependsOn(dep);
  242. torrentRg->belongsTo(rg->getGID());
  243. }
  244. #endif // ENABLE_BITTORRENT
  245. groups.push_back(rg);
  246. }
  247. }
  248. } // namespace aria2