libzypp  17.32.4
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-core/base/Logger.h>
17 #include <zypp-core/base/String.h>
18 #include <zypp-core/fs/WatchFile>
19 #include <zypp-core/Pathname.h>
20 
21 #include <zypp-curl/proxyinfo/ProxyInfoLibproxy>
22 
23 using std::endl;
24 using namespace zypp::base;
25 
26 namespace zypp {
27  namespace media {
28 
29  struct TmpUnsetEnv
30  {
31  TmpUnsetEnv(const char *var_r) : _set(false), _var(var_r) {
32  const char * val = getenv( _var.c_str() );
33  if ( val )
34  {
35  _set = true;
36  _val = val;
37  ::unsetenv( _var.c_str() );
38  }
39  }
40 
41  TmpUnsetEnv(const TmpUnsetEnv &) = delete;
42  TmpUnsetEnv(TmpUnsetEnv &&) = delete;
43  TmpUnsetEnv &operator=(const TmpUnsetEnv &) = delete;
44  TmpUnsetEnv &operator=(TmpUnsetEnv &&) = delete;
45 
47  {
48  if ( _set )
49  {
50  setenv( _var.c_str(), _val.c_str(), 1 );
51  }
52  }
53 
54  bool _set;
55  std::string _var;
56  std::string _val;
57  };
58 
59  static pxProxyFactory * getProxyFactory()
60  {
61  static pxProxyFactory * proxyFactory = 0;
62 
63  // Force libproxy into using "/etc/sysconfig/proxy"
64  // if it exists.
65  static WatchFile sysconfigProxy( "/etc/sysconfig/proxy", WatchFile::NO_INIT );
66  if ( sysconfigProxy.hasChanged() )
67  {
68  MIL << "Build Libproxy Factory from /etc/sysconfig/proxy" << endl;
69  if ( proxyFactory )
70  ::px_proxy_factory_free( proxyFactory );
71 
72  TmpUnsetEnv envguard[] __attribute__ ((__unused__)) = { "KDE_FULL_SESSION", "GNOME_DESKTOP_SESSION_ID", "DESKTOP_SESSION" };
73  proxyFactory = ::px_proxy_factory_new();
74  }
75  else if ( ! proxyFactory )
76  {
77  MIL << "Build Libproxy Factory" << endl;
78  proxyFactory = ::px_proxy_factory_new();
79  }
80 
81  return proxyFactory;
82  }
83 
84  ProxyInfoLibproxy::ProxyInfoLibproxy()
85  : ProxyInfo::Impl()
86  {
88  _enabled = !(_factory == NULL);
89  }
90 
92  {}
93 
94  std::string ProxyInfoLibproxy::proxy(const Url & url_r) const
95  {
96  if (!_enabled)
97  return "";
98 
99  const url::ViewOption vopt =
104 
105  char **proxies = px_proxy_factory_get_proxies(_factory,
106  (char *)url_r.asString(vopt).c_str());
107  if (!proxies)
108  return "";
109 
110  /* cURL can only handle HTTP proxies, not SOCKS. And can only handle
111  one. So look through the list and find an appropriate one. */
112  char *result = NULL;
113 
114  for (int i = 0; proxies[i]; i++) {
115  if (!result &&
116  !strncmp(proxies[i], "http://", 7))
117  result = proxies[i];
118  else
119  free(proxies[i]);
120  }
121  free(proxies);
122 
123  if (!result)
124  return "";
125 
126  std::string sresult = result;
127  free(result);
128  return sresult;
129  }
130 
132  { return _no_proxy.begin(); }
133 
135  { return _no_proxy.end(); }
136 
137  } // namespace media
138 } // namespace zypp
#define MIL
Definition: Logger.h:96
std::string proxy(const Url &url_r) const override
bool hasChanged()
Definition: watchfile.h:80
std::list< std::string >::const_iterator NoProxyIterator
Definition: proxyinfo.h:35
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:501
DefaultIntegral< bool, false > _enabled
ProxyInfo::NoProxyList _no_proxy
ProxyInfo::NoProxyIterator noProxyEnd() const override
TmpUnsetEnv(const char *var_r)
struct zypp::media::MediaBlock __attribute__
ProxyInfo::NoProxyIterator noProxyBegin() const override
static const ViewOption WITH_PORT
Option to include port number in the URL string.
Definition: UrlBase.h:81
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
Url manipulation class.
Definition: Url.h:91