libzypp 17.31.23
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
23using std::endl;
24using namespace zypp::base;
25
26namespace zypp {
27 namespace media {
28
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
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
RepoManager implementation.
Url manipulation class.
Definition: Url.h:92
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:497
Remember a files attributes to detect content changes.
Definition: watchfile.h:50
bool hasChanged()
Definition: watchfile.h:80
virtual ProxyInfo::NoProxyIterator noProxyEnd() const
DefaultIntegral< bool, false > _enabled
virtual ProxyInfo::NoProxyIterator noProxyBegin() const
std::string proxy(const Url &url_r) const
ProxyInfo::NoProxyList _no_proxy
std::list< std::string >::const_iterator NoProxyIterator
Definition: proxyinfo.h:35
static pxProxyFactory * getProxyFactory()
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
TmpUnsetEnv(const char *var_r)
Url::asString() view options.
Definition: UrlBase.h:40
static const ViewOption WITH_SCHEME
Option to include scheme name in the URL string.
Definition: UrlBase.h:51
static const ViewOption WITH_PORT
Option to include port number in the URL string.
Definition: UrlBase.h:81
static const ViewOption WITH_PATH_NAME
Option to include path name in the URL string.
Definition: UrlBase.h:87
static const ViewOption WITH_HOST
Option to include hostname in the URL string.
Definition: UrlBase.h:74
#define MIL
Definition: Logger.h:96