libzypp  13.10.6
ProxyInfoLibproxy.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <iostream>
14 #include <fstream>
15 
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/String.h"
18 #include "zypp/base/WatchFile.h"
19 #include "zypp/Pathname.h"
20 
22 
23 using namespace std;
24 using namespace zypp::base;
25 
26 namespace zypp {
27  namespace media {
28 
29  struct TmpUnsetEnv
30  {
31  TmpUnsetEnv( const char * var_r )
32  : _set( false )
33  , _var( var_r )
34  {
35  const char * val = getenv( _var.c_str() );
36  if ( val )
37  {
38  _set = true;
39  _val = val;
40  ::unsetenv( _var.c_str() );
41  }
42  }
43 
45  {
46  if ( _set )
47  {
48  setenv( _var.c_str(), _val.c_str(), 1 );
49  }
50  }
51 
52  bool _set;
53  std::string _var;
54  std::string _val;
55  };
56 
57  static pxProxyFactory * getProxyFactory()
58  {
59  static pxProxyFactory * proxyFactory = 0;
60 
61  // Force libproxy into using "/etc/sysconfig/proxy"
62  // if it exists.
63  static WatchFile sysconfigProxy( "/etc/sysconfig/proxy", WatchFile::NO_INIT );
64  if ( sysconfigProxy.hasChanged() )
65  {
66  MIL << "Build Libproxy Factory from /etc/sysconfig/proxy" << endl;
67  if ( proxyFactory )
68  ::px_proxy_factory_free( proxyFactory );
69 
70  TmpUnsetEnv envguard[] __attribute__ ((__unused__)) = { "KDE_FULL_SESSION", "GNOME_DESKTOP_SESSION_ID", "DESKTOP_SESSION" };
71  proxyFactory = ::px_proxy_factory_new();
72  }
73  else if ( ! proxyFactory )
74  {
75  MIL << "Build Libproxy Factory" << endl;
76  proxyFactory = ::px_proxy_factory_new();
77  }
78 
79  return proxyFactory;
80  }
81 
82  ProxyInfoLibproxy::ProxyInfoLibproxy()
83  : ProxyInfo::Impl()
84  {
86  _enabled = !(_factory == NULL);
87  }
88 
90  {}
91 
92  std::string ProxyInfoLibproxy::proxy(const Url & url_r) const
93  {
94  if (!_enabled)
95  return "";
96 
97  const url::ViewOption vopt =
102 
103  char **proxies = px_proxy_factory_get_proxies(_factory,
104  (char *)url_r.asString(vopt).c_str());
105  if (!proxies)
106  return "";
107 
108  /* cURL can only handle HTTP proxies, not SOCKS. And can only handle
109  one. So look through the list and find an appropriate one. */
110  char *result = NULL;
111 
112  for (int i = 0; proxies[i]; i++) {
113  if (!result &&
114  !strncmp(proxies[i], "http://", 7))
115  result = proxies[i];
116  else
117  free(proxies[i]);
118  }
119  free(proxies);
120 
121  if (!result)
122  return "";
123 
124  std::string sresult = result;
125  free(result);
126  return sresult;
127  }
128 
130  { return _no_proxy.begin(); }
131 
133  { return _no_proxy.end(); }
134 
135  } // namespace media
136 } // namespace zypp
#define MIL
Definition: Logger.h:47
bool hasChanged()
Definition: WatchFile.h:68
static const ViewOption WITH_SCHEME
Option to include scheme name in the URL string.
Definition: UrlBase.h:51
static const ViewOption WITH_HOST
Option to include hostname in the URL string.
Definition: UrlBase.h:74
static const ViewOption WITH_PATH_NAME
Option to include path name in the URL string.
Definition: UrlBase.h:87
Url::asString() view options.
Definition: UrlBase.h:39
static pxProxyFactory * getProxyFactory()
Remember a files attributes to detect content changes.
Definition: WatchFile.h:49
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:491
DefaultIntegral< bool, false > _enabled
virtual ProxyInfo::NoProxyIterator noProxyBegin() const
ProxyInfo::NoProxyList _no_proxy
std::list< std::string >::const_iterator NoProxyIterator
Definition: ProxyInfo.h:35
virtual ProxyInfo::NoProxyIterator noProxyEnd() const
TmpUnsetEnv(const char *var_r)
std::string proxy(const Url &url_r) const
static const ViewOption WITH_PORT
Option to include port number in the URL string.
Definition: UrlBase.h:81
Url manipulation class.
Definition: Url.h:87