diff --git a/src/AbstractAuthResolver.cc b/src/AbstractAuthResolver.cc index a9730f3d..9eceeba4 100644 --- a/src/AbstractAuthResolver.cc +++ b/src/AbstractAuthResolver.cc @@ -34,6 +34,7 @@ /* copyright --> */ #include "AbstractAuthResolver.h" #include "AuthConfig.h" +#include "a2functional.h" namespace aria2 { @@ -41,16 +42,29 @@ AbstractAuthResolver::AbstractAuthResolver() {} AbstractAuthResolver::~AbstractAuthResolver() {} -void AbstractAuthResolver::setUserDefinedAuthConfig -(const std::shared_ptr& authConfig) +void AbstractAuthResolver::setUserDefinedCred +(std::string user, std::string password) { - userDefinedAuthConfig_ = authConfig; + userDefinedUser_ = std::move(user); + userDefinedPassword_ = std::move(password); } -void AbstractAuthResolver::setDefaultAuthConfig -(const std::shared_ptr& authConfig) +std::unique_ptr +AbstractAuthResolver::getUserDefinedAuthConfig() const { - defaultAuthConfig_ = authConfig; + return AuthConfig::create(userDefinedUser_, userDefinedPassword_); +} + +void AbstractAuthResolver::setDefaultCred +(std::string user, std::string password) +{ + defaultUser_ = std::move(user); + defaultPassword_ = std::move(password); +} + +std::unique_ptr AbstractAuthResolver::getDefaultAuthConfig() const +{ + return AuthConfig::create(defaultUser_, defaultPassword_); } } // namespace aria2 diff --git a/src/AbstractAuthResolver.h b/src/AbstractAuthResolver.h index 4801490f..25dd923e 100644 --- a/src/AbstractAuthResolver.h +++ b/src/AbstractAuthResolver.h @@ -40,29 +40,24 @@ namespace aria2 { class AbstractAuthResolver : public AuthResolver { -private: - std::shared_ptr userDefinedAuthConfig_; - - std::shared_ptr defaultAuthConfig_; public: AbstractAuthResolver(); virtual ~AbstractAuthResolver(); - void setUserDefinedAuthConfig(const std::shared_ptr& authConfig); + void setUserDefinedCred(std::string user, std::string password); - const std::shared_ptr& getUserDefinedAuthConfig() const - { - return userDefinedAuthConfig_; - } + std::unique_ptr getUserDefinedAuthConfig() const; - void setDefaultAuthConfig(const std::shared_ptr& authConfig); + void setDefaultCred(std::string user, std::string password); - const std::shared_ptr& getDefaultAuthConfig() const - { - return defaultAuthConfig_; - } + std::unique_ptr getDefaultAuthConfig() const; +private: + std::string userDefinedUser_; + std::string userDefinedPassword_; + std::string defaultUser_; + std::string defaultPassword_; }; } // namespace aria2 diff --git a/src/AuthConfig.cc b/src/AuthConfig.cc index ed9c465f..379eb6f8 100644 --- a/src/AuthConfig.cc +++ b/src/AuthConfig.cc @@ -42,9 +42,9 @@ namespace aria2 { AuthConfig::AuthConfig() {} -AuthConfig::AuthConfig(const std::string& user, const std::string& password) - : user_(user), - password_(password) +AuthConfig::AuthConfig(std::string user, std::string password) + : user_(std::move(user)), + password_(std::move(password)) {} AuthConfig::~AuthConfig() {} @@ -57,6 +57,16 @@ std::string AuthConfig::getAuthText() const return s; } +std::unique_ptr AuthConfig::create +(std::string user, std::string password) +{ + if(user.empty()) { + return std::unique_ptr(); + } else { + return make_unique(std::move(user), std::move(password)); + } +} + std::ostream& operator<<(std::ostream& o, const std::shared_ptr& authConfig) { diff --git a/src/AuthConfig.h b/src/AuthConfig.h index cae54a40..e25c0af3 100644 --- a/src/AuthConfig.h +++ b/src/AuthConfig.h @@ -50,7 +50,7 @@ private: std::string password_; public: AuthConfig(); - AuthConfig(const std::string& user, const std::string& password); + AuthConfig(std::string user, std::string password); ~AuthConfig(); // Don't allow copying @@ -68,6 +68,9 @@ public: { return password_; } + + static std::unique_ptr create + (std::string user, std::string password); }; std::ostream& operator<<(std::ostream& o, diff --git a/src/AuthConfigFactory.cc b/src/AuthConfigFactory.cc index 802ad367..784e9ba4 100644 --- a/src/AuthConfigFactory.cc +++ b/src/AuthConfigFactory.cc @@ -56,33 +56,32 @@ AuthConfigFactory::AuthConfigFactory() {} AuthConfigFactory::~AuthConfigFactory() {} -std::shared_ptr +std::unique_ptr AuthConfigFactory::createAuthConfig (const std::shared_ptr& request, const Option* op) { if(request->getProtocol() == "http" || request->getProtocol() == "https") { - if(op->getAsBool(PREF_HTTP_AUTH_CHALLENGE)) { if(!request->getUsername().empty()) { - std::shared_ptr bc(new BasicCred(request->getUsername(), - request->getPassword(), - request->getHost(), - request->getPort(), - request->getDir(), true)); - updateBasicCred(bc); - return createAuthConfig(request->getUsername(), request->getPassword()); + updateBasicCred(make_unique(request->getUsername(), + request->getPassword(), + request->getHost(), + request->getPort(), + request->getDir(), true)); + return AuthConfig::create(request->getUsername(), + request->getPassword()); } - BasicCredSet::iterator i = - findBasicCred(request->getHost(), request->getPort(), - request->getDir()); - if(i == basicCreds_.end()) { - return std::shared_ptr(); + auto i = findBasicCred(request->getHost(), request->getPort(), + request->getDir()); + if(i == std::end(basicCreds_)) { + return std::unique_ptr(); } else { - return createAuthConfig((*i)->user_, (*i)->password_); + return AuthConfig::create((*i)->user_, (*i)->password_); } } else { if(!request->getUsername().empty()) { - return createAuthConfig(request->getUsername(), request->getPassword()); + return AuthConfig::create(request->getUsername(), + request->getPassword()); } else { return createHttpAuthResolver(op)->resolveAuthConfig(request->getHost()); @@ -91,93 +90,80 @@ AuthConfigFactory::createAuthConfig } else if(request->getProtocol() == "ftp") { if(!request->getUsername().empty()) { if(request->hasPassword()) { - return createAuthConfig(request->getUsername(), request->getPassword()); + return AuthConfig::create(request->getUsername(), + request->getPassword()); } else { if(!op->getAsBool(PREF_NO_NETRC)) { // First, check we have password corresponding to host and // username NetrcAuthResolver authResolver; - authResolver.setNetrc(netrc_); + authResolver.setNetrc(netrc_.get()); - std::shared_ptr ac = - authResolver.resolveAuthConfig(request->getHost()); + auto ac = authResolver.resolveAuthConfig(request->getHost()); if(ac && ac->getUser() == request->getUsername()) { return ac; } } // We don't have password for host and username. Return // password specified by --ftp-passwd - return - createAuthConfig(request->getUsername(), op->get(PREF_FTP_PASSWD)); + return AuthConfig::create(request->getUsername(), + op->get(PREF_FTP_PASSWD)); } } else { return createFtpAuthResolver(op)->resolveAuthConfig(request->getHost()); } } else { - return std::shared_ptr(); + return std::unique_ptr(); } } -std::shared_ptr -AuthConfigFactory::createAuthConfig(const std::string& user, const std::string& password) const -{ - std::shared_ptr ac; - if(!user.empty()) { - ac.reset(new AuthConfig(user, password)); - } - return ac; -} - -std::shared_ptr AuthConfigFactory::createHttpAuthResolver +std::unique_ptr AuthConfigFactory::createHttpAuthResolver (const Option* op) const { - AbstractAuthResolver* resolver; + std::unique_ptr resolver; if(op->getAsBool(PREF_NO_NETRC)) { - resolver = new DefaultAuthResolver(); + resolver.reset(new DefaultAuthResolver()); } else { - NetrcAuthResolver* authResolver(new NetrcAuthResolver()); - authResolver->setNetrc(netrc_); + auto authResolver = make_unique(); + authResolver->setNetrc(netrc_.get()); authResolver->ignoreDefault(); - resolver = authResolver; + resolver = std::move(authResolver); } - resolver->setUserDefinedAuthConfig - (createAuthConfig(op->get(PREF_HTTP_USER), op->get(PREF_HTTP_PASSWD))); - return std::shared_ptr(resolver); + resolver->setUserDefinedCred(op->get(PREF_HTTP_USER), + op->get(PREF_HTTP_PASSWD)); + return std::move(resolver); } -std::shared_ptr AuthConfigFactory::createFtpAuthResolver +std::unique_ptr AuthConfigFactory::createFtpAuthResolver (const Option* op) const { - AbstractAuthResolver* resolver; + std::unique_ptr resolver; if(op->getAsBool(PREF_NO_NETRC)) { - resolver = new DefaultAuthResolver(); + resolver.reset(new DefaultAuthResolver()); } else { - NetrcAuthResolver* authResolver(new NetrcAuthResolver()); - authResolver->setNetrc(netrc_); - resolver = authResolver; + auto authResolver = make_unique(); + authResolver->setNetrc(netrc_.get()); + resolver = std::move(authResolver); } - resolver->setUserDefinedAuthConfig - (createAuthConfig(op->get(PREF_FTP_USER), op->get(PREF_FTP_PASSWD))); - std::shared_ptr defaultAuthConfig - (new AuthConfig(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD)); - resolver->setDefaultAuthConfig(defaultAuthConfig); - return std::shared_ptr(resolver); + resolver->setUserDefinedCred(op->get(PREF_FTP_USER), + op->get(PREF_FTP_PASSWD)); + resolver->setDefaultCred(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD); + return std::move(resolver); } -void AuthConfigFactory::setNetrc(const std::shared_ptr& netrc) +void AuthConfigFactory::setNetrc(std::unique_ptr netrc) { - netrc_ = netrc; + netrc_ = std::move(netrc); } -void AuthConfigFactory::updateBasicCred -(const std::shared_ptr& basicCred) +void AuthConfigFactory::updateBasicCred(std::unique_ptr basicCred) { - BasicCredSet::iterator i = basicCreds_.lower_bound(basicCred); - if(i != basicCreds_.end() && *(*i) == *basicCred) { - *(*i) = *basicCred; + auto i = basicCreds_.lower_bound(basicCred); + if(i != std::end(basicCreds_) && *i == basicCred) { + *(*i) = std::move(*basicCred); } else { - basicCreds_.insert(i, basicCred); + basicCreds_.insert(i, std::move(basicCred)); } } @@ -187,17 +173,15 @@ bool AuthConfigFactory::activateBasicCred const std::string& path, const Option* op) { - BasicCredSet::iterator i = findBasicCred(host, port, path); - if(i == basicCreds_.end()) { - std::shared_ptr authConfig = - createHttpAuthResolver(op)->resolveAuthConfig(host); + auto i = findBasicCred(host, port, path); + if(i == std::end(basicCreds_)) { + auto authConfig = createHttpAuthResolver(op)->resolveAuthConfig(host); if(!authConfig) { return false; } else { - std::shared_ptr bc - (new BasicCred(authConfig->getUser(), authConfig->getPassword(), - host, port, path, true)); - basicCreds_.insert(bc); + basicCreds_.insert(make_unique(authConfig->getUser(), + authConfig->getPassword(), + host, port, path, true)); return true; } } else { @@ -206,55 +190,59 @@ bool AuthConfigFactory::activateBasicCred } } -AuthConfigFactory::BasicCred::BasicCred -(const std::string& user, const std::string& password, - const std::string& host, uint16_t port, const std::string& path, - bool activated): - user_(user), password_(password), - host_(host), port_(port), path_(path), activated_(activated) +BasicCred::BasicCred +(std::string user, std::string password, + std::string host, uint16_t port, std::string path, + bool activated) + : user_(std::move(user)), + password_(std::move(password)), + host_(std::move(host)), + port_(port), + path_(std::move(path)), + activated_(activated) { if(path_.empty() || path_[path_.size()-1] != '/') { path_ += "/"; } } -void AuthConfigFactory::BasicCred::activate() +void BasicCred::activate() { activated_ = true; } -bool AuthConfigFactory::BasicCred::isActivated() const +bool BasicCred::isActivated() const { return activated_; } -bool AuthConfigFactory::BasicCred::operator==(const BasicCred& cred) const +bool BasicCred::operator==(const BasicCred& cred) const { return host_ == cred.host_ && port_ == cred.port_ && path_ == cred.path_; } -bool AuthConfigFactory::BasicCred::operator<(const BasicCred& cred) const +bool BasicCred::operator<(const BasicCred& cred) const { return host_ < cred.host_ || (!(cred.host_ < host_) && (port_ < cred.port_ || (!(cred.port_ < port_) && path_ > cred.path_))); } -AuthConfigFactory::BasicCredSet::iterator -AuthConfigFactory::findBasicCred +AuthConfigFactory::BasicCredSet::iterator AuthConfigFactory::findBasicCred (const std::string& host, uint16_t port, const std::string& path) { - std::shared_ptr bc(new BasicCred("", "", host, port, path)); - BasicCredSet::iterator i = basicCreds_.lower_bound(bc); - for(; i != basicCreds_.end() && (*i)->host_ == host && (*i)->port_ == port; - ++i) { + auto bc = make_unique("", "", host, port, path); + auto i = basicCreds_.lower_bound(bc); + for(; i != std::end(basicCreds_) && + (*i)->host_ == host && + (*i)->port_ == port; ++i) { if(util::startsWith(bc->path_, (*i)->path_)) { return i; } } - return basicCreds_.end(); + return std::end(basicCreds_); } } // namespace aria2 diff --git a/src/AuthConfigFactory.h b/src/AuthConfigFactory.h index e863db6d..03c85bd4 100644 --- a/src/AuthConfigFactory.h +++ b/src/AuthConfigFactory.h @@ -52,42 +52,39 @@ class AuthConfig; class Request; class AuthResolver; -class AuthConfigFactory { -private: - std::shared_ptr netrc_; - - std::shared_ptr createAuthConfig(const std::string& user, - const std::string& password) const; - - std::shared_ptr createHttpAuthResolver(const Option* op) const; - - std::shared_ptr createFtpAuthResolver(const Option* op) const; +class BasicCred { public: - class BasicCred { - public: - std::string user_; - std::string password_; - std::string host_; - uint16_t port_; - std::string path_; - bool activated_; + std::string user_; + std::string password_; + std::string host_; + uint16_t port_; + std::string path_; + bool activated_; - BasicCred(const std::string& user, const std::string& password, - const std::string& host, uint16_t port, const std::string& path, - bool activated = false); + BasicCred(std::string user, std::string password, + std::string host, uint16_t port, std::string path, + bool activated = false); - void activate(); + void activate(); - bool isActivated() const; + bool isActivated() const; - bool operator==(const BasicCred& cred) const; + bool operator==(const BasicCred& cred) const; - bool operator<(const BasicCred& cred) const; - }; + bool operator<(const BasicCred& cred) const; +}; - typedef std::set, - DerefLess > > BasicCredSet; +class AuthConfigFactory { +public: + typedef std::set, + DerefLess>> BasicCredSet; private: + std::unique_ptr netrc_; + + std::unique_ptr createHttpAuthResolver(const Option* op) const; + + std::unique_ptr createFtpAuthResolver(const Option* op) const; + BasicCredSet basicCreds_; public: AuthConfigFactory(); @@ -98,10 +95,10 @@ public: // are used in this method: PREF_HTTP_USER, PREF_HTTP_PASSWD, // PREF_FTP_USER, PREF_FTP_PASSWD, PREF_NO_NETRC and // PREF_HTTP_AUTH_CHALLENGE. - std::shared_ptr createAuthConfig + std::unique_ptr createAuthConfig (const std::shared_ptr& request, const Option* op); - void setNetrc(const std::shared_ptr& netrc); + void setNetrc(std::unique_ptr netrc); // Find a BasicCred using findBasicCred() and activate it then // return true. If matching BasicCred is not found, AuthConfig @@ -127,7 +124,9 @@ public: // If the same BasicCred is already added, then it is replaced with // given basicCred. Otherwise, insert given basicCred to // basicCreds_. - void updateBasicCred(const std::shared_ptr& basicCred); + // + // Made public for unit test. + void updateBasicCred(std::unique_ptr basicCred); }; } // namespace aria2 diff --git a/src/AuthResolver.h b/src/AuthResolver.h index e3b83449..c02e5ec5 100644 --- a/src/AuthResolver.h +++ b/src/AuthResolver.h @@ -48,7 +48,8 @@ class AuthResolver { public: virtual ~AuthResolver() {} - virtual std::shared_ptr resolveAuthConfig(const std::string& hostname) = 0; + virtual std::unique_ptr resolveAuthConfig + (const std::string& hostname) = 0; }; } // namespace aria2 diff --git a/src/DefaultAuthResolver.cc b/src/DefaultAuthResolver.cc index 2cb589cf..35357f8e 100644 --- a/src/DefaultAuthResolver.cc +++ b/src/DefaultAuthResolver.cc @@ -37,13 +37,14 @@ namespace aria2 { -std::shared_ptr DefaultAuthResolver::resolveAuthConfig +std::unique_ptr DefaultAuthResolver::resolveAuthConfig (const std::string& hostname) { - if(!getUserDefinedAuthConfig()) { - return getDefaultAuthConfig(); + auto authConfig = getUserDefinedAuthConfig(); + if(authConfig) { + return authConfig; } else { - return getUserDefinedAuthConfig(); + return getDefaultAuthConfig(); } } diff --git a/src/DefaultAuthResolver.h b/src/DefaultAuthResolver.h index e3ddf509..ea2bcf61 100644 --- a/src/DefaultAuthResolver.h +++ b/src/DefaultAuthResolver.h @@ -41,7 +41,7 @@ namespace aria2 { class DefaultAuthResolver : public AbstractAuthResolver { public: - virtual std::shared_ptr resolveAuthConfig + virtual std::unique_ptr resolveAuthConfig (const std::string& hostname); }; diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 107d3ce0..8517b045 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -200,9 +200,9 @@ int MultiUrlRequestInfo::prepare() A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION, option_->get(PREF_NETRC_PATH).c_str())); } else { - std::shared_ptr netrc(new Netrc()); + auto netrc = make_unique(); netrc->parse(option_->get(PREF_NETRC_PATH)); - authConfigFactory->setNetrc(netrc); + authConfigFactory->setNetrc(std::move(netrc)); } } e_->setAuthConfigFactory(std::move(authConfigFactory)); diff --git a/src/Netrc.cc b/src/Netrc.cc index 0c7743d5..fc7b3aaa 100644 --- a/src/Netrc.cc +++ b/src/Netrc.cc @@ -50,14 +50,14 @@ namespace aria2 { Authenticator::Authenticator() {} Authenticator::Authenticator -(const std::string& machine, - const std::string& login, - const std::string& password, - const std::string& account) - : machine_(machine), - login_(login), - password_(password), - account_(account) +(std::string machine, + std::string login, + std::string password, + std::string account) + : machine_(std::move(machine)), + login_(std::move(login)), + password_(std::move(password)), + account_(std::move(account)) {} Authenticator::~Authenticator() {} @@ -67,33 +67,34 @@ bool Authenticator::match(const std::string& hostname) const return util::noProxyDomainMatch(hostname, machine_); } -void Authenticator::setMachine(const std::string& machine) +void Authenticator::setMachine(std::string machine) { - machine_ = machine; + machine_ = std::move(machine); } -void Authenticator::setLogin(const std::string& login) +void Authenticator::setLogin(std::string login) { - login_ = login; + login_ = std::move(login); } -void Authenticator::setPassword(const std::string& password) +void Authenticator::setPassword(std::string password) { - password_ = password; + password_ = std::move(password); } -void Authenticator::setAccount(const std::string& account) +void Authenticator::setAccount(std::string account) { - account_ = account; + account_ = std::move(account); } DefaultAuthenticator::DefaultAuthenticator() {} DefaultAuthenticator::DefaultAuthenticator -(const std::string& login, - const std::string& password, - const std::string& account) - : Authenticator(A2STR::NIL, login, password, account) +(std::string login, + std::string password, + std::string account) + : Authenticator("", std::move(login), std::move(password), + std::move(account)) {} DefaultAuthenticator::~DefaultAuthenticator() {} @@ -107,9 +108,9 @@ Netrc::Netrc() {} Netrc::~Netrc() {} -void Netrc::addAuthenticator(const std::shared_ptr& authenticator) +void Netrc::addAuthenticator(std::unique_ptr authenticator) { - authenticators_.push_back(authenticator); + authenticators_.push_back(std::move(authenticator)); } namespace { @@ -146,7 +147,7 @@ void Netrc::parse(const std::string& path) SET_ACCOUNT, SET_MACDEF }; - std::shared_ptr authenticator; + std::unique_ptr authenticator; STATE state = GET_TOKEN; while(1) { std::string line = fp.getLine(); @@ -169,11 +170,11 @@ void Netrc::parse(const std::string& path) eoi = tokens.end(); iter != eoi; ++iter) { if(state == GET_TOKEN) { if(util::streq((*iter).first, (*iter).second, "machine")) { - storeAuthenticator(authenticator); + storeAuthenticator(std::move(authenticator)); authenticator.reset(new Authenticator()); state = SET_MACHINE; } else if(util::streq((*iter).first, (*iter).second, "default")) { - storeAuthenticator(authenticator); + storeAuthenticator(std::move(authenticator)); authenticator.reset(new DefaultAuthenticator()); } else { if(!authenticator) { @@ -194,13 +195,13 @@ void Netrc::parse(const std::string& path) } } else { if(state == SET_MACHINE) { - authenticator->setMachine((*iter).first, (*iter).second); + authenticator->setMachine({(*iter).first, (*iter).second}); } else if(state == SET_LOGIN) { - authenticator->setLogin((*iter).first, (*iter).second); + authenticator->setLogin({(*iter).first, (*iter).second}); } else if(state == SET_PASSWORD) { - authenticator->setPassword((*iter).first, (*iter).second); + authenticator->setPassword({(*iter).first, (*iter).second}); } else if(state == SET_ACCOUNT) { - authenticator->setAccount((*iter).first, (*iter).second); + authenticator->setAccount({(*iter).first, (*iter).second}); } else if(state == SET_MACDEF) { skipMacdef(fp); } @@ -212,13 +213,13 @@ void Netrc::parse(const std::string& path) throw DL_ABORT_EX ("Netrc:parse error. EOF reached where a token expected."); } - storeAuthenticator(authenticator); + storeAuthenticator(std::move(authenticator)); } -void Netrc::storeAuthenticator(const std::shared_ptr& authenticator) +void Netrc::storeAuthenticator(std::unique_ptr authenticator) { if(authenticator) { - authenticators_.push_back(authenticator); + authenticators_.push_back(std::move(authenticator)); } } @@ -229,24 +230,31 @@ private: public: AuthHostMatch(const std::string& hostname):hostname(hostname) {} - bool operator()(const std::shared_ptr& authenticator) + bool operator()(const std::unique_ptr& authenticator) { return authenticator->match(hostname); } }; } // namespace -std::shared_ptr +const Authenticator* Netrc::findAuthenticator(const std::string& hostname) const { - std::shared_ptr res; - std::vector >::const_iterator itr = - std::find_if(authenticators_.begin(), authenticators_.end(), - AuthHostMatch(hostname)); - if(itr != authenticators_.end()) { - res = *itr; + std::unique_ptr res; + auto itr = std::find_if(std::begin(authenticators_), + std::end(authenticators_), + AuthHostMatch(hostname)); + if(itr == std::end(authenticators_)) { + return nullptr; + } else { + return (*itr).get(); } - return res; +} + +const std::vector>& +Netrc::getAuthenticators() const +{ + return authenticators_; } } // namespace aria2 diff --git a/src/Netrc.h b/src/Netrc.h index 1fd25b27..a2845f78 100644 --- a/src/Netrc.h +++ b/src/Netrc.h @@ -59,10 +59,10 @@ private: public: Authenticator(); - Authenticator(const std::string& machine, - const std::string& login, - const std::string& password, - const std::string& account); + Authenticator(std::string machine, + std::string login, + std::string password, + std::string account); virtual ~Authenticator(); @@ -73,61 +73,37 @@ public: return machine_; } - void setMachine(const std::string& machine); - - template - void setMachine(InputIterator first, InputIterator last) - { - machine_.assign(first, last); - } + void setMachine(std::string machine); const std::string& getLogin() const { return login_; } - void setLogin(const std::string& login); - - template - void setLogin(InputIterator first, InputIterator last) - { - login_.assign(first, last); - } + void setLogin(std::string login); const std::string& getPassword() const { return password_; } - void setPassword(const std::string& password); - - template - void setPassword(InputIterator first, InputIterator last) - { - password_.assign(first, last); - } + void setPassword(std::string password); const std::string& getAccount() const { return account_; } - void setAccount(const std::string& account); - - template - void setAccount(InputIterator first, InputIterator last) - { - account_.assign(first, last); - } + void setAccount(std::string account); }; class DefaultAuthenticator : public Authenticator { public: DefaultAuthenticator(); - DefaultAuthenticator(const std::string& login, - const std::string& password, - const std::string& account); + DefaultAuthenticator(std::string login, + std::string password, + std::string account); virtual ~DefaultAuthenticator(); @@ -136,9 +112,9 @@ public: class Netrc { private: - std::vector > authenticators_; + std::vector> authenticators_; - void storeAuthenticator(const std::shared_ptr& authenticator); + void storeAuthenticator(std::unique_ptr authenticator); public: Netrc(); @@ -146,15 +122,11 @@ public: void parse(const std::string& path); - std::shared_ptr findAuthenticator - (const std::string& hostname) const; + const Authenticator* findAuthenticator(const std::string& hostname) const; - const std::vector >& getAuthenticators() const - { - return authenticators_; - } + const std::vector>& getAuthenticators() const; - void addAuthenticator(const std::shared_ptr& authenticator); + void addAuthenticator(std::unique_ptr authenticator); }; } // namespace aria2 diff --git a/src/NetrcAuthResolver.cc b/src/NetrcAuthResolver.cc index 04fcc9c1..593fd81e 100644 --- a/src/NetrcAuthResolver.cc +++ b/src/NetrcAuthResolver.cc @@ -35,42 +35,44 @@ #include "NetrcAuthResolver.h" #include "AuthConfig.h" #include "Netrc.h" +#include "a2functional.h" namespace aria2 { -NetrcAuthResolver::NetrcAuthResolver():ignoreDefault_(false) {} +NetrcAuthResolver::NetrcAuthResolver() + : netrc_(nullptr), ignoreDefault_(false) {} -std::shared_ptr +std::unique_ptr NetrcAuthResolver::resolveAuthConfig(const std::string& hostname) { - if(!getUserDefinedAuthConfig()) { - return findNetrcAuthenticator(hostname); + auto authConfig = getUserDefinedAuthConfig(); + if(authConfig) { + return authConfig; } else { - return getUserDefinedAuthConfig(); + return findNetrcAuthenticator(hostname); } } -std::shared_ptr +std::unique_ptr NetrcAuthResolver::findNetrcAuthenticator(const std::string& hostname) const { if(!netrc_) { return getDefaultAuthConfig(); } else { - std::shared_ptr auth = netrc_->findAuthenticator(hostname); + auto auth = netrc_->findAuthenticator(hostname); if(!auth) { return getDefaultAuthConfig(); } else { if(ignoreDefault_ && auth->getMachine().empty()) { return getDefaultAuthConfig(); } else { - return std::shared_ptr - (new AuthConfig(auth->getLogin(), auth->getPassword())); + return make_unique(auth->getLogin(), auth->getPassword()); } } } } -void NetrcAuthResolver::setNetrc(const std::shared_ptr& netrc) +void NetrcAuthResolver::setNetrc(Netrc* netrc) { netrc_ = netrc; } diff --git a/src/NetrcAuthResolver.h b/src/NetrcAuthResolver.h index a92b5dff..d9b74acd 100644 --- a/src/NetrcAuthResolver.h +++ b/src/NetrcAuthResolver.h @@ -43,24 +43,19 @@ class Netrc; class NetrcAuthResolver : public AbstractAuthResolver { private: - std::shared_ptr netrc_; + Netrc* netrc_; bool ignoreDefault_; - std::shared_ptr findNetrcAuthenticator + std::unique_ptr findNetrcAuthenticator (const std::string& hostname) const; public: NetrcAuthResolver(); - virtual std::shared_ptr resolveAuthConfig + virtual std::unique_ptr resolveAuthConfig (const std::string& hostname); - void setNetrc(const std::shared_ptr& netrc); - - const std::shared_ptr& getNetrc() const - { - return netrc_; - } + void setNetrc(Netrc* netrc); // Ignores default token of netrc void ignoreDefault(); diff --git a/test/AuthConfigFactoryTest.cc b/test/AuthConfigFactoryTest.cc index 557bd51f..0ffe2632 100644 --- a/test/AuthConfigFactoryTest.cc +++ b/test/AuthConfigFactoryTest.cc @@ -44,15 +44,15 @@ void AuthConfigFactoryTest::testCreateAuthConfig_http() CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option)); // with Netrc - std::shared_ptr netrc(new Netrc()); - netrc->addAuthenticator - (std::shared_ptr(new Authenticator("localhost", - "localhostuser", - "localhostpass", - "localhostacct"))); - netrc->addAuthenticator - (std::shared_ptr(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"))); - factory.setNetrc(netrc); + auto netrc = make_unique(); + netrc->addAuthenticator(make_unique("localhost", + "localhostuser", + "localhostpass", + "localhostacct")); + netrc->addAuthenticator(make_unique("default", + "defaultpassword", + "defaultaccount")); + factory.setNetrc(std::move(netrc)); // not activated CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option)); @@ -100,15 +100,15 @@ void AuthConfigFactoryTest::testCreateAuthConfig_httpNoChallenge() CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option)); // with Netrc - std::shared_ptr netrc(new Netrc()); - netrc->addAuthenticator - (std::shared_ptr(new Authenticator("localhost", - "localhostuser", - "localhostpass", - "localhostacct"))); - netrc->addAuthenticator - (std::shared_ptr(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"))); - factory.setNetrc(netrc); + auto netrc = make_unique(); + netrc->addAuthenticator(make_unique("localhost", + "localhostuser", + "localhostpass", + "localhostacct")); + netrc->addAuthenticator(make_unique("default", + "defaultpassword", + "defaultaccount")); + factory.setNetrc(std::move(netrc)); // not activated CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"), @@ -147,10 +147,11 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp() factory.createAuthConfig(req, &option)->getAuthText()); // with Netrc - std::shared_ptr netrc(new Netrc()); - netrc->addAuthenticator - (std::shared_ptr(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"))); - factory.setNetrc(netrc); + auto netrc = make_unique(); + netrc->addAuthenticator(make_unique("default", + "defaultpassword", + "defaultaccount")); + factory.setNetrc(std::move(netrc)); CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"), factory.createAuthConfig(req, &option)->getAuthText()); @@ -179,12 +180,11 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp() // Recreate netrc with entry for user aria2user netrc.reset(new Netrc()); - netrc->addAuthenticator - (std::shared_ptr(new Authenticator("localhost", - "aria2user", - "netrcpass", - "netrcacct"))); - factory.setNetrc(netrc); + netrc->addAuthenticator(make_unique("localhost", + "aria2user", + "netrcpass", + "netrcacct")); + factory.setNetrc(std::move(netrc)); // This time, we can find same username "aria2user" in netrc, so the // password "netrcpass" is used, instead of "userDefinedPassword" CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"), @@ -196,17 +196,14 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp() } namespace { -std::shared_ptr +std::unique_ptr createBasicCred(const std::string& user, const std::string& password, const std::string& host, uint16_t port, const std::string& path, bool activated = false) { - std::shared_ptr bc - (new AuthConfigFactory::BasicCred(user, password, host, port, path, - activated)); - return bc; + return make_unique(user, password, host, port, path, activated); } } // namespace diff --git a/test/DefaultAuthResolverTest.cc b/test/DefaultAuthResolverTest.cc index 547321bf..c0104d2d 100644 --- a/test/DefaultAuthResolverTest.cc +++ b/test/DefaultAuthResolverTest.cc @@ -12,9 +12,7 @@ class DefaultAuthResolverTest : public CppUnit::TestFixture { CPPUNIT_TEST(testResolveAuthConfig_with_userDefined); CPPUNIT_TEST_SUITE_END(); private: - //NetrcHandle _netrc; - //std::shared_ptr