MultiUrlRequestInfo.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <cstring>
  38. #include <ostream>
  39. #include "RequestGroupMan.h"
  40. #include "DownloadEngine.h"
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "RequestGroup.h"
  44. #include "prefs.h"
  45. #include "DownloadEngineFactory.h"
  46. #include "RecoverableException.h"
  47. #include "message.h"
  48. #include "util.h"
  49. #include "Option.h"
  50. #include "StatCalc.h"
  51. #include "CookieStorage.h"
  52. #include "File.h"
  53. #include "Netrc.h"
  54. #include "AuthConfigFactory.h"
  55. #include "SessionSerializer.h"
  56. #include "TimeA2.h"
  57. #include "fmt.h"
  58. #include "SocketCore.h"
  59. #include "OutputFile.h"
  60. #include "UriListParser.h"
  61. #include "SingletonHolder.h"
  62. #include "Notifier.h"
  63. #ifdef ENABLE_WEBSOCKET
  64. # include "WebSocketSessionMan.h"
  65. #else // !ENABLE_WEBSOCKET
  66. # include "NullWebSocketSessionMan.h"
  67. #endif // !ENABLE_WEBSOCKET
  68. #ifdef ENABLE_SSL
  69. # include "TLSContext.h"
  70. #endif // ENABLE_SSL
  71. #ifdef ENABLE_ASYNC_DNS
  72. #include "AsyncNameResolver.h"
  73. #endif // ENABLE_ASYNC_DNS
  74. namespace aria2 {
  75. namespace global {
  76. extern volatile sig_atomic_t globalHaltRequested;
  77. } // namespace global
  78. namespace {
  79. void handler(int signal) {
  80. if(
  81. #ifdef SIGHUP
  82. signal == SIGHUP ||
  83. #endif // SIGHUP
  84. signal == SIGTERM) {
  85. if(global::globalHaltRequested == 0 || global::globalHaltRequested == 2) {
  86. global::globalHaltRequested = 3;
  87. }
  88. } else {
  89. if(global::globalHaltRequested == 0) {
  90. global::globalHaltRequested = 1;
  91. } else if(global::globalHaltRequested == 2) {
  92. global::globalHaltRequested = 3;
  93. }
  94. }
  95. }
  96. } // namespace
  97. MultiUrlRequestInfo::MultiUrlRequestInfo
  98. (std::vector<SharedHandle<RequestGroup> >& requestGroups,
  99. const SharedHandle<Option>& op,
  100. const SharedHandle<StatCalc>& statCalc,
  101. const SharedHandle<OutputFile>& summaryOut,
  102. const SharedHandle<UriListParser>& uriListParser)
  103. : requestGroups_(requestGroups),
  104. option_(op),
  105. statCalc_(statCalc),
  106. summaryOut_(summaryOut),
  107. uriListParser_(uriListParser)
  108. {}
  109. MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
  110. void MultiUrlRequestInfo::printMessageForContinue()
  111. {
  112. summaryOut_->printf
  113. ("\n%s\n%s\n",
  114. _("aria2 will resume download if the transfer is restarted."),
  115. _("If there are any errors, then see the log file. See '-l' option in help/man page for details."));
  116. }
  117. error_code::Value MultiUrlRequestInfo::execute()
  118. {
  119. error_code::Value returnValue = error_code::FINISHED;
  120. sigset_t mask;
  121. try {
  122. SharedHandle<rpc::WebSocketSessionMan> wsSessionMan;
  123. if(option_->getAsBool(PREF_ENABLE_RPC)) {
  124. wsSessionMan.reset(new rpc::WebSocketSessionMan());
  125. }
  126. Notifier notifier(wsSessionMan);
  127. SingletonHolder<Notifier>::instance(&notifier);
  128. #ifdef ENABLE_SSL
  129. if(option_->getAsBool(PREF_ENABLE_RPC) &&
  130. option_->getAsBool(PREF_RPC_SECURE)) {
  131. if(!option_->blank(PREF_RPC_CERTIFICATE)
  132. #ifndef HAVE_APPLETLS
  133. && !option_->blank(PREF_RPC_PRIVATE_KEY)
  134. #endif // HAVE_APPLETLS
  135. ) {
  136. // We set server TLS context to the SocketCore before creating
  137. // DownloadEngine instance.
  138. SharedHandle<TLSContext> svTlsContext(TLSContext::make(TLS_SERVER));
  139. svTlsContext->addCredentialFile(option_->get(PREF_RPC_CERTIFICATE),
  140. option_->get(PREF_RPC_PRIVATE_KEY));
  141. SocketCore::setServerTLSContext(svTlsContext);
  142. } else {
  143. throw DL_ABORT_EX("Specify --rpc-certificate and --rpc-private-key "
  144. "options in order to use secure RPC.");
  145. }
  146. }
  147. #endif // ENABLE_SSL
  148. SharedHandle<DownloadEngine> e =
  149. DownloadEngineFactory().newDownloadEngine(option_.get(), requestGroups_);
  150. // Avoid keeping RequestGroups alive longer than necessary
  151. requestGroups_.clear();
  152. if(!option_->blank(PREF_LOAD_COOKIES)) {
  153. File cookieFile(option_->get(PREF_LOAD_COOKIES));
  154. if(cookieFile.isFile() &&
  155. e->getCookieStorage()->load(cookieFile.getPath(), Time().getTime())) {
  156. A2_LOG_INFO(fmt("Loaded cookies from '%s'.",
  157. cookieFile.getPath().c_str()));
  158. } else {
  159. A2_LOG_ERROR(fmt(MSG_LOADING_COOKIE_FAILED,
  160. cookieFile.getPath().c_str()));
  161. }
  162. }
  163. SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory());
  164. File netrccf(option_->get(PREF_NETRC_PATH));
  165. if(!option_->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
  166. #ifdef __MINGW32__
  167. // Windows OS does not have permission, so set it to 0.
  168. mode_t mode = 0;
  169. #else // !__MINGW32__
  170. mode_t mode = netrccf.mode();
  171. #endif // !__MINGW32__
  172. if(mode&(S_IRWXG|S_IRWXO)) {
  173. A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION,
  174. option_->get(PREF_NETRC_PATH).c_str()));
  175. } else {
  176. SharedHandle<Netrc> netrc(new Netrc());
  177. netrc->parse(option_->get(PREF_NETRC_PATH));
  178. authConfigFactory->setNetrc(netrc);
  179. }
  180. }
  181. e->setAuthConfigFactory(authConfigFactory);
  182. #ifdef ENABLE_SSL
  183. SharedHandle<TLSContext> clTlsContext(TLSContext::make(TLS_CLIENT));
  184. if(!option_->blank(PREF_CERTIFICATE) &&
  185. !option_->blank(PREF_PRIVATE_KEY)) {
  186. clTlsContext->addCredentialFile(option_->get(PREF_CERTIFICATE),
  187. option_->get(PREF_PRIVATE_KEY));
  188. }
  189. if(!option_->blank(PREF_CA_CERTIFICATE)) {
  190. if(!clTlsContext->addTrustedCACertFile
  191. (option_->get(PREF_CA_CERTIFICATE))) {
  192. A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
  193. }
  194. } else if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
  195. if(!clTlsContext->addSystemTrustedCACerts()) {
  196. A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
  197. }
  198. }
  199. clTlsContext->setVerifyPeer(option_->getAsBool(PREF_CHECK_CERTIFICATE));
  200. SocketCore::setClientTLSContext(clTlsContext);
  201. #endif
  202. #ifdef HAVE_ARES_ADDR_NODE
  203. ares_addr_node* asyncDNSServers =
  204. parseAsyncDNSServers(option_->get(PREF_ASYNC_DNS_SERVER));
  205. e->setAsyncDNSServers(asyncDNSServers);
  206. #endif // HAVE_ARES_ADDR_NODE
  207. if(!Timer::monotonicClock()) {
  208. A2_LOG_WARN("Don't change system time while aria2c is running."
  209. " Doing this may make aria2c hang for long time.");
  210. }
  211. std::string serverStatIf = option_->get(PREF_SERVER_STAT_IF);
  212. if(!serverStatIf.empty()) {
  213. e->getRequestGroupMan()->loadServerStat(serverStatIf);
  214. e->getRequestGroupMan()->removeStaleServerStat
  215. (option_->getAsInt(PREF_SERVER_STAT_TIMEOUT));
  216. }
  217. e->setStatCalc(statCalc_);
  218. if(uriListParser_) {
  219. e->getRequestGroupMan()->setUriListParser(uriListParser_);
  220. }
  221. #ifdef HAVE_SIGACTION
  222. sigemptyset(&mask);
  223. sigaddset(&mask, SIGINT);
  224. sigaddset(&mask, SIGTERM);
  225. #ifdef SIGHUP
  226. sigaddset(&mask, SIGHUP);
  227. #endif // SIGHUP
  228. #else // !HAVE_SIGACTION
  229. mask = 0;
  230. #endif // !HAVE_SIGACTION
  231. #ifdef SIGHUP
  232. util::setGlobalSignalHandler(SIGHUP, &mask, handler, 0);
  233. #endif // SIGHUP
  234. util::setGlobalSignalHandler(SIGINT, &mask, handler, 0);
  235. util::setGlobalSignalHandler(SIGTERM, &mask, handler, 0);
  236. e->getRequestGroupMan()->getNetStat().downloadStart();
  237. e->run();
  238. if(!option_->blank(PREF_SAVE_COOKIES)) {
  239. e->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
  240. }
  241. const std::string& serverStatOf = option_->get(PREF_SERVER_STAT_OF);
  242. if(!serverStatOf.empty()) {
  243. e->getRequestGroupMan()->saveServerStat(serverStatOf);
  244. }
  245. e->getRequestGroupMan()->showDownloadResults
  246. (*summaryOut_, option_->get(PREF_DOWNLOAD_RESULT) == A2_V_FULL);
  247. summaryOut_->flush();
  248. RequestGroupMan::DownloadStat s =
  249. e->getRequestGroupMan()->getDownloadStat();
  250. if(!s.allCompleted()) {
  251. printMessageForContinue();
  252. if(s.getLastErrorResult() == error_code::FINISHED &&
  253. s.getInProgress() > 0) {
  254. returnValue = error_code::IN_PROGRESS;
  255. } else {
  256. returnValue = s.getLastErrorResult();
  257. }
  258. }
  259. SessionSerializer sessionSerializer(e->getRequestGroupMan());
  260. // TODO Add option: --save-session-status=error,inprogress,waiting
  261. if(!option_->blank(PREF_SAVE_SESSION)) {
  262. const std::string& filename = option_->get(PREF_SAVE_SESSION);
  263. if(sessionSerializer.save(filename)) {
  264. A2_LOG_NOTICE(fmt(_("Serialized session to '%s' successfully."),
  265. filename.c_str()));
  266. } else {
  267. A2_LOG_NOTICE(fmt(_("Failed to serialize session to '%s'."),
  268. filename.c_str()));
  269. }
  270. }
  271. } catch(RecoverableException& e) {
  272. if(returnValue == error_code::FINISHED) {
  273. returnValue = error_code::UNKNOWN_ERROR;
  274. }
  275. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  276. }
  277. SingletonHolder<Notifier>::instance(0);
  278. #ifdef HAVE_SIGACTION
  279. sigemptyset(&mask);
  280. #endif // HAVE_SIGACTION
  281. #ifdef SIGHUP
  282. util::setGlobalSignalHandler(SIGHUP, &mask, SIG_DFL, 0);
  283. #endif // SIGHUP
  284. util::setGlobalSignalHandler(SIGINT, &mask, SIG_DFL, 0);
  285. util::setGlobalSignalHandler(SIGTERM, &mask, SIG_DFL, 0);
  286. return returnValue;
  287. }
  288. } // namespace aria2