MultiUrlRequestInfo.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "DownloadContext.h"
  55. #ifdef ENABLE_SSL
  56. # include "SocketCore.h"
  57. # include "TLSContext.h"
  58. #endif // ENABLE_SSL
  59. namespace aria2 {
  60. #ifndef SA_RESETHAND
  61. # define SA_RESETHAND 0x80000000
  62. #endif // SA_RESETHAND
  63. extern volatile sig_atomic_t globalHaltRequested;
  64. static void handler(int signal) {
  65. if(globalHaltRequested == 0) {
  66. globalHaltRequested = 1;
  67. } else if(globalHaltRequested == 2) {
  68. globalHaltRequested = 3;
  69. }
  70. }
  71. MultiUrlRequestInfo::MultiUrlRequestInfo
  72. (const RequestGroups& requestGroups,
  73. const SharedHandle<Option>& op,
  74. const SharedHandle<StatCalc>& statCalc,
  75. std::ostream& summaryOut)
  76. :
  77. _requestGroups(requestGroups),
  78. _option(op),
  79. _statCalc(statCalc),
  80. _summaryOut(summaryOut),
  81. _logger(LogFactory::getInstance())
  82. {}
  83. MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
  84. void MultiUrlRequestInfo::printMessageForContinue()
  85. {
  86. _summaryOut << "\n"
  87. << _("aria2 will resume download if the transfer is restarted.")
  88. << "\n"
  89. << _("If there are any errors, then see the log file. See '-l' option in help/man page for details.")
  90. << "\n";
  91. }
  92. downloadresultcode::RESULT MultiUrlRequestInfo::execute()
  93. {
  94. downloadresultcode::RESULT returnValue = downloadresultcode::FINISHED;
  95. try {
  96. DownloadEngineHandle e =
  97. DownloadEngineFactory().newDownloadEngine(_option.get(), _requestGroups);
  98. if(!_option->blank(PREF_LOAD_COOKIES)) {
  99. File cookieFile(_option->get(PREF_LOAD_COOKIES));
  100. if(cookieFile.isFile()) {
  101. e->getCookieStorage()->load(_option->get(PREF_LOAD_COOKIES));
  102. } else {
  103. _logger->error(MSG_LOADING_COOKIE_FAILED,
  104. _option->get(PREF_LOAD_COOKIES).c_str());
  105. }
  106. }
  107. SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory());
  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. #ifdef SIGHUP
  148. util::setGlobalSignalHandler(SIGHUP, handler, 0);
  149. #endif // SIGHUP
  150. util::setGlobalSignalHandler(SIGINT, handler, 0);
  151. util::setGlobalSignalHandler(SIGTERM, handler, 0);
  152. e->run();
  153. if(!_option->blank(PREF_SAVE_COOKIES)) {
  154. e->getCookieStorage()->saveNsFormat(_option->get(PREF_SAVE_COOKIES));
  155. }
  156. std::string serverStatOf = _option->get(PREF_SERVER_STAT_OF);
  157. if(!serverStatOf.empty()) {
  158. e->_requestGroupMan->saveServerStat(serverStatOf);
  159. }
  160. e->_requestGroupMan->showDownloadResults(_summaryOut);
  161. _summaryOut << std::flush;
  162. RequestGroupMan::DownloadStat s = e->_requestGroupMan->getDownloadStat();
  163. if(!s.allCompleted()) {
  164. printMessageForContinue();
  165. if(s.getLastErrorResult() == downloadresultcode::FINISHED &&
  166. s.getInProgress() > 0) {
  167. returnValue = downloadresultcode::IN_PROGRESS;
  168. } else {
  169. returnValue = s.getLastErrorResult();
  170. }
  171. }
  172. } catch(RecoverableException& e) {
  173. _logger->error(EX_EXCEPTION_CAUGHT, e);
  174. }
  175. #ifdef SIGHUP
  176. util::setGlobalSignalHandler(SIGHUP, SIG_DFL, 0);
  177. #endif // SIGHUP
  178. util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
  179. util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
  180. return returnValue;
  181. }
  182. } // namespace aria2