MultiUrlRequestInfo.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #ifdef ENABLE_SSL
  60. # include "TLSContext.h"
  61. #endif // ENABLE_SSL
  62. namespace aria2 {
  63. namespace global {
  64. extern volatile sig_atomic_t globalHaltRequested;
  65. } // namespace global
  66. namespace {
  67. void handler(int signal) {
  68. if(
  69. #ifdef SIGHUP
  70. signal == SIGHUP ||
  71. #endif // SIGHUP
  72. signal == SIGTERM) {
  73. if(global::globalHaltRequested == 0 || global::globalHaltRequested == 2) {
  74. global::globalHaltRequested = 3;
  75. }
  76. } else {
  77. if(global::globalHaltRequested == 0) {
  78. global::globalHaltRequested = 1;
  79. } else if(global::globalHaltRequested == 2) {
  80. global::globalHaltRequested = 3;
  81. }
  82. }
  83. }
  84. } // namespace
  85. #ifdef HAVE_ARES_ADDR_NODE
  86. namespace {
  87. ares_addr_node* parseAsyncDNSServers(const std::string& serversOpt)
  88. {
  89. std::vector<std::string> servers;
  90. util::split(serversOpt,
  91. std::back_inserter(servers),
  92. A2STR::COMMA_C,
  93. true /* doStrip */);
  94. ares_addr_node root;
  95. root.next = 0;
  96. ares_addr_node* tail = &root;
  97. for(std::vector<std::string>::const_iterator i = servers.begin(),
  98. eoi = servers.end(); i != eoi; ++i) {
  99. struct addrinfo* res;
  100. int s = callGetaddrinfo(&res, (*i).c_str(), 0, AF_UNSPEC,
  101. 0, AI_NUMERICHOST, 0);
  102. if(s != 0) {
  103. continue;
  104. }
  105. WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
  106. if(res) {
  107. ares_addr_node* node = new ares_addr_node();
  108. node->next = 0;
  109. node->family = res->ai_family;
  110. if(node->family == AF_INET) {
  111. struct sockaddr_in* in =
  112. reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
  113. memcpy(&node->addr.addr4, &(in->sin_addr), 4);
  114. } else {
  115. struct sockaddr_in6* in =
  116. reinterpret_cast<struct sockaddr_in6*>(res->ai_addr);
  117. memcpy(&node->addr.addr6, &(in->sin6_addr), 16);
  118. }
  119. tail->next = node;
  120. tail = node;
  121. }
  122. }
  123. return root.next;
  124. }
  125. } // namespace
  126. #endif // HAVE_ARES_ADDR_NODE
  127. MultiUrlRequestInfo::MultiUrlRequestInfo
  128. (const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  129. const SharedHandle<Option>& op,
  130. const SharedHandle<StatCalc>& statCalc,
  131. std::ostream& summaryOut)
  132. : requestGroups_(requestGroups),
  133. option_(op),
  134. statCalc_(statCalc),
  135. summaryOut_(summaryOut)
  136. {}
  137. MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
  138. void MultiUrlRequestInfo::printMessageForContinue()
  139. {
  140. summaryOut_ << "\n"
  141. << _("aria2 will resume download if the transfer is restarted.")
  142. << "\n"
  143. << _("If there are any errors, then see the log file. See '-l' option in help/man page for details.")
  144. << "\n";
  145. }
  146. error_code::Value MultiUrlRequestInfo::execute()
  147. {
  148. error_code::Value returnValue = error_code::FINISHED;
  149. try {
  150. DownloadEngineHandle e =
  151. DownloadEngineFactory().newDownloadEngine(option_.get(), requestGroups_);
  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> tlsContext(new TLSContext());
  184. if(!option_->blank(PREF_CERTIFICATE) &&
  185. !option_->blank(PREF_PRIVATE_KEY)) {
  186. tlsContext->addClientKeyFile(option_->get(PREF_CERTIFICATE),
  187. option_->get(PREF_PRIVATE_KEY));
  188. }
  189. if(!option_->blank(PREF_CA_CERTIFICATE)) {
  190. if(!tlsContext->addTrustedCACertFile(option_->get(PREF_CA_CERTIFICATE))) {
  191. A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
  192. }
  193. } else if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
  194. A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
  195. }
  196. if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
  197. tlsContext->enablePeerVerification();
  198. }
  199. SocketCore::setTLSContext(tlsContext);
  200. #endif
  201. #ifdef HAVE_ARES_ADDR_NODE
  202. ares_addr_node* asyncDNSServers =
  203. parseAsyncDNSServers(option_->get(PREF_ASYNC_DNS_SERVER));
  204. e->setAsyncDNSServers(asyncDNSServers);
  205. #endif // HAVE_ARES_ADDR_NODE
  206. if(!Timer::monotonicClock()) {
  207. A2_LOG_WARN("Don't change system time while aria2c is running."
  208. " Doing this may make aria2c hang for long time.");
  209. }
  210. std::string serverStatIf = option_->get(PREF_SERVER_STAT_IF);
  211. if(!serverStatIf.empty()) {
  212. e->getRequestGroupMan()->loadServerStat(serverStatIf);
  213. e->getRequestGroupMan()->removeStaleServerStat
  214. (option_->getAsInt(PREF_SERVER_STAT_TIMEOUT));
  215. }
  216. e->setStatCalc(statCalc_);
  217. #ifdef SIGHUP
  218. util::setGlobalSignalHandler(SIGHUP, handler, 0);
  219. #endif // SIGHUP
  220. util::setGlobalSignalHandler(SIGINT, handler, 0);
  221. util::setGlobalSignalHandler(SIGTERM, handler, 0);
  222. e->run();
  223. if(!option_->blank(PREF_SAVE_COOKIES)) {
  224. e->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
  225. }
  226. const std::string& serverStatOf = option_->get(PREF_SERVER_STAT_OF);
  227. if(!serverStatOf.empty()) {
  228. e->getRequestGroupMan()->saveServerStat(serverStatOf);
  229. }
  230. e->getRequestGroupMan()->showDownloadResults(summaryOut_);
  231. summaryOut_ << std::flush;
  232. RequestGroupMan::DownloadStat s =
  233. e->getRequestGroupMan()->getDownloadStat();
  234. if(!s.allCompleted()) {
  235. printMessageForContinue();
  236. if(s.getLastErrorResult() == error_code::FINISHED &&
  237. s.getInProgress() > 0) {
  238. returnValue = error_code::IN_PROGRESS;
  239. } else {
  240. returnValue = s.getLastErrorResult();
  241. }
  242. }
  243. SessionSerializer sessionSerializer(e->getRequestGroupMan());
  244. // TODO Add option: --save-session-status=error,inprogress,waiting
  245. if(!option_->blank(PREF_SAVE_SESSION)) {
  246. const std::string& filename = option_->get(PREF_SAVE_SESSION);
  247. if(sessionSerializer.save(filename)) {
  248. A2_LOG_NOTICE(fmt("Serialized session to '%s' successfully.",
  249. filename.c_str()));
  250. } else {
  251. A2_LOG_NOTICE(fmt("Failed to serialize session to '%s'.",
  252. filename.c_str()));
  253. }
  254. }
  255. } catch(RecoverableException& e) {
  256. if(returnValue == error_code::FINISHED) {
  257. returnValue = error_code::UNKNOWN_ERROR;
  258. }
  259. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  260. }
  261. #ifdef SIGHUP
  262. util::setGlobalSignalHandler(SIGHUP, SIG_DFL, 0);
  263. #endif // SIGHUP
  264. util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
  265. util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
  266. return returnValue;
  267. }
  268. } // namespace aria2