MultiUrlRequestInfo.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include "SessionSerializer.h"
  56. #include "ServerStatMan.h"
  57. #ifdef ENABLE_SSL
  58. # include "SocketCore.h"
  59. # include "TLSContext.h"
  60. #endif // ENABLE_SSL
  61. namespace aria2 {
  62. #ifndef SA_RESETHAND
  63. # define SA_RESETHAND 0x80000000
  64. #endif // SA_RESETHAND
  65. namespace global {
  66. extern volatile sig_atomic_t globalHaltRequested;
  67. } // namespace global
  68. static void handler(int signal) {
  69. if(
  70. #ifdef SIGHUP
  71. signal == SIGHUP ||
  72. #endif // SIGHUP
  73. signal == SIGTERM) {
  74. if(global::globalHaltRequested == 0 || global::globalHaltRequested == 2) {
  75. global::globalHaltRequested = 3;
  76. }
  77. } else {
  78. if(global::globalHaltRequested == 0) {
  79. global::globalHaltRequested = 1;
  80. } else if(global::globalHaltRequested == 2) {
  81. global::globalHaltRequested = 3;
  82. }
  83. }
  84. }
  85. MultiUrlRequestInfo::MultiUrlRequestInfo
  86. (const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  87. const SharedHandle<Option>& op,
  88. const SharedHandle<StatCalc>& statCalc,
  89. std::ostream& summaryOut)
  90. :
  91. _requestGroups(requestGroups),
  92. _option(op),
  93. _statCalc(statCalc),
  94. _summaryOut(summaryOut),
  95. _logger(LogFactory::getInstance())
  96. {}
  97. MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
  98. void MultiUrlRequestInfo::printMessageForContinue()
  99. {
  100. _summaryOut << "\n"
  101. << _("aria2 will resume download if the transfer is restarted.")
  102. << "\n"
  103. << _("If there are any errors, then see the log file. See '-l' option in help/man page for details.")
  104. << "\n";
  105. }
  106. downloadresultcode::RESULT MultiUrlRequestInfo::execute()
  107. {
  108. downloadresultcode::RESULT returnValue = downloadresultcode::FINISHED;
  109. try {
  110. DownloadEngineHandle e =
  111. DownloadEngineFactory().newDownloadEngine(_option.get(), _requestGroups);
  112. if(!_option->blank(PREF_LOAD_COOKIES)) {
  113. File cookieFile(_option->get(PREF_LOAD_COOKIES));
  114. if(cookieFile.isFile()) {
  115. e->getCookieStorage()->load(cookieFile.getPath());
  116. _logger->info("Loaded cookies from '%s'.",
  117. cookieFile.getPath().c_str());
  118. } else {
  119. _logger->error(MSG_LOADING_COOKIE_FAILED, cookieFile.getPath().c_str());
  120. }
  121. }
  122. SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory());
  123. File netrccf(_option->get(PREF_NETRC_PATH));
  124. if(!_option->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  125. mode_t mode = netrccf.mode();
  126. if(mode&(S_IRWXG|S_IRWXO)) {
  127. _logger->notice(MSG_INCORRECT_NETRC_PERMISSION,
  128. _option->get(PREF_NETRC_PATH).c_str());
  129. } else {
  130. SharedHandle<Netrc> netrc(new Netrc());
  131. netrc->parse(_option->get(PREF_NETRC_PATH));
  132. authConfigFactory->setNetrc(netrc);
  133. }
  134. }
  135. e->setAuthConfigFactory(authConfigFactory);
  136. #ifdef ENABLE_SSL
  137. SharedHandle<TLSContext> tlsContext(new TLSContext());
  138. if(!_option->blank(PREF_CERTIFICATE) &&
  139. !_option->blank(PREF_PRIVATE_KEY)) {
  140. tlsContext->addClientKeyFile(_option->get(PREF_CERTIFICATE),
  141. _option->get(PREF_PRIVATE_KEY));
  142. }
  143. if(!_option->blank(PREF_CA_CERTIFICATE)) {
  144. if(!tlsContext->addTrustedCACertFile(_option->get(PREF_CA_CERTIFICATE))) {
  145. _logger->warn(MSG_WARN_NO_CA_CERT);
  146. }
  147. } else if(_option->getAsBool(PREF_CHECK_CERTIFICATE)) {
  148. _logger->warn(MSG_WARN_NO_CA_CERT);
  149. }
  150. if(_option->getAsBool(PREF_CHECK_CERTIFICATE)) {
  151. tlsContext->enablePeerVerification();
  152. }
  153. SocketCore::setTLSContext(tlsContext);
  154. #endif
  155. if(!Timer::monotonicClock()) {
  156. _logger->warn("Don't change system time while aria2c is running."
  157. " Doing this may make aria2c hang for long time.");
  158. }
  159. std::string serverStatIf = _option->get(PREF_SERVER_STAT_IF);
  160. if(!serverStatIf.empty()) {
  161. e->_requestGroupMan->loadServerStat(serverStatIf);
  162. e->_requestGroupMan->removeStaleServerStat
  163. (_option->getAsInt(PREF_SERVER_STAT_TIMEOUT));
  164. }
  165. e->setStatCalc(_statCalc);
  166. #ifdef SIGHUP
  167. util::setGlobalSignalHandler(SIGHUP, handler, 0);
  168. #endif // SIGHUP
  169. util::setGlobalSignalHandler(SIGINT, handler, 0);
  170. util::setGlobalSignalHandler(SIGTERM, handler, 0);
  171. e->run();
  172. if(!_option->blank(PREF_SAVE_COOKIES)) {
  173. e->getCookieStorage()->saveNsFormat(_option->get(PREF_SAVE_COOKIES));
  174. }
  175. std::string serverStatOf = _option->get(PREF_SERVER_STAT_OF);
  176. if(!serverStatOf.empty()) {
  177. e->_requestGroupMan->saveServerStat(serverStatOf);
  178. }
  179. e->_requestGroupMan->showDownloadResults(_summaryOut);
  180. _summaryOut << std::flush;
  181. RequestGroupMan::DownloadStat s = e->_requestGroupMan->getDownloadStat();
  182. if(!s.allCompleted()) {
  183. printMessageForContinue();
  184. if(s.getLastErrorResult() == downloadresultcode::FINISHED &&
  185. s.getInProgress() > 0) {
  186. returnValue = downloadresultcode::IN_PROGRESS;
  187. } else {
  188. returnValue = s.getLastErrorResult();
  189. }
  190. }
  191. SessionSerializer sessionSerializer(e->_requestGroupMan);
  192. // TODO Add option: --save-session-status=error,inprogress,waiting
  193. if(!_option->blank(PREF_SAVE_SESSION)) {
  194. const std::string& filename = _option->get(PREF_SAVE_SESSION);
  195. if(sessionSerializer.save(filename)) {
  196. _logger->notice("Serialized session to '%s' successfully.",
  197. filename.c_str());
  198. } else {
  199. _logger->notice("Failed to serialize session to '%s'.",
  200. filename.c_str());
  201. }
  202. }
  203. } catch(RecoverableException& e) {
  204. if(returnValue == downloadresultcode::FINISHED) {
  205. returnValue = downloadresultcode::UNKNOWN_ERROR;
  206. }
  207. _logger->error(EX_EXCEPTION_CAUGHT, e);
  208. }
  209. #ifdef SIGHUP
  210. util::setGlobalSignalHandler(SIGHUP, SIG_DFL, 0);
  211. #endif // SIGHUP
  212. util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
  213. util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
  214. return returnValue;
  215. }
  216. } // namespace aria2