From 0ba6f8c352da64db26907cbfa4ff9579700562e0 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 29 Oct 2011 00:16:46 +0900 Subject: [PATCH] Use same domain-match algorithm for no-proxy and netrc. Now "example.org" does not domain-match ".example.org" in both functions. --- src/AbstractCommand.cc | 25 +------------------------ src/Netrc.cc | 10 +--------- src/util.cc | 12 ++++++++++++ src/util.h | 11 +++++++++++ test/NetrcTest.cc | 2 +- test/UtilTest.cc | 13 +++++++++++++ 6 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index 491a9a24..e2636193 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -585,24 +585,6 @@ bool isProxyRequest } } // namespace -namespace { -class DomainMatch { -private: - std::string hostname_; -public: - DomainMatch(const std::string& hostname):hostname_(hostname) {} - - bool operator()(const std::string& domain) const - { - if(util::startsWith(domain, A2STR::DOT_C)) { - return util::endsWith(hostname_, domain); - } else { - return util::endsWith(hostname_, A2STR::DOT_C+domain); - } - } -}; -} // namespace - namespace { bool inNoProxy(const SharedHandle& req, const std::string& noProxy) @@ -612,16 +594,11 @@ bool inNoProxy(const SharedHandle& req, if(entries.empty()) { return false; } - DomainMatch domainMatch(A2STR::DOT_C+req->getHost()); for(std::vector::const_iterator i = entries.begin(), eoi = entries.end(); i != eoi; ++i) { std::string::size_type slashpos = (*i).find('/'); if(slashpos == std::string::npos) { - if(util::isNumericHost(*i)) { - if(req->getHost() == *i) { - return true; - } - } else if(domainMatch(*i)) { + if(util::noProxyDomainMatch(req->getHost(), *i)) { return true; } } else { diff --git a/src/Netrc.cc b/src/Netrc.cc index dc7618b9..29306a71 100644 --- a/src/Netrc.cc +++ b/src/Netrc.cc @@ -63,15 +63,7 @@ Authenticator::~Authenticator() {} bool Authenticator::match(const std::string& hostname) const { - if(util::isNumericHost(hostname)) { - return hostname == machine_; - } else { - if(util::startsWith(machine_, A2STR::DOT_C)) { - return util::endsWith(A2STR::DOT_C+hostname, machine_); - } else { - return hostname == machine_; - } - } + return util::noProxyDomainMatch(hostname, machine_); } void Authenticator::setMachine(const std::string& machine) diff --git a/src/util.cc b/src/util.cc index bd76b39f..b8e8782c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1752,6 +1752,18 @@ std::string safeStrerror(int errNum) return makeString(strerror(errNum)); } +bool noProxyDomainMatch +(const std::string& hostname, + const std::string& domain) +{ + if(!util::isNumericHost(hostname) && + util::startsWith(domain, A2STR::DOT_C)) { + return util::endsWith(hostname, domain); + } else { + return hostname == domain; + } +} + } // namespace util } // namespace aria2 diff --git a/src/util.h b/src/util.h index d51348fb..1fffd10a 100644 --- a/src/util.h +++ b/src/util.h @@ -520,6 +520,17 @@ SharedHandle copy(const SharedHandle& a) return SharedHandle(new T(*a.get())); } +// This is a bit different from cookie_helper::domainMatch(). If +// hostname is numeric host, then returns true if domain == hostname. +// That is if domain starts with ".", then returns true if domain is a +// suffix of hostname. If domain does not start with ".", then +// returns true if domain == hostname. Otherwise returns true. +// For example, +// +// * noProxyDomainMatch("aria2.sf.net", ".sf.net") returns true. +// * noProxyDomainMatch("sf.net", ".sf.net") returns false. +bool noProxyDomainMatch(const std::string& hostname, const std::string& domain); + } // namespace util } // namespace aria2 diff --git a/test/NetrcTest.cc b/test/NetrcTest.cc index 361a8da3..a47c046a 100644 --- a/test/NetrcTest.cc +++ b/test/NetrcTest.cc @@ -64,7 +64,7 @@ void NetrcTest::testFindAuthenticator() SharedHandle domainMatchAuth2 = netrc.findAuthenticator("my.domain"); CPPUNIT_ASSERT(domainMatchAuth2); - CPPUNIT_ASSERT_EQUAL(std::string("dmname"), domainMatchAuth2->getLogin()); + CPPUNIT_ASSERT_EQUAL(std::string("default"), domainMatchAuth2->getLogin()); } void NetrcTest::testParse() diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 74fc3101..141435c8 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -74,6 +74,7 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testInSameCidrBlock); CPPUNIT_TEST(testIsUtf8String); CPPUNIT_TEST(testNextParam); + CPPUNIT_TEST(testNoProxyDomainMatch); CPPUNIT_TEST_SUITE_END(); private: @@ -133,6 +134,7 @@ public: void testInSameCidrBlock(); void testIsUtf8String(); void testNextParam(); + void testNoProxyDomainMatch(); }; @@ -1277,4 +1279,15 @@ void UtilTest::testNextParam() CPPUNIT_ASSERT(!r.second); } +void UtilTest::testNoProxyDomainMatch() +{ + CPPUNIT_ASSERT(util::noProxyDomainMatch("localhost", "localhost")); + CPPUNIT_ASSERT(util::noProxyDomainMatch("192.168.0.1", "192.168.0.1")); + CPPUNIT_ASSERT(util::noProxyDomainMatch("www.example.org", ".example.org")); + CPPUNIT_ASSERT(!util::noProxyDomainMatch("www.example.org", "example.org")); + CPPUNIT_ASSERT(!util::noProxyDomainMatch("192.168.0.1", "0.1")); + CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "example.com")); + CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org")); +} + } // namespace aria2