summaryrefslogtreecommitdiff
path: root/src/MultiUrlRequestInfo.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/MultiUrlRequestInfo.cc')
-rw-r--r--src/MultiUrlRequestInfo.cc209
1 files changed, 108 insertions, 101 deletions
diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc
index 82df08d5..3ba739f6 100644
--- a/src/MultiUrlRequestInfo.cc
+++ b/src/MultiUrlRequestInfo.cc
@@ -66,12 +66,12 @@
#include "Notifier.h"
#include "console.h"
#ifdef ENABLE_WEBSOCKET
-# include "WebSocketSessionMan.h"
+#include "WebSocketSessionMan.h"
#else // !ENABLE_WEBSOCKET
-# include "NullWebSocketSessionMan.h"
+#include "NullWebSocketSessionMan.h"
#endif // !ENABLE_WEBSOCKET
#ifdef ENABLE_SSL
-# include "TLSContext.h"
+#include "TLSContext.h"
#endif // ENABLE_SSL
#ifdef ENABLE_ASYNC_DNS
#include "AsyncNameResolver.h"
@@ -93,12 +93,12 @@ static const DWORD mainThread = GetCurrentThreadId();
static void handler(int signal)
{
- if(
+ if (
#ifdef SIGHUP
- signal == SIGHUP ||
+ signal == SIGHUP ||
#endif // SIGHUP
- signal == SIGTERM) {
- if(global::globalHaltRequested <= 2) {
+ signal == SIGTERM) {
+ if (global::globalHaltRequested <= 2) {
global::globalHaltRequested = 3;
}
#ifdef _WIN32
@@ -132,7 +132,7 @@ namespace {
std::unique_ptr<StatCalc> getStatCalc(const std::shared_ptr<Option>& op)
{
- if(op->getAsBool(PREF_QUIET)) {
+ if (op->getAsBool(PREF_QUIET)) {
return make_unique<NullStatCalc>();
}
auto impl = make_unique<ConsoleStatCalc>(
@@ -145,18 +145,18 @@ std::unique_ptr<StatCalc> getStatCalc(const std::shared_ptr<Option>& op)
} // namespace
-MultiUrlRequestInfo::MultiUrlRequestInfo
-(std::vector<std::shared_ptr<RequestGroup> > requestGroups,
- const std::shared_ptr<Option>& op,
- const std::shared_ptr<UriListParser>& uriListParser)
- : requestGroups_(std::move(requestGroups)),
- option_(op),
- uriListParser_(uriListParser),
- useSignalHandler_(true)
+MultiUrlRequestInfo::MultiUrlRequestInfo(
+ std::vector<std::shared_ptr<RequestGroup>> requestGroups,
+ const std::shared_ptr<Option>& op,
+ const std::shared_ptr<UriListParser>& uriListParser)
+ : requestGroups_(std::move(requestGroups)),
+ option_(op),
+ uriListParser_(uriListParser),
+ useSignalHandler_(true)
{
#ifdef HAVE_SIGACTION
sigemptyset(&mask_);
-#else // !HAVE_SIGACTION
+#else // !HAVE_SIGACTION
mask_ = 0;
#endif // !HAVE_SIGACTION
}
@@ -165,11 +165,12 @@ MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
void MultiUrlRequestInfo::printMessageForContinue()
{
- if(!option_->getAsBool(PREF_QUIET)) {
- global::cout()->printf
- ("\n%s\n%s\n",
- _("aria2 will resume download if the transfer is restarted."),
- _("If there are any errors, then see the log file. See '-l' option in help/man page for details."));
+ if (!option_->getAsBool(PREF_QUIET)) {
+ global::cout()->printf(
+ "\n%s\n%s\n",
+ _("aria2 will resume download if the transfer is restarted."),
+ _("If there are any errors, then see the log file. See '-l' option in "
+ "help/man page for details."));
}
}
@@ -180,20 +181,20 @@ int MultiUrlRequestInfo::prepare()
SingletonHolder<Notifier>::instance(make_unique<Notifier>());
#ifdef ENABLE_SSL
- if(option_->getAsBool(PREF_ENABLE_RPC) &&
- option_->getAsBool(PREF_RPC_SECURE)) {
- if(option_->blank(PREF_RPC_CERTIFICATE)) {
+ if (option_->getAsBool(PREF_ENABLE_RPC) &&
+ option_->getAsBool(PREF_RPC_SECURE)) {
+ if (option_->blank(PREF_RPC_CERTIFICATE)) {
throw DL_ABORT_EX("Specify --rpc-certificate and --rpc-private-key "
"options in order to use secure RPC.");
}
// We set server TLS context to the SocketCore before creating
// DownloadEngine instance.
auto minTLSVer = util::toTLSVersion(option_->get(PREF_MIN_TLS_VERSION));
- std::shared_ptr<TLSContext> svTlsContext
- (TLSContext::make(TLS_SERVER, minTLSVer));
- if(!svTlsContext->addCredentialFile
- (option_->get(PREF_RPC_CERTIFICATE),
- option_->get(PREF_RPC_PRIVATE_KEY))) {
+ std::shared_ptr<TLSContext> svTlsContext(
+ TLSContext::make(TLS_SERVER, minTLSVer));
+ if (!svTlsContext->addCredentialFile(
+ option_->get(PREF_RPC_CERTIFICATE),
+ option_->get(PREF_RPC_PRIVATE_KEY))) {
throw DL_ABORT_EX("Loading private key and/or certificate for secure "
"RPC failed.");
}
@@ -206,39 +207,41 @@ int MultiUrlRequestInfo::prepare()
std::move(requestGroups_));
#ifdef ENABLE_WEBSOCKET
- if(option_->getAsBool(PREF_ENABLE_RPC)) {
+ if (option_->getAsBool(PREF_ENABLE_RPC)) {
e_->setWebSocketSessionMan(make_unique<rpc::WebSocketSessionMan>());
- SingletonHolder<Notifier>::instance()->addDownloadEventListener
- (e_->getWebSocketSessionMan().get());
+ SingletonHolder<Notifier>::instance()->addDownloadEventListener(
+ e_->getWebSocketSessionMan().get());
}
#endif // ENABLE_WEBSOCKET
- if(!option_->blank(PREF_LOAD_COOKIES)) {
+ if (!option_->blank(PREF_LOAD_COOKIES)) {
File cookieFile(option_->get(PREF_LOAD_COOKIES));
- if(cookieFile.isFile() &&
- e_->getCookieStorage()->load(cookieFile.getPath(),
- Time().getTimeFromEpoch())) {
- A2_LOG_INFO(fmt("Loaded cookies from '%s'.",
- cookieFile.getPath().c_str()));
- } else {
- A2_LOG_ERROR(fmt(MSG_LOADING_COOKIE_FAILED,
- cookieFile.getPath().c_str()));
+ if (cookieFile.isFile() &&
+ e_->getCookieStorage()->load(cookieFile.getPath(),
+ Time().getTimeFromEpoch())) {
+ A2_LOG_INFO(
+ fmt("Loaded cookies from '%s'.", cookieFile.getPath().c_str()));
+ }
+ else {
+ A2_LOG_ERROR(
+ fmt(MSG_LOADING_COOKIE_FAILED, cookieFile.getPath().c_str()));
}
}
auto authConfigFactory = make_unique<AuthConfigFactory>();
File netrccf(option_->get(PREF_NETRC_PATH));
- if(!option_->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
+ if (!option_->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
#ifdef __MINGW32__
// Windows OS does not have permission, so set it to 0.
- mode_t mode = 0;
-#else // !__MINGW32__
+ mode_t mode = 0;
+#else // !__MINGW32__
mode_t mode = netrccf.mode();
#endif // !__MINGW32__
- if(mode&(S_IRWXG|S_IRWXO)) {
+ if (mode & (S_IRWXG | S_IRWXO)) {
A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION,
option_->get(PREF_NETRC_PATH).c_str()));
- } else {
+ }
+ else {
auto netrc = make_unique<Netrc>();
netrc->parse(option_->get(PREF_NETRC_PATH));
authConfigFactory->setNetrc(std::move(netrc));
@@ -248,20 +251,21 @@ int MultiUrlRequestInfo::prepare()
#ifdef ENABLE_SSL
auto minTLSVer = util::toTLSVersion(option_->get(PREF_MIN_TLS_VERSION));
- std::shared_ptr<TLSContext> clTlsContext
- (TLSContext::make(TLS_CLIENT, minTLSVer));
- if(!option_->blank(PREF_CERTIFICATE)) {
+ std::shared_ptr<TLSContext> clTlsContext(
+ TLSContext::make(TLS_CLIENT, minTLSVer));
+ if (!option_->blank(PREF_CERTIFICATE)) {
clTlsContext->addCredentialFile(option_->get(PREF_CERTIFICATE),
option_->get(PREF_PRIVATE_KEY));
}
- if(!option_->blank(PREF_CA_CERTIFICATE)) {
- if(!clTlsContext->addTrustedCACertFile
- (option_->get(PREF_CA_CERTIFICATE))) {
+ if (!option_->blank(PREF_CA_CERTIFICATE)) {
+ if (!clTlsContext->addTrustedCACertFile(
+ option_->get(PREF_CA_CERTIFICATE))) {
A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
}
- } else if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
- if(!clTlsContext->addSystemTrustedCACerts()) {
+ }
+ else if (option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
+ if (!clTlsContext->addSystemTrustedCACerts()) {
A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
}
}
@@ -270,28 +274,29 @@ int MultiUrlRequestInfo::prepare()
#endif
#ifdef HAVE_ARES_ADDR_NODE
ares_addr_node* asyncDNSServers =
- parseAsyncDNSServers(option_->get(PREF_ASYNC_DNS_SERVER));
+ parseAsyncDNSServers(option_->get(PREF_ASYNC_DNS_SERVER));
e_->setAsyncDNSServers(asyncDNSServers);
#endif // HAVE_ARES_ADDR_NODE
std::string serverStatIf = option_->get(PREF_SERVER_STAT_IF);
- if(!serverStatIf.empty()) {
+ if (!serverStatIf.empty()) {
e_->getRequestGroupMan()->loadServerStat(serverStatIf);
- e_->getRequestGroupMan()->removeStaleServerStat
- (std::chrono::seconds(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT)));
+ e_->getRequestGroupMan()->removeStaleServerStat(
+ std::chrono::seconds(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT)));
}
e_->setStatCalc(getStatCalc(option_));
- if(uriListParser_) {
+ if (uriListParser_) {
e_->getRequestGroupMan()->setUriListParser(uriListParser_);
}
- if(useSignalHandler_) {
+ if (useSignalHandler_) {
setupSignalHandlers();
}
e_->getRequestGroupMan()->getNetStat().downloadStart();
- } catch(RecoverableException& e) {
+ }
+ catch (RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
SingletonHolder<Notifier>::clear();
- if(useSignalHandler_) {
+ if (useSignalHandler_) {
resetSignalHandlers();
}
return -1;
@@ -302,41 +307,42 @@ int MultiUrlRequestInfo::prepare()
error_code::Value MultiUrlRequestInfo::getResult()
{
error_code::Value returnValue = error_code::FINISHED;
- if(!option_->blank(PREF_SAVE_COOKIES)) {
+ if (!option_->blank(PREF_SAVE_COOKIES)) {
e_->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
}
const std::string& serverStatOf = option_->get(PREF_SERVER_STAT_OF);
- if(!serverStatOf.empty()) {
+ if (!serverStatOf.empty()) {
e_->getRequestGroupMan()->saveServerStat(serverStatOf);
}
- if(!option_->getAsBool(PREF_QUIET)) {
- e_->getRequestGroupMan()->showDownloadResults
- (*global::cout(), option_->get(PREF_DOWNLOAD_RESULT) == A2_V_FULL);
+ if (!option_->getAsBool(PREF_QUIET)) {
+ e_->getRequestGroupMan()->showDownloadResults(
+ *global::cout(), option_->get(PREF_DOWNLOAD_RESULT) == A2_V_FULL);
global::cout()->flush();
}
- RequestGroupMan::DownloadStat s =
- e_->getRequestGroupMan()->getDownloadStat();
- if(!s.allCompleted()) {
+ RequestGroupMan::DownloadStat s = e_->getRequestGroupMan()->getDownloadStat();
+ if (!s.allCompleted()) {
printMessageForContinue();
- if(s.getLastErrorResult() == error_code::FINISHED &&
- s.getInProgress() > 0) {
+ if (s.getLastErrorResult() == error_code::FINISHED &&
+ s.getInProgress() > 0) {
returnValue = error_code::IN_PROGRESS;
- } else {
+ }
+ else {
returnValue = s.getLastErrorResult();
}
}
SessionSerializer sessionSerializer{e_->getRequestGroupMan().get()};
// TODO Add option: --save-session-status=error,inprogress,waiting
- if(!option_->blank(PREF_SAVE_SESSION)) {
+ if (!option_->blank(PREF_SAVE_SESSION)) {
const std::string& filename = option_->get(PREF_SAVE_SESSION);
- if(sessionSerializer.save(filename)) {
- A2_LOG_NOTICE(fmt(_("Serialized session to '%s' successfully."),
- filename.c_str()));
- } else {
- A2_LOG_NOTICE(fmt(_("Failed to serialize session to '%s'."),
- filename.c_str()));
+ if (sessionSerializer.save(filename)) {
+ A2_LOG_NOTICE(
+ fmt(_("Serialized session to '%s' successfully."), filename.c_str()));
+ }
+ else {
+ A2_LOG_NOTICE(
+ fmt(_("Failed to serialize session to '%s'."), filename.c_str()));
}
}
SingletonHolder<Notifier>::clear();
@@ -345,18 +351,19 @@ error_code::Value MultiUrlRequestInfo::getResult()
error_code::Value MultiUrlRequestInfo::execute()
{
- if(prepare() != 0) {
+ if (prepare() != 0) {
return error_code::UNKNOWN_ERROR;
}
// TODO Enclosed in try..catch block for just in case. Really need
// this?
try {
e_->run();
- } catch(RecoverableException& e) {
+ }
+ catch (RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
}
error_code::Value returnValue = getResult();
- if(useSignalHandler_) {
+ if (useSignalHandler_) {
resetSignalHandlers();
}
return returnValue;
@@ -365,32 +372,32 @@ error_code::Value MultiUrlRequestInfo::execute()
void MultiUrlRequestInfo::setupSignalHandlers()
{
#ifdef HAVE_SIGACTION
- sigemptyset(&mask_);
-#else // !HAVE_SIGACTION
- mask_ = 0;
+ sigemptyset(&mask_);
+#else // !HAVE_SIGACTION
+ mask_ = 0;
#endif // !HAVE_SIGACTION
#ifdef SIGPIPE
- util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_IGN, 0);
+ util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_IGN, 0);
#endif // SIGPIPE
#ifdef SIGCHLD
- // Avoid to create zombie process when forked child processes are
- // died.
- util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_IGN, 0);
+ // Avoid to create zombie process when forked child processes are
+ // died.
+ util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_IGN, 0);
#endif // SIGCHILD
#ifdef HAVE_SIGACTION
- sigaddset(&mask_, SIGINT);
- sigaddset(&mask_, SIGTERM);
-# ifdef SIGHUP
- sigaddset(&mask_, SIGHUP);
-# endif // SIGHUP
+ sigaddset(&mask_, SIGINT);
+ sigaddset(&mask_, SIGTERM);
+#ifdef SIGHUP
+ sigaddset(&mask_, SIGHUP);
+#endif // SIGHUP
#endif // HAVE_SIGACTION
#ifdef SIGHUP
- util::setGlobalSignalHandler(SIGHUP, &mask_, handler, 0);
+ util::setGlobalSignalHandler(SIGHUP, &mask_, handler, 0);
#endif // SIGHUP
- util::setGlobalSignalHandler(SIGINT, &mask_, handler, 0);
- util::setGlobalSignalHandler(SIGTERM, &mask_, handler, 0);
+ util::setGlobalSignalHandler(SIGINT, &mask_, handler, 0);
+ util::setGlobalSignalHandler(SIGTERM, &mask_, handler, 0);
}
void MultiUrlRequestInfo::resetSignalHandlers()
@@ -405,10 +412,10 @@ void MultiUrlRequestInfo::resetSignalHandlers()
util::setGlobalSignalHandler(SIGTERM, &mask_, SIG_DFL, 0);
#ifdef SIGCHLD
- util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_DFL, 0);
+ util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_DFL, 0);
#endif // SIGCHILD
#ifdef SIGPIPE
- util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_DFL, 0);
+ util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_DFL, 0);
#endif // SIGPIPE
}