libzypp  17.36.3
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/base/String.h>
18 #include <zypp-core/base/Logger.h>
19 #include <zypp-core/fs/WatchFile>
23 #include <zypp-media/MediaConfig>
24 
25 #include <zypp-core/Globals.h>
26 
27 using std::endl;
28 
29 #define CURL_BINARY "/usr/bin/curl"
30 
31 namespace zypp
32 {
33  namespace media
34  {
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),
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;
96 
101 
107 
109 
110  // workarounds
112  };
113 
115  : _impl(new TransferSettings::Impl())
116  {}
117 
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 
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 
171  {
172  setUsername("anonymous");
173  setPassword("yast@" LIBZYPP_VERSION_STRING);
174  }
175 
176 
178  { _impl->_useproxy = enabled; }
179 
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 
213  {
214  std::string userpwd = proxyUsername();
215  if ( proxyPassword().size() ) {
216  userpwd += ":" + proxyPassword();
217  }
218  return userpwd;
219  }
220 
221 
223  { _impl->_timeout = (t); }
224 
226  { return _impl->_timeout; }
227 
228 
230  { _impl->_connect_timeout = (t); }
231 
233  { return _impl->_connect_timeout; }
234 
235 
237  { _impl->_maxConcurrentConnections = (v); }
238 
240  { return _impl->_maxConcurrentConnections; }
241 
242 
244  { _impl->_minDownloadSpeed = (v); }
245 
247  { return _impl->_minDownloadSpeed; }
248 
249 
251  { _impl->_maxDownloadSpeed = (v); }
252 
254  { return _impl->_maxDownloadSpeed; }
255 
256 
258  { _impl->_maxSilentTries = (v); }
259 
261  { return _impl->_maxSilentTries; }
262 
263 
265  { _impl->_verify_host = (enabled); }
266 
268  { return _impl->_verify_host; }
269 
270 
272  { _impl->_verify_peer = enabled; }
273 
275  { return _impl->_verify_peer; }
276 
278  { _impl->_client_cert_path = val_r; }
279 
281  { _impl->_client_cert_path = std::move( val_r ); }
282 
284  { return _impl->_client_cert_path; }
285 
286 
288  { _impl->_client_key_path = val_r; }
289 
291  { _impl->_client_key_path = std::move( val_r ); }
292 
294  { return _impl->_client_key_path; }
295 
297  { _impl->_enableCookieFile = enable; }
298 
300  { return _impl->_enableCookieFile; }
301 
303  { _impl->_ca_path = val_r; }
304 
306  { _impl->_ca_path = std::move(val_r); }
307 
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 
323  { _impl->_head_requests_allowed = allowed; }
324 
326  { return _impl->_head_requests_allowed; }
327 
328  } // namespace media
329 } // 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
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
Provides API related macros.
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
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:224
static shared_ptr< Impl > nullimpl()
Offer default Impl.
long connectTimeout() const
connection timeout
#define WAR
Definition: Logger.h:101
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
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 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
std::string userPassword() const
returns the user and password as a user:pass string
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: Application.cc:19
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