libzypp: zypp-curl/transfersettings.cc Source File

libzypp 17.36.6

transfersettings.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include "transfersettings.h"
14#include <iostream>
15#include <sstream>
16
17#include <zypp-core/base/String.h>
18#include <zypp-core/base/Logger.h>
19#include <zypp-core/fs/WatchFile>
20#include <zypp-core/base/ReferenceCounted.h>
21#include <zypp-core/base/NonCopyable.h>
22#include <zypp-core/ExternalProgram.h>
23#include <zypp-media/MediaConfig>
24
25#include <zypp-core/Globals.h>
26
27using std::endl;
28
29#define CURL_BINARY "/usr/bin/curl"
30
31namespace zypp
32{
33 namespace media
34 {
35 class TransferSettings::Impl
36 {
37 public:
38 Impl() : _useproxy( false ),
39 _timeout( MediaConfig::instance().download_transfer_timeout() ),
40 _connect_timeout( MediaConfig::instance().download_connect_timeout() ),
41 _maxConcurrentConnections( MediaConfig::instance().download_max_concurrent_connections() ),
42 _minDownloadSpeed(MediaConfig::instance().download_min_download_speed()),
43 _maxDownloadSpeed(MediaConfig::instance().download_max_download_speed()),
44 _maxSilentTries(MediaConfig::instance().download_max_silent_tries() ),
45 _verify_host(false),
46 _verify_peer(false),
47 _ca_path("/etc/ssl/certs"),
48 _enableCookieFile(false),
49 _head_requests_allowed(true)
50 {}
51
52 Impl(const Impl &) = default;
53 Impl(Impl &&) = default;
54 Impl &operator=(const Impl &) = delete;
55 Impl &operator=(Impl &&) = delete;
56 virtual ~Impl() {}
57
59 static shared_ptr<Impl> nullimpl()
60 {
61 static shared_ptr<Impl> _nullimpl( new Impl );
62 return _nullimpl;
63 }
64
65 private:
66 friend Impl * rwcowClone<Impl>( const Impl * rhs );
68 Impl * clone() const
69 { return new Impl( *this ); }
70
71 public:
72 void safeAddHeader( std::string val_r ) {
73 // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space.
74 // Trim and discard empty header.
75 val_r = str::trim( std::move(val_r) );
76 if ( not val_r.empty() )
77 _headers.push_back( std::move(val_r) );
78 else
79 WAR << "Discard empty header" << endl;
80 }
81
82 public:
83 std::vector<std::string> _headers;
84 std::string _useragent;
85 std::string _username;
86 std::string _password;
87 bool _useproxy;
88 std::string _proxy;
89 std::string _proxy_username;
90 std::string _proxy_password;
91 std::string _authtype;
92 long _timeout;
93 long _connect_timeout;
94 Url _url;
95 Pathname _targetdir;
96
97 long _maxConcurrentConnections;
98 long _minDownloadSpeed;
99 long _maxDownloadSpeed;
100 long _maxSilentTries;
101
102 bool _verify_host;
103 bool _verify_peer;
104 Pathname _ca_path;
105 Pathname _client_cert_path;
106 Pathname _client_key_path;
107
108 bool _enableCookieFile;
109
110 // workarounds
111 bool _head_requests_allowed;
112 };
113
114 TransferSettings::TransferSettings()
115 : _impl(new TransferSettings::Impl())
116 {}
117
118 void TransferSettings::reset()
119 { _impl.reset(new TransferSettings::Impl()); }
120
121
122 void TransferSettings::addHeader( const std::string & val_r )
123 { _impl->safeAddHeader( val_r ); }
124 void TransferSettings::addHeader( std::string && val_r )
125 { _impl->safeAddHeader( std::move(val_r) ); }
126
127 const TransferSettings::Headers &TransferSettings::headers() const
128 {
129 //@TODO check if we could use a vector of std::string_view here
130 return _impl->_headers;
131 }
132
133 void TransferSettings::setUserAgentString( const std::string &val_r )
134 { _impl->_useragent = str::trim( val_r ); } // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
135
136 void TransferSettings::setUserAgentString( std::string && val_r )
137 { _impl->_useragent = str::trim( std::move(val_r) ); } // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
138
139 const std::string &TransferSettings::userAgentString() const
140 { return _impl->_useragent; }
141
142
143 void TransferSettings::setUsername( const std::string &val_r )
144 { _impl->_username = val_r; }
145
146 void TransferSettings::setUsername( std::string && val_r )
147 { _impl->_username = std::move(val_r); }
148
149 const std::string &TransferSettings::username() const
150 { return _impl->_username; }
151
152 void TransferSettings::setPassword( const std::string & val_r )
153 { _impl->_password = val_r; }
154
155 void TransferSettings::setPassword( std::string && val_r )
156 { _impl->_password = std::move(val_r); }
157
158 const std::string &TransferSettings::password() const
159 { return _impl->_password; }
160
161 std::string TransferSettings::userPassword() const
162 {
163 std::string userpwd = username();
164 if ( password().size() ) {
165 userpwd += ":" + password();
166 }
167 return userpwd;
168 }
169
170 void TransferSettings::setAnonymousAuth()
171 {
172 setUsername("anonymous");
173 setPassword("yast@" LIBZYPP_VERSION_STRING);
174 }
175
176
177 void TransferSettings::setProxyEnabled( bool enabled )
178 { _impl->_useproxy = enabled; }
179
180 bool TransferSettings::proxyEnabled() const
181 { return _impl->_useproxy; }
182
183
184 void TransferSettings::setProxy( const std::string &val_r )
185 { _impl->_proxy = val_r; }
186
187 void TransferSettings::setProxy( std::string && val_r )
188 { _impl->_proxy = std::move(val_r); }
189
190 const std::string &TransferSettings::proxy() const
191 { return _impl->_proxy; }
192
193
194 void TransferSettings::setProxyUsername( const std::string &val_r )
195 { _impl->_proxy_username = val_r; }
196
197 void TransferSettings::setProxyUsername( std::string && val_r )
198 { _impl->_proxy_username = std::move(val_r); }
199
200 const std::string &TransferSettings::proxyUsername() const
201 { return _impl->_proxy_username; }
202
203 void TransferSettings::setProxyPassword( const std::string &val_r )
204 { _impl->_proxy_password = val_r; }
205
206 void TransferSettings::setProxyPassword( std::string && val_r )
207 { _impl->_proxy_password = std::move(val_r); }
208
209 const std::string &TransferSettings::proxyPassword() const
210 { return _impl->_proxy_password; }
211
212 std::string TransferSettings::proxyUserPassword() const
213 {
214 std::string userpwd = proxyUsername();
215 if ( proxyPassword().size() ) {
216 userpwd += ":" + proxyPassword();
217 }
218 return userpwd;
219 }
220
221
222 void TransferSettings::setTimeout( long t )
223 { _impl->_timeout = (t); }
224
225 long TransferSettings::timeout() const
226 { return _impl->_timeout; }
227
228
229 void TransferSettings::setConnectTimeout( long t )
230 { _impl->_connect_timeout = (t); }
231
232 long TransferSettings::connectTimeout() const
233 { return _impl->_connect_timeout; }
234
235
236 void TransferSettings::setMaxConcurrentConnections( long v )
237 { _impl->_maxConcurrentConnections = (v); }
238
239 long TransferSettings::maxConcurrentConnections() const
240 { return _impl->_maxConcurrentConnections; }
241
242
243 void TransferSettings::setMinDownloadSpeed( long v )
244 { _impl->_minDownloadSpeed = (v); }
245
246 long TransferSettings::minDownloadSpeed() const
247 { return _impl->_minDownloadSpeed; }
248
249
250 void TransferSettings::setMaxDownloadSpeed( long v )
251 { _impl->_maxDownloadSpeed = (v); }
252
253 long TransferSettings::maxDownloadSpeed() const
254 { return _impl->_maxDownloadSpeed; }
255
256
257 void TransferSettings::setMaxSilentTries( long v )
258 { _impl->_maxSilentTries = (v); }
259
260 long TransferSettings::maxSilentTries() const
261 { return _impl->_maxSilentTries; }
262
263
264 void TransferSettings::setVerifyHostEnabled( bool enabled )
265 { _impl->_verify_host = (enabled); }
266
267 bool TransferSettings::verifyHostEnabled() const
268 { return _impl->_verify_host; }
269
270
271 void TransferSettings::setVerifyPeerEnabled( bool enabled )
272 { _impl->_verify_peer = enabled; }
273
274 bool TransferSettings::verifyPeerEnabled() const
275 { return _impl->_verify_peer; }
276
277 void TransferSettings::setClientCertificatePath( const Pathname &val_r )
278 { _impl->_client_cert_path = val_r; }
279
280 void TransferSettings::setClientCertificatePath( Pathname && val_r )
281 { _impl->_client_cert_path = std::move( val_r ); }
282
283 const Pathname &TransferSettings::clientCertificatePath() const
284 { return _impl->_client_cert_path; }
285
286
287 void TransferSettings::setClientKeyPath( const Pathname &val_r )
288 { _impl->_client_key_path = val_r; }
289
290 void TransferSettings::setClientKeyPath( Pathname && val_r )
291 { _impl->_client_key_path = std::move( val_r ); }
292
293 const Pathname &TransferSettings::clientKeyPath() const
294 { return _impl->_client_key_path; }
295
296 void TransferSettings::setEnableCookieFile(bool enable)
297 { _impl->_enableCookieFile = enable; }
298
299 bool TransferSettings::cookieFileEnabled() const
300 { return _impl->_enableCookieFile; }
301
302 void TransferSettings::setCertificateAuthoritiesPath( const Pathname &val_r )
303 { _impl->_ca_path = val_r; }
304
305 void TransferSettings::setCertificateAuthoritiesPath( Pathname && val_r )
306 { _impl->_ca_path = std::move(val_r); }
307
308 const Pathname &TransferSettings::certificateAuthoritiesPath() const
309 { return _impl->_ca_path; }
310
311
312 void TransferSettings::setAuthType( const std::string &val_r )
313 { _impl->_authtype = val_r; }
314
315 void TransferSettings::setAuthType( std::string && val_r )
316 { _impl->_authtype = std::move(val_r); }
317
318 const std::string &TransferSettings::authType() const
319 { return _impl->_authtype; }
320
321
322 void TransferSettings::setHeadRequestsAllowed( bool allowed )
323 { _impl->_head_requests_allowed = allowed; }
324
325 bool TransferSettings::headRequestsAllowed() const
326 { return _impl->_head_requests_allowed; }
327
328 } // namespace media
329} // namespace zypp
shared_ptr
zypp::MediaConfig
Definition mediaconfig.h:43
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::media::TransferSettings::Impl
Definition transfersettings.cc:36
zypp::media::TransferSettings::Impl::_minDownloadSpeed
long _minDownloadSpeed
Definition transfersettings.cc:98
zypp::media::TransferSettings::Impl::Impl
Impl(const Impl &)=default
zypp::media::TransferSettings::Impl::_proxy_password
std::string _proxy_password
Definition transfersettings.cc:90
zypp::media::TransferSettings::Impl::_proxy
std::string _proxy
Definition transfersettings.cc:88
zypp::media::TransferSettings::Impl::_maxSilentTries
long _maxSilentTries
Definition transfersettings.cc:100
zypp::media::TransferSettings::Impl::_enableCookieFile
bool _enableCookieFile
Definition transfersettings.cc:108
zypp::media::TransferSettings::Impl::_useragent
std::string _useragent
Definition transfersettings.cc:84
zypp::media::TransferSettings::Impl::_url
Url _url
Definition transfersettings.cc:94
zypp::media::TransferSettings::Impl::~Impl
virtual ~Impl()
Definition transfersettings.cc:56
zypp::media::TransferSettings::Impl::_useproxy
bool _useproxy
Definition transfersettings.cc:87
zypp::media::TransferSettings::Impl::rwcowClone
friend Impl * rwcowClone(const Impl *rhs)
zypp::media::TransferSettings::Impl::_headers
std::vector< std::string > _headers
Definition transfersettings.cc:83
zypp::media::TransferSettings::Impl::_password
std::string _password
Definition transfersettings.cc:86
zypp::media::TransferSettings::Impl::_proxy_username
std::string _proxy_username
Definition transfersettings.cc:89
zypp::media::TransferSettings::Impl::_ca_path
Pathname _ca_path
Definition transfersettings.cc:104
zypp::media::TransferSettings::Impl::safeAddHeader
void safeAddHeader(std::string val_r)
Definition transfersettings.cc:72
zypp::media::TransferSettings::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition transfersettings.cc:68
zypp::media::TransferSettings::Impl::_verify_peer
bool _verify_peer
Definition transfersettings.cc:103
zypp::media::TransferSettings::Impl::_client_cert_path
Pathname _client_cert_path
Definition transfersettings.cc:105
zypp::media::TransferSettings::Impl::_connect_timeout
long _connect_timeout
Definition transfersettings.cc:93
zypp::media::TransferSettings::Impl::_targetdir
Pathname _targetdir
Definition transfersettings.cc:95
zypp::media::TransferSettings::Impl::_head_requests_allowed
bool _head_requests_allowed
Definition transfersettings.cc:111
zypp::media::TransferSettings::Impl::_timeout
long _timeout
Definition transfersettings.cc:92
zypp::media::TransferSettings::Impl::nullimpl
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Definition transfersettings.cc:59
zypp::media::TransferSettings::Impl::_verify_host
bool _verify_host
Definition transfersettings.cc:102
zypp::media::TransferSettings::Impl::_username
std::string _username
Definition transfersettings.cc:85
zypp::media::TransferSettings::Impl::operator=
Impl & operator=(const Impl &)=delete
zypp::media::TransferSettings::Impl::_authtype
std::string _authtype
Definition transfersettings.cc:91
zypp::media::TransferSettings::Impl::_maxDownloadSpeed
long _maxDownloadSpeed
Definition transfersettings.cc:99
zypp::media::TransferSettings::Impl::operator=
Impl & operator=(Impl &&)=delete
zypp::media::TransferSettings::Impl::Impl
Impl()
Definition transfersettings.cc:38
zypp::media::TransferSettings::Impl::_client_key_path
Pathname _client_key_path
Definition transfersettings.cc:106
zypp::media::TransferSettings::Impl::_maxConcurrentConnections
long _maxConcurrentConnections
Definition transfersettings.cc:97
zypp::media::TransferSettings::Impl::Impl
Impl(Impl &&)=default
zypp::media::TransferSettings::password
const std::string & password() const
auth password
Definition transfersettings.cc:158
zypp::media::TransferSettings::setProxy
void setProxy(const std::string &val_r)
proxy to use if it is enabled
Definition transfersettings.cc:184
zypp::media::TransferSettings::setProxyEnabled
void setProxyEnabled(bool enabled)
whether the proxy is used or not
Definition transfersettings.cc:177
zypp::media::TransferSettings::maxDownloadSpeed
long maxDownloadSpeed() const
Maximum download speed (bytes per second)
Definition transfersettings.cc:253
zypp::media::TransferSettings::TransferSettings
TransferSettings()
Constructs a transfer program cmd line access.
Definition transfersettings.cc:114
zypp::media::TransferSettings::connectTimeout
long connectTimeout() const
connection timeout
Definition transfersettings.cc:232
zypp::media::TransferSettings::authType
const std::string & authType() const
get the allowed authentication types
Definition transfersettings.cc:318
zypp::media::TransferSettings::timeout
long timeout() const
transfer timeout
Definition transfersettings.cc:225
zypp::media::TransferSettings::setUsername
void setUsername(const std::string &val_r)
sets the auth username
Definition transfersettings.cc:143
zypp::media::TransferSettings::reset
void reset()
reset the settings to the defaults
Definition transfersettings.cc:118
zypp::media::TransferSettings::maxSilentTries
long maxSilentTries() const
Maximum silent retries.
Definition transfersettings.cc:260
zypp::media::TransferSettings::clientCertificatePath
const Pathname & clientCertificatePath() const
SSL client certificate file.
Definition transfersettings.cc:283
zypp::media::TransferSettings::userPassword
std::string userPassword() const
returns the user and password as a user:pass string
Definition transfersettings.cc:161
zypp::media::TransferSettings::minDownloadSpeed
long minDownloadSpeed() const
Minimum download speed (bytes per second) until the connection is dropped.
Definition transfersettings.cc:246
zypp::media::TransferSettings::setProxyUsername
void setProxyUsername(const std::string &val_r)
sets the proxy user
Definition transfersettings.cc:194
zypp::media::TransferSettings::setHeadRequestsAllowed
void setHeadRequestsAllowed(bool allowed)
set whether HEAD requests are allowed
Definition transfersettings.cc:322
zypp::media::TransferSettings::headers
const Headers & headers() const
returns a list of all added headers (trimmed)
Definition transfersettings.cc:127
zypp::media::TransferSettings::proxy
const std::string & proxy() const
proxy host
Definition transfersettings.cc:190
zypp::media::TransferSettings::clientKeyPath
const Pathname & clientKeyPath() const
SSL client key file.
Definition transfersettings.cc:293
zypp::media::TransferSettings::setVerifyHostEnabled
void setVerifyHostEnabled(bool enabled)
Sets whether to verify host for ssl.
Definition transfersettings.cc:264
zypp::media::TransferSettings::setUserAgentString
void setUserAgentString(std::string &&val_r)
sets the user agent ie: "Mozilla v3" (trims)
Definition transfersettings.cc:136
zypp::media::TransferSettings::setConnectTimeout
void setConnectTimeout(long t)
set the connect timeout
Definition transfersettings.cc:229
zypp::media::TransferSettings::setClientKeyPath
void setClientKeyPath(const Pathname &val_r)
Sets the SSL client key file.
Definition transfersettings.cc:287
zypp::media::TransferSettings::addHeader
void addHeader(std::string &&val_r)
add a header, on the form "Foo: Bar" (trims)
Definition transfersettings.cc:124
zypp::media::TransferSettings::setMinDownloadSpeed
void setMinDownloadSpeed(long v)
Set minimum download speed (bytes per second) until the connection is dropped.
Definition transfersettings.cc:243
zypp::media::TransferSettings::maxConcurrentConnections
long maxConcurrentConnections() const
Maximum number of concurrent connections for a single transfer.
Definition transfersettings.cc:239
zypp::media::TransferSettings::proxyUserPassword
std::string proxyUserPassword() const
returns the proxy user and password as a user:pass string
Definition transfersettings.cc:212
zypp::media::TransferSettings::setClientCertificatePath
void setClientCertificatePath(const Pathname &val_r)
Sets the SSL client certificate file.
Definition transfersettings.cc:277
zypp::media::TransferSettings::verifyHostEnabled
bool verifyHostEnabled() const
Whether to verify host for ssl.
Definition transfersettings.cc:267
zypp::media::TransferSettings::userAgentString
const std::string & userAgentString() const
user agent string (trimmed)
Definition transfersettings.cc:139
zypp::media::TransferSettings::setPassword
void setPassword(const std::string &val_r)
sets the auth password
Definition transfersettings.cc:152
zypp::media::TransferSettings::headRequestsAllowed
bool headRequestsAllowed() const
whether HEAD requests are allowed
Definition transfersettings.cc:325
zypp::media::TransferSettings::proxyPassword
const std::string & proxyPassword() const
proxy auth password
Definition transfersettings.cc:209
zypp::media::TransferSettings::setVerifyPeerEnabled
void setVerifyPeerEnabled(bool enabled)
Sets whether to verify host for ssl.
Definition transfersettings.cc:271
zypp::media::TransferSettings::cookieFileEnabled
bool cookieFileEnabled() const
Definition transfersettings.cc:299
zypp::media::TransferSettings::proxyEnabled
bool proxyEnabled() const
proxy is enabled
Definition transfersettings.cc:180
zypp::media::TransferSettings::setMaxDownloadSpeed
void setMaxDownloadSpeed(long v)
Set max download speed (bytes per second)
Definition transfersettings.cc:250
zypp::media::TransferSettings::setAnonymousAuth
void setAnonymousAuth()
sets anonymous authentication (ie: for ftp)
Definition transfersettings.cc:170
zypp::media::TransferSettings::Headers
std::vector< std::string > Headers
Definition transfersettings.h:39
zypp::media::TransferSettings::username
const std::string & username() const
auth username
Definition transfersettings.cc:149
zypp::media::TransferSettings::setEnableCookieFile
void setEnableCookieFile(bool enable=true)
Enable or disable the use of the cookie file.
Definition transfersettings.cc:296
zypp::media::TransferSettings::_impl
RWCOW_pointer< Impl > _impl
Definition transfersettings.h:213
zypp::media::TransferSettings::proxyUsername
const std::string & proxyUsername() const
proxy auth username
Definition transfersettings.cc:200
zypp::media::TransferSettings::setAuthType
void setAuthType(const std::string &val_r)
set the allowed authentication types
Definition transfersettings.cc:312
zypp::media::TransferSettings::setCertificateAuthoritiesPath
void setCertificateAuthoritiesPath(const Pathname &val_r)
Sets the SSL certificate authorities path.
Definition transfersettings.cc:302
zypp::media::TransferSettings::setProxyPassword
void setProxyPassword(const std::string &val_r)
sets the proxy password
Definition transfersettings.cc:203
zypp::media::TransferSettings::setMaxConcurrentConnections
void setMaxConcurrentConnections(long v)
Set maximum number of concurrent connections for a single transfer.
Definition transfersettings.cc:236
zypp::media::TransferSettings::certificateAuthoritiesPath
const Pathname & certificateAuthoritiesPath() const
SSL certificate authorities path ( default: /etc/ssl/certs )
Definition transfersettings.cc:308
zypp::media::TransferSettings::setTimeout
void setTimeout(long t)
set the transfer timeout
Definition transfersettings.cc:222
zypp::media::TransferSettings::setMaxSilentTries
void setMaxSilentTries(long v)
Set maximum silent retries.
Definition transfersettings.cc:257
zypp::media::TransferSettings::verifyPeerEnabled
bool verifyPeerEnabled() const
Whether to verify peer for ssl.
Definition transfersettings.cc:274
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:224
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
transfersettings.h
ExternalProgram.h
Globals.h
Provides API related macros.
Logger.h
WAR
#define WAR
Definition Logger.h:101
NonCopyable.h
ReferenceCounted.h
String.h