download_helper.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 "download_helper.h"
  36. #include <iostream>
  37. #include <algorithm>
  38. #include <sstream>
  39. #include "RequestGroup.h"
  40. #include "Option.h"
  41. #include "prefs.h"
  42. #include "Metalink2RequestGroup.h"
  43. #include "ProtocolDetector.h"
  44. #include "ParameterizedStringParser.h"
  45. #include "PStringBuildVisitor.h"
  46. #include "UriListParser.h"
  47. #include "DownloadContext.h"
  48. #include "RecoverableException.h"
  49. #include "DlAbortEx.h"
  50. #include "message.h"
  51. #include "fmt.h"
  52. #include "FileEntry.h"
  53. #include "LogFactory.h"
  54. #include "File.h"
  55. #include "util.h"
  56. #include "array_fun.h"
  57. #include "OptionHandler.h"
  58. #include "ByteArrayDiskWriter.h"
  59. #include "a2functional.h"
  60. #include "ByteArrayDiskWriterFactory.h"
  61. #include "MetadataInfo.h"
  62. #include "OptionParser.h"
  63. #include "SegList.h"
  64. #ifdef ENABLE_BITTORRENT
  65. # include "bittorrent_helper.h"
  66. # include "BtConstants.h"
  67. # include "UTMetadataPostDownloadHandler.h"
  68. #endif // ENABLE_BITTORRENT
  69. namespace aria2 {
  70. namespace {
  71. void unfoldURI
  72. (std::vector<std::string>& result, const std::vector<std::string>& args)
  73. {
  74. ParameterizedStringParser p;
  75. PStringBuildVisitor v;
  76. for(std::vector<std::string>::const_iterator itr = args.begin(),
  77. eoi = args.end(); itr != eoi; ++itr) {
  78. v.reset();
  79. p.parse(*itr)->accept(v);
  80. result.insert(result.end(), v.getURIs().begin(), v.getURIs().end());
  81. }
  82. }
  83. } // namespace
  84. namespace {
  85. template<typename InputIterator>
  86. void splitURI(std::vector<std::string>& result,
  87. InputIterator begin,
  88. InputIterator end,
  89. size_t numSplit,
  90. size_t maxIter)
  91. {
  92. size_t numURIs = std::distance(begin, end);
  93. if(numURIs >= numSplit) {
  94. result.insert(result.end(), begin, end);
  95. } else if(numURIs > 0) {
  96. size_t num = std::min(numSplit/numURIs, maxIter);
  97. for(size_t i = 0; i < num; ++i) {
  98. result.insert(result.end(), begin, end);
  99. }
  100. if(num < maxIter) {
  101. result.insert(result.end(), begin, begin+(numSplit%numURIs));
  102. }
  103. }
  104. }
  105. } // namespace
  106. namespace {
  107. SharedHandle<RequestGroup> createRequestGroup
  108. (const SharedHandle<Option>& optionTemplate,
  109. const std::vector<std::string>& uris,
  110. bool useOutOption = false)
  111. {
  112. SharedHandle<Option> option = util::copy(optionTemplate);
  113. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  114. SharedHandle<DownloadContext> dctx
  115. (new DownloadContext
  116. (option->getAsInt(PREF_PIECE_LENGTH),
  117. 0,
  118. useOutOption&&!option->blank(PREF_OUT)?
  119. util::applyDir(option->get(PREF_DIR), option->get(PREF_OUT)):A2STR::NIL));
  120. dctx->getFirstFileEntry()->setUris(uris);
  121. dctx->getFirstFileEntry()->setMaxConnectionPerServer
  122. (option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER));
  123. #ifdef ENABLE_MESSAGE_DIGEST
  124. const std::string& checksum = option->get(PREF_CHECKSUM);
  125. if(!checksum.empty()) {
  126. std::pair<Scip, Scip> p;
  127. util::divide(p, checksum.begin(), checksum.end(), '=');
  128. std::string hashType(p.first.first, p.first.second);
  129. std::string hexDigest(p.second.first, p.second.second);
  130. util::lowercase(hashType);
  131. util::lowercase(hexDigest);
  132. dctx->setDigest(hashType, hexDigest);
  133. }
  134. #endif // ENABLE_MESSAGE_DIGEST
  135. rg->setDownloadContext(dctx);
  136. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  137. removeOneshotOption(option);
  138. return rg;
  139. }
  140. } // namespace
  141. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  142. namespace {
  143. SharedHandle<MetadataInfo> createMetadataInfo(const std::string& uri)
  144. {
  145. return SharedHandle<MetadataInfo>(new MetadataInfo(uri));
  146. }
  147. } // namespace
  148. namespace {
  149. SharedHandle<MetadataInfo> createMetadataInfoDataOnly()
  150. {
  151. return SharedHandle<MetadataInfo>(new MetadataInfo());
  152. }
  153. } // namespace
  154. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  155. #ifdef ENABLE_BITTORRENT
  156. namespace {
  157. SharedHandle<RequestGroup>
  158. createBtRequestGroup(const std::string& torrentFilePath,
  159. const SharedHandle<Option>& optionTemplate,
  160. const std::vector<std::string>& auxUris,
  161. const std::string& torrentData = "",
  162. bool adjustAnnounceUri = true)
  163. {
  164. SharedHandle<Option> option = util::copy(optionTemplate);
  165. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  166. SharedHandle<DownloadContext> dctx(new DownloadContext());
  167. if(torrentData.empty()) {
  168. // may throw exception
  169. bittorrent::load(torrentFilePath, dctx, option, auxUris);
  170. rg->setMetadataInfo(createMetadataInfo(torrentFilePath));
  171. } else {
  172. // may throw exception
  173. bittorrent::loadFromMemory(torrentData, dctx, option, auxUris, "default");
  174. rg->setMetadataInfo(createMetadataInfoDataOnly());
  175. }
  176. if(adjustAnnounceUri) {
  177. bittorrent::adjustAnnounceUri(bittorrent::getTorrentAttrs(dctx), option);
  178. }
  179. SegList<int> sgl;
  180. util::parseIntSegments(sgl, option->get(PREF_SELECT_FILE));
  181. sgl.normalize();
  182. dctx->setFileFilter(sgl);
  183. std::istringstream indexOutIn(option->get(PREF_INDEX_OUT));
  184. std::vector<std::pair<size_t, std::string> > indexPaths =
  185. util::createIndexPaths(indexOutIn);
  186. for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
  187. indexPaths.begin(), eoi = indexPaths.end(); i != eoi; ++i) {
  188. dctx->setFilePathWithIndex
  189. ((*i).first, util::applyDir(option->get(PREF_DIR), (*i).second));
  190. }
  191. rg->setDownloadContext(dctx);
  192. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  193. // Remove "metalink" from Accept Type list to avoid server from
  194. // responding Metalink file for web-seeding URIs.
  195. util::removeMetalinkContentTypes(rg);
  196. removeOneshotOption(option);
  197. return rg;
  198. }
  199. } // namespace
  200. namespace {
  201. SharedHandle<RequestGroup>
  202. createBtMagnetRequestGroup
  203. (const std::string& magnetLink,
  204. const SharedHandle<Option>& optionTemplate)
  205. {
  206. SharedHandle<Option> option = util::copy(optionTemplate);
  207. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  208. SharedHandle<DownloadContext> dctx
  209. (new DownloadContext(METADATA_PIECE_SIZE, 0,
  210. A2STR::NIL));
  211. // We only know info hash. Total Length is unknown at this moment.
  212. dctx->markTotalLengthIsUnknown();
  213. rg->setFileAllocationEnabled(false);
  214. rg->setPreLocalFileCheckEnabled(false);
  215. bittorrent::loadMagnet(magnetLink, dctx);
  216. SharedHandle<TorrentAttribute> torrentAttrs =
  217. bittorrent::getTorrentAttrs(dctx);
  218. bittorrent::adjustAnnounceUri(torrentAttrs, option);
  219. dctx->getFirstFileEntry()->setPath(torrentAttrs->name);
  220. rg->setDownloadContext(dctx);
  221. rg->clearPostDownloadHandler();
  222. SharedHandle<UTMetadataPostDownloadHandler> utMetadataPostHandler
  223. (new UTMetadataPostDownloadHandler());
  224. rg->addPostDownloadHandler(utMetadataPostHandler);
  225. rg->setDiskWriterFactory
  226. (SharedHandle<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
  227. rg->setMetadataInfo(createMetadataInfo(magnetLink));
  228. rg->markInMemoryDownload();
  229. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  230. removeOneshotOption(option);
  231. return rg;
  232. }
  233. } // namespace
  234. void createRequestGroupForBitTorrent
  235. (std::vector<SharedHandle<RequestGroup> >& result,
  236. const SharedHandle<Option>& option,
  237. const std::vector<std::string>& uris,
  238. const std::string& torrentData,
  239. bool adjustAnnounceUri)
  240. {
  241. std::vector<std::string> nargs;
  242. if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
  243. unfoldURI(nargs, uris);
  244. } else {
  245. nargs = uris;
  246. }
  247. // we ignore -Z option here
  248. size_t numSplit = option->getAsInt(PREF_SPLIT);
  249. SharedHandle<RequestGroup> rg =
  250. createBtRequestGroup(option->get(PREF_TORRENT_FILE), option, nargs,
  251. torrentData, adjustAnnounceUri);
  252. rg->setNumConcurrentCommand(numSplit);
  253. result.push_back(rg);
  254. }
  255. #endif // ENABLE_BITTORRENT
  256. #ifdef ENABLE_METALINK
  257. void createRequestGroupForMetalink
  258. (std::vector<SharedHandle<RequestGroup> >& result,
  259. const SharedHandle<Option>& option,
  260. const std::string& metalinkData)
  261. {
  262. if(metalinkData.empty()) {
  263. Metalink2RequestGroup().generate(result,
  264. option->get(PREF_METALINK_FILE),
  265. option,
  266. option->get(PREF_METALINK_BASE_URI));
  267. } else {
  268. SharedHandle<ByteArrayDiskWriter> dw(new ByteArrayDiskWriter());
  269. dw->setString(metalinkData);
  270. Metalink2RequestGroup().generate(result, dw, option,
  271. option->get(PREF_METALINK_BASE_URI));
  272. }
  273. }
  274. #endif // ENABLE_METALINK
  275. namespace {
  276. class AccRequestGroup {
  277. private:
  278. std::vector<SharedHandle<RequestGroup> >& requestGroups_;
  279. ProtocolDetector detector_;
  280. SharedHandle<Option> option_;
  281. bool ignoreLocalPath_;
  282. bool throwOnError_;
  283. public:
  284. AccRequestGroup(std::vector<SharedHandle<RequestGroup> >& requestGroups,
  285. const SharedHandle<Option>& option,
  286. bool ignoreLocalPath = false,
  287. bool throwOnError = false):
  288. requestGroups_(requestGroups), option_(option),
  289. ignoreLocalPath_(ignoreLocalPath),
  290. throwOnError_(throwOnError)
  291. {}
  292. void
  293. operator()(const std::string& uri)
  294. {
  295. if(detector_.isStreamProtocol(uri)) {
  296. std::vector<std::string> streamURIs;
  297. size_t numIter = option_->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
  298. size_t numSplit = option_->getAsInt(PREF_SPLIT);
  299. size_t num = std::min(numIter, numSplit);
  300. for(size_t i = 0; i < num; ++i) {
  301. streamURIs.push_back(uri);
  302. }
  303. SharedHandle<RequestGroup> rg = createRequestGroup(option_, streamURIs);
  304. rg->setNumConcurrentCommand(numSplit);
  305. requestGroups_.push_back(rg);
  306. }
  307. #ifdef ENABLE_BITTORRENT
  308. else if(detector_.guessTorrentMagnet(uri)) {
  309. SharedHandle<RequestGroup> group =
  310. createBtMagnetRequestGroup(uri, option_);
  311. requestGroups_.push_back(group);
  312. } else if(!ignoreLocalPath_ && detector_.guessTorrentFile(uri)) {
  313. try {
  314. requestGroups_.push_back
  315. (createBtRequestGroup(uri, option_, std::vector<std::string>()));
  316. } catch(RecoverableException& e) {
  317. if(throwOnError_) {
  318. throw;
  319. } else {
  320. // error occurred while parsing torrent file.
  321. // We simply ignore it.
  322. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  323. }
  324. }
  325. }
  326. #endif // ENABLE_BITTORRENT
  327. #ifdef ENABLE_METALINK
  328. else if(!ignoreLocalPath_ && detector_.guessMetalinkFile(uri)) {
  329. try {
  330. Metalink2RequestGroup().generate(requestGroups_, uri, option_,
  331. option_->get(PREF_METALINK_BASE_URI));
  332. } catch(RecoverableException& e) {
  333. if(throwOnError_) {
  334. throw;
  335. } else {
  336. // error occurred while parsing metalink file.
  337. // We simply ignore it.
  338. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  339. }
  340. }
  341. }
  342. #endif // ENABLE_METALINK
  343. else {
  344. if(throwOnError_) {
  345. throw DL_ABORT_EX(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
  346. } else {
  347. A2_LOG_ERROR(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
  348. }
  349. }
  350. }
  351. };
  352. } // namespace
  353. namespace {
  354. class StreamProtocolFilter {
  355. private:
  356. ProtocolDetector detector_;
  357. public:
  358. bool operator()(const std::string& uri) {
  359. return detector_.isStreamProtocol(uri);
  360. }
  361. };
  362. } // namespace
  363. void createRequestGroupForUri
  364. (std::vector<SharedHandle<RequestGroup> >& result,
  365. const SharedHandle<Option>& option,
  366. const std::vector<std::string>& uris,
  367. bool ignoreForceSequential,
  368. bool ignoreLocalPath,
  369. bool throwOnError)
  370. {
  371. std::vector<std::string> nargs;
  372. if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
  373. unfoldURI(nargs, uris);
  374. } else {
  375. nargs = uris;
  376. }
  377. if(!ignoreForceSequential && option->get(PREF_FORCE_SEQUENTIAL) == A2_V_TRUE) {
  378. std::for_each(nargs.begin(), nargs.end(),
  379. AccRequestGroup(result, option, ignoreLocalPath,
  380. throwOnError));
  381. } else {
  382. std::vector<std::string>::iterator strmProtoEnd =
  383. std::stable_partition(nargs.begin(), nargs.end(), StreamProtocolFilter());
  384. // let's process http/ftp protocols first.
  385. if(nargs.begin() != strmProtoEnd) {
  386. size_t numIter = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
  387. size_t numSplit = option->getAsInt(PREF_SPLIT);
  388. std::vector<std::string> streamURIs;
  389. splitURI(streamURIs, nargs.begin(), strmProtoEnd, numSplit, numIter);
  390. SharedHandle<RequestGroup> rg =
  391. createRequestGroup(option, streamURIs, true);
  392. rg->setNumConcurrentCommand(numSplit);
  393. result.push_back(rg);
  394. }
  395. // process remaining URIs(local metalink, BitTorrent files)
  396. std::for_each(strmProtoEnd, nargs.end(),
  397. AccRequestGroup(result, option, ignoreLocalPath,
  398. throwOnError));
  399. }
  400. }
  401. namespace {
  402. void createRequestGroupForUriList
  403. (std::vector<SharedHandle<RequestGroup> >& result,
  404. const SharedHandle<Option>& option,
  405. const std::string& filename)
  406. {
  407. UriListParser p(filename);
  408. while(p.hasNext()) {
  409. std::vector<std::string> uris;
  410. Option tempOption;
  411. p.parseNext(uris, tempOption);
  412. if(uris.empty()) {
  413. continue;
  414. }
  415. SharedHandle<Option> requestOption(new Option(*option.get()));
  416. requestOption->remove(PREF_OUT);
  417. const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
  418. for(size_t i = 1, len = option::countOption(); i < len; ++i) {
  419. const Pref* pref = option::i2p(i);
  420. const SharedHandle<OptionHandler>& h = oparser->find(pref);
  421. if(h && h->getInitialOption() && tempOption.defined(pref)) {
  422. requestOption->put(pref, tempOption.get(pref));
  423. }
  424. }
  425. createRequestGroupForUri(result, requestOption, uris);
  426. }
  427. }
  428. } // namespace
  429. void createRequestGroupForUriList
  430. (std::vector<SharedHandle<RequestGroup> >& result,
  431. const SharedHandle<Option>& option)
  432. {
  433. if(option->get(PREF_INPUT_FILE) == "-") {
  434. createRequestGroupForUriList(result, option, DEV_STDIN);
  435. } else {
  436. if(!File(option->get(PREF_INPUT_FILE)).isFile()) {
  437. throw DL_ABORT_EX
  438. (fmt(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(),
  439. "No such file"));
  440. }
  441. createRequestGroupForUriList(result, option, option->get(PREF_INPUT_FILE));
  442. }
  443. }
  444. SharedHandle<MetadataInfo>
  445. createMetadataInfoFromFirstFileEntry(const SharedHandle<DownloadContext>& dctx)
  446. {
  447. if(dctx->getFileEntries().empty()) {
  448. return SharedHandle<MetadataInfo>();
  449. } else {
  450. std::vector<std::string> uris;
  451. dctx->getFileEntries()[0]->getUris(uris);
  452. if(uris.empty()) {
  453. return SharedHandle<MetadataInfo>();
  454. }
  455. return SharedHandle<MetadataInfo>(new MetadataInfo(uris[0]));
  456. }
  457. }
  458. void removeOneshotOption(const SharedHandle<Option>& option)
  459. {
  460. option->remove(PREF_PAUSE);
  461. }
  462. } // namespace aria2