Browse Source

Use same domain-match algorithm for no-proxy and netrc.

Now "example.org" does not domain-match ".example.org" in both
functions.
Tatsuhiro Tsujikawa 14 years ago
parent
commit
0ba6f8c352
6 changed files with 39 additions and 34 deletions
  1. 1 24
      src/AbstractCommand.cc
  2. 1 9
      src/Netrc.cc
  3. 12 0
      src/util.cc
  4. 11 0
      src/util.h
  5. 1 1
      test/NetrcTest.cc
  6. 13 0
      test/UtilTest.cc

+ 1 - 24
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<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 {

+ 1 - 9
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)

+ 12 - 0
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

+ 11 - 0
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

+ 1 - 1
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()

+ 13 - 0
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