Belle II Software  release-08-01-10
Downloader.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #pragma once
10 
11 #include <framework/utilities/ScopeGuard.h>
12 
13 #include <iosfwd>
14 #include <memory>
15 
16 namespace Belle2::Conditions {
18  struct CurlSession;
19 
21  class Downloader final {
22  public:
25  Downloader() = default;
27  ~Downloader();
28 
37  bool startSession();
39  void finishSession();
44  {
45  bool started = startSession();
46  return ScopeGuard([this, started] {if (started) finishSession();});
47  }
48 
50  unsigned int getConnectionTimeout() const { return m_connectionTimeout; }
52  unsigned int getStalledTimeout() const { return m_stalledTimeout; }
54  unsigned int getMaxRetries() const { return m_maxRetries; }
56  unsigned int getBackoffFactor() const { return m_backoffFactor; }
58  void setConnectionTimeout(unsigned int timeout);
60  void setStalledTimeout(unsigned int timeout);
62  void setMaxRetries(unsigned int retries) { m_maxRetries = retries; }
64  void setBackoffFactor(unsigned int factor) { m_backoffFactor = std::max(1u, factor); }
65 
75  bool download(const std::string& url, std::ostream& stream, bool silentOnMissing = false);
76 
82  bool verifyChecksum(std::istream& input, const std::string& checksum) { return calculateChecksum(input) == checksum; }
84  std::string escapeString(const std::string& text);
86  std::string joinWithSlash(const std::string& base, const std::string& second);
87 
88  private:
93  static std::string calculateChecksum(std::istream& input);
94 
96  std::unique_ptr<CurlSession> m_session;
98  static bool s_globalInit;
100  unsigned int m_connectionTimeout{60};
102  unsigned int m_stalledTimeout{60};
104  unsigned int m_maxRetries{5};
106  unsigned int m_backoffFactor{5};
107  };
108 } // namespace Belle2::Conditions
Simple class to encapsulate libcurl as used by the ConditionsDatabase.
Definition: Downloader.h:21
void finishSession()
Finish an existing curl session if any is active at the moment.
Definition: Downloader.cc:214
void setBackoffFactor(unsigned int factor)
Set the backoff factor for retries in seconds.
Definition: Downloader.h:64
static bool s_globalInit
flag to indicate whether curl has been initialized already
Definition: Downloader.h:98
unsigned int getMaxRetries() const
Get the number of retries to perform when downloading fails with HTTP response code >=500,...
Definition: Downloader.h:54
unsigned int getStalledTimeout() const
Get the timeout to wait for stalled connections (<10KB/s), 0 means no timeout.
Definition: Downloader.h:52
bool startSession()
Start a new curl session if none is active at the moment.
Definition: Downloader.cc:161
bool download(const std::string &url, std::ostream &stream, bool silentOnMissing=false)
get an url and save the content to stream This function raises exceptions when there are any problems
Definition: Downloader.cc:258
unsigned int m_maxRetries
Number of retries to perform when downloading fails with HTTP response code >=500.
Definition: Downloader.h:104
unsigned int m_connectionTimeout
Timeout to wait for connections in seconds.
Definition: Downloader.h:100
Downloader()=default
Create a new payload downloader.
void setStalledTimeout(unsigned int timeout)
Set the timeout to wait for stalled connections (<10KB/s), 0 disables timeout.
Definition: Downloader.cc:250
unsigned int getBackoffFactor() const
Get the backoff factor for retries in seconds.
Definition: Downloader.h:56
std::unique_ptr< CurlSession > m_session
curl session handle
Definition: Downloader.h:96
unsigned int m_stalledTimeout
Timeout to wait for stalled connections (<10KB/s)
Definition: Downloader.h:102
std::string joinWithSlash(const std::string &base, const std::string &second)
Join two strings and make sure that there is exactly one '/' between them.
Definition: Downloader.cc:155
static std::string calculateChecksum(std::istream &input)
calculate the digest/checksum on a given string.
Definition: Downloader.cc:224
void setConnectionTimeout(unsigned int timeout)
Set the timeout to wait for connections in seconds, 0 means built in curl default.
Definition: Downloader.cc:242
std::string escapeString(const std::string &text)
Escape a string to make it safe to be used in web requests.
Definition: Downloader.cc:141
unsigned int getConnectionTimeout() const
Get the timeout to wait for connections in seconds, 0 means the built in curl default.
Definition: Downloader.h:50
ScopeGuard ensureSession()
Make sure there's an active session and return a ScopeGuard object that closes the session on destruc...
Definition: Downloader.h:43
void setMaxRetries(unsigned int retries)
Set the number of retries to perform when downloading fails with HTTP response code >=500,...
Definition: Downloader.h:62
bool verifyChecksum(std::istream &input, const std::string &checksum)
check the digest of a stream
Definition: Downloader.h:82
unsigned int m_backoffFactor
Backoff factor for retries in seconds.
Definition: Downloader.h:106
static Downloader & getDefaultInstance()
Return the default instance.
Definition: Downloader.cc:133
Simple ScopeGuard to execute a function at the end of the object lifetime.
Definition: ScopeGuard.h:36