Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 316:180714442770 | 317:b1f4d1867ab1 |
|---|---|
| 5 #include <iostream> | 5 #include <iostream> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 namespace HTTP { | 9 namespace HTTP { |
| 10 | |
| 11 std::string UrlEncode(const std::string& data) { | |
| 12 /* why do I need to init curl just for this? wtf? */ | |
| 13 CURL *curl = curl_easy_init(); | |
| 14 if (!curl) | |
| 15 return ""; /* no way! */ | |
| 16 | |
| 17 char* output = curl_easy_escape(curl, data.data(), data.size()); | |
| 18 if (!output) { | |
| 19 curl_easy_cleanup(curl); | |
| 20 return ""; | |
| 21 } | |
| 22 | |
| 23 std::string str(output); | |
| 24 | |
| 25 curl_free(output); | |
| 26 curl_easy_cleanup(curl); | |
| 27 | |
| 28 return str; | |
| 29 } | |
| 30 | |
| 31 std::string UrlDecode(const std::string& data) { | |
| 32 CURL *curl = curl_easy_init(); | |
| 33 if (!curl) | |
| 34 return ""; | |
| 35 | |
| 36 int outlength; | |
| 37 char* output = curl_easy_unescape(curl, data.data(), data.size(), &outlength); | |
| 38 if (!output) { | |
| 39 curl_easy_cleanup(curl); | |
| 40 return ""; | |
| 41 } | |
| 42 | |
| 43 std::string str(output, outlength); | |
| 44 | |
| 45 curl_free(output); | |
| 46 curl_easy_cleanup(curl); | |
| 47 | |
| 48 return str; | |
| 49 } | |
| 50 | |
| 51 std::string EncodeParamsList(std::string base, const std::map<std::string, std::string>& params) { | |
| 52 std::size_t count = 0; | |
| 53 for (const auto& param : params) { | |
| 54 base += (!count ? "?" : "&"); | |
| 55 base += UrlEncode(param.first); | |
| 56 base += "="; | |
| 57 base += UrlEncode(param.second); | |
| 58 | |
| 59 count++; | |
| 60 } | |
| 61 | |
| 62 return base; | |
| 63 } | |
| 10 | 64 |
| 11 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | 65 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { |
| 12 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); | 66 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); |
| 13 return size * nmemb; | 67 return size * nmemb; |
| 14 } | 68 } |
| 33 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // threading | 87 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // threading |
| 34 CURLcode res = curl_easy_perform(curl); | 88 CURLcode res = curl_easy_perform(curl); |
| 35 session.IncrementRequests(); | 89 session.IncrementRequests(); |
| 36 curl_easy_cleanup(curl); | 90 curl_easy_cleanup(curl); |
| 37 if (res != CURLE_OK) | 91 if (res != CURLE_OK) |
| 38 std::cerr << "curl_easy_perform(curl) failed!: " << curl_easy_strerror(res) << std::endl; | 92 session.SetStatusBar(std::string("curl_easy_perform(curl) failed!: ") + curl_easy_strerror(res)); |
| 39 } | 93 } |
| 40 return userdata; | 94 return userdata; |
| 41 } | 95 } |
| 42 | 96 |
| 43 /* this function is static */ | 97 /* this function is static */ |
| 120 session.IncrementRequests(); | 174 session.IncrementRequests(); |
| 121 curl_easy_cleanup(curl); | 175 curl_easy_cleanup(curl); |
| 122 | 176 |
| 123 callback_data_mutex_.lock(); | 177 callback_data_mutex_.lock(); |
| 124 if (res != CURLE_OK && !(res == CURLE_WRITE_ERROR && cancelled_)) | 178 if (res != CURLE_OK && !(res == CURLE_WRITE_ERROR && cancelled_)) |
| 125 std::cerr << "curl_easy_perform(curl) failed!: " << curl_easy_strerror(res) << std::endl; | 179 session.SetStatusBar(std::string("curl_easy_perform(curl) failed!: ") + curl_easy_strerror(res)); |
| 126 callback_data_mutex_.unlock(); | 180 callback_data_mutex_.unlock(); |
| 127 } | 181 } |
| 128 | 182 |
| 129 emit ReceivedData(array_); | 183 emit ReceivedData(array_); |
| 130 array_.clear(); | 184 array_.clear(); |
