Add ProxyInfo::useProxyFor(Url) to evaluate Url matching a no_proxy entry.
authorMichael Andres <ma@suse.de>
Thu, 1 Oct 2009 12:27:59 +0000 (14:27 +0200)
committerMichael Andres <ma@suse.de>
Thu, 1 Oct 2009 12:57:35 +0000 (14:57 +0200)
zypp/media/ProxyInfo.cc
zypp/media/ProxyInfo.h
zypp/media/proxyinfo/ProxyInfoImpl.h

index f17e039..0b496ec 100644 (file)
@@ -47,5 +47,8 @@ namespace zypp {
     ProxyInfo::NoProxyIterator ProxyInfo::noProxyEnd() const
     { return _pimpl->noProxyEnd(); }
 
+    bool ProxyInfo::useProxyFor( const Url & url_r ) const
+    { return _pimpl->useProxyFor( url_r ); }
+
   } // namespace media
 } // namespace zypp
index d704f23..2c7bed8 100644 (file)
@@ -18,6 +18,9 @@
 #include "zypp/base/PtrTypes.h"
 
 namespace zypp {
+
+  class Url;
+
   namespace media {
 
     ///////////////////////////////////////////////////////////////////
@@ -30,7 +33,7 @@ namespace zypp {
       typedef intrusive_ptr<ProxyInfo> constPtr;
       typedef std::list<std::string> NoProxyList;
       typedef std::list<std::string>::const_iterator NoProxyIterator;
-      
+
       /** Implementation */
       struct Impl;
       typedef shared_ptr<Impl> ImplPtr;
@@ -43,6 +46,10 @@ namespace zypp {
       NoProxyList noProxy() const;
       NoProxyIterator noProxyBegin() const;
       NoProxyIterator noProxyEnd() const;
+
+      /** Return \c true if  \ref enabled and \a url_r does not match \ref noProxy. */
+      bool useProxyFor( const Url & url_r ) const;
+
     private:
       /** Pointer to implementation */
       RW_pointer<Impl> _pimpl;
index 9a07c67..9996b3a 100644 (file)
@@ -15,6 +15,8 @@
 #include <string>
 #include <list>
 
+#include "zypp/Url.h"
+#include "zypp/base/String.h"
 #include "zypp/media/ProxyInfo.h"
 
 namespace zypp {
@@ -29,7 +31,7 @@ namespace zypp {
       /** Dtor */
       virtual ~Impl()
       {}
-  
+
     public:
       /**  */
       virtual bool enabled() const = 0;
@@ -41,7 +43,33 @@ namespace zypp {
       virtual ProxyInfo::NoProxyIterator noProxyBegin() const = 0;
       /**  */
       virtual ProxyInfo::NoProxyIterator noProxyEnd() const = 0;
-  
+
+      /** Return \c true if  \ref enabled and \a url_r does not match \ref noProxy. */
+      bool useProxyFor( const Url & url_r ) const
+      {
+        if ( ! enabled() )
+          return false;
+
+        ProxyInfo::NoProxyList noproxy( noProxy() );
+        if ( noproxy.size() == 1 && noproxy.front() == "*" )
+          return false; // just an asterisk disables all.
+
+        // No proxy: Either an exact match, or the previous character
+        // is a '.', so host is within the same domain.
+        // A leading '.' in the pattern is ignored. Some implementations
+        // need '.foo.ba' to prevent 'foo.ba' from matching 'xfoo.ba'.
+        std::string host( str::toLower( url_r.getHost() ) );
+        for_( it, noproxy.begin(), noproxy.end() )
+        {
+          std::string pattern( str::toLower( (*it)[0] == '.' ? it->c_str() + 1 : it->c_str() ) );
+          if ( str::hasSuffix( host, pattern )
+               && ( host.size() == pattern.size()
+                    || host[host.size()-pattern.size()-1] == '.' ) )
+            return false;
+        }
+        return true;
+      }
+
     public:
       /** Default Impl: empty sets. */
       static shared_ptr<Impl> _nullimpl;