2020-03-26 11:50:42 +00:00
|
|
|
#include "utils/http.hpp"
|
|
|
|
|
2016-12-19 21:01:37 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/easy.h>
|
2020-03-26 11:50:42 +00:00
|
|
|
|
2016-12-19 21:01:37 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "errors.hpp"
|
2017-01-11 02:07:28 +00:00
|
|
|
#include "settings.hpp"
|
2016-12-19 21:01:37 +00:00
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
http_downloader::http_downloader(int connection_timeout) {
|
|
|
|
m_curl = curl_easy_init();
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_ACCEPT_ENCODING, "deflate");
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_CONNECTTIMEOUT, connection_timeout);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, true);
|
2019-10-28 00:56:21 +00:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_USERAGENT, ("polybar/" + string{APP_VERSION}).c_str());
|
2016-12-19 21:01:37 +00:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, http_downloader::write);
|
2017-12-17 17:17:07 +00:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_FORBID_REUSE, true);
|
2016-12-19 21:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http_downloader::~http_downloader() {
|
|
|
|
curl_easy_cleanup(m_curl);
|
|
|
|
}
|
|
|
|
|
2020-03-26 11:50:42 +00:00
|
|
|
string http_downloader::get(const string& url, const string& user, const string& password) {
|
2017-01-13 10:09:56 +00:00
|
|
|
std::stringstream out{};
|
2016-12-19 21:01:37 +00:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &out);
|
2020-03-26 11:50:42 +00:00
|
|
|
if (!user.empty()) {
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_USERNAME, user.c_str());
|
|
|
|
}
|
|
|
|
if (!password.empty()) {
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_PASSWORD, password.c_str());
|
|
|
|
}
|
2016-12-19 21:01:37 +00:00
|
|
|
|
|
|
|
auto res = curl_easy_perform(m_curl);
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
throw application_error(curl_easy_strerror(res), res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
long http_downloader::response_code() {
|
|
|
|
long code{0};
|
|
|
|
curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t http_downloader::write(void* p, size_t size, size_t bytes, void* stream) {
|
|
|
|
string data{static_cast<const char*>(p), size * bytes};
|
2017-01-13 10:09:56 +00:00
|
|
|
*(static_cast<std::stringstream*>(stream)) << data << '\n';
|
2016-12-19 21:01:37 +00:00
|
|
|
return size * bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|