de2092a4956d8ef5f2f5ce99c23b1e025c7ca7c0
[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     void TransferSettings::reset()
90     { _impl.reset(new TransferSettings::Impl()); }
91
92
93     void TransferSettings::addHeader( std::string && val_r )
94     { if ( ! val_r.empty() ) _impl->_headers.push_back( std::move(val_r) ); }
95
96     TransferSettings::Headers::const_iterator TransferSettings::headersBegin() const
97     { return _impl->_headers.begin(); }
98
99     TransferSettings::Headers::const_iterator TransferSettings::headersEnd() const
100     { return _impl->_headers.end(); }
101
102
103     void TransferSettings::setUserAgentString( std::string && val_r )
104     { _impl->_useragent = std::move(val_r); }
105
106     std::string TransferSettings::userAgentString() const
107     { return _impl->_useragent; }
108
109
110     void TransferSettings::setUsername( std::string && val_r )
111     { _impl->_username = std::move(val_r); }
112
113     std::string TransferSettings::username() const
114     { return _impl->_username; }
115
116     void TransferSettings::setPassword( std::string && val_r )
117     { _impl->_password = std::move(val_r); }
118
119     std::string TransferSettings::password() const
120     { return _impl->_password; }
121
122     std::string TransferSettings::userPassword() const
123     {
124       string userpwd = username();
125       if ( password().size() ) {
126         userpwd += ":" + password();
127       }
128       return userpwd;
129     }
130
131     void TransferSettings::setAnonymousAuth()
132     {
133       setUsername("anonymous");
134       setPassword("yast@" LIBZYPP_VERSION_STRING);
135     }
136
137
138     void TransferSettings::setProxyEnabled( bool enabled )
139     { _impl->_useproxy = enabled; }
140
141     bool TransferSettings::proxyEnabled() const
142     { return _impl->_useproxy; }
143
144
145     void TransferSettings::setProxy( std::string && val_r )
146     { _impl->_proxy = std::move(val_r); }
147
148     std::string TransferSettings::proxy() const
149     { return _impl->_proxy; }
150
151
152     void TransferSettings::setProxyUsername( std::string && val_r )
153     { _impl->_proxy_username = std::move(val_r); }
154
155     std::string TransferSettings::proxyUsername() const
156     { return _impl->_proxy_username; }
157
158     void TransferSettings::setProxyPassword( std::string && val_r )
159     { _impl->_proxy_password = std::move(val_r); }
160
161     std::string TransferSettings::proxyPassword() const
162     { return _impl->_proxy_password; }
163
164     std::string TransferSettings::proxyUserPassword() const
165     {
166       string userpwd = proxyUsername();
167       if ( proxyPassword().size() ) {
168         userpwd += ":" + proxyPassword();
169       }
170       return userpwd;
171     }
172
173
174     void TransferSettings::setTimeout( long t )
175     { _impl->_timeout = t; }
176
177     long TransferSettings::timeout() const
178     { return _impl->_timeout; }
179
180
181     void TransferSettings::setConnectTimeout( long t )
182     { _impl->_connect_timeout = t; }
183
184     long TransferSettings::connectTimeout() const
185     { return _impl->_connect_timeout; }
186
187
188     void TransferSettings::setMaxConcurrentConnections( long v )
189     { _impl->_maxConcurrentConnections = v; }
190
191     long TransferSettings::maxConcurrentConnections() const
192     { return _impl->_maxConcurrentConnections; }
193
194
195     void TransferSettings::setMinDownloadSpeed( long v )
196     { _impl->_minDownloadSpeed = v; }
197
198     long TransferSettings::minDownloadSpeed() const
199     { return _impl->_minDownloadSpeed; }
200
201
202     void TransferSettings::setMaxDownloadSpeed( long v )
203     { _impl->_maxDownloadSpeed = v; }
204
205     long TransferSettings::maxDownloadSpeed() const
206     { return _impl->_maxDownloadSpeed; }
207
208
209     void TransferSettings::setMaxSilentTries( long v )
210     { _impl->_maxSilentTries = v; }
211
212     long TransferSettings::maxSilentTries() const
213     { return _impl->_maxSilentTries; }
214
215
216     void TransferSettings::setVerifyHostEnabled( bool enabled )
217     { _impl->_verify_host = enabled; }
218
219     bool TransferSettings::verifyHostEnabled() const
220     { return _impl->_verify_host; }
221
222
223     void TransferSettings::setVerifyPeerEnabled( bool enabled )
224     { _impl->_verify_peer = enabled; }
225
226     bool TransferSettings::verifyPeerEnabled() const
227     { return _impl->_verify_peer; }
228
229
230     void TransferSettings::setClientCertificatePath( Pathname && val_r )
231     { _impl->_client_cert_path = std::move(val_r); }
232
233     Pathname TransferSettings::clientCertificatePath() const
234     { return _impl->_client_cert_path; }
235
236
237     void TransferSettings::setClientKeyPath( Pathname && val_r )
238     { _impl->_client_key_path = std::move(val_r); }
239
240     Pathname TransferSettings::clientKeyPath() const
241     { return _impl->_client_key_path; }
242
243
244     void TransferSettings::setCertificateAuthoritiesPath( Pathname && val_r )
245     { _impl->_ca_path = std::move(val_r); }
246
247     Pathname TransferSettings::certificateAuthoritiesPath() const
248     { return _impl->_ca_path; }
249
250
251     void TransferSettings::setAuthType( std::string && val_r )
252     { _impl->_authtype = std::move(val_r); }
253
254     std::string TransferSettings::authType() const
255     { return _impl->_authtype; }
256
257
258     void TransferSettings::setHeadRequestsAllowed( bool allowed )
259     { _impl->_head_requests_allowed = allowed; }
260
261     bool TransferSettings::headRequestsAllowed() const
262     { return _impl->_head_requests_allowed; }
263
264   } // namespace media
265 } // namespace zypp
266