Metalink2RequestGroup.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "MetalinkMetaurl.h"
  52. #include "FileEntry.h"
  53. #include "A2STR.h"
  54. #include "a2functional.h"
  55. #include "download_helper.h"
  56. #ifdef ENABLE_BITTORRENT
  57. # include "BtDependency.h"
  58. # include "download_helper.h"
  59. #endif // ENABLE_BITTORRENT
  60. #ifdef ENABLE_MESSAGE_DIGEST
  61. # include "Checksum.h"
  62. # include "ChunkChecksum.h"
  63. #endif // ENABLE_MESSAGE_DIGEST
  64. namespace aria2 {
  65. Metalink2RequestGroup::Metalink2RequestGroup():
  66. _logger(LogFactory::getInstance()) {}
  67. class AccumulateNonP2PUri {
  68. private:
  69. std::vector<std::string>& urisPtr;
  70. public:
  71. AccumulateNonP2PUri(std::vector<std::string>& urisPtr)
  72. :urisPtr(urisPtr) {}
  73. void operator()(const SharedHandle<MetalinkResource>& resource) {
  74. switch(resource->type) {
  75. case MetalinkResource::TYPE_HTTP:
  76. case MetalinkResource::TYPE_HTTPS:
  77. case MetalinkResource::TYPE_FTP:
  78. urisPtr.push_back(resource->url);
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. };
  85. class FindBitTorrentUri {
  86. public:
  87. FindBitTorrentUri() {}
  88. bool operator()(const SharedHandle<MetalinkResource>& resource) {
  89. if(resource->type == MetalinkResource::TYPE_BITTORRENT) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. };
  96. void
  97. Metalink2RequestGroup::generate
  98. (std::vector<SharedHandle<RequestGroup> >& groups,
  99. const std::string& metalinkFile,
  100. const SharedHandle<Option>& option)
  101. {
  102. std::vector<SharedHandle<MetalinkEntry> > entries;
  103. MetalinkHelper::parseAndQuery(entries, metalinkFile, option.get());
  104. std::vector<SharedHandle<RequestGroup> > tempgroups;
  105. createRequestGroup(tempgroups, entries, option);
  106. SharedHandle<MetadataInfo> mi;
  107. if(metalinkFile == DEV_STDIN) {
  108. mi.reset(new MetadataInfo());
  109. } else {
  110. mi.reset(new MetadataInfo(metalinkFile));
  111. }
  112. setMetadataInfo(tempgroups.begin(), tempgroups.end(), mi);
  113. groups.insert(groups.end(), tempgroups.begin(), tempgroups.end());
  114. }
  115. void
  116. Metalink2RequestGroup::generate
  117. (std::vector<SharedHandle<RequestGroup> >& groups,
  118. const SharedHandle<BinaryStream>& binaryStream,
  119. const SharedHandle<Option>& option)
  120. {
  121. std::vector<SharedHandle<MetalinkEntry> > entries;
  122. MetalinkHelper::parseAndQuery(entries, binaryStream, option.get());
  123. std::vector<SharedHandle<RequestGroup> > tempgroups;
  124. createRequestGroup(tempgroups, entries, option);
  125. SharedHandle<MetadataInfo> mi(new MetadataInfo());
  126. setMetadataInfo(tempgroups.begin(), tempgroups.end(), mi);
  127. groups.insert(groups.end(), tempgroups.begin(), tempgroups.end());
  128. }
  129. void
  130. Metalink2RequestGroup::createRequestGroup
  131. (std::vector<SharedHandle<RequestGroup> >& groups,
  132. const std::vector<SharedHandle<MetalinkEntry> >& entries,
  133. const SharedHandle<Option>& option)
  134. {
  135. if(entries.empty()) {
  136. _logger->notice(EX_NO_RESULT_WITH_YOUR_PREFS);
  137. return;
  138. }
  139. std::vector<int32_t> selectIndexes =
  140. util::parseIntRange(option->get(PREF_SELECT_FILE)).flush();
  141. std::sort(selectIndexes.begin(), selectIndexes.end());
  142. std::vector<std::string> locations;
  143. if(option->defined(PREF_METALINK_LOCATION)) {
  144. util::split(option->get(PREF_METALINK_LOCATION),
  145. std::back_inserter(locations), ",", true);
  146. std::transform
  147. (locations.begin(), locations.end(), locations.begin(), util::toLower);
  148. }
  149. std::string preferredProtocol;
  150. if(option->get(PREF_METALINK_PREFERRED_PROTOCOL) != V_NONE) {
  151. preferredProtocol = option->get(PREF_METALINK_PREFERRED_PROTOCOL);
  152. }
  153. std::vector<SharedHandle<MetalinkEntry> > selectedEntries;
  154. selectedEntries.reserve(entries.size());
  155. {
  156. int32_t count = 1;
  157. for(std::vector<SharedHandle<MetalinkEntry> >::const_iterator i =
  158. entries.begin(), eoi = entries.end(); i != eoi; ++i, ++count) {
  159. (*i)->dropUnsupportedResource();
  160. if((*i)->resources.empty() && (*i)->metaurls.empty()) {
  161. continue;
  162. }
  163. (*i)->setLocationPriority
  164. (locations, -MetalinkResource::getLowestPriority());
  165. if(!preferredProtocol.empty()) {
  166. (*i)->setProtocolPriority
  167. (preferredProtocol, -MetalinkResource::getLowestPriority());
  168. }
  169. if(selectIndexes.empty() ||
  170. std::binary_search(selectIndexes.begin(), selectIndexes.end(), count)){
  171. selectedEntries.push_back(*i);
  172. }
  173. }
  174. }
  175. std::for_each(selectedEntries.begin(), selectedEntries.end(),
  176. mem_fun_sh(&MetalinkEntry::reorderMetaurlsByPriority));
  177. std::vector<std::pair<std::string,
  178. std::vector<SharedHandle<MetalinkEntry> > > > entryGroups;
  179. MetalinkHelper::groupEntryByMetaurlName(entryGroups, selectedEntries);
  180. for(std::vector<std::pair<std::string,
  181. std::vector<SharedHandle<MetalinkEntry> > > >::const_iterator itr =
  182. entryGroups.begin(), eoi = entryGroups.end(); itr != eoi; ++itr) {
  183. const std::string& metaurl = (*itr).first;
  184. const std::vector<SharedHandle<MetalinkEntry> >& mes = (*itr).second;
  185. _logger->info("Processing metaurl group metaurl=%s", metaurl.c_str());
  186. #ifdef ENABLE_BITTORRENT
  187. SharedHandle<RequestGroup> torrentRg;
  188. if(!metaurl.empty()) {
  189. std::vector<std::string> uris;
  190. uris.push_back(metaurl);
  191. {
  192. std::vector<SharedHandle<RequestGroup> > result;
  193. createRequestGroupForUri(result, option, uris,
  194. /* ignoreForceSequential = */true,
  195. /* ignoreLocalPath = */true);
  196. if(!uris.empty()) {
  197. torrentRg = result[0];
  198. }
  199. }
  200. if(!torrentRg.isNull()) {
  201. torrentRg->setNumConcurrentCommand(1);
  202. torrentRg->clearPreDownloadHandler();
  203. torrentRg->clearPostDownloadHandler();
  204. // remove "metalink" from Accept Type list to avoid loop in
  205. // tranparent metalink
  206. util::removeMetalinkContentTypes(torrentRg);
  207. // make it in-memory download
  208. SharedHandle<PreDownloadHandler> preh
  209. (new MemoryBufferPreDownloadHandler());
  210. SharedHandle<RequestGroupCriteria> cri(new TrueRequestGroupCriteria());
  211. preh->setCriteria(cri);
  212. torrentRg->addPreDownloadHandler(preh);
  213. groups.push_back(torrentRg);
  214. }
  215. }
  216. #endif // ENABLE_BITTORRENT
  217. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  218. SharedHandle<DownloadContext> dctx;
  219. if(mes.size() == 1) {
  220. SharedHandle<MetalinkEntry> entry = mes[0];
  221. _logger->info(MSG_METALINK_QUEUEING, entry->getPath().c_str());
  222. entry->reorderResourcesByPriority();
  223. std::vector<std::string> uris;
  224. std::for_each(entry->resources.begin(), entry->resources.end(),
  225. AccumulateNonP2PUri(uris));
  226. // If piece hash is specified in the metalink,
  227. // make segment size equal to piece hash size.
  228. size_t pieceLength;
  229. #ifdef ENABLE_MESSAGE_DIGEST
  230. if(entry->chunkChecksum.isNull()) {
  231. pieceLength = option->getAsInt(PREF_SEGMENT_SIZE);
  232. } else {
  233. pieceLength = entry->chunkChecksum->getChecksumLength();
  234. }
  235. #else
  236. pieceLength = option->getAsInt(PREF_SEGMENT_SIZE);
  237. #endif // ENABLE_MESSAGE_DIGEST
  238. dctx.reset(new DownloadContext
  239. (pieceLength,
  240. entry->getLength(),
  241. util::applyDir(option->get(PREF_DIR),
  242. entry->file->getPath())));
  243. dctx->getFirstFileEntry()->setUris(uris);
  244. if(option->getAsBool(PREF_METALINK_ENABLE_UNIQUE_PROTOCOL)) {
  245. dctx->getFirstFileEntry()->disableSingleHostMultiConnection();
  246. }
  247. #ifdef ENABLE_MESSAGE_DIGEST
  248. if(!entry->checksum.isNull()) {
  249. dctx->setChecksum(entry->checksum->getMessageDigest());
  250. dctx->setChecksumHashAlgo(entry->checksum->getAlgo());
  251. }
  252. if(!entry->chunkChecksum.isNull()) {
  253. dctx->setPieceHashes(entry->chunkChecksum->getChecksums().begin(),
  254. entry->chunkChecksum->getChecksums().end());
  255. dctx->setPieceHashAlgo(entry->chunkChecksum->getAlgo());
  256. }
  257. #endif // ENABLE_MESSAGE_DIGEST
  258. dctx->setSignature(entry->getSignature());
  259. rg->setNumConcurrentCommand
  260. (entry->maxConnections < 0 ?
  261. option->getAsInt(PREF_METALINK_SERVERS) :
  262. std::min(option->getAsInt(PREF_METALINK_SERVERS),
  263. static_cast<int32_t>(entry->maxConnections)));
  264. } else {
  265. dctx.reset(new DownloadContext());
  266. // piece length is overridden by the one in torrent file.
  267. dctx->setPieceLength(option->getAsInt(PREF_SEGMENT_SIZE));
  268. std::vector<SharedHandle<FileEntry> > fileEntries;
  269. off_t offset = 0;
  270. for(std::vector<SharedHandle<MetalinkEntry> >::const_iterator i =
  271. mes.begin(), eoi = mes.end(); i != eoi; ++i) {
  272. _logger->info("Metalink: Queueing %s for download as a member.",
  273. (*i)->getPath().c_str());
  274. _logger->debug("originalName = %s", (*i)->metaurls[0]->name.c_str());
  275. (*i)->reorderResourcesByPriority();
  276. std::vector<std::string> uris;
  277. std::for_each((*i)->resources.begin(), (*i)->resources.end(),
  278. AccumulateNonP2PUri(uris));
  279. SharedHandle<FileEntry> fe
  280. (new FileEntry
  281. (util::applyDir(option->get(PREF_DIR), (*i)->file->getPath()),
  282. (*i)->file->getLength(), offset, uris));
  283. if(option->getAsBool(PREF_METALINK_ENABLE_UNIQUE_PROTOCOL)) {
  284. fe->disableSingleHostMultiConnection();
  285. }
  286. fe->setOriginalName((*i)->metaurls[0]->name);
  287. fileEntries.push_back(fe);
  288. offset += (*i)->file->getLength();
  289. }
  290. dctx->setFileEntries(fileEntries.begin(), fileEntries.end());
  291. rg->setNumConcurrentCommand(option->getAsInt(PREF_METALINK_SERVERS));
  292. }
  293. dctx->setDir(option->get(PREF_DIR));
  294. rg->setDownloadContext(dctx);
  295. // remove "metalink" from Accept Type list to avoid loop in
  296. // tranparent metalink
  297. util::removeMetalinkContentTypes(rg);
  298. #ifdef ENABLE_BITTORRENT
  299. // Inject depenency between rg and torrentRg here if
  300. // torrentRg.isNull() == false
  301. if(!torrentRg.isNull()) {
  302. SharedHandle<Dependency> dep(new BtDependency(rg, torrentRg));
  303. rg->dependsOn(dep);
  304. torrentRg->belongsTo(rg->getGID());
  305. }
  306. #endif // ENABLE_BITTORRENT
  307. groups.push_back(rg);
  308. }
  309. }
  310. } // namespace aria2