From 92d702fa5349c722c6b59256bafe552c8b5cd8a1 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 25 Sep 2008 14:37:28 +0000 Subject: [PATCH] 2008-09-25 Tatsuhiro Tsujikawa Use netrc for HTTP. Now FTP user/password is sent in Authorization header when --ftp-via-http-proxy=get is given. * src/AuthConfigFactory.cc * src/HttpRequest.cc * src/HttpRequest.h * src/NetrcAuthResolver.cc * src/NetrcAuthResolver.h * src/OptionHandlerFactory.cc * src/option_processing.cc * src/prefs.cc * src/prefs.h * test/AuthConfigFactoryTest.cc * test/HttpRequestTest.cc * test/NetrcAuthResolverTest.cc --- ChangeLog | 18 +++++++ src/AuthConfigFactory.cc | 3 +- src/HttpRequest.cc | 8 ++-- src/HttpRequest.h | 11 +---- src/NetrcAuthResolver.cc | 19 +++++++- src/NetrcAuthResolver.h | 10 ++++ src/OptionHandlerFactory.cc | 1 + src/option_processing.cc | 3 -- src/prefs.cc | 2 - src/prefs.h | 2 - test/AuthConfigFactoryTest.cc | 21 +++++--- test/HttpRequestTest.cc | 90 ++++++++++++++++------------------- test/NetrcAuthResolverTest.cc | 15 ++++++ 13 files changed, 125 insertions(+), 78 deletions(-) diff --git a/ChangeLog b/ChangeLog index 64ed2c63..2206e1d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2008-09-25 Tatsuhiro Tsujikawa + + Use netrc for HTTP. + Now FTP user/password is sent in Authorization header when + --ftp-via-http-proxy=get is given. + * src/AuthConfigFactory.cc + * src/HttpRequest.cc + * src/HttpRequest.h + * src/NetrcAuthResolver.cc + * src/NetrcAuthResolver.h + * src/OptionHandlerFactory.cc + * src/option_processing.cc + * src/prefs.cc + * src/prefs.h + * test/AuthConfigFactoryTest.cc + * test/HttpRequestTest.cc + * test/NetrcAuthResolverTest.cc + 2008-09-25 Tatsuhiro Tsujikawa Issue PWD command first and get working directory and use it as a prefix diff --git a/src/AuthConfigFactory.cc b/src/AuthConfigFactory.cc index 9291e442..bbcdba1b 100644 --- a/src/AuthConfigFactory.cc +++ b/src/AuthConfigFactory.cc @@ -88,11 +88,12 @@ AuthConfigFactory::createAuthConfig(const std::string& user, const std::string& AuthResolverHandle AuthConfigFactory::createHttpAuthResolver() const { AbstractAuthResolverHandle resolver; - if(true || _option->getAsBool(PREF_NO_NETRC)) { + if(_option->getAsBool(PREF_NO_NETRC)) { resolver.reset(new DefaultAuthResolver()); } else { NetrcAuthResolverHandle authResolver(new NetrcAuthResolver()); authResolver->setNetrc(_netrc); + authResolver->ignoreDefault(); resolver = authResolver; } resolver->setUserDefinedAuthConfig(createAuthConfig(_option->get(PREF_HTTP_USER), _option->get(PREF_HTTP_PASSWD))); diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index 08235683..82d9c443 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -52,7 +52,6 @@ namespace aria2 { const std::string HttpRequest::USER_AGENT("aria2"); HttpRequest::HttpRequest():entityLength(0), - authEnabled(false), proxyEnabled(false), proxyAuthEnabled(false), _contentEncodingEnabled(true), @@ -193,9 +192,11 @@ std::string HttpRequest::createRequest() const if(proxyEnabled && proxyAuthEnabled) { requestLine += getProxyAuthString(); } - if(authEnabled) { + std::string authString = AuthConfigFactorySingleton::instance() + ->createAuthConfig(request)->getAuthText(); + if(authString != ":") { requestLine += "Authorization: Basic "+ - Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfig(request)->getAuthText())+"\r\n"; + Base64::encode(authString)+"\r\n"; } if(getPreviousURI().size()) { requestLine += "Referer: "+getPreviousURI()+"\r\n"; @@ -274,7 +275,6 @@ void HttpRequest::addAcceptType(const std::string& type) void HttpRequest::configure(const Option* option) { - authEnabled = option->getAsBool(PREF_HTTP_AUTH_ENABLED); proxyEnabled = option->getAsBool(PREF_HTTP_PROXY_ENABLED) && option->get(PREF_HTTP_PROXY_METHOD) == V_GET; diff --git a/src/HttpRequest.h b/src/HttpRequest.h index c6f05b94..9f364873 100644 --- a/src/HttpRequest.h +++ b/src/HttpRequest.h @@ -59,8 +59,6 @@ private: uint64_t entityLength; - bool authEnabled; - bool proxyEnabled; bool proxyAuthEnabled; @@ -147,10 +145,8 @@ public: /** * Configures this object with option. * Following values are evaluated: - * PREF_HTTP_AUTH_ENABLED, PREF_HTTP_PROXY_ENABLED, + * PREF_HTTP_PROXY_ENABLED, * PREF_HTTP_PROXY_METHOD, PREF_HTTP_PROXY_AUTH_ENABLED, - * PREF_HTTP_USER, PREF_HTTP_PASSWD, - * PREF_HTTP_PROXY_USER, PREF_HTTP_PROXY_PASSWD * The evaluation results are stored in instance variables. */ void configure(const Option* option); @@ -165,11 +161,6 @@ public: this->proxyAuthEnabled = proxyAuthEnabled; } - void setAuthEnabled(bool authEnabled) - { - this->authEnabled = authEnabled; - } - void enableContentEncoding(); void disableContentEncoding(); diff --git a/src/NetrcAuthResolver.cc b/src/NetrcAuthResolver.cc index 647af81a..6a77d16d 100644 --- a/src/NetrcAuthResolver.cc +++ b/src/NetrcAuthResolver.cc @@ -38,6 +38,8 @@ namespace aria2 { +NetrcAuthResolver::NetrcAuthResolver():_ignoreDefault(false) {} + AuthConfigHandle NetrcAuthResolver::resolveAuthConfig(const std::string& hostname) { if(_userDefinedAuthConfig.isNull()) { @@ -56,7 +58,12 @@ AuthConfigHandle NetrcAuthResolver::findNetrcAuthenticator(const std::string& ho if(auth.isNull()) { return _defaultAuthConfig; } else { - return SharedHandle(new AuthConfig(auth->getLogin(), auth->getPassword())); + if(_ignoreDefault && auth->getMachine() == "") { + return _defaultAuthConfig; + } else { + return SharedHandle + (new AuthConfig(auth->getLogin(), auth->getPassword())); + } } } } @@ -71,4 +78,14 @@ NetrcHandle NetrcAuthResolver::getNetrc() const return _netrc; } +void NetrcAuthResolver::ignoreDefault() +{ + _ignoreDefault = true; +} + +void NetrcAuthResolver::useDefault() +{ + _ignoreDefault = false; +} + } // namespace aria2 diff --git a/src/NetrcAuthResolver.h b/src/NetrcAuthResolver.h index bf19559f..290dcbe7 100644 --- a/src/NetrcAuthResolver.h +++ b/src/NetrcAuthResolver.h @@ -45,8 +45,12 @@ class NetrcAuthResolver : public AbstractAuthResolver { private: SharedHandle _netrc; + bool _ignoreDefault; + SharedHandle findNetrcAuthenticator(const std::string& hostname) const; public: + NetrcAuthResolver(); + virtual ~NetrcAuthResolver() {} virtual SharedHandle resolveAuthConfig(const std::string& hostname); @@ -54,6 +58,12 @@ public: void setNetrc(const SharedHandle& netrc); SharedHandle getNetrc() const; + + // Ignores default token of netrc + void ignoreDefault(); + + // Uses default token of netrc + void useDefault(); }; typedef SharedHandle NetrcAuthResolverHandle; diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index bd9825d1..ae39e29a 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -612,6 +612,7 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers() TEXT_NO_NETRC, V_FALSE)); // TODO ommit? op->addTag(TAG_FTP); + op->addTag(TAG_HTTP); handlers.push_back(op); } // BitTorrent/Metalink Options diff --git a/src/option_processing.cc b/src/option_processing.cc index 7045fd7b..50c4bf5a 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -524,9 +524,6 @@ Option* option_processing(int argc, char* const argv[]) exit(EXIT_FAILURE); } } - if(op->defined(PREF_HTTP_USER)) { - op->put(PREF_HTTP_AUTH_ENABLED, V_TRUE); - } if(op->defined(PREF_HTTP_PROXY_USER)) { op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE); } diff --git a/src/prefs.cc b/src/prefs.cc index 27d9c2be..e55b7332 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -172,8 +172,6 @@ const std::string PREF_HTTP_PASSWD("http-passwd"); // values: basic const std::string PREF_HTTP_AUTH_SCHEME("http-auth-scheme"); const std::string V_BASIC("basic"); -// values: true | false -const std::string PREF_HTTP_AUTH_ENABLED("http-auth-enabled"); // values: string const std::string PREF_USER_AGENT("user-agent"); // value: string that your file system recognizes as a file name. diff --git a/src/prefs.h b/src/prefs.h index af4bee9f..75822be6 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -176,8 +176,6 @@ extern const std::string PREF_HTTP_PASSWD; // values: basic extern const std::string PREF_HTTP_AUTH_SCHEME; extern const std::string V_BASIC; -// values: true | false -extern const std::string PREF_HTTP_AUTH_ENABLED; // values: string extern const std::string PREF_USER_AGENT; // value: string that your file system recognizes as a file name. diff --git a/test/AuthConfigFactoryTest.cc b/test/AuthConfigFactoryTest.cc index 8a9fcd44..67eaac18 100644 --- a/test/AuthConfigFactoryTest.cc +++ b/test/AuthConfigFactoryTest.cc @@ -39,14 +39,26 @@ void AuthConfigFactoryTest::testCreateAuthConfig_http() CPPUNIT_ASSERT_EQUAL(std::string(":"), factory.createAuthConfig(req)->getAuthText()); - // with Netrc: disabled by default + // with Netrc SharedHandle netrc(new Netrc()); + netrc->addAuthenticator + (SharedHandle(new Authenticator("localhost", + "localhostuser", + "localhostpass", + "localhostacct"))); netrc->addAuthenticator (SharedHandle(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount"))); factory.setNetrc(netrc); - CPPUNIT_ASSERT_EQUAL(std::string(":"), + + CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"), factory.createAuthConfig(req)->getAuthText()); + // See default token in netrc is ignored. + SharedHandle mirrorReq(new Request()); + req->setUrl("http://mirror/"); + CPPUNIT_ASSERT_EQUAL(std::string(":"), + factory.createAuthConfig(mirrorReq)->getAuthText()); + // with Netrc + user defined option.put(PREF_HTTP_USER, "userDefinedUser"); option.put(PREF_HTTP_PASSWD, "userDefinedPassword"); @@ -56,10 +68,7 @@ void AuthConfigFactoryTest::testCreateAuthConfig_http() // username and password in URI: disabled by default. req->setUrl("http://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2"); CPPUNIT_ASSERT_EQUAL(std::string("userDefinedUser:userDefinedPassword"), - factory.createAuthConfig(req)->getAuthText()); - -// CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"), -// factory.createAuthConfig(req)->getAuthText()); + factory.createAuthConfig(req)->getAuthText()); } void AuthConfigFactoryTest::testCreateAuthConfigForHttpProxy() diff --git a/test/HttpRequestTest.cc b/test/HttpRequestTest.cc index 12491548..41c58888 100644 --- a/test/HttpRequestTest.cc +++ b/test/HttpRequestTest.cc @@ -29,9 +29,17 @@ class HttpRequestTest : public CppUnit::TestFixture { CPPUNIT_TEST(testEnableAcceptEncoding); CPPUNIT_TEST_SUITE_END(); private: - + SharedHandle