libzypp: zypp-curl/transfersettings.cc Source File

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