MultiUrlRequestInfo.cc 7.9 KB

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