MultiUrlRequestInfo.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "MultiUrlRequestInfo.h"
  36. #include <signal.h>
  37. #include <ostream>
  38. #include "RequestGroupMan.h"
  39. #include "DownloadEngine.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. #include "RequestGroup.h"
  43. #include "prefs.h"
  44. #include "DownloadEngineFactory.h"
  45. #include "RecoverableException.h"
  46. #include "message.h"
  47. #include "Util.h"
  48. #include "Option.h"
  49. #include "StatCalc.h"
  50. #include "CookieStorage.h"
  51. #include "File.h"
  52. #include "Netrc.h"
  53. #include "AuthConfigFactory.h"
  54. #ifdef ENABLE_SSL
  55. # include "SocketCore.h"
  56. # include "TLSContext.h"
  57. #endif // ENABLE_SSL
  58. namespace aria2 {
  59. #ifndef SA_RESETHAND
  60. # define SA_RESETHAND 0x80000000
  61. #endif // SA_RESETHAND
  62. extern volatile sig_atomic_t globalHaltRequested;
  63. static void handler(int signal) {
  64. if(globalHaltRequested == 0) {
  65. globalHaltRequested = 1;
  66. } else if(globalHaltRequested == 2) {
  67. globalHaltRequested = 3;
  68. }
  69. }
  70. MultiUrlRequestInfo::MultiUrlRequestInfo
  71. (const RequestGroups& requestGroups,
  72. const SharedHandle<Option>& op,
  73. const SharedHandle<StatCalc>& statCalc,
  74. std::ostream& summaryOut)
  75. :
  76. _requestGroups(requestGroups),
  77. _option(op),
  78. _statCalc(statCalc),
  79. _summaryOut(summaryOut),
  80. _logger(LogFactory::getInstance())
  81. {}
  82. MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
  83. void MultiUrlRequestInfo::printMessageForContinue()
  84. {
  85. _summaryOut << "\n"
  86. << _("aria2 will resume download if the transfer is restarted.")
  87. << "\n"
  88. << _("If there are any errors, then see the log file. See '-l' option in help/man page for details.")
  89. << "\n";
  90. }
  91. DownloadResult::RESULT MultiUrlRequestInfo::execute()
  92. {
  93. DownloadResult::RESULT returnValue = DownloadResult::FINISHED;
  94. try {
  95. DownloadEngineHandle e =
  96. DownloadEngineFactory().newDownloadEngine(_option.get(), _requestGroups);
  97. if(!_option->blank(PREF_LOAD_COOKIES)) {
  98. File cookieFile(_option->get(PREF_LOAD_COOKIES));
  99. if(cookieFile.isFile()) {
  100. e->getCookieStorage()->load(_option->get(PREF_LOAD_COOKIES));
  101. } else {
  102. _logger->error(MSG_LOADING_COOKIE_FAILED,
  103. _option->get(PREF_LOAD_COOKIES).c_str());
  104. }
  105. }
  106. SharedHandle<AuthConfigFactory> authConfigFactory
  107. (new AuthConfigFactory(_option.get()));
  108. File netrccf(_option->get(PREF_NETRC_PATH));
  109. if(!_option->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  110. mode_t mode = netrccf.mode();
  111. if(mode&(S_IRWXG|S_IRWXO)) {
  112. _logger->notice(MSG_INCORRECT_NETRC_PERMISSION,
  113. _option->get(PREF_NETRC_PATH).c_str());
  114. } else {
  115. SharedHandle<Netrc> netrc(new Netrc());
  116. netrc->parse(_option->get(PREF_NETRC_PATH));
  117. authConfigFactory->setNetrc(netrc);
  118. }
  119. }
  120. e->setAuthConfigFactory(authConfigFactory);
  121. #ifdef ENABLE_SSL
  122. SharedHandle<TLSContext> tlsContext(new TLSContext());
  123. if(!_option->blank(PREF_CERTIFICATE) &&
  124. !_option->blank(PREF_PRIVATE_KEY)) {
  125. tlsContext->addClientKeyFile(_option->get(PREF_CERTIFICATE),
  126. _option->get(PREF_PRIVATE_KEY));
  127. }
  128. if(!_option->blank(PREF_CA_CERTIFICATE)) {
  129. if(!tlsContext->addTrustedCACertFile(_option->get(PREF_CA_CERTIFICATE))) {
  130. _logger->warn(MSG_WARN_NO_CA_CERT);
  131. }
  132. } else if(_option->getAsBool(PREF_CHECK_CERTIFICATE)) {
  133. _logger->warn(MSG_WARN_NO_CA_CERT);
  134. }
  135. if(_option->getAsBool(PREF_CHECK_CERTIFICATE)) {
  136. tlsContext->enablePeerVerification();
  137. }
  138. SocketCore::setTLSContext(tlsContext);
  139. #endif
  140. std::string serverStatIf = _option->get(PREF_SERVER_STAT_IF);
  141. if(!serverStatIf.empty()) {
  142. e->_requestGroupMan->loadServerStat(serverStatIf);
  143. e->_requestGroupMan->removeStaleServerStat
  144. (_option->getAsInt(PREF_SERVER_STAT_TIMEOUT));
  145. }
  146. e->setStatCalc(_statCalc);
  147. e->fillCommand();
  148. // The number of simultaneous download is specified by
  149. // PREF_MAX_CONCURRENT_DOWNLOADS.
  150. // The remaining urls are queued into FillRequestGroupCommand.
  151. // It observes the number of simultaneous downloads and if it is under
  152. // the limit, it adds RequestGroup object from its queue to DownloadEngine.
  153. // This is done every 1 second. At the same time, it removes finished/error
  154. // RequestGroup from DownloadEngine.
  155. Util::setGlobalSignalHandler(SIGINT, handler, 0);
  156. Util::setGlobalSignalHandler(SIGTERM, handler, 0);
  157. e->run();
  158. if(!_option->blank(PREF_SAVE_COOKIES)) {
  159. e->getCookieStorage()->saveNsFormat(_option->get(PREF_SAVE_COOKIES));
  160. }
  161. std::string serverStatOf = _option->get(PREF_SERVER_STAT_OF);
  162. if(!serverStatOf.empty()) {
  163. e->_requestGroupMan->saveServerStat(serverStatOf);
  164. }
  165. e->_requestGroupMan->showDownloadResults(_summaryOut);
  166. _summaryOut << std::flush;
  167. RequestGroupMan::DownloadStat s = e->_requestGroupMan->getDownloadStat();
  168. if(!s.allCompleted()) {
  169. printMessageForContinue();
  170. if(s.getLastErrorResult() == DownloadResult::FINISHED &&
  171. s.getInProgress() > 0) {
  172. returnValue = DownloadResult::IN_PROGRESS;
  173. } else {
  174. returnValue = s.getLastErrorResult();
  175. }
  176. }
  177. } catch(RecoverableException& e) {
  178. _logger->error(EX_EXCEPTION_CAUGHT, e);
  179. }
  180. Util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
  181. Util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
  182. return returnValue;
  183. }
  184. } // namespace aria2