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