Mercurial > minori
diff 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 |
line wrap: on
line diff
--- a/src/core/http.cc Fri Jul 25 10:05:23 2025 -0400 +++ b/src/core/http.cc Fri Jul 25 10:16:02 2025 -0400 @@ -8,13 +8,14 @@ namespace HTTP { -std::string UrlEncode(const std::string& data) { +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()); + char *output = curl_easy_escape(curl, data.data(), data.size()); if (!output) { curl_easy_cleanup(curl); return ""; @@ -28,13 +29,14 @@ return str; } -std::string UrlDecode(const std::string& data) { +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); + char *output = curl_easy_unescape(curl, data.data(), data.size(), &outlength); if (!output) { curl_easy_cleanup(curl); return ""; @@ -48,9 +50,10 @@ return str; } -std::string EncodeParamsList(std::string base, const std::map<std::string, std::string>& params) { +std::string EncodeParamsList(std::string base, const std::map<std::string, std::string> ¶ms) +{ std::size_t count = 0; - for (const auto& param : params) { + for (const auto ¶m : params) { base += (!count ? "?" : "&"); base += UrlEncode(param.first); base += "="; @@ -62,18 +65,20 @@ 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); +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; } -QByteArray Request(const std::string& url, const std::vector<std::string>& headers, const std::string& data, Type type) { - struct curl_slist* list = NULL; +QByteArray Request(const std::string &url, const std::vector<std::string> &headers, const std::string &data, Type type) +{ + struct curl_slist *list = NULL; QByteArray userdata; - CURL* curl = curl_easy_init(); + CURL *curl = curl_easy_init(); if (curl) { - for (const std::string& h : headers) + for (const std::string &h : headers) list = curl_slist_append(list, h.c_str()); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); @@ -95,8 +100,9 @@ } /* this function is static */ -size_t RequestThread::WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { - RequestThread* thread = reinterpret_cast<RequestThread*>(userdata); +size_t RequestThread::WriteCallback(void *contents, size_t size, size_t nmemb, void *userdata) +{ + RequestThread *thread = reinterpret_cast<RequestThread *>(userdata); const std::lock_guard<std::mutex> lock(thread->callback_data_mutex_); @@ -105,56 +111,64 @@ return CURL_WRITEFUNC_ERROR; /* else, continue on as normal */ - thread->array_.append(reinterpret_cast<char*>(contents), size * nmemb); + thread->array_.append(reinterpret_cast<char *>(contents), size * nmemb); return size * nmemb; } -RequestThread::RequestThread(Type type, QObject* parent) : QThread(parent) { +RequestThread::RequestThread(Type type, QObject *parent) : QThread(parent) +{ SetType(type); } -RequestThread::RequestThread(const std::string& url, const std::vector<std::string>& headers, - const std::string& data, Type type, QObject* parent) - : QThread(parent) { +RequestThread::RequestThread(const std::string &url, const std::vector<std::string> &headers, const std::string &data, + Type type, QObject *parent) + : QThread(parent) +{ SetUrl(url); SetData(data); SetHeaders(headers); SetType(type); } -RequestThread::~RequestThread() { +RequestThread::~RequestThread() +{ /* block until the function can safely exit */ Stop(); wait(); } -void RequestThread::SetUrl(const std::string& url) { +void RequestThread::SetUrl(const std::string &url) +{ url_ = url; } -void RequestThread::SetHeaders(const std::vector<std::string>& headers) { +void RequestThread::SetHeaders(const std::vector<std::string> &headers) +{ headers_ = headers; } -void RequestThread::SetData(const std::string& data) { +void RequestThread::SetData(const std::string &data) +{ data_ = data; } -void RequestThread::SetType(Type type) { +void RequestThread::SetType(Type type) +{ type_ = type; } -void RequestThread::run() { - struct curl_slist* list = NULL; +void RequestThread::run() +{ + struct curl_slist *list = NULL; - CURL* curl = curl_easy_init(); + CURL *curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url_.c_str()); if (type_ == Type::Post) curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_.c_str()); - for (const std::string& h : headers_) + for (const std::string &h : headers_) list = curl_slist_append(list, h.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); @@ -181,7 +195,8 @@ array_.clear(); } -void RequestThread::Stop() { +void RequestThread::Stop() +{ const std::lock_guard<std::mutex> lock(callback_data_mutex_); cancelled_ = true; }