Metalink2RequestGroup.cc 8.5 KB

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