libzypp  17.38.17
transfersettings.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include "transfersettings.h"
14 #include <iostream>
15 #include <sstream>
16 
17 #include <zypp-core/CheckSum.h>
18 #include <zypp-core/base/String.h>
20 #include <zypp-core/fs/WatchFile>
24 #include <zypp-media/MediaConfig>
25 
26 #include <zypp-core/Globals.h>
27 
28 using std::endl;
29 
30 #define CURL_BINARY "/usr/bin/curl"
31 
32 namespace 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 
61  static shared_ptr<Impl> nullimpl()
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;
89  bool _useproxy;
90  std::string _proxy;
91  std::string _proxy_username;
92  std::string _proxy_password;
93  std::string _authtype;
94  long _timeout;
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 
131  : _impl(new TransferSettings::Impl())
132  {}
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 
240  { _impl->_connect_timeout = (t); }
241 
243  { return _impl->_connect_timeout; }
244 
245 
247  { _impl->_maxConcurrentConnections = (v); }
248 
250  { return _impl->_maxConcurrentConnections; }
251 
252 
254  { _impl->_minDownloadSpeed = (v); }
255 
257  { return _impl->_minDownloadSpeed; }
258 
259 
261  { _impl->_minDownloadSpeedTime = v; }
262 
264  { return _impl->_minDownloadSpeedTime; }
265 
266 
268  { _impl->_maxDownloadSpeed = (v); }
269 
271  { return _impl->_maxDownloadSpeed; }
272 
273 
275  { _impl->_maxSilentTries = (v); }
276 
278  { return _impl->_maxSilentTries; }
279 
280 
282  { _impl->_verify_host = (enabled); }
283 
285  { return _impl->_verify_host; }
286 
287 
289  { _impl->_verify_peer = enabled; }
290 
292  { return _impl->_verify_peer; }
293 
295  { _impl->_client_cert_path = val_r; }
296 
298  { _impl->_client_cert_path = std::move( val_r ); }
299 
301  { return _impl->_client_cert_path; }
302 
303 
305  { _impl->_client_key_path = val_r; }
306 
308  { _impl->_client_key_path = std::move( val_r ); }
309 
311  { return _impl->_client_key_path; }
312 
314  { _impl->_enableCookieFile = enable; }
315 
317  { return _impl->_enableCookieFile; }
318 
320  { _impl->_ca_path = val_r; }
321 
323  { _impl->_ca_path = std::move(val_r); }
324 
326  { return _impl->_ca_path; }
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 
340  { _impl->_head_requests_allowed = allowed; }
341 
343  { return _impl->_head_requests_allowed; }
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
long timeout() const
transfer timeout
const Pathname & certificateAuthoritiesPath() const
SSL certificate authorities path ( default: /etc/ssl/certs )
const Pathname & clientCertificatePath() const
SSL client certificate file.
void addHeader(std::string &&val_r)
add a header, on the form "Foo: Bar" (trims)
std::string proxyUserPassword() const
returns the proxy user and password as a user:pass string
const std::string & proxyPassword() const
proxy auth password
void setPassword(const std::string &val_r)
sets the auth password
long maxDownloadSpeed() const
Maximum download speed (bytes per second)
Holds transfer setting.
const std::string & authType() const
get the allowed authentication types
bool verifyHostEnabled() const
Whether to verify host for ssl.
const std::string & proxyUsername() const
proxy auth username
String related utilities and Regular expression matching.
void setUsername(const std::string &val_r)
sets the auth username
void setHeadRequestsAllowed(bool allowed)
set whether HEAD requests are allowed
void setConnectTimeout(long t)
set the connect timeout
std::ostream & dumpOn(std::ostream &str, const TransferSettings &obj)
relates: TransferSettings
const std::string & password() const
auth password
const std::string & username() const
auth username
const Headers & headers() const
returns a list of all added headers (trimmed)
void setAnonymousAuth()
sets anonymous authentication (ie: for ftp)
bool verifyPeerEnabled() const
Whether to verify peer for ssl.
long maxSilentTries() const
Maximum silent retries.
long minDownloadSpeedTime() const
The time in seconds that the transfer speed must remain below minDownloadSpeed before the transfer is...
Impl & operator=(const Impl &)=delete
bool hasCredentials() const
has a non-empty username and a non-empty password
void setAuthType(const std::string &val_r)
set the allowed authentication types
void setProxy(const std::string &val_r)
proxy to use if it is enabled
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:226
static shared_ptr< Impl > nullimpl()
Offer default Impl.
long connectTimeout() const
connection timeout
#define WAR
Definition: Logger.h:131
Provides API related macros.
std::vector< std::string > _headers
TransferSettings()
Constructs a transfer program cmd line access.
void setMaxSilentTries(long v)
Set maximum silent retries.
void setTimeout(long t)
set the transfer timeout
void safeAddHeader(std::string val_r)
const Pathname & clientKeyPath() const
SSL client key file.
RWCOW_pointer< Impl > _impl
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:########]").
void setMaxDownloadSpeed(long v)
Set max download speed (bytes per second)
long minDownloadSpeed() const
Minimum download speed (bytes per second) until the connection is dropped.
Impl * clone() const
clone for RWCOW_pointer
bool proxyEnabled() const
proxy is enabled
void setProxyPassword(const std::string &val_r)
sets the proxy password
void reset()
reset the settings to the defaults
void setMaxConcurrentConnections(long v)
Set maximum number of concurrent connections for a single transfer.
long maxConcurrentConnections() const
Maximum number of concurrent connections for a single transfer.
void setMinDownloadSpeedTime(long v)
Set the time in seconds that the transfer speed must remain below minDownloadSpeed before the transfe...
std::ostream & logCredentials(std::ostream &str) const
log credentials to stream hiding the password.
void setUserAgentString(std::string &&val_r)
sets the user agent ie: "Mozilla v3" (trims)
void setClientCertificatePath(const Pathname &val_r)
Sets the SSL client certificate file.
void setProxyUsername(const std::string &val_r)
sets the proxy user
void setEnableCookieFile(bool enable=true)
Enable or disable the use of the cookie file.
std::vector< std::string > Headers
void setCertificateAuthoritiesPath(const Pathname &val_r)
Sets the SSL certificate authorities path.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
void setMinDownloadSpeed(long v)
Set minimum download speed (bytes per second) until the connection is dropped.
const std::string & proxy() const
proxy host
void setVerifyPeerEnabled(bool enabled)
Sets whether to verify host for ssl.
void setClientKeyPath(const Pathname &val_r)
Sets the SSL client key file.
void setVerifyHostEnabled(bool enabled)
Sets whether to verify host for ssl.
const std::string & userAgentString() const
user agent string (trimmed)
Url manipulation class.
Definition: Url.h:92
bool headRequestsAllowed() const
whether HEAD requests are allowed
void setProxyEnabled(bool enabled)
whether the proxy is used or not