Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / media / TransferSettings.cc
1 #include <iostream>
2 #include <sstream>
3
4 #include "zypp/base/String.h"
5 #include "zypp/base/Logger.h"
6 #include "zypp/base/WatchFile.h"
7 #include "zypp/base/ReferenceCounted.h"
8 #include "zypp/base/NonCopyable.h"
9 #include "zypp/ExternalProgram.h"
10 #include "zypp/media/TransferSettings.h"
11 #include "zypp/ZConfig.h"
12
13 using namespace std;
14
15 #define CURL_BINARY "/usr/bin/curl"
16
17 namespace zypp
18 {
19 namespace media
20 {
21
22 class TransferSettings::Impl
23 {
24 public:
25     Impl()
26         : _useproxy(false)
27         , _timeout(0)
28         , _connect_timeout(0)
29         , _maxConcurrentConnections(ZConfig::instance().download_max_concurrent_connections())
30         , _minDownloadSpeed(ZConfig::instance().download_min_download_speed())
31         , _maxDownloadSpeed(ZConfig::instance().download_max_download_speed())
32         , _maxSilentTries(ZConfig::instance().download_max_silent_tries())
33         , _verify_host(false)
34         , _verify_peer(false)
35         , _ca_path("/etc/ssl/certs")
36         , _head_requests_allowed(true)
37     {}
38
39     virtual ~Impl()
40     {}
41
42     /** Offer default Impl. */
43     static shared_ptr<Impl> nullimpl()
44     {
45       static shared_ptr<Impl> _nullimpl( new Impl );
46       return _nullimpl;
47     }
48
49 private:
50     friend Impl * rwcowClone<Impl>( const Impl * rhs );
51     /** clone for RWCOW_pointer */
52     Impl * clone() const
53     { return new Impl( *this ); }
54
55 public:
56     vector<string> _headers;
57     string _useragent;
58     string _username;
59     string _password;
60     bool _useproxy;
61     string _proxy;
62     string _proxy_username;
63     string _proxy_password;
64     string _authtype;
65     long _timeout;
66     long _connect_timeout;
67     Url _url;
68     Pathname _targetdir;
69
70     long _maxConcurrentConnections;
71     long _minDownloadSpeed;
72     long _maxDownloadSpeed;
73     long _maxSilentTries;
74
75     bool _verify_host;
76     bool _verify_peer;
77     Pathname _ca_path;
78     Pathname _client_cert_path;
79     Pathname _client_key_path;
80
81     // workarounds
82     bool _head_requests_allowed;
83 };
84
85 TransferSettings::TransferSettings()
86     : _impl(new TransferSettings::Impl())
87 {
88
89 }
90
91 void TransferSettings::reset()
92 {
93     _impl.reset(new TransferSettings::Impl());
94 }
95
96 void TransferSettings::addHeader( const std::string &header )
97 {
98     _impl->_headers.push_back(header);
99 }
100
101 TransferSettings::Headers::const_iterator TransferSettings::headersBegin() const
102 {
103     return _impl->_headers.begin();
104 }
105
106 TransferSettings::Headers::const_iterator TransferSettings::headersEnd() const
107 {
108     return _impl->_headers.end();
109 }
110
111 void TransferSettings::setUserAgentString( const std::string &agent )
112 {
113     _impl->_useragent = agent;
114 }
115
116 std::string TransferSettings::userAgentString() const
117 {
118     return _impl->_useragent;
119 }
120
121 void TransferSettings::setUsername( const std::string &username )
122 {
123     _impl->_username = username;
124 }
125
126 std::string TransferSettings::username() const
127 {
128     return _impl->_username;
129 }
130
131 void TransferSettings::setPassword( const std::string &password )
132 {
133     _impl->_password = password;
134 }
135
136 void TransferSettings::setAnonymousAuth()
137 {
138     setUsername("anonymous");
139     string id = "yast@";
140     setPassword(id + VERSION);
141 }
142
143 std::string TransferSettings::password() const
144 {
145     return _impl->_password;
146 }
147
148 std::string TransferSettings::userPassword() const
149 {
150     string userpwd = username();
151     if ( password().size() ) {
152         userpwd += ":" + password();
153     }
154     return userpwd;
155 }
156
157 void TransferSettings::setProxyEnabled( bool enabled )
158 {
159     _impl->_useproxy = enabled;
160 }
161
162 bool TransferSettings::proxyEnabled() const
163 {
164     return _impl->_useproxy;
165 }
166
167 void TransferSettings::setProxy( const std::string &proxy )
168 {
169     _impl->_proxy = proxy;
170 }
171
172 std::string TransferSettings::proxy() const
173 {
174     return _impl->_proxy;
175 }
176
177 void TransferSettings::setProxyUsername( const std::string &proxyuser )
178 {
179     _impl->_proxy_username = proxyuser;
180 }
181
182 std::string TransferSettings::proxyUsername() const
183 {
184     return _impl->_proxy_username;
185 }
186
187 void TransferSettings::setProxyPassword( const std::string &proxypass )
188 {
189     _impl->_proxy_password = proxypass;
190 }
191
192 std::string TransferSettings::proxyPassword() const
193 {
194     return _impl->_proxy_password;
195 }
196
197 std::string TransferSettings::proxyUserPassword() const
198 {
199     string userpwd = proxyUsername();
200     if ( proxyPassword().size() ) {
201         userpwd += ":" + proxyPassword();
202     }
203     return userpwd;
204 }
205
206 void TransferSettings::setTimeout( long t )
207 {
208     _impl->_timeout = t;
209 }
210
211 long TransferSettings::timeout() const
212 {
213     return _impl->_timeout;
214 }
215
216 void TransferSettings::setConnectTimeout( long t )
217 {
218     _impl->_connect_timeout = t;
219 }
220
221 long TransferSettings::connectTimeout() const
222 {
223     return _impl->_connect_timeout;
224 }
225
226 long TransferSettings::maxConcurrentConnections() const
227 {
228     return _impl->_maxConcurrentConnections;
229 }
230
231 void TransferSettings::setMaxConcurrentConnections(long v)
232 {
233     _impl->_maxConcurrentConnections = v;
234 }
235
236 long TransferSettings::minDownloadSpeed() const
237 {
238     return _impl->_minDownloadSpeed;
239 }
240
241 void TransferSettings::setMinDownloadSpeed(long v)
242 {
243     _impl->_minDownloadSpeed = v;
244 }
245
246 long TransferSettings::maxDownloadSpeed() const
247 {
248     return _impl->_maxDownloadSpeed;
249 }
250
251 void TransferSettings::setMaxDownloadSpeed(long v)
252 {
253     _impl->_maxDownloadSpeed = v;
254 }
255
256 long TransferSettings::maxSilentTries() const
257 {
258     return _impl->_maxSilentTries;
259 }
260
261 void TransferSettings::setMaxSilentTries(long v)
262 {
263     _impl->_maxSilentTries = v;
264 }
265
266 bool TransferSettings::verifyHostEnabled() const
267 {
268     return _impl->_verify_host;
269 }
270
271 void TransferSettings::setVerifyHostEnabled( bool enabled )
272 {
273     _impl->_verify_host = enabled;
274 }
275
276 bool TransferSettings::verifyPeerEnabled() const
277 {
278     return _impl->_verify_peer;
279 }
280
281 Pathname TransferSettings::clientCertificatePath() const
282 {
283     return _impl->_client_cert_path;
284 }
285
286 void TransferSettings::setClientCertificatePath( const zypp::Pathname &path )
287 {
288     _impl->_client_cert_path = path;
289 }
290
291 Pathname TransferSettings::clientKeyPath() const
292 {
293     return _impl->_client_key_path;
294 }
295
296 void TransferSettings::setClientKeyPath( const zypp::Pathname &path )
297 {
298     _impl->_client_key_path = path;
299 }
300
301
302 void TransferSettings::setVerifyPeerEnabled( bool enabled )
303 {
304     _impl->_verify_peer = enabled;
305 }
306
307 Pathname TransferSettings::certificateAuthoritiesPath() const
308 {
309     return _impl->_ca_path;
310 }
311
312 void TransferSettings::setCertificateAuthoritiesPath( const zypp::Pathname &path )
313 {
314     _impl->_ca_path = path;
315 }
316
317 void TransferSettings::setAuthType( const std::string &authtype)
318 {
319     _impl->_authtype = authtype;
320 }
321
322 std::string TransferSettings::authType() const
323 {
324     return _impl->_authtype;
325 }
326
327 void TransferSettings::setHeadRequestsAllowed(bool allowed)
328 {
329     _impl->_head_requests_allowed = allowed;
330 }
331
332 bool TransferSettings::headRequestsAllowed() const
333 {
334     return _impl->_head_requests_allowed;
335 }
336
337 } // ns media
338 } // ns zypp
339