32 #ifndef NETWORK_DOWNLOADER_HPP_ 33 #define NETWORK_DOWNLOADER_HPP_ 35 #include "../Wrapper/Curl.hpp" 37 #ifndef CRAWLSERVPP_TESTING 39 #include "../Helper/Portability/curl.h" 43 #include "FakeCurl/FakeCurl.hpp" 66 Downloader(
const std::string& url,
const std::string& proxy = std::string{});
73 [[nodiscard]]
bool isRunning()
const noexcept;
75 [[nodiscard]] std::string
getError()
const;
81 std::atomic<bool> running{
true};
86 void threadFunction(
const std::string& url,
const std::string& proxy);
89 void configure(
Wrapper::Curl& curl,
const std::string& url,
const std::string& proxy);
91 void check(CURLcode result);
94 static std::size_t writer(
void * data, std::size_t size, std::size_t nmemb,
void * ptr);
113 : thread(&
Downloader::threadFunction, this, url, proxy) {}
120 if(this->thread.joinable()) {
137 return this->running;
152 return std::string{};
155 return this->content;
170 return std::string{};
181 inline void Downloader::threadFunction(
const std::string& url,
const std::string& proxy) {
186 this->configure(curl, url, proxy);
187 this->download(curl);
189 catch(
const std::runtime_error& e) {
190 this->error = e.what();
194 this->running =
false;
202 inline void Downloader::configure(
Wrapper::Curl& curl,
const std::string& url,
const std::string& proxy) {
203 this->check(curl_easy_setopt(curl.
get(), CURLOPT_URL, url.c_str()));
204 this->check(curl_easy_setopt(curl.
get(), CURLOPT_WRITEFUNCTION, Downloader::writer));
205 this->check(curl_easy_setopt(curl.
get(), CURLOPT_WRITEDATA,
static_cast<void *
>(&(this->content))));
208 this->check(curl_easy_setopt(curl.
get(), CURLOPT_PROXY, proxy.c_str()));
214 this->check(curl_easy_perform(curl.
get()));
218 inline void Downloader::check(CURLcode code) {
219 if(code != CURLE_OK) {
220 throw std::runtime_error(curl_easy_strerror(code));
229 inline std::size_t Downloader::writer(
245 const auto bytes{size * nmemb};
248 static_cast<std::string *
>(content)->
append(static_cast<const char *>(data), size * nmemb);
bool valid() const noexcept
Checks whether the underlying libcurl handle is valid.
Definition: Curl.hpp:214
bool isRunning() const noexcept
Returns whether the download is still in progress.
Definition: Downloader.hpp:136
RAII wrapper for handles of the libcurl API.
Definition: Curl.hpp:70
static T::size_type bytes(const T &container)
Returns the number of bytes in an iterable container.
Definition: Container.hpp:144
CURL * get() noexcept
Gets a pointer to the underlying libcurl handle.
Definition: Curl.hpp:193
static void append(T &to, const T &from, typename T::size_type startAt, typename T::size_type endAt)
Appends (part of) an iterable container to another container.
Definition: Container.hpp:51
std::string getError() const
Returns the download error, if one occured.
Definition: Downloader.hpp:168
Downloader(const std::string &url, const std::string &proxy=std::string{})
Constructor starting to download a URL using a specific proxy server.
Definition: Downloader.hpp:112
~Downloader()
Destructor.
Definition: Downloader.hpp:119
Downloader using the libcurl library to download a URL in an extra thread.
Definition: Downloader.hpp:61
Namespace for networking classes.
Definition: Config.hpp:45
std::string getContent() const
Returns the downloaded content, if successfully downloaded.
Definition: Downloader.hpp:150