summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-10-29 00:16:46 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-10-29 00:16:46 +0900
commit0ba6f8c352da64db26907cbfa4ff9579700562e0 (patch)
treea243284f5bfc73faa984762f54cce587b49b4c56
parent69966d7ac995436695b59ac8e9b736343308c277 (diff)
Use same domain-match algorithm for no-proxy and netrc.
Now "example.org" does not domain-match ".example.org" in both functions.
-rw-r--r--src/AbstractCommand.cc25
-rw-r--r--src/Netrc.cc10
-rw-r--r--src/util.cc12
-rw-r--r--src/util.h11
-rw-r--r--test/NetrcTest.cc2
-rw-r--r--test/UtilTest.cc13
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
@@ -586,24 +586,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<Request>& req,
const std::string& noProxy)
{
@@ -612,16 +594,11 @@ bool inNoProxy(const SharedHandle<Request>& req,
if(entries.empty()) {
return false;
}
- DomainMatch domainMatch(A2STR::DOT_C+req->getHost());
for(std::vector<std::string>::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<T> copy(const SharedHandle<T>& a)
return SharedHandle<T>(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<Authenticator> 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