libzypp 17.38.17
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>
20#include <zypp-core/fs/WatchFile>
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 {
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 _minDownloadSpeedTime(MediaConfig::instance().download_min_download_speed_time()),
45 _maxDownloadSpeed(MediaConfig::instance().download_max_download_speed()),
46 _maxSilentTries(MediaConfig::instance().download_max_silent_tries() ),
47 _verify_host(false),
48 _verify_peer(false),
49 _ca_path("/etc/ssl/certs"),
50 _enableCookieFile(false),
52 {}
53
54 Impl(const Impl &) = default;
55 Impl(Impl &&) = default;
56 Impl &operator=(const Impl &) = delete;
57 Impl &operator=(Impl &&) = delete;
58 virtual ~Impl() {}
59
62 {
63 static shared_ptr<Impl> _nullimpl( new Impl );
64 return _nullimpl;
65 }
66
67 private:
68 friend Impl * rwcowClone<Impl>( const Impl * rhs );
70 Impl * clone() const
71 { return new Impl( *this ); }
72
73 public:
74 void safeAddHeader( std::string val_r ) {
75 // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space.
76 // Trim and discard empty header.
77 val_r = str::trim( std::move(val_r) );
78 if ( not val_r.empty() )
79 _headers.push_back( std::move(val_r) );
80 else
81 WAR << "Discard empty header" << endl;
82 }
83
84 public:
85 std::vector<std::string> _headers;
86 std::string _useragent;
87 std::string _username;
88 std::string _password;
90 std::string _proxy;
91 std::string _proxy_username;
92 std::string _proxy_password;
93 std::string _authtype;
98
104
110
112
113 // workarounds
115 };
116
117 std::ostream & TransferSettings::logUserPass( std::ostream & str, const std::string & user_r, const std::string & pass_r )
118 {
119 if ( not user_r.empty() ) {
120 str << "[" << user_r;
121 if ( not pass_r.empty() ) {
122 str << ":" << "########"; //zypp::CheckSum::md5FromString(pass_r);
123 }
124 str << "]";
125 }
126 return str;
127 }
128
129
133
136
137
138 void TransferSettings::addHeader( const std::string & val_r )
139 { _impl->safeAddHeader( val_r ); }
140 void TransferSettings::addHeader( std::string && val_r )
141 { _impl->safeAddHeader( std::move(val_r) ); }
142
144 {
145 //@TODO check if we could use a vector of std::string_view here
146 return _impl->_headers;
147 }
148
149 void TransferSettings::setUserAgentString( const std::string &val_r )
150 { _impl->_useragent = str::trim( val_r ); } // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
151
152 void TransferSettings::setUserAgentString( std::string && val_r )
153 { _impl->_useragent = str::trim( std::move(val_r) ); } // bsc#1212187: HTTP/2 RFC 9113 forbids fields ending with a space
154
155 const std::string &TransferSettings::userAgentString() const
156 { return _impl->_useragent; }
157
158
160 { return not _impl->_username.empty() && not _impl->_password.empty(); }
161
162 void TransferSettings::setUsername( const std::string &val_r )
163 { _impl->_username = val_r; }
164
165 void TransferSettings::setUsername( std::string && val_r )
166 { _impl->_username = std::move(val_r); }
167
168 const std::string &TransferSettings::username() const
169 { return _impl->_username; }
170
171 void TransferSettings::setPassword( const std::string & val_r )
172 { _impl->_password = val_r; }
173
174 void TransferSettings::setPassword( std::string && val_r )
175 { _impl->_password = std::move(val_r); }
176
177 const std::string &TransferSettings::password() const
178 { return _impl->_password; }
179
181 {
182 setUsername("anonymous");
183 setPassword("zypp@" LIBZYPP_VERSION_STRING);
184 }
185
186
188 { _impl->_useproxy = enabled; }
189
191 { return _impl->_useproxy; }
192
193
194 void TransferSettings::setProxy( const std::string &val_r )
195 { _impl->_proxy = val_r; }
196
197 void TransferSettings::setProxy( std::string && val_r )
198 { _impl->_proxy = std::move(val_r); }
199
200 const std::string &TransferSettings::proxy() const
201 { return _impl->_proxy; }
202
203
204 void TransferSettings::setProxyUsername( const std::string &val_r )
205 { _impl->_proxy_username = val_r; }
206
207 void TransferSettings::setProxyUsername( std::string && val_r )
208 { _impl->_proxy_username = std::move(val_r); }
209
210 const std::string &TransferSettings::proxyUsername() const
211 { return _impl->_proxy_username; }
212
213 void TransferSettings::setProxyPassword( const std::string &val_r )
214 { _impl->_proxy_password = val_r; }
215
216 void TransferSettings::setProxyPassword( std::string && val_r )
217 { _impl->_proxy_password = std::move(val_r); }
218
219 const std::string &TransferSettings::proxyPassword() const
220 { return _impl->_proxy_password; }
221
223 {
224 std::string userpwd = proxyUsername();
225 if ( proxyPassword().size() ) {
226 userpwd += ":" + proxyPassword();
227 }
228 return userpwd;
229 }
230
231
233 { _impl->_timeout = (t); }
234
236 { return _impl->_timeout; }
237
238
241
244
245
248
251
252
255
258
259
262
265
266
269
272
273
276
279
280
282 { _impl->_verify_host = (enabled); }
283
286
287
289 { _impl->_verify_peer = enabled; }
290
293
296
298 { _impl->_client_cert_path = std::move( val_r ); }
299
302
303
306
308 { _impl->_client_key_path = std::move( val_r ); }
309
312
315
318
321
323 { _impl->_ca_path = std::move(val_r); }
324
327
328
329 void TransferSettings::setAuthType( const std::string &val_r )
330 { _impl->_authtype = val_r; }
331
332 void TransferSettings::setAuthType( std::string && val_r )
333 { _impl->_authtype = std::move(val_r); }
334
335 const std::string &TransferSettings::authType() const
336 { return _impl->_authtype; }
337
338
341
344
345
346 std::ostream & dumpOn( std::ostream & str, const TransferSettings & obj )
347 {
348 return obj.logCredentials( str );
349 }
350
351 } // namespace media
352} // namespace zypp
Provides API related macros.
#define WAR
Definition Logger.h:131
Url manipulation class.
Definition Url.h:93
friend Impl * rwcowClone(const Impl *rhs)
std::vector< std::string > _headers
void safeAddHeader(std::string val_r)
Impl * clone() const
clone for RWCOW_pointer
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Impl & operator=(const Impl &)=delete
Impl & operator=(Impl &&)=delete
Holds transfer setting.
const std::string & password() const
auth password
void setProxy(const std::string &val_r)
proxy to use if it is enabled
void setProxyEnabled(bool enabled)
whether the proxy is used or not
long maxDownloadSpeed() const
Maximum download speed (bytes per second).
TransferSettings()
Constructs a transfer program cmd line access.
long connectTimeout() const
connection timeout
const std::string & authType() const
get the allowed authentication types
long timeout() const
transfer timeout
void setUsername(const std::string &val_r)
sets the auth username
void reset()
reset the settings to the defaults
long maxSilentTries() const
Maximum silent retries.
const Pathname & clientCertificatePath() const
SSL client certificate file.
long minDownloadSpeed() const
Minimum download speed (bytes per second) until the connection is dropped.
void setProxyUsername(const std::string &val_r)
sets the proxy user
void setHeadRequestsAllowed(bool allowed)
set whether HEAD requests are allowed
const Headers & headers() const
returns a list of all added headers (trimmed)
long minDownloadSpeedTime() const
The time in seconds that the transfer speed must remain below minDownloadSpeed before the transfer is...
const std::string & proxy() const
proxy host
const Pathname & clientKeyPath() const
SSL client key file.
void setVerifyHostEnabled(bool enabled)
Sets whether to verify host for ssl.
void setUserAgentString(std::string &&val_r)
sets the user agent ie: "Mozilla v3" (trims)
void setConnectTimeout(long t)
set the connect timeout
void setClientKeyPath(const Pathname &val_r)
Sets the SSL client key file.
void addHeader(std::string &&val_r)
add a header, on the form "Foo: Bar" (trims)
void setMinDownloadSpeed(long v)
Set minimum download speed (bytes per second) until the connection is dropped.
long maxConcurrentConnections() const
Maximum number of concurrent connections for a single transfer.
std::string proxyUserPassword() const
returns the proxy user and password as a user:pass string
void setClientCertificatePath(const Pathname &val_r)
Sets the SSL client certificate file.
bool verifyHostEnabled() const
Whether to verify host for ssl.
const std::string & userAgentString() const
user agent string (trimmed)
bool hasCredentials() const
has a non-empty username and a non-empty password
void setPassword(const std::string &val_r)
sets the auth password
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:########]").
bool headRequestsAllowed() const
whether HEAD requests are allowed
const std::string & proxyPassword() const
proxy auth password
void setVerifyPeerEnabled(bool enabled)
Sets whether to verify host for ssl.
bool proxyEnabled() const
proxy is enabled
void setMaxDownloadSpeed(long v)
Set max download speed (bytes per second).
void setAnonymousAuth()
sets anonymous authentication (ie: for ftp)
std::vector< std::string > Headers
const std::string & username() const
auth username
void setMinDownloadSpeedTime(long v)
Set the time in seconds that the transfer speed must remain below minDownloadSpeed before the transfe...
void setEnableCookieFile(bool enable=true)
Enable or disable the use of the cookie file.
const std::string & proxyUsername() const
proxy auth username
void setAuthType(const std::string &val_r)
set the allowed authentication types
void setCertificateAuthoritiesPath(const Pathname &val_r)
Sets the SSL certificate authorities path.
void setProxyPassword(const std::string &val_r)
sets the proxy password
void setMaxConcurrentConnections(long v)
Set maximum number of concurrent connections for a single transfer.
const Pathname & certificateAuthoritiesPath() const
SSL certificate authorities path ( default: /etc/ssl/certs ).
void setTimeout(long t)
set the transfer timeout
std::ostream & logCredentials(std::ostream &str) const
log credentials to stream hiding the password.
void setMaxSilentTries(long v)
Set maximum silent retries.
bool verifyPeerEnabled() const
Whether to verify peer for ssl.
String related utilities and Regular expression matching.
std::ostream & dumpOn(std::ostream &str, const TransferSettings &obj)
relates: TransferSettings
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
Easy-to use interface to the ZYPP dependency resolver.