MultiUrlRequestInfo.cc 9.7 KB

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