diff --git a/ChangeLog b/ChangeLog index 73ce999a..5425e261 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2007-10-30 Tatsuhiro Tsujikawa + + Added ftp://USER:PASSWD@Servername automatic parsing. + * src/Request.{h, cc}: Removed AuthResolvers. Added _username and + _password. Recognize username and password in URI. + * src/main.cc: Use AuthConfigFactory instead of RequestFactory. + * src/RequestGroup.cc: Use AuthConfigFactory instead of RequestFactory. + * src/RequestFactory.{h, cc}: Removed. + * src/AuthConfigFactory.{h, cc}: New class. + * src/FtpConnection.cc: Use AuthConfigFactory. + * src/HttpRequest.cc: Use AuthConfigFactory. + * test/HttpRequestTest.cc: Updated. + * test/RequestTest.cc: Updated. + * test/AuthConfigFactoryTest.cc: New class. + * test/RequestFactoryTest.cc: Removed. + 2007-10-29 Tatsuhiro Tsujikawa Use RequestGroup::allDownloadFinished() to decide whether the control diff --git a/TODO b/TODO index cf4d60bc..8a1c79ea 100644 --- a/TODO +++ b/TODO @@ -52,7 +52,6 @@ * Reimplement ChecksumCommand(validation using 1 checksum for 1 file) * Implement duplicate download checking in Bt * Add PeerListenCommand to DownloadEngine only when it is really necessary. -* ftp://USER:PASSWD@Servername automatic parsing * improve --metalink-location field * Use content-type for PostDownloadHandler * Torrent information diff --git a/src/RequestFactory.cc b/src/AuthConfigFactory.cc similarity index 69% rename from src/RequestFactory.cc rename to src/AuthConfigFactory.cc index e1e0350c..1dfac71d 100644 --- a/src/RequestFactory.cc +++ b/src/AuthConfigFactory.cc @@ -32,24 +32,41 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#include "RequestFactory.h" -#include "prefs.h" -#include "AbstractAuthResolver.h" -#include "NetrcAuthResolver.h" +#include "AuthConfigFactory.h" +#include "Option.h" +#include "AuthConfig.h" +#include "Netrc.h" #include "DefaultAuthResolver.h" +#include "NetrcAuthResolver.h" +#include "prefs.h" +#include "Request.h" -RequestHandle RequestFactory::createRequest() +AuthConfigFactory::AuthConfigFactory(const Option* option): + _option(option), _netrc(0) {} + +AuthConfigFactory::~AuthConfigFactory() {} + +AuthConfigHandle AuthConfigFactory::createAuthConfig(const RequestHandle& request) const { - RequestHandle request = new Request(); - request->setMethod(_method); - request->setReferer(_referer); - request->setHttpAuthResolver(createHttpAuthResolver()); - request->setHttpProxyAuthResolver(createHttpProxyAuthResolver()); - request->setFtpAuthResolver(createFtpAuthResolver()); - return request; + if(request->getProtocol() == "http" || request->getProtocol() == "https") { + return createHttpAuthResolver()->resolveAuthConfig(request->getHost()); + } else if(request->getProtocol() == "ftp") { + if(!request->getUsername().empty()) { + return createAuthConfig(request->getUsername(), request->getPassword()); + } else { + return createFtpAuthResolver()->resolveAuthConfig(request->getHost()); + } + } else { + return new AuthConfig(); + } } -AuthConfigHandle RequestFactory::createAuthConfig(const string& user, const string& password) const +AuthConfigHandle AuthConfigFactory::createAuthConfigForHttpProxy(const RequestHandle& request) const +{ + return createHttpProxyAuthResolver()->resolveAuthConfig(request->getHost()); +} + +AuthConfigHandle AuthConfigFactory::createAuthConfig(const string& user, const string& password) const { if(user.length() > 0) { return new AuthConfig(user, password); @@ -58,7 +75,7 @@ AuthConfigHandle RequestFactory::createAuthConfig(const string& user, const stri } } -AuthResolverHandle RequestFactory::createHttpAuthResolver() +AuthResolverHandle AuthConfigFactory::createHttpAuthResolver() const { AbstractAuthResolverHandle resolver = 0; if(true || _option->getAsBool(PREF_NO_NETRC)) { @@ -72,7 +89,7 @@ AuthResolverHandle RequestFactory::createHttpAuthResolver() return resolver; } -AuthResolverHandle RequestFactory::createFtpAuthResolver() +AuthResolverHandle AuthConfigFactory::createFtpAuthResolver() const { AbstractAuthResolverHandle resolver = 0; if(_option->getAsBool(PREF_NO_NETRC)) { @@ -87,7 +104,7 @@ AuthResolverHandle RequestFactory::createFtpAuthResolver() return resolver; } -AuthResolverHandle RequestFactory::createHttpProxyAuthResolver() +AuthResolverHandle AuthConfigFactory::createHttpProxyAuthResolver() const { AbstractAuthResolverHandle resolver = 0; if(true || _option->getAsBool(PREF_NO_NETRC)) { @@ -100,3 +117,8 @@ AuthResolverHandle RequestFactory::createHttpProxyAuthResolver() resolver->setUserDefinedAuthConfig(createAuthConfig(_option->get(PREF_HTTP_PROXY_USER), _option->get(PREF_HTTP_PROXY_PASSWD))); return resolver; } + +void AuthConfigFactory::setNetrc(const NetrcHandle& netrc) +{ + _netrc = netrc; +} diff --git a/src/RequestFactory.h b/src/AuthConfigFactory.h similarity index 63% rename from src/RequestFactory.h rename to src/AuthConfigFactory.h index 2ca56e20..ac1ecbcb 100644 --- a/src/RequestFactory.h +++ b/src/AuthConfigFactory.h @@ -32,59 +32,49 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_REQUEST_FACTORY_H_ -#define _D_REQUEST_FACTORY_H_ +#ifndef _D_AUTH_CONFIG_FACTORY_H_ +#define _D_AUTH_CONFIG_FACTORY_H_ #include "common.h" -#include "Request.h" -#include "Option.h" -#include "Netrc.h" -class RequestFactory { +class Option; +class Netrc; +extern typedef SharedHandle NetrcHandle; +class AuthConfig; +extern typedef SharedHandle AuthConfigHandle; +class Request; +extern typedef SharedHandle RequestHandle; +class AuthResolver; +extern typedef SharedHandle AuthResolverHandle; + +class AuthConfigFactory { private: const Option* _option; - NetrcHandle _netrc; - string _method; - string _referer; + NetrcHandle _netrc; + AuthConfigHandle createAuthConfig(const string& user, const string& password) const; + AuthResolverHandle createHttpAuthResolver() const; + + AuthResolverHandle createHttpProxyAuthResolver() const; + + AuthResolverHandle createFtpAuthResolver() const; + public: - RequestFactory():_option(0), - _netrc(0), - _method(Request::METHOD_GET) - {} + + AuthConfigFactory(const Option* option); - RequestHandle createRequest(); + ~AuthConfigFactory(); - AuthResolverHandle createHttpAuthResolver(); + AuthConfigHandle createAuthConfig(const RequestHandle& request) const; - AuthResolverHandle createHttpProxyAuthResolver(); + AuthConfigHandle createAuthConfigForHttpProxy(const RequestHandle& request) const; - AuthResolverHandle createFtpAuthResolver(); - - void setOption(const Option* option) - { - _option = option; - } - - void setNetrc(const NetrcHandle& netrc) - { - _netrc = netrc; - } - - void setMethod(const string& method) - { - _method = method; - } - - void setReferer(const string& referer) - { - _referer = referer; - } + void setNetrc(const NetrcHandle& netrc); }; -typedef SharedHandle RequestFactoryHandle; -typedef SingletonHolder RequestFactorySingletonHolder; +typedef SharedHandle AuthConfigFactoryHandle; +typedef SingletonHolder AuthConfigFactorySingleton; -#endif // _D_REQUEST_FACTORY_H_ +#endif // _D_AUTH_CONFIG_FACTORY_H_ diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index f87d37b7..7a5cb419 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -37,6 +37,8 @@ #include "message.h" #include "prefs.h" #include "LogFactory.h" +#include "AuthConfigFactory.h" +#include "AuthConfig.h" FtpConnection::FtpConnection(int32_t cuid, const SocketHandle& socket, const RequestHandle req, const Option* op) @@ -49,7 +51,7 @@ FtpConnection::~FtpConnection() {} void FtpConnection::sendUser() const throw(DlRetryEx*) { - string request = "USER "+req->resolveFtpAuthConfig()->getUser()+"\r\n"; + string request = "USER "+AuthConfigFactorySingleton::instance()->createAuthConfig(req)->getUser()+"\r\n"; logger->info(MSG_SENDING_REQUEST, cuid, request.c_str()); socket->writeData(request); } @@ -57,7 +59,7 @@ void FtpConnection::sendUser() const void FtpConnection::sendPass() const throw(DlRetryEx*) { - string request = "PASS "+req->resolveFtpAuthConfig()->getPassword()+"\r\n"; + string request = "PASS "+AuthConfigFactorySingleton::instance()->createAuthConfig(req)->getPassword()+"\r\n"; logger->info(MSG_SENDING_REQUEST, cuid, "PASS ********"); socket->writeData(request); } diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index d0b27557..7bd647a5 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -36,6 +36,8 @@ #include "Util.h" #include "Base64.h" #include "prefs.h" +#include "AuthConfigFactory.h" +#include "AuthConfig.h" RangeHandle HttpRequest::getRange() const { @@ -112,7 +114,7 @@ string HttpRequest::createRequest() const } if(authEnabled) { requestLine += "Authorization: Basic "+ - Base64::encode(request->resolveHttpAuthConfig()->getAuthText())+"\r\n"; + Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfig(request)->getAuthText())+"\r\n"; } if(getPreviousURI().size()) { requestLine += "Referer: "+getPreviousURI()+"\r\n"; @@ -154,7 +156,7 @@ string HttpRequest::createProxyRequest() const string HttpRequest::getProxyAuthString() const { return "Proxy-Authorization: Basic "+ - Base64::encode(request->resolveHttpProxyAuthConfig()->getAuthText())+"\r\n"; + Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfigForHttpProxy(request)->getAuthText())+"\r\n"; } void HttpRequest::configure(const Option* option) diff --git a/src/Makefile.am b/src/Makefile.am index 58c7446f..2ccafab6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -74,7 +74,7 @@ SRCS = Socket.h\ AbstractAuthResolver.h\ DefaultAuthResolver.cc DefaultAuthResolver.h\ NetrcAuthResolver.cc NetrcAuthResolver.h\ - RequestFactory.cc RequestFactory.h\ + AuthConfigFactory.cc AuthConfigFactory.h\ OptionParser.cc OptionParser.h\ OptionHandlerFactory.cc OptionHandlerFactory.h\ NameResolver.cc NameResolver.h\ diff --git a/src/Makefile.in b/src/Makefile.in index 30ecdfa9..ea417584 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -233,8 +233,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ AbstractProxyResponseCommand.h Netrc.cc Netrc.h AuthConfig.cc \ AuthConfig.h AuthResolver.h AbstractAuthResolver.h \ DefaultAuthResolver.cc DefaultAuthResolver.h \ - NetrcAuthResolver.cc NetrcAuthResolver.h RequestFactory.cc \ - RequestFactory.h OptionParser.cc OptionParser.h \ + NetrcAuthResolver.cc NetrcAuthResolver.h AuthConfigFactory.cc \ + AuthConfigFactory.h OptionParser.cc OptionParser.h \ OptionHandlerFactory.cc OptionHandlerFactory.h NameResolver.cc \ NameResolver.h RequestGroup.cc RequestGroup.h \ RequestGroupAware.cc RequestGroupAware.h RequestGroupMan.cc \ @@ -462,7 +462,7 @@ am__objects_12 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ HttpRequest.$(OBJEXT) AbstractProxyRequestCommand.$(OBJEXT) \ AbstractProxyResponseCommand.$(OBJEXT) Netrc.$(OBJEXT) \ AuthConfig.$(OBJEXT) DefaultAuthResolver.$(OBJEXT) \ - NetrcAuthResolver.$(OBJEXT) RequestFactory.$(OBJEXT) \ + NetrcAuthResolver.$(OBJEXT) AuthConfigFactory.$(OBJEXT) \ OptionParser.$(OBJEXT) OptionHandlerFactory.$(OBJEXT) \ NameResolver.$(OBJEXT) RequestGroup.$(OBJEXT) \ RequestGroupAware.$(OBJEXT) RequestGroupMan.$(OBJEXT) \ @@ -723,8 +723,8 @@ SRCS = Socket.h SocketCore.cc SocketCore.h Command.cc Command.h \ AbstractProxyResponseCommand.h Netrc.cc Netrc.h AuthConfig.cc \ AuthConfig.h AuthResolver.h AbstractAuthResolver.h \ DefaultAuthResolver.cc DefaultAuthResolver.h \ - NetrcAuthResolver.cc NetrcAuthResolver.h RequestFactory.cc \ - RequestFactory.h OptionParser.cc OptionParser.h \ + NetrcAuthResolver.cc NetrcAuthResolver.h AuthConfigFactory.cc \ + AuthConfigFactory.h OptionParser.cc OptionParser.h \ OptionHandlerFactory.cc OptionHandlerFactory.h NameResolver.cc \ NameResolver.h RequestGroup.cc RequestGroup.h \ RequestGroupAware.cc RequestGroupAware.h RequestGroupMan.cc \ @@ -857,6 +857,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ActivePeerConnectionCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AnnounceList.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfig.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfigFactory.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AutoSaveCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldMan.Po@am__quote@ @@ -988,7 +989,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PostDownloadHandler.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RealtimeCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Request.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestFactory.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestGroup.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestGroupAware.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestGroupEntry.Po@am__quote@ diff --git a/src/Request.cc b/src/Request.cc index 132d5c9e..c519c501 100644 --- a/src/Request.cc +++ b/src/Request.cc @@ -42,9 +42,6 @@ const string Request::METHOD_GET = "get"; const string Request::METHOD_HEAD = "head"; Request::Request():port(0), tryCount(0), keepAlive(false), method(METHOD_GET), - _httpAuthResolver(0), - _httpProxyAuthResolver(0), - _ftpAuthResolver(0), cookieBox(CookieBoxFactorySingletonHolder::instance()->createNewInstance()) {} Request::~Request() {} @@ -84,14 +81,18 @@ bool Request::parseUrl(const string& url) { port = 0; dir = ""; file = ""; + _username = ""; + _password = ""; if(tempUrl.find_first_not_of(SAFE_CHARS) != string::npos) { return false; } + // find query part string::size_type startQueryIndex = tempUrl.find("?"); if(startQueryIndex != string::npos) { query = tempUrl.substr(startQueryIndex); tempUrl.erase(startQueryIndex); } + // find protocol string::size_type hp = tempUrl.find("://"); if(hp == string::npos) return false; protocol = tempUrl.substr(0, hp); @@ -100,13 +101,24 @@ bool Request::parseUrl(const string& url) { return false; } hp += 3; + // find host part if(tempUrl.size() <= hp) return false; string::size_type hep = tempUrl.find("/", hp); if(hep == string::npos) { hep = tempUrl.size(); } + string hostPart = tempUrl.substr(hp, hep-hp); + // find username and password in host part if they exist + string::size_type atmarkp = hostPart.find_last_of("@"); + if(atmarkp != string::npos) { + string authPart = hostPart.substr(0, atmarkp); + pair userPass = Util::split(authPart, ":"); + _username = Util::urldecode(userPass.first); + _password = Util::urldecode(userPass.second); + hostPart.erase(0, atmarkp+1); + } pair hostAndPort; - Util::split(hostAndPort, tempUrl.substr(hp, hep-hp), ':'); + Util::split(hostAndPort, hostPart, ':'); host = hostAndPort.first; if(hostAndPort.second != "") { port = strtol(hostAndPort.second.c_str(), NULL, 10); @@ -117,6 +129,7 @@ bool Request::parseUrl(const string& url) { // If port is not specified, then we set it to default port of its protocol.. port = defPort; } + // find directory and file part string::size_type direp = tempUrl.find_last_of("/"); if(direp == string::npos || direp <= hep) { dir = "/"; @@ -139,18 +152,3 @@ bool Request::parseUrl(const string& url) { file += query; return true; } - -AuthConfigHandle Request::resolveHttpAuthConfig() -{ - return _httpAuthResolver->resolveAuthConfig(getHost()); -} - -AuthConfigHandle Request::resolveFtpAuthConfig() -{ - return _ftpAuthResolver->resolveAuthConfig(getHost()); -} - -AuthConfigHandle Request::resolveHttpProxyAuthConfig() -{ - return _httpProxyAuthResolver->resolveAuthConfig(getHost()); -} diff --git a/src/Request.h b/src/Request.h index 73fdd833..3dc26c35 100644 --- a/src/Request.h +++ b/src/Request.h @@ -36,8 +36,6 @@ #define _D_REQUEST_H_ #include "common.h" #include "CookieBox.h" -#include "AuthConfig.h" -#include "AuthResolver.h" #define SAFE_CHARS "abcdefghijklmnopqrstuvwxyz"\ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\ @@ -80,11 +78,9 @@ private: bool keepAlive; string method; - AuthResolverHandle _httpAuthResolver; + string _username; - AuthResolverHandle _httpProxyAuthResolver; - - AuthResolverHandle _ftpAuthResolver; + string _password; bool parseUrl(const string& url); public: @@ -125,27 +121,16 @@ public: this->method = method; } - void setHttpAuthResolver(const AuthResolverHandle& authResolver) + const string& getUsername() const { - _httpAuthResolver = authResolver; + return _username; } - void setHttpProxyAuthResolver(const AuthResolverHandle& authResolver) + const string& getPassword() const { - _httpProxyAuthResolver = authResolver; + return _password; } - void setFtpAuthResolver(const AuthResolverHandle& authResolver) - { - _ftpAuthResolver = authResolver; - } - - AuthConfigHandle resolveHttpAuthConfig(); - - AuthConfigHandle resolveFtpAuthConfig(); - - AuthConfigHandle resolveHttpProxyAuthConfig(); - const string& getMethod() const { return method; } diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 395e1d68..99f8bfc9 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -39,7 +39,6 @@ #include "SegmentManFactory.h" #include "Dependency.h" #include "prefs.h" -#include "RequestFactory.h" #include "InitiateConnectionCommandFactory.h" #include "CUIDCounter.h" #include "File.h" @@ -236,7 +235,7 @@ Commands RequestGroup::createNextCommand(DownloadEngine* e, int32_t numCommand, for(;!_uris.empty() && numCommand--; _uris.pop_front()) { string uri = _uris.front(); _spentUris.push_back(uri); - RequestHandle req = RequestFactorySingletonHolder::instance()->createRequest(); + RequestHandle req = new Request(); req->setReferer(_option->get(PREF_REFERER)); req->setMethod(method); if(req->setUrl(uri)) { diff --git a/src/main.cc b/src/main.cc index b7e171b4..761a3939 100644 --- a/src/main.cc +++ b/src/main.cc @@ -40,7 +40,7 @@ #include "BitfieldManFactory.h" #include "SimpleRandomizer.h" #include "Netrc.h" -#include "RequestFactory.h" +#include "AuthConfigFactory.h" #include "FatalException.h" #include "File.h" #include "CUIDCounter.h" @@ -58,6 +58,7 @@ #include "SingleFileDownloadContext.h" #include "DefaultBtContext.h" #include "RequestGroup.h" +#include "Option.h" #include #include #include @@ -153,8 +154,7 @@ int main(int argc, char* argv[]) { logger->info("%s %s", PACKAGE, PACKAGE_VERSION); logger->info(MSG_LOGGING_STARTED); - RequestFactoryHandle requestFactory = new RequestFactory(); - requestFactory->setOption(op); + AuthConfigFactoryHandle authConfigFactory = new AuthConfigFactory(op); File netrccf(op->get(PREF_NETRC_PATH)); if(!op->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) { mode_t mode = netrccf.mode(); @@ -164,7 +164,7 @@ int main(int argc, char* argv[]) { } else { NetrcHandle netrc = new Netrc(); netrc->parse(op->get(PREF_NETRC_PATH)); - requestFactory->setNetrc(netrc); + authConfigFactory->setNetrc(netrc); } } @@ -181,7 +181,7 @@ int main(int argc, char* argv[]) { } } - RequestFactorySingletonHolder::instance(requestFactory); + AuthConfigFactorySingleton::instance(authConfigFactory); CUIDCounterHandle cuidCounter = new CUIDCounter(); CUIDCounterSingletonHolder::instance(cuidCounter); #ifdef SIGPIPE diff --git a/test/AuthConfigFactoryTest.cc b/test/AuthConfigFactoryTest.cc new file mode 100644 index 00000000..57233e60 --- /dev/null +++ b/test/AuthConfigFactoryTest.cc @@ -0,0 +1,124 @@ +#include "AuthConfigFactory.h" +#include "Netrc.h" +#include "prefs.h" +#include "Request.h" +#include "AuthConfig.h" +#include "Option.h" +#include + +class AuthConfigFactoryTest:public CppUnit::TestFixture { + + CPPUNIT_TEST_SUITE(AuthConfigFactoryTest); + CPPUNIT_TEST(testCreateAuthConfig_http); + CPPUNIT_TEST(testCreateAuthConfigForHttpProxy); + CPPUNIT_TEST(testCreateAuthConfig_ftp); + CPPUNIT_TEST_SUITE_END(); + +public: + void testCreateAuthConfig_http(); + void testCreateAuthConfigForHttpProxy(); + void testCreateAuthConfig_ftp(); +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION( AuthConfigFactoryTest ); + +void AuthConfigFactoryTest::testCreateAuthConfig_http() +{ + RequestHandle req = new Request(); + req->setUrl("http://localhost/download/aria2-1.0.0.tar.bz2"); + + Option option; + option.put(PREF_NO_NETRC, V_FALSE); + + AuthConfigFactory factory(&option); + + // without auth info + CPPUNIT_ASSERT_EQUAL(string(":"), + factory.createAuthConfig(req)->getAuthText()); + + // with Netrc: disabled by default + NetrcHandle netrc = new Netrc(); + netrc->addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")); + factory.setNetrc(netrc); + CPPUNIT_ASSERT_EQUAL(string(":"), + factory.createAuthConfig(req)->getAuthText()); + + // with Netrc + user defined + option.put(PREF_HTTP_USER, "userDefinedUser"); + option.put(PREF_HTTP_PASSWD, "userDefinedPassword"); + CPPUNIT_ASSERT_EQUAL(string("userDefinedUser:userDefinedPassword"), + factory.createAuthConfig(req)->getAuthText()); + + // username and password in URI: disabled by default. + req->setUrl("http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2"); + CPPUNIT_ASSERT_EQUAL(string("userDefinedUser:userDefinedPassword"), + factory.createAuthConfig(req)->getAuthText()); + +// CPPUNIT_ASSERT_EQUAL(string("aria2user:aria2password"), +// factory.createAuthConfig(req)->getAuthText()); +} + +void AuthConfigFactoryTest::testCreateAuthConfigForHttpProxy() +{ + RequestHandle req = new Request(); + req->setUrl("http://localhost/download/aria2-1.0.0.tar.bz2"); + // with Netrc + NetrcHandle netrc = new Netrc(); + netrc->addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")); + + Option option; + option.put(PREF_NO_NETRC, V_FALSE); + + AuthConfigFactory factory(&option); + factory.setNetrc(netrc); + + // netrc is not used in http proxy auth + CPPUNIT_ASSERT_EQUAL(string(":"), + factory.createAuthConfigForHttpProxy(req)->getAuthText()); + + option.put(PREF_HTTP_PROXY_USER, "userDefinedUser"); + option.put(PREF_HTTP_PROXY_PASSWD, "userDefinedPassword"); + CPPUNIT_ASSERT_EQUAL(string("userDefinedUser:userDefinedPassword"), + factory.createAuthConfigForHttpProxy(req)->getAuthText()); + +} + +void AuthConfigFactoryTest::testCreateAuthConfig_ftp() +{ + RequestHandle req = new Request(); + req->setUrl("ftp://localhost/download/aria2-1.0.0.tar.bz2"); + + Option option; + option.put(PREF_NO_NETRC, V_FALSE); + + AuthConfigFactory factory(&option); + + // without auth info + CPPUNIT_ASSERT_EQUAL(string("anonymous:ARIA2USER@"), + factory.createAuthConfig(req)->getAuthText()); + + // with Netrc + NetrcHandle netrc = new Netrc(); + netrc->addAuthenticator(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")); + factory.setNetrc(netrc); + CPPUNIT_ASSERT_EQUAL(string("default:defaultpassword"), + factory.createAuthConfig(req)->getAuthText()); + + // disable Netrc + option.put(PREF_NO_NETRC, V_TRUE); + CPPUNIT_ASSERT_EQUAL(string("anonymous:ARIA2USER@"), + factory.createAuthConfig(req)->getAuthText()); + + // with Netrc + user defined + option.put(PREF_NO_NETRC, V_FALSE); + option.put(PREF_FTP_USER, "userDefinedUser"); + option.put(PREF_FTP_PASSWD, "userDefinedPassword"); + CPPUNIT_ASSERT_EQUAL(string("userDefinedUser:userDefinedPassword"), + factory.createAuthConfig(req)->getAuthText()); + + // username and password in URI: disabled by default. + req->setUrl("ftp://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2"); + CPPUNIT_ASSERT_EQUAL(string("aria2user:aria2password"), + factory.createAuthConfig(req)->getAuthText()); +} diff --git a/test/HttpRequestTest.cc b/test/HttpRequestTest.cc index 836b7dd9..df79b514 100644 --- a/test/HttpRequestTest.cc +++ b/test/HttpRequestTest.cc @@ -1,6 +1,6 @@ #include "HttpRequest.h" #include "prefs.h" -#include "RequestFactory.h" +#include "AuthConfigFactory.h" #include "PiecedSegment.h" #include "Piece.h" #include @@ -84,31 +84,30 @@ void HttpRequestTest::testGetEndByte() void HttpRequestTest::testCreateRequest() { - SharedHandle