polybar-dwm/src/utils/http.cpp
Lucas Araújo 15496bfb4a
Update: Using another way to authenticate github module (#2029)
The github module only authenticate by query string, and this method is deprecated:
https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters


There is no reason to remove it before the method stop working, so I've made possible to the user choose which authentication method he will use:

* The parameter token remain unchanged.
* If the parameter user is passed then the module will use the not deprecated method, passing user and token on the body of the requisition. Otherwise the module will use the deprecated method.

Co-authored-by: Lucas <araujo.lucasvale@gmail.com>

Fixes #2002
2020-03-26 12:50:42 +01:00

60 lines
1.7 KiB
C++

#include "utils/http.hpp"
#include <curl/curl.h>
#include <curl/easy.h>
#include <sstream>
#include "errors.hpp"
#include "settings.hpp"
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);
curl_easy_setopt(m_curl, CURLOPT_USERAGENT, ("polybar/" + string{APP_VERSION}).c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, http_downloader::write);
curl_easy_setopt(m_curl, CURLOPT_FORBID_REUSE, true);
}
http_downloader::~http_downloader() {
curl_easy_cleanup(m_curl);
}
string http_downloader::get(const string& url, const string& user, const string& password) {
std::stringstream out{};
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &out);
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());
}
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};
*(static_cast<std::stringstream*>(stream)) << data << '\n';
return size * bytes;
}
POLYBAR_NS_END