Force libproxy into using "/etc/sysconfig/proxy" if it exists (bnc#679322)
authorMichael Andres <ma@suse.de>
Tue, 13 Mar 2012 10:45:22 +0000 (11:45 +0100)
committerMichael Andres <ma@suse.de>
Tue, 13 Mar 2012 10:45:22 +0000 (11:45 +0100)
zypp/media/MediaCurl.cc
zypp/media/ProxyInfo.cc
zypp/media/ProxyInfo.h
zypp/media/proxyinfo/ProxyInfoLibproxy.cc
zypp/media/proxyinfo/ProxyInfoSysconfig.cc

index bd10ddc..8a5b4d6 100644 (file)
@@ -21,7 +21,6 @@
 #include "zypp/base/Gettext.h"
 
 #include "zypp/media/MediaCurl.h"
-#include "zypp/media/proxyinfo/ProxyInfos.h"
 #include "zypp/media/ProxyInfo.h"
 #include "zypp/media/MediaUserAuth.h"
 #include "zypp/media/CredentialManager.h"
@@ -328,11 +327,7 @@ void fillSettingsFromUrl( const Url &url, TransferSettings &s )
  */
 void fillSettingsSystemProxy( const Url&url, TransferSettings &s )
 {
-#ifdef _WITH_LIBPROXY_SUPPORT_
-    ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoLibproxy()));
-#else
-    ProxyInfo proxy_info (ProxyInfo::ImplPtr(new ProxyInfoSysconfig("proxy")));
-#endif
+    ProxyInfo proxy_info;
     if ( proxy_info.useProxyFor( url ) )
     {
       // We must extract any 'user:pass' from the proxy url
index 6e05271..b50593c 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "zypp/media/ProxyInfo.h"
 #include "zypp/media/proxyinfo/ProxyInfoImpl.h"
+#include "zypp/media/proxyinfo/ProxyInfos.h"
 
 using namespace std;
 using namespace zypp::base;
@@ -26,8 +27,13 @@ namespace zypp {
     shared_ptr<ProxyInfo::Impl> ProxyInfo::Impl::_nullimpl;
 
     ProxyInfo::ProxyInfo()
-    : _pimpl( Impl::_nullimpl )
+#ifdef _WITH_LIBPROXY_SUPPORT_
+    : _pimpl( new ProxyInfoLibproxy() )
+#else
+    : _pimpl( new ProxyInfoSysconfig("proxy") )
+#endif
     {}
+
     ProxyInfo::ProxyInfo(ProxyInfo::ImplPtr pimpl_r)
     : _pimpl(pimpl_r)
     {}
index 2e790b2..6724194 100644 (file)
@@ -37,10 +37,12 @@ namespace zypp {
       /** Implementation */
       struct Impl;
       typedef shared_ptr<Impl> ImplPtr;
-      /** Ctor */
+
+      /** Default Ctor: guess the best available implementation. */
       ProxyInfo();
-      /** Ctor */
-      ProxyInfo(ProxyInfo::ImplPtr pimpl_r);
+      /** Ctor taking a specific implementation. */
+      ProxyInfo( ProxyInfo::ImplPtr pimpl_r );
+
       bool enabled() const;
       std::string proxy(const Url & url) const;
       NoProxyList noProxy() const;
index 9baaa22..c8217bd 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/String.h"
+#include "zypp/base/WatchFile.h"
 #include "zypp/Pathname.h"
 
 #include "zypp/media/proxyinfo/ProxyInfoLibproxy.h"
@@ -25,21 +26,68 @@ using namespace zypp::base;
 namespace zypp {
   namespace media {
 
+    struct TmpUnsetEnv
+    {
+      TmpUnsetEnv( const char * var_r )
+      : _set( false )
+      , _var( var_r )
+      {
+       const char * val = getenv( _var.c_str() );
+       if ( val )
+       {
+         _set = true;
+         _val = val;
+         ::unsetenv( _var.c_str() );
+       }
+      }
+
+      ~TmpUnsetEnv()
+      {
+       if ( _set )
+       {
+         setenv( _var.c_str(), _val.c_str(), 1 );
+       }
+      }
+
+      bool _set;
+      std::string _var;
+      std::string _val;
+    };
+
+    static pxProxyFactory * getProxyFactory()
+    {
+      static pxProxyFactory * proxyFactory = 0;
+
+      // Force libproxy into using "/etc/sysconfig/proxy"
+      // if it exists.
+      static WatchFile sysconfigProxy( "/etc/sysconfig/proxy", WatchFile::NO_INIT );
+      if ( sysconfigProxy.hasChanged() )
+      {
+       MIL << "Build Libproxy Factory from /etc/sysconfig/proxy" << endl;
+       if ( proxyFactory )
+         ::px_proxy_factory_free( proxyFactory );
+
+       TmpUnsetEnv env[] = { "KDE_FULL_SESSION", "GNOME_DESKTOP_SESSION_ID", "DESKTOP_SESSION" };
+       proxyFactory = ::px_proxy_factory_new();
+      }
+      else if ( ! proxyFactory )
+      {
+       MIL << "Build Libproxy Factory" << endl;
+       proxyFactory = ::px_proxy_factory_new();
+      }
+
+      return proxyFactory;
+    }
+
     ProxyInfoLibproxy::ProxyInfoLibproxy()
     : ProxyInfo::Impl()
     {
-      _factory = px_proxy_factory_new();
+      _factory = getProxyFactory();
       _enabled = !(_factory == NULL);
     }
 
     ProxyInfoLibproxy::~ProxyInfoLibproxy()
-    {
-      if (_enabled) {
-       px_proxy_factory_free(_factory);
-       _factory = NULL;
-       _enabled = false;
-      }
-    }
+    {}
 
     std::string ProxyInfoLibproxy::proxy(const Url & url_r) const
     {
index 0af3daf..2a80539 100644 (file)
@@ -50,7 +50,7 @@ namespace zypp {
     }
 
     std::string ProxyInfoSysconfig::proxy(const Url & url_r) const
-    { 
+    {
       map<string,string>::const_iterator it = _proxies.find(url_r.getScheme());
       if (it != _proxies.end())
        return it->second;