81262c7b164ddd7b1a4b7ba9f2de09f312f77bb2
[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   if ( ! header.empty() )
99     _impl->_headers.push_back(header);
100 }
101
102 TransferSettings::Headers::const_iterator TransferSettings::headersBegin() const
103 {
104     return _impl->_headers.begin();
105 }
106
107 TransferSettings::Headers::const_iterator TransferSettings::headersEnd() const
108 {
109     return _impl->_headers.end();
110 }
111
112 void TransferSettings::setUserAgentString( const std::string &agent )
113 {
114     _impl->_useragent = agent;
115 }
116
117 std::string TransferSettings::userAgentString() const
118 {
119     return _impl->_useragent;
120 }
121
122 void TransferSettings::setUsername( const std::string &username )
123 {
124     _impl->_username = username;
125 }
126
127 std::string TransferSettings::username() const
128 {
129     return _impl->_username;
130 }
131
132 void TransferSettings::setPassword( const std::string &password )
133 {
134     _impl->_password = password;
135 }
136
137 void TransferSettings::setAnonymousAuth()
138 {
139     setUsername("anonymous");
140     string id = "yast@";
141     setPassword(id + VERSION);
142 }
143
144 std::string TransferSettings::password() const
145 {
146     return _impl->_password;
147 }
148
149 std::string TransferSettings::userPassword() const
150 {
151     string userpwd = username();
152     if ( password().size() ) {
153         userpwd += ":" + password();
154     }
155     return userpwd;
156 }
157
158 void TransferSettings::setProxyEnabled( bool enabled )
159 {
160     _impl->_useproxy = enabled;
161 }
162
163 bool TransferSettings::proxyEnabled() const
164 {
165     return _impl->_useproxy;
166 }
167
168 void TransferSettings::setProxy( const std::string &proxy )
169 {
170     _impl->_proxy = proxy;
171 }
172
173 std::string TransferSettings::proxy() const
174 {
175     return _impl->_proxy;
176 }
177
178 void TransferSettings::setProxyUsername( const std::string &proxyuser )
179 {
180     _impl->_proxy_username = proxyuser;
181 }
182
183 std::string TransferSettings::proxyUsername() const
184 {
185     return _impl->_proxy_username;
186 }
187
188 void TransferSettings::setProxyPassword( const std::string &proxypass )
189 {
190     _impl->_proxy_password = proxypass;
191 }
192
193 std::string TransferSettings::proxyPassword() const
194 {
195     return _impl->_proxy_password;
196 }
197
198 std::string TransferSettings::proxyUserPassword() const
199 {
200     string userpwd = proxyUsername();
201     if ( proxyPassword().size() ) {
202         userpwd += ":" + proxyPassword();
203     }
204     return userpwd;
205 }
206
207 void TransferSettings::setTimeout( long t )
208 {
209     _impl->_timeout = t;
210 }
211
212 long TransferSettings::timeout() const
213 {
214     return _impl->_timeout;
215 }
216
217 void TransferSettings::setConnectTimeout( long t )
218 {
219     _impl->_connect_timeout = t;
220 }
221
222 long TransferSettings::connectTimeout() const
223 {
224     return _impl->_connect_timeout;
225 }
226
227 long TransferSettings::maxConcurrentConnections() const
228 {
229     return _impl->_maxConcurrentConnections;
230 }
231
232 void TransferSettings::setMaxConcurrentConnections(long v)
233 {
234     _impl->_maxConcurrentConnections = v;
235 }
236
237 long TransferSettings::minDownloadSpeed() const
238 {
239     return _impl->_minDownloadSpeed;
240 }
241
242 void TransferSettings::setMinDownloadSpeed(long v)
243 {
244     _impl->_minDownloadSpeed = v;
245 }
246
247 long TransferSettings::maxDownloadSpeed() const
248 {
249     return _impl->_maxDownloadSpeed;
250 }
251
252 void TransferSettings::setMaxDownloadSpeed(long v)
253 {
254     _impl->_maxDownloadSpeed = v;
255 }
256
257 long TransferSettings::maxSilentTries() const
258 {
259     return _impl->_maxSilentTries;
260 }
261
262 void TransferSettings::setMaxSilentTries(long v)
263 {
264     _impl->_maxSilentTries = v;
265 }
266
267 bool TransferSettings::verifyHostEnabled() const
268 {
269     return _impl->_verify_host;
270 }
271
272 void TransferSettings::setVerifyHostEnabled( bool enabled )
273 {
274     _impl->_verify_host = enabled;
275 }
276
277 bool TransferSettings::verifyPeerEnabled() const
278 {
279     return _impl->_verify_peer;
280 }
281
282 Pathname TransferSettings::clientCertificatePath() const
283 {
284     return _impl->_client_cert_path;
285 }
286
287 void TransferSettings::setClientCertificatePath( const zypp::Pathname &path )
288 {
289     _impl->_client_cert_path = path;
290 }
291
292 Pathname TransferSettings::clientKeyPath() const
293 {
294     return _impl->_client_key_path;
295 }
296
297 void TransferSettings::setClientKeyPath( const zypp::Pathname &path )
298 {
299     _impl->_client_key_path = path;
300 }
301
302
303 void TransferSettings::setVerifyPeerEnabled( bool enabled )
304 {
305     _impl->_verify_peer = enabled;
306 }
307
308 Pathname TransferSettings::certificateAuthoritiesPath() const
309 {
310     return _impl->_ca_path;
311 }
312
313 void TransferSettings::setCertificateAuthoritiesPath( const zypp::Pathname &path )
314 {
315     _impl->_ca_path = path;
316 }
317
318 void TransferSettings::setAuthType( const std::string &authtype)
319 {
320     _impl->_authtype = authtype;
321 }
322
323 std::string TransferSettings::authType() const
324 {
325     return _impl->_authtype;
326 }
327
328 void TransferSettings::setHeadRequestsAllowed(bool allowed)
329 {
330     _impl->_head_requests_allowed = allowed;
331 }
332
333 bool TransferSettings::headRequestsAllowed() const
334 {
335     return _impl->_head_requests_allowed;
336 }
337
338 } // ns media
339 } // ns zypp
340