Mercurial > minori
comparison src/core/http.cc @ 369:47c9f8502269
*: clang-format all the things
I've edited the formatting a bit. Now pointer asterisks (and reference
ampersands) are on the variable instead of the type, as well as having
newlines for function braces (but nothing else)
| author | Paper <paper@tflc.us> |
|---|---|
| date | Fri, 25 Jul 2025 10:16:02 -0400 |
| parents | d928ec7b6a0d |
| children | aa38b904de4f |
comparison
equal
deleted
inserted
replaced
| 368:6d37a998cf91 | 369:47c9f8502269 |
|---|---|
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 namespace HTTP { | 9 namespace HTTP { |
| 10 | 10 |
| 11 std::string UrlEncode(const std::string& data) { | 11 std::string UrlEncode(const std::string &data) |
| 12 { | |
| 12 /* why do I need to init curl just for this? wtf? */ | 13 /* why do I need to init curl just for this? wtf? */ |
| 13 CURL *curl = curl_easy_init(); | 14 CURL *curl = curl_easy_init(); |
| 14 if (!curl) | 15 if (!curl) |
| 15 return ""; /* no way! */ | 16 return ""; /* no way! */ |
| 16 | 17 |
| 17 char* output = curl_easy_escape(curl, data.data(), data.size()); | 18 char *output = curl_easy_escape(curl, data.data(), data.size()); |
| 18 if (!output) { | 19 if (!output) { |
| 19 curl_easy_cleanup(curl); | 20 curl_easy_cleanup(curl); |
| 20 return ""; | 21 return ""; |
| 21 } | 22 } |
| 22 | 23 |
| 26 curl_easy_cleanup(curl); | 27 curl_easy_cleanup(curl); |
| 27 | 28 |
| 28 return str; | 29 return str; |
| 29 } | 30 } |
| 30 | 31 |
| 31 std::string UrlDecode(const std::string& data) { | 32 std::string UrlDecode(const std::string &data) |
| 33 { | |
| 32 CURL *curl = curl_easy_init(); | 34 CURL *curl = curl_easy_init(); |
| 33 if (!curl) | 35 if (!curl) |
| 34 return ""; | 36 return ""; |
| 35 | 37 |
| 36 int outlength; | 38 int outlength; |
| 37 char* output = curl_easy_unescape(curl, data.data(), data.size(), &outlength); | 39 char *output = curl_easy_unescape(curl, data.data(), data.size(), &outlength); |
| 38 if (!output) { | 40 if (!output) { |
| 39 curl_easy_cleanup(curl); | 41 curl_easy_cleanup(curl); |
| 40 return ""; | 42 return ""; |
| 41 } | 43 } |
| 42 | 44 |
| 46 curl_easy_cleanup(curl); | 48 curl_easy_cleanup(curl); |
| 47 | 49 |
| 48 return str; | 50 return str; |
| 49 } | 51 } |
| 50 | 52 |
| 51 std::string EncodeParamsList(std::string base, const std::map<std::string, std::string>& params) { | 53 std::string EncodeParamsList(std::string base, const std::map<std::string, std::string> ¶ms) |
| 54 { | |
| 52 std::size_t count = 0; | 55 std::size_t count = 0; |
| 53 for (const auto& param : params) { | 56 for (const auto ¶m : params) { |
| 54 base += (!count ? "?" : "&"); | 57 base += (!count ? "?" : "&"); |
| 55 base += UrlEncode(param.first); | 58 base += UrlEncode(param.first); |
| 56 base += "="; | 59 base += "="; |
| 57 base += UrlEncode(param.second); | 60 base += UrlEncode(param.second); |
| 58 | 61 |
| 60 } | 63 } |
| 61 | 64 |
| 62 return base; | 65 return base; |
| 63 } | 66 } |
| 64 | 67 |
| 65 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | 68 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userdata) |
| 66 reinterpret_cast<QByteArray*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); | 69 { |
| 70 reinterpret_cast<QByteArray *>(userdata)->append(reinterpret_cast<char *>(contents), size * nmemb); | |
| 67 return size * nmemb; | 71 return size * nmemb; |
| 68 } | 72 } |
| 69 | 73 |
| 70 QByteArray Request(const std::string& url, const std::vector<std::string>& headers, const std::string& data, Type type) { | 74 QByteArray Request(const std::string &url, const std::vector<std::string> &headers, const std::string &data, Type type) |
| 71 struct curl_slist* list = NULL; | 75 { |
| 76 struct curl_slist *list = NULL; | |
| 72 QByteArray userdata; | 77 QByteArray userdata; |
| 73 | 78 |
| 74 CURL* curl = curl_easy_init(); | 79 CURL *curl = curl_easy_init(); |
| 75 if (curl) { | 80 if (curl) { |
| 76 for (const std::string& h : headers) | 81 for (const std::string &h : headers) |
| 77 list = curl_slist_append(list, h.c_str()); | 82 list = curl_slist_append(list, h.c_str()); |
| 78 | 83 |
| 79 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | 84 curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 80 if (type == Type::Post) | 85 if (type == Type::Post) |
| 81 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | 86 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); |
| 93 } | 98 } |
| 94 return userdata; | 99 return userdata; |
| 95 } | 100 } |
| 96 | 101 |
| 97 /* this function is static */ | 102 /* this function is static */ |
| 98 size_t RequestThread::WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | 103 size_t RequestThread::WriteCallback(void *contents, size_t size, size_t nmemb, void *userdata) |
| 99 RequestThread* thread = reinterpret_cast<RequestThread*>(userdata); | 104 { |
| 105 RequestThread *thread = reinterpret_cast<RequestThread *>(userdata); | |
| 100 | 106 |
| 101 const std::lock_guard<std::mutex> lock(thread->callback_data_mutex_); | 107 const std::lock_guard<std::mutex> lock(thread->callback_data_mutex_); |
| 102 | 108 |
| 103 /* stop writing, then! */ | 109 /* stop writing, then! */ |
| 104 if (thread->cancelled_) | 110 if (thread->cancelled_) |
| 105 return CURL_WRITEFUNC_ERROR; | 111 return CURL_WRITEFUNC_ERROR; |
| 106 | 112 |
| 107 /* else, continue on as normal */ | 113 /* else, continue on as normal */ |
| 108 thread->array_.append(reinterpret_cast<char*>(contents), size * nmemb); | 114 thread->array_.append(reinterpret_cast<char *>(contents), size * nmemb); |
| 109 return size * nmemb; | 115 return size * nmemb; |
| 110 } | 116 } |
| 111 | 117 |
| 112 RequestThread::RequestThread(Type type, QObject* parent) : QThread(parent) { | 118 RequestThread::RequestThread(Type type, QObject *parent) : QThread(parent) |
| 119 { | |
| 113 SetType(type); | 120 SetType(type); |
| 114 } | 121 } |
| 115 | 122 |
| 116 RequestThread::RequestThread(const std::string& url, const std::vector<std::string>& headers, | 123 RequestThread::RequestThread(const std::string &url, const std::vector<std::string> &headers, const std::string &data, |
| 117 const std::string& data, Type type, QObject* parent) | 124 Type type, QObject *parent) |
| 118 : QThread(parent) { | 125 : QThread(parent) |
| 126 { | |
| 119 SetUrl(url); | 127 SetUrl(url); |
| 120 SetData(data); | 128 SetData(data); |
| 121 SetHeaders(headers); | 129 SetHeaders(headers); |
| 122 SetType(type); | 130 SetType(type); |
| 123 } | 131 } |
| 124 | 132 |
| 125 RequestThread::~RequestThread() { | 133 RequestThread::~RequestThread() |
| 134 { | |
| 126 /* block until the function can safely exit */ | 135 /* block until the function can safely exit */ |
| 127 Stop(); | 136 Stop(); |
| 128 wait(); | 137 wait(); |
| 129 } | 138 } |
| 130 | 139 |
| 131 void RequestThread::SetUrl(const std::string& url) { | 140 void RequestThread::SetUrl(const std::string &url) |
| 141 { | |
| 132 url_ = url; | 142 url_ = url; |
| 133 } | 143 } |
| 134 | 144 |
| 135 void RequestThread::SetHeaders(const std::vector<std::string>& headers) { | 145 void RequestThread::SetHeaders(const std::vector<std::string> &headers) |
| 146 { | |
| 136 headers_ = headers; | 147 headers_ = headers; |
| 137 } | 148 } |
| 138 | 149 |
| 139 void RequestThread::SetData(const std::string& data) { | 150 void RequestThread::SetData(const std::string &data) |
| 151 { | |
| 140 data_ = data; | 152 data_ = data; |
| 141 } | 153 } |
| 142 | 154 |
| 143 void RequestThread::SetType(Type type) { | 155 void RequestThread::SetType(Type type) |
| 156 { | |
| 144 type_ = type; | 157 type_ = type; |
| 145 } | 158 } |
| 146 | 159 |
| 147 void RequestThread::run() { | 160 void RequestThread::run() |
| 148 struct curl_slist* list = NULL; | 161 { |
| 149 | 162 struct curl_slist *list = NULL; |
| 150 CURL* curl = curl_easy_init(); | 163 |
| 164 CURL *curl = curl_easy_init(); | |
| 151 if (curl) { | 165 if (curl) { |
| 152 curl_easy_setopt(curl, CURLOPT_URL, url_.c_str()); | 166 curl_easy_setopt(curl, CURLOPT_URL, url_.c_str()); |
| 153 | 167 |
| 154 if (type_ == Type::Post) | 168 if (type_ == Type::Post) |
| 155 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_.c_str()); | 169 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_.c_str()); |
| 156 | 170 |
| 157 for (const std::string& h : headers_) | 171 for (const std::string &h : headers_) |
| 158 list = curl_slist_append(list, h.c_str()); | 172 list = curl_slist_append(list, h.c_str()); |
| 159 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | 173 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); |
| 160 | 174 |
| 161 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); | 175 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); |
| 162 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &RequestThread::WriteCallback); | 176 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &RequestThread::WriteCallback); |
| 179 | 193 |
| 180 emit ReceivedData(array_); | 194 emit ReceivedData(array_); |
| 181 array_.clear(); | 195 array_.clear(); |
| 182 } | 196 } |
| 183 | 197 |
| 184 void RequestThread::Stop() { | 198 void RequestThread::Stop() |
| 199 { | |
| 185 const std::lock_guard<std::mutex> lock(callback_data_mutex_); | 200 const std::lock_guard<std::mutex> lock(callback_data_mutex_); |
| 186 cancelled_ = true; | 201 cancelled_ = true; |
| 187 } | 202 } |
| 188 | 203 |
| 189 } // namespace HTTP | 204 } // namespace HTTP |
