11#include <framework/utilities/ScopeGuard.h>
17namespace Belle2::Conditions {
74 bool download(
const std::string& url, std::ostream& stream,
bool silentOnMissing =
false);
85 std::string
joinWithSlash(
const std::string& base,
const std::string& second);
119 std::unique_ptr<std::mt19937>
m_rnd{std::make_unique<std::mt19937>()};
121 std::unique_ptr<std::uniform_real_distribution<double>>
m_rndDistribution{std::make_unique<std::uniform_real_distribution<double>>()};
void finishSession()
Finish an existing curl session if any is active at the moment.
void setBackoffFactor(unsigned int factor)
Set the backoff factor for retries in seconds.
static bool s_globalInit
flag to indicate whether curl has been initialized already
unsigned int getMaxRetries() const
Get the number of retries to perform when downloading fails with HTTP response code >=500,...
std::unique_ptr< std::uniform_real_distribution< double > > m_rndDistribution
A uniform real distribution for extracting random numbers.
unsigned int getStalledTimeout() const
Get the timeout to wait for stalled connections (<10KB/s), 0 means no timeout.
bool startSession()
Start a new curl session if none is active at the moment.
void initializeRandomGeneratorSeed()
Initialize the seed of the internal random number generator.
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
unsigned int m_maxRetries
Number of retries to perform when downloading fails with HTTP response code >=300.
unsigned int m_connectionTimeout
Timeout to wait for connections in seconds.
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.
unsigned int getBackoffFactor() const
Get the backoff factor for retries in seconds.
std::unique_ptr< CurlSession > m_session
curl session handle
unsigned int m_stalledTimeout
Timeout to wait for stalled connections (<10KB/s)
bool m_rndIsInitialized
Flag for keeping track if the internal random generator is correctly initialized or not.
std::unique_ptr< std::mt19937 > m_rnd
This is a special exception in basf2 where an instance of gRandom is NOT used: since this class inter...
std::string joinWithSlash(const std::string &base, const std::string &second)
Join two strings and make sure that there is exactly one '/' between them.
static std::string calculateChecksum(std::istream &input)
calculate the digest/checksum on a given string.
void setConnectionTimeout(unsigned int timeout)
Set the timeout to wait for connections in seconds, 0 means built in curl default.
std::string escapeString(const std::string &text)
Escape a string to make it safe to be used in web requests.
unsigned int getConnectionTimeout() const
Get the timeout to wait for connections in seconds, 0 means the built in curl default.
ScopeGuard ensureSession()
Make sure there's an active session and return a ScopeGuard object that closes the session on destruc...
void setMaxRetries(unsigned int retries)
Set the number of retries to perform when downloading fails with HTTP response code >=500,...
bool verifyChecksum(std::istream &input, const std::string &checksum)
check the digest of a stream
unsigned int m_backoffFactor
Backoff factor for retries in seconds.
static Downloader & getDefaultInstance()
Return the default instance.
Simple ScopeGuard to execute a function at the end of the object lifetime.
struct encapsulating all the state information needed by curl