libzypp  17.38.7
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  _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),
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;
97 
102 
108 
110 
111  // workarounds
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 
129  : _impl(new TransferSettings::Impl())
130  {}
131 
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 
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 
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 
179  {
180  setUsername("anonymous");
181  setPassword("zypp@" LIBZYPP_VERSION_STRING);
182  }
183 
184 
186  { _impl->_useproxy = enabled; }
187 
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 
221  {
222  std::string userpwd = proxyUsername();
223  if ( proxyPassword().size() ) {
224  userpwd += ":" + proxyPassword();
225  }
226  return userpwd;
227  }
228 
229 
231  { _impl->_timeout = (t); }
232 
234  { return _impl->_timeout; }
235 
236 
238  { _impl->_connect_timeout = (t); }
239 
241  { return _impl->_connect_timeout; }
242 
243 
245  { _impl->_maxConcurrentConnections = (v); }
246 
248  { return _impl->_maxConcurrentConnections; }
249 
250 
252  { _impl->_minDownloadSpeed = (v); }
253 
255  { return _impl->_minDownloadSpeed; }
256 
257 
259  { _impl->_maxDownloadSpeed = (v); }
260 
262  { return _impl->_maxDownloadSpeed; }
263 
264 
266  { _impl->_maxSilentTries = (v); }
267 
269  { return _impl->_maxSilentTries; }
270 
271 
273  { _impl->_verify_host = (enabled); }
274 
276  { return _impl->_verify_host; }
277 
278 
280  { _impl->_verify_peer = enabled; }
281 
283  { return _impl->_verify_peer; }
284 
286  { _impl->_client_cert_path = val_r; }
287 
289  { _impl->_client_cert_path = std::move( val_r ); }
290 
292  { return _impl->_client_cert_path; }
293 
294 
296  { _impl->_client_key_path = val_r; }
297 
299  { _impl->_client_key_path = std::move( val_r ); }
300 
302  { return _impl->_client_key_path; }
303 
305  { _impl->_enableCookieFile = enable; }
306 
308  { return _impl->_enableCookieFile; }
309 
311  { _impl->_ca_path = val_r; }
312 
314  { _impl->_ca_path = std::move(val_r); }
315 
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 
331  { _impl->_head_requests_allowed = allowed; }
332 
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
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.
Impl & operator=(const Impl &)=delete
bool hasCredentials() const
has a username, maybe even the 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:104
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.
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