download_helper.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. #ifdef ENABLE_BITTORRENT
  63. # include "bittorrent_helper.h"
  64. # include "BtConstants.h"
  65. # include "UTMetadataPostDownloadHandler.h"
  66. #endif // ENABLE_BITTORRENT
  67. namespace aria2 {
  68. const std::set<std::string>& listRequestOptions()
  69. {
  70. static const std::string REQUEST_OPTIONS[] = {
  71. PREF_DIR,
  72. PREF_CHECK_INTEGRITY,
  73. PREF_CONTINUE,
  74. PREF_ALL_PROXY,
  75. PREF_ALL_PROXY_USER,
  76. PREF_ALL_PROXY_PASSWD,
  77. PREF_CONNECT_TIMEOUT,
  78. PREF_DRY_RUN,
  79. PREF_LOWEST_SPEED_LIMIT,
  80. PREF_MAX_FILE_NOT_FOUND,
  81. PREF_MAX_TRIES,
  82. PREF_NO_PROXY,
  83. PREF_OUT,
  84. PREF_PROXY_METHOD,
  85. PREF_REMOTE_TIME,
  86. PREF_SPLIT,
  87. PREF_TIMEOUT,
  88. PREF_HTTP_AUTH_CHALLENGE,
  89. PREF_HTTP_NO_CACHE,
  90. PREF_HTTP_USER,
  91. PREF_HTTP_PASSWD,
  92. PREF_HTTP_PROXY,
  93. PREF_HTTP_PROXY_USER,
  94. PREF_HTTP_PROXY_PASSWD,
  95. PREF_HTTPS_PROXY,
  96. PREF_HTTPS_PROXY_USER,
  97. PREF_HTTPS_PROXY_PASSWD,
  98. PREF_REFERER,
  99. PREF_ENABLE_HTTP_KEEP_ALIVE,
  100. PREF_ENABLE_HTTP_PIPELINING,
  101. PREF_HEADER,
  102. PREF_USE_HEAD,
  103. PREF_USER_AGENT,
  104. PREF_FTP_USER,
  105. PREF_FTP_PASSWD,
  106. PREF_FTP_PASV,
  107. PREF_FTP_PROXY,
  108. PREF_FTP_PROXY_USER,
  109. PREF_FTP_PROXY_PASSWD,
  110. PREF_FTP_TYPE,
  111. PREF_FTP_REUSE_CONNECTION,
  112. PREF_NO_NETRC,
  113. PREF_REUSE_URI,
  114. PREF_SELECT_FILE,
  115. PREF_BT_ENABLE_LPD,
  116. PREF_BT_EXTERNAL_IP,
  117. PREF_BT_HASH_CHECK_SEED,
  118. PREF_BT_MAX_OPEN_FILES,
  119. PREF_BT_MAX_PEERS,
  120. PREF_BT_METADATA_ONLY,
  121. PREF_BT_MIN_CRYPTO_LEVEL,
  122. PREF_BT_PRIORITIZE_PIECE,
  123. PREF_BT_REQUIRE_CRYPTO,
  124. PREF_BT_REQUEST_PEER_SPEED_LIMIT,
  125. PREF_BT_SAVE_METADATA,
  126. PREF_BT_SEED_UNVERIFIED,
  127. PREF_BT_STOP_TIMEOUT,
  128. PREF_BT_TRACKER_INTERVAL,
  129. PREF_BT_TRACKER_TIMEOUT,
  130. PREF_BT_TRACKER_CONNECT_TIMEOUT,
  131. PREF_ENABLE_PEER_EXCHANGE,
  132. PREF_FOLLOW_TORRENT,
  133. PREF_INDEX_OUT,
  134. PREF_MAX_UPLOAD_LIMIT,
  135. PREF_SEED_RATIO,
  136. PREF_SEED_TIME,
  137. PREF_FOLLOW_METALINK,
  138. PREF_METALINK_SERVERS,
  139. PREF_METALINK_LANGUAGE,
  140. PREF_METALINK_LOCATION,
  141. PREF_METALINK_OS,
  142. PREF_METALINK_VERSION,
  143. PREF_METALINK_PREFERRED_PROTOCOL,
  144. PREF_METALINK_ENABLE_UNIQUE_PROTOCOL,
  145. PREF_ALLOW_OVERWRITE,
  146. PREF_ALLOW_PIECE_LENGTH_CHANGE,
  147. PREF_ASYNC_DNS,
  148. PREF_AUTO_FILE_RENAMING,
  149. PREF_FILE_ALLOCATION,
  150. PREF_MAX_DOWNLOAD_LIMIT,
  151. PREF_NO_FILE_ALLOCATION_LIMIT,
  152. PREF_PARAMETERIZED_URI,
  153. PREF_REALTIME_CHUNK_CHECKSUM,
  154. PREF_REMOVE_CONTROL_FILE,
  155. PREF_ALWAYS_RESUME,
  156. PREF_MAX_RESUME_FAILURE_TRIES,
  157. PREF_HTTP_ACCEPT_GZIP,
  158. PREF_MAX_CONNECTION_PER_SERVER,
  159. PREF_MIN_SPLIT_SIZE,
  160. PREF_CONDITIONAL_GET,
  161. PREF_ENABLE_ASYNC_DNS6,
  162. PREF_BT_TRACKER,
  163. PREF_BT_EXCLUDE_TRACKER,
  164. PREF_RETRY_WAIT,
  165. PREF_METALINK_BASE_URI,
  166. PREF_PAUSE,
  167. PREF_STREAM_PIECE_SELECTOR,
  168. PREF_HASH_CHECK_ONLY,
  169. PREF_CHECKSUM,
  170. PREF_PIECE_LENGTH
  171. };
  172. static std::set<std::string> requestOptions
  173. (vbegin(REQUEST_OPTIONS), vend(REQUEST_OPTIONS));
  174. return requestOptions;
  175. }
  176. namespace {
  177. void unfoldURI
  178. (std::vector<std::string>& result, const std::vector<std::string>& args)
  179. {
  180. ParameterizedStringParser p;
  181. PStringBuildVisitor v;
  182. for(std::vector<std::string>::const_iterator itr = args.begin(),
  183. eoi = args.end(); itr != eoi; ++itr) {
  184. v.reset();
  185. p.parse(*itr)->accept(v);
  186. result.insert(result.end(), v.getURIs().begin(), v.getURIs().end());
  187. }
  188. }
  189. } // namespace
  190. namespace {
  191. template<typename InputIterator>
  192. void splitURI(std::vector<std::string>& result,
  193. InputIterator begin,
  194. InputIterator end,
  195. size_t numSplit,
  196. size_t maxIter)
  197. {
  198. size_t numURIs = std::distance(begin, end);
  199. if(numURIs >= numSplit) {
  200. result.insert(result.end(), begin, end);
  201. } else if(numURIs > 0) {
  202. size_t num = std::min(numSplit/numURIs, maxIter);
  203. for(size_t i = 0; i < num; ++i) {
  204. result.insert(result.end(), begin, end);
  205. }
  206. if(num < maxIter) {
  207. result.insert(result.end(), begin, begin+(numSplit%numURIs));
  208. }
  209. }
  210. }
  211. } // namespace
  212. namespace {
  213. SharedHandle<RequestGroup> createRequestGroup
  214. (const SharedHandle<Option>& option, const std::vector<std::string>& uris,
  215. bool useOutOption = false)
  216. {
  217. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  218. SharedHandle<DownloadContext> dctx
  219. (new DownloadContext
  220. (option->getAsInt(PREF_PIECE_LENGTH),
  221. 0,
  222. useOutOption&&!option->blank(PREF_OUT)?
  223. util::applyDir(option->get(PREF_DIR), option->get(PREF_OUT)):A2STR::NIL));
  224. dctx->getFirstFileEntry()->setUris(uris);
  225. dctx->getFirstFileEntry()->setMaxConnectionPerServer
  226. (option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER));
  227. #ifdef ENABLE_MESSAGE_DIGEST
  228. const std::string& checksum = option->get(PREF_CHECKSUM);
  229. if(!checksum.empty()) {
  230. std::pair<std::string, std::string> p;
  231. util::divide(p, checksum, '=');
  232. util::lowercase(p.first);
  233. util::lowercase(p.second);
  234. dctx->setDigest(p.first, p.second);
  235. }
  236. #endif // ENABLE_MESSAGE_DIGEST
  237. rg->setDownloadContext(dctx);
  238. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  239. removeOneshotOption(rg->getOption());
  240. return rg;
  241. }
  242. } // namespace
  243. #if defined ENABLE_BITTORRENT || ENABLE_METALINK
  244. namespace {
  245. SharedHandle<MetadataInfo> createMetadataInfo(const std::string& uri)
  246. {
  247. return SharedHandle<MetadataInfo>(new MetadataInfo(uri));
  248. }
  249. } // namespace
  250. namespace {
  251. SharedHandle<MetadataInfo> createMetadataInfoDataOnly()
  252. {
  253. return SharedHandle<MetadataInfo>(new MetadataInfo());
  254. }
  255. } // namespace
  256. #endif // ENABLE_BITTORRENT || ENABLE_METALINK
  257. #ifdef ENABLE_BITTORRENT
  258. namespace {
  259. SharedHandle<RequestGroup>
  260. createBtRequestGroup(const std::string& torrentFilePath,
  261. const SharedHandle<Option>& option,
  262. const std::vector<std::string>& auxUris,
  263. const std::string& torrentData = "",
  264. bool adjustAnnounceUri = true)
  265. {
  266. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  267. SharedHandle<DownloadContext> dctx(new DownloadContext());
  268. if(torrentData.empty()) {
  269. // may throw exception
  270. bittorrent::load(torrentFilePath, dctx, option, auxUris);
  271. rg->setMetadataInfo(createMetadataInfo(torrentFilePath));
  272. } else {
  273. // may throw exception
  274. bittorrent::loadFromMemory(torrentData, dctx, option, auxUris, "default");
  275. rg->setMetadataInfo(createMetadataInfoDataOnly());
  276. }
  277. if(adjustAnnounceUri) {
  278. bittorrent::adjustAnnounceUri(bittorrent::getTorrentAttrs(dctx), option);
  279. }
  280. dctx->setFileFilter(util::parseIntRange(option->get(PREF_SELECT_FILE)));
  281. std::istringstream indexOutIn(option->get(PREF_INDEX_OUT));
  282. std::map<size_t, std::string> indexPathMap =
  283. util::createIndexPathMap(indexOutIn);
  284. for(std::map<size_t, std::string>::const_iterator i = indexPathMap.begin(),
  285. eoi = indexPathMap.end(); i != eoi; ++i) {
  286. dctx->setFilePathWithIndex
  287. ((*i).first, util::applyDir(option->get(PREF_DIR), (*i).second));
  288. }
  289. rg->setDownloadContext(dctx);
  290. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  291. // Remove "metalink" from Accept Type list to avoid server from
  292. // responding Metalink file for web-seeding URIs.
  293. util::removeMetalinkContentTypes(rg);
  294. removeOneshotOption(rg->getOption());
  295. return rg;
  296. }
  297. } // namespace
  298. namespace {
  299. SharedHandle<RequestGroup>
  300. createBtMagnetRequestGroup(const std::string& magnetLink,
  301. const SharedHandle<Option>& option)
  302. {
  303. SharedHandle<RequestGroup> rg(new RequestGroup(option));
  304. SharedHandle<DownloadContext> dctx
  305. (new DownloadContext(METADATA_PIECE_SIZE, 0,
  306. A2STR::NIL));
  307. // We only know info hash. Total Length is unknown at this moment.
  308. dctx->markTotalLengthIsUnknown();
  309. rg->setFileAllocationEnabled(false);
  310. rg->setPreLocalFileCheckEnabled(false);
  311. bittorrent::loadMagnet(magnetLink, dctx);
  312. SharedHandle<TorrentAttribute> torrentAttrs =
  313. bittorrent::getTorrentAttrs(dctx);
  314. bittorrent::adjustAnnounceUri(torrentAttrs, rg->getOption());
  315. dctx->getFirstFileEntry()->setPath(torrentAttrs->name);
  316. rg->setDownloadContext(dctx);
  317. rg->clearPostDownloadHandler();
  318. SharedHandle<UTMetadataPostDownloadHandler> utMetadataPostHandler
  319. (new UTMetadataPostDownloadHandler());
  320. rg->addPostDownloadHandler(utMetadataPostHandler);
  321. rg->setDiskWriterFactory
  322. (SharedHandle<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
  323. rg->setMetadataInfo(createMetadataInfo(magnetLink));
  324. rg->markInMemoryDownload();
  325. rg->setPauseRequested(option->getAsBool(PREF_PAUSE));
  326. removeOneshotOption(rg->getOption());
  327. return rg;
  328. }
  329. } // namespace
  330. void createRequestGroupForBitTorrent
  331. (std::vector<SharedHandle<RequestGroup> >& result,
  332. const SharedHandle<Option>& option,
  333. const std::vector<std::string>& uris,
  334. const std::string& torrentData,
  335. bool adjustAnnounceUri)
  336. {
  337. std::vector<std::string> nargs;
  338. if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
  339. unfoldURI(nargs, uris);
  340. } else {
  341. nargs = uris;
  342. }
  343. // we ignore -Z option here
  344. size_t numSplit = option->getAsInt(PREF_SPLIT);
  345. SharedHandle<RequestGroup> rg =
  346. createBtRequestGroup(option->get(PREF_TORRENT_FILE), option, nargs,
  347. torrentData, adjustAnnounceUri);
  348. rg->setNumConcurrentCommand(numSplit);
  349. result.push_back(rg);
  350. }
  351. #endif // ENABLE_BITTORRENT
  352. #ifdef ENABLE_METALINK
  353. void createRequestGroupForMetalink
  354. (std::vector<SharedHandle<RequestGroup> >& result,
  355. const SharedHandle<Option>& option,
  356. const std::string& metalinkData)
  357. {
  358. if(metalinkData.empty()) {
  359. Metalink2RequestGroup().generate(result,
  360. option->get(PREF_METALINK_FILE),
  361. option,
  362. option->get(PREF_METALINK_BASE_URI));
  363. } else {
  364. SharedHandle<ByteArrayDiskWriter> dw(new ByteArrayDiskWriter());
  365. dw->setString(metalinkData);
  366. Metalink2RequestGroup().generate(result, dw, option,
  367. option->get(PREF_METALINK_BASE_URI));
  368. }
  369. }
  370. #endif // ENABLE_METALINK
  371. namespace {
  372. class AccRequestGroup {
  373. private:
  374. std::vector<SharedHandle<RequestGroup> >& requestGroups_;
  375. ProtocolDetector detector_;
  376. SharedHandle<Option> option_;
  377. bool ignoreLocalPath_;
  378. bool throwOnError_;
  379. public:
  380. AccRequestGroup(std::vector<SharedHandle<RequestGroup> >& requestGroups,
  381. const SharedHandle<Option>& option,
  382. bool ignoreLocalPath = false,
  383. bool throwOnError = false):
  384. requestGroups_(requestGroups), option_(option),
  385. ignoreLocalPath_(ignoreLocalPath),
  386. throwOnError_(throwOnError)
  387. {}
  388. void
  389. operator()(const std::string& uri)
  390. {
  391. if(detector_.isStreamProtocol(uri)) {
  392. std::vector<std::string> streamURIs;
  393. size_t numIter = option_->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
  394. size_t numSplit = option_->getAsInt(PREF_SPLIT);
  395. size_t num = std::min(numIter, numSplit);
  396. for(size_t i = 0; i < num; ++i) {
  397. streamURIs.push_back(uri);
  398. }
  399. SharedHandle<RequestGroup> rg = createRequestGroup(option_, streamURIs);
  400. rg->setNumConcurrentCommand(numSplit);
  401. requestGroups_.push_back(rg);
  402. }
  403. #ifdef ENABLE_BITTORRENT
  404. else if(detector_.guessTorrentMagnet(uri)) {
  405. SharedHandle<RequestGroup> group =
  406. createBtMagnetRequestGroup(uri, option_);
  407. requestGroups_.push_back(group);
  408. } else if(!ignoreLocalPath_ && detector_.guessTorrentFile(uri)) {
  409. try {
  410. requestGroups_.push_back
  411. (createBtRequestGroup(uri, option_, std::vector<std::string>()));
  412. } catch(RecoverableException& e) {
  413. if(throwOnError_) {
  414. throw;
  415. } else {
  416. // error occurred while parsing torrent file.
  417. // We simply ignore it.
  418. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  419. }
  420. }
  421. }
  422. #endif // ENABLE_BITTORRENT
  423. #ifdef ENABLE_METALINK
  424. else if(!ignoreLocalPath_ && detector_.guessMetalinkFile(uri)) {
  425. try {
  426. Metalink2RequestGroup().generate(requestGroups_, uri, option_,
  427. option_->get(PREF_METALINK_BASE_URI));
  428. } catch(RecoverableException& e) {
  429. if(throwOnError_) {
  430. throw;
  431. } else {
  432. // error occurred while parsing metalink file.
  433. // We simply ignore it.
  434. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  435. }
  436. }
  437. }
  438. #endif // ENABLE_METALINK
  439. else {
  440. if(throwOnError_) {
  441. throw DL_ABORT_EX(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
  442. } else {
  443. A2_LOG_ERROR(fmt(MSG_UNRECOGNIZED_URI, uri.c_str()));
  444. }
  445. }
  446. }
  447. };
  448. } // namespace
  449. namespace {
  450. class StreamProtocolFilter {
  451. private:
  452. ProtocolDetector detector_;
  453. public:
  454. bool operator()(const std::string& uri) {
  455. return detector_.isStreamProtocol(uri);
  456. }
  457. };
  458. } // namespace
  459. void createRequestGroupForUri
  460. (std::vector<SharedHandle<RequestGroup> >& result,
  461. const SharedHandle<Option>& option,
  462. const std::vector<std::string>& uris,
  463. bool ignoreForceSequential,
  464. bool ignoreLocalPath,
  465. bool throwOnError)
  466. {
  467. std::vector<std::string> nargs;
  468. if(option->get(PREF_PARAMETERIZED_URI) == A2_V_TRUE) {
  469. unfoldURI(nargs, uris);
  470. } else {
  471. nargs = uris;
  472. }
  473. if(!ignoreForceSequential && option->get(PREF_FORCE_SEQUENTIAL) == A2_V_TRUE) {
  474. std::for_each(nargs.begin(), nargs.end(),
  475. AccRequestGroup(result, option, ignoreLocalPath,
  476. throwOnError));
  477. } else {
  478. std::vector<std::string>::iterator strmProtoEnd =
  479. std::stable_partition(nargs.begin(), nargs.end(), StreamProtocolFilter());
  480. // let's process http/ftp protocols first.
  481. if(nargs.begin() != strmProtoEnd) {
  482. size_t numIter = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
  483. size_t numSplit = option->getAsInt(PREF_SPLIT);
  484. std::vector<std::string> streamURIs;
  485. splitURI(streamURIs, nargs.begin(), strmProtoEnd, numSplit, numIter);
  486. SharedHandle<RequestGroup> rg =
  487. createRequestGroup(option, streamURIs, true);
  488. rg->setNumConcurrentCommand(numSplit);
  489. result.push_back(rg);
  490. }
  491. // process remaining URIs(local metalink, BitTorrent files)
  492. std::for_each(strmProtoEnd, nargs.end(),
  493. AccRequestGroup(result, option, ignoreLocalPath,
  494. throwOnError));
  495. }
  496. }
  497. namespace {
  498. void createRequestGroupForUriList
  499. (std::vector<SharedHandle<RequestGroup> >& result,
  500. const SharedHandle<Option>& option,
  501. const std::string& filename)
  502. {
  503. UriListParser p(filename);
  504. while(p.hasNext()) {
  505. std::vector<std::string> uris;
  506. SharedHandle<Option> tempOption(new Option());
  507. p.parseNext(uris, *tempOption.get());
  508. if(uris.empty()) {
  509. continue;
  510. }
  511. SharedHandle<Option> requestOption(new Option(*option.get()));
  512. requestOption->remove(PREF_OUT);
  513. for(std::set<std::string>::const_iterator i =
  514. listRequestOptions().begin(), eoi = listRequestOptions().end();
  515. i != eoi; ++i) {
  516. if(tempOption->defined(*i)) {
  517. requestOption->put(*i, tempOption->get(*i));
  518. }
  519. }
  520. createRequestGroupForUri(result, requestOption, uris);
  521. }
  522. }
  523. } // namespace
  524. void createRequestGroupForUriList
  525. (std::vector<SharedHandle<RequestGroup> >& result,
  526. const SharedHandle<Option>& option)
  527. {
  528. if(option->get(PREF_INPUT_FILE) == "-") {
  529. createRequestGroupForUriList(result, option, DEV_STDIN);
  530. } else {
  531. if(!File(option->get(PREF_INPUT_FILE)).isFile()) {
  532. throw DL_ABORT_EX
  533. (fmt(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(),
  534. "No such file"));
  535. }
  536. createRequestGroupForUriList(result, option, option->get(PREF_INPUT_FILE));
  537. }
  538. }
  539. SharedHandle<MetadataInfo>
  540. createMetadataInfoFromFirstFileEntry(const SharedHandle<DownloadContext>& dctx)
  541. {
  542. if(dctx->getFileEntries().empty()) {
  543. return SharedHandle<MetadataInfo>();
  544. } else {
  545. std::vector<std::string> uris;
  546. dctx->getFileEntries()[0]->getUris(uris);
  547. if(uris.empty()) {
  548. return SharedHandle<MetadataInfo>();
  549. }
  550. return SharedHandle<MetadataInfo>(new MetadataInfo(uris[0]));
  551. }
  552. }
  553. void removeOneshotOption(const SharedHandle<Option>& option)
  554. {
  555. option->remove(PREF_PAUSE);
  556. }
  557. } // namespace aria2