2007-05-20 13:57:56 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-05-20 13:57:56 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "MultiUrlRequestInfo.h"
|
2008-11-03 08:18:58 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroup.h"
|
2007-05-20 13:57:56 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "DownloadEngineFactory.h"
|
|
|
|
#include "RecoverableException.h"
|
|
|
|
#include "message.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-04-22 10:29:48 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "StatCalc.h"
|
2008-09-01 13:46:03 +00:00
|
|
|
#include "CookieStorage.h"
|
|
|
|
#include "File.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "Netrc.h"
|
|
|
|
#include "AuthConfigFactory.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2010-04-08 12:54:14 +00:00
|
|
|
#include "SessionSerializer.h"
|
|
|
|
#include "ServerStatMan.h"
|
2008-11-08 13:21:32 +00:00
|
|
|
#ifdef ENABLE_SSL
|
|
|
|
# include "SocketCore.h"
|
|
|
|
# include "TLSContext.h"
|
|
|
|
#endif // ENABLE_SSL
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#ifndef SA_RESETHAND
|
|
|
|
# define SA_RESETHAND 0x80000000
|
|
|
|
#endif // SA_RESETHAND
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
namespace global {
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
extern volatile sig_atomic_t globalHaltRequested;
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
} // namespace global
|
|
|
|
|
2007-05-20 13:57:56 +00:00
|
|
|
static void handler(int signal) {
|
2010-03-06 08:29:53 +00:00
|
|
|
if(global::globalHaltRequested == 0) {
|
|
|
|
global::globalHaltRequested = 1;
|
|
|
|
} else if(global::globalHaltRequested == 2) {
|
|
|
|
global::globalHaltRequested = 3;
|
2007-12-07 13:33:59 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 10:29:48 +00:00
|
|
|
MultiUrlRequestInfo::MultiUrlRequestInfo
|
2010-02-28 12:30:11 +00:00
|
|
|
(const std::vector<SharedHandle<RequestGroup> >& requestGroups,
|
2009-05-08 16:41:21 +00:00
|
|
|
const SharedHandle<Option>& op,
|
2008-04-22 10:29:48 +00:00
|
|
|
const SharedHandle<StatCalc>& statCalc,
|
|
|
|
std::ostream& summaryOut)
|
|
|
|
:
|
2007-10-11 16:58:24 +00:00
|
|
|
_requestGroups(requestGroups),
|
|
|
|
_option(op),
|
2008-04-22 10:29:48 +00:00
|
|
|
_statCalc(statCalc),
|
|
|
|
_summaryOut(summaryOut),
|
2007-10-11 16:58:24 +00:00
|
|
|
_logger(LogFactory::getInstance())
|
|
|
|
{}
|
|
|
|
|
|
|
|
MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
|
|
|
|
|
2007-10-13 15:47:22 +00:00
|
|
|
void MultiUrlRequestInfo::printMessageForContinue()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-04-22 10:29:48 +00:00
|
|
|
_summaryOut << "\n"
|
2010-01-05 16:01:46 +00:00
|
|
|
<< _("aria2 will resume download if the transfer is restarted.")
|
|
|
|
<< "\n"
|
|
|
|
<< _("If there are any errors, then see the log file. See '-l' option in help/man page for details.")
|
|
|
|
<< "\n";
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 08:42:58 +00:00
|
|
|
downloadresultcode::RESULT MultiUrlRequestInfo::execute()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2009-06-29 08:42:58 +00:00
|
|
|
downloadresultcode::RESULT returnValue = downloadresultcode::FINISHED;
|
2007-05-20 13:57:56 +00:00
|
|
|
try {
|
2007-10-11 16:58:24 +00:00
|
|
|
DownloadEngineHandle e =
|
2009-05-08 16:41:21 +00:00
|
|
|
DownloadEngineFactory().newDownloadEngine(_option.get(), _requestGroups);
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2009-06-21 10:26:14 +00:00
|
|
|
if(!_option->blank(PREF_LOAD_COOKIES)) {
|
|
|
|
File cookieFile(_option->get(PREF_LOAD_COOKIES));
|
|
|
|
if(cookieFile.isFile()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
e->getCookieStorage()->load(_option->get(PREF_LOAD_COOKIES));
|
2009-06-21 10:26:14 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
_logger->error(MSG_LOADING_COOKIE_FAILED,
|
|
|
|
_option->get(PREF_LOAD_COOKIES).c_str());
|
2008-09-01 13:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-14 12:37:34 +00:00
|
|
|
SharedHandle<AuthConfigFactory> authConfigFactory(new AuthConfigFactory());
|
2008-11-03 10:06:25 +00:00
|
|
|
File netrccf(_option->get(PREF_NETRC_PATH));
|
|
|
|
if(!_option->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
|
|
|
|
mode_t mode = netrccf.mode();
|
|
|
|
if(mode&(S_IRWXG|S_IRWXO)) {
|
2010-01-05 16:01:46 +00:00
|
|
|
_logger->notice(MSG_INCORRECT_NETRC_PERMISSION,
|
|
|
|
_option->get(PREF_NETRC_PATH).c_str());
|
2008-11-03 10:06:25 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
SharedHandle<Netrc> netrc(new Netrc());
|
|
|
|
netrc->parse(_option->get(PREF_NETRC_PATH));
|
|
|
|
authConfigFactory->setNetrc(netrc);
|
2008-11-03 10:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e->setAuthConfigFactory(authConfigFactory);
|
|
|
|
|
2008-11-08 13:21:32 +00:00
|
|
|
#ifdef ENABLE_SSL
|
|
|
|
SharedHandle<TLSContext> tlsContext(new TLSContext());
|
2008-12-04 13:05:05 +00:00
|
|
|
if(!_option->blank(PREF_CERTIFICATE) &&
|
|
|
|
!_option->blank(PREF_PRIVATE_KEY)) {
|
2008-11-08 13:21:32 +00:00
|
|
|
tlsContext->addClientKeyFile(_option->get(PREF_CERTIFICATE),
|
2010-01-05 16:01:46 +00:00
|
|
|
_option->get(PREF_PRIVATE_KEY));
|
2008-11-08 13:21:32 +00:00
|
|
|
}
|
2008-12-04 13:05:05 +00:00
|
|
|
if(!_option->blank(PREF_CA_CERTIFICATE)) {
|
2009-06-21 10:41:50 +00:00
|
|
|
if(!tlsContext->addTrustedCACertFile(_option->get(PREF_CA_CERTIFICATE))) {
|
2010-01-05 16:01:46 +00:00
|
|
|
_logger->warn(MSG_WARN_NO_CA_CERT);
|
2008-12-03 16:07:14 +00:00
|
|
|
}
|
|
|
|
} else if(_option->getAsBool(PREF_CHECK_CERTIFICATE)) {
|
|
|
|
_logger->warn(MSG_WARN_NO_CA_CERT);
|
2008-11-09 07:36:44 +00:00
|
|
|
}
|
2008-12-03 16:07:14 +00:00
|
|
|
|
2008-11-09 07:36:44 +00:00
|
|
|
if(_option->getAsBool(PREF_CHECK_CERTIFICATE)) {
|
|
|
|
tlsContext->enablePeerVerification();
|
|
|
|
}
|
2008-11-08 13:21:32 +00:00
|
|
|
SocketCore::setTLSContext(tlsContext);
|
|
|
|
#endif
|
2010-04-12 13:05:41 +00:00
|
|
|
if(!Timer::monotonicClock()) {
|
|
|
|
_logger->warn("Don't change system time while aria2c is running."
|
|
|
|
" Doing this may make aria2c hang for long time.");
|
|
|
|
}
|
2008-11-08 13:21:32 +00:00
|
|
|
|
2008-08-10 15:22:55 +00:00
|
|
|
std::string serverStatIf = _option->get(PREF_SERVER_STAT_IF);
|
|
|
|
if(!serverStatIf.empty()) {
|
|
|
|
e->_requestGroupMan->loadServerStat(serverStatIf);
|
|
|
|
e->_requestGroupMan->removeStaleServerStat
|
2010-01-05 16:01:46 +00:00
|
|
|
(_option->getAsInt(PREF_SERVER_STAT_TIMEOUT));
|
2008-08-10 15:22:55 +00:00
|
|
|
}
|
2008-04-22 10:29:48 +00:00
|
|
|
e->setStatCalc(_statCalc);
|
2010-01-09 13:06:45 +00:00
|
|
|
#ifdef SIGHUP
|
2009-12-26 13:18:23 +00:00
|
|
|
util::setGlobalSignalHandler(SIGHUP, handler, 0);
|
2010-01-09 13:06:45 +00:00
|
|
|
#endif // SIGHUP
|
2009-10-22 15:09:00 +00:00
|
|
|
util::setGlobalSignalHandler(SIGINT, handler, 0);
|
|
|
|
util::setGlobalSignalHandler(SIGTERM, handler, 0);
|
2007-05-20 13:57:56 +00:00
|
|
|
|
|
|
|
e->run();
|
|
|
|
|
2009-05-22 14:51:57 +00:00
|
|
|
if(!_option->blank(PREF_SAVE_COOKIES)) {
|
2009-06-21 10:26:14 +00:00
|
|
|
e->getCookieStorage()->saveNsFormat(_option->get(PREF_SAVE_COOKIES));
|
2009-05-22 14:51:57 +00:00
|
|
|
}
|
|
|
|
|
2008-08-10 15:22:55 +00:00
|
|
|
std::string serverStatOf = _option->get(PREF_SERVER_STAT_OF);
|
|
|
|
if(!serverStatOf.empty()) {
|
|
|
|
e->_requestGroupMan->saveServerStat(serverStatOf);
|
|
|
|
}
|
2008-04-22 10:29:48 +00:00
|
|
|
e->_requestGroupMan->showDownloadResults(_summaryOut);
|
|
|
|
_summaryOut << std::flush;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-20 16:46:56 +00:00
|
|
|
RequestGroupMan::DownloadStat s = e->_requestGroupMan->getDownloadStat();
|
2009-01-12 12:27:34 +00:00
|
|
|
if(!s.allCompleted()) {
|
2007-10-13 15:47:22 +00:00
|
|
|
printMessageForContinue();
|
2009-06-29 08:42:58 +00:00
|
|
|
if(s.getLastErrorResult() == downloadresultcode::FINISHED &&
|
2010-01-05 16:01:46 +00:00
|
|
|
s.getInProgress() > 0) {
|
|
|
|
returnValue = downloadresultcode::IN_PROGRESS;
|
2009-01-12 12:27:34 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
returnValue = s.getLastErrorResult();
|
2009-01-12 12:27:34 +00:00
|
|
|
}
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2010-04-08 12:54:14 +00:00
|
|
|
SessionSerializer sessionSerializer(e->_requestGroupMan);
|
|
|
|
// TODO Add option: --save-session-status=error,inprogress,waiting
|
|
|
|
if(!_option->blank(PREF_SAVE_SESSION)) {
|
|
|
|
if(sessionSerializer.save(_option->get(PREF_SAVE_SESSION))) {
|
|
|
|
_logger->notice("Serialized session successfully.");
|
|
|
|
} else {
|
|
|
|
_logger->notice("Failed to serialize session.");
|
|
|
|
}
|
|
|
|
}
|
2008-11-03 10:06:25 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2010-02-12 13:53:52 +00:00
|
|
|
if(returnValue == downloadresultcode::FINISHED) {
|
|
|
|
returnValue = downloadresultcode::UNKNOWN_ERROR;
|
|
|
|
}
|
2008-11-03 10:06:25 +00:00
|
|
|
_logger->error(EX_EXCEPTION_CAUGHT, e);
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2010-01-09 13:06:45 +00:00
|
|
|
#ifdef SIGHUP
|
2009-12-26 13:18:23 +00:00
|
|
|
util::setGlobalSignalHandler(SIGHUP, SIG_DFL, 0);
|
2010-01-09 13:06:45 +00:00
|
|
|
#endif // SIGHUP
|
2009-10-22 15:09:00 +00:00
|
|
|
util::setGlobalSignalHandler(SIGINT, SIG_DFL, 0);
|
|
|
|
util::setGlobalSignalHandler(SIGTERM, SIG_DFL, 0);
|
2008-02-20 16:46:56 +00:00
|
|
|
return returnValue;
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|