Mercurial > minori
diff src/core/http.cc @ 317:b1f4d1867ab1
services: VERY initial Kitsu support
it only supports user authentication for now, but it's definitely
a start.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 12 Jun 2024 04:07:10 -0400 |
parents | b1f625b0227c |
children | d928ec7b6a0d |
line wrap: on
line diff
--- a/src/core/http.cc Tue Jun 11 15:11:09 2024 -0400 +++ b/src/core/http.cc Wed Jun 12 04:07:10 2024 -0400 @@ -8,6 +8,60 @@ namespace HTTP { +std::string UrlEncode(const std::string& data) { + /* why do I need to init curl just for this? wtf? */ + CURL *curl = curl_easy_init(); + if (!curl) + return ""; /* no way! */ + + char* output = curl_easy_escape(curl, data.data(), data.size()); + if (!output) { + curl_easy_cleanup(curl); + return ""; + } + + std::string str(output); + + curl_free(output); + curl_easy_cleanup(curl); + + return str; +} + +std::string UrlDecode(const std::string& data) { + CURL *curl = curl_easy_init(); + if (!curl) + return ""; + + int outlength; + char* output = curl_easy_unescape(curl, data.data(), data.size(), &outlength); + if (!output) { + curl_easy_cleanup(curl); + return ""; + } + + std::string str(output, outlength); + + curl_free(output); + curl_easy_cleanup(curl); + + return str; +} + +std::string EncodeParamsList(std::string base, const std::map<std::string, std::string>& params) { + std::size_t count = 0; + for (const auto& param : params) { + base += (!count ? "?" : "&"); + base += UrlEncode(param.first); + base += "="; + base += UrlEncode(param.second); + + count++; + } + + return base; +} + static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); return size * nmemb; @@ -35,7 +89,7 @@ session.IncrementRequests(); curl_easy_cleanup(curl); if (res != CURLE_OK) - std::cerr << "curl_easy_perform(curl) failed!: " << curl_easy_strerror(res) << std::endl; + session.SetStatusBar(std::string("curl_easy_perform(curl) failed!: ") + curl_easy_strerror(res)); } return userdata; } @@ -122,7 +176,7 @@ callback_data_mutex_.lock(); if (res != CURLE_OK && !(res == CURLE_WRITE_ERROR && cancelled_)) - std::cerr << "curl_easy_perform(curl) failed!: " << curl_easy_strerror(res) << std::endl; + session.SetStatusBar(std::string("curl_easy_perform(curl) failed!: ") + curl_easy_strerror(res)); callback_data_mutex_.unlock(); }