Mercurial > minori
diff include/core/http.h @ 291:9a88e1725fd2
*: refactor lots of stuff
I forgot to put this into different commits, oops!
anyway, it doesn't really matter *that* much since this is an
unfinished hobby project anyway. once it starts getting stable
commit history will be more important, but for now it's not
that big of a deal
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 12 May 2024 16:31:07 -0400 |
parents | 3ec7804abf17 |
children | b1f625b0227c |
line wrap: on
line diff
--- a/include/core/http.h Wed May 08 17:32:28 2024 -0400 +++ b/include/core/http.h Sun May 12 16:31:07 2024 -0400 @@ -3,55 +3,54 @@ #include <QByteArray> #include <QThread> + #include <string> #include <vector> +#include <mutex> namespace HTTP { -QByteArray Get(const std::string& url, const std::vector<std::string>& headers = {}); -QByteArray Post(const std::string& url, const std::string& data, const std::vector<std::string>& headers = {}); +enum class Type { + Get, + Post +}; -class GetThread : public QThread { +QByteArray Request(const std::string& url, const std::vector<std::string>& headers = {}, const std::string& data = "", Type type = Type::Get); + +class RequestThread final : public QThread { Q_OBJECT public: - GetThread(const std::string& u, const std::vector<std::string>& h = {}, QObject* parent = nullptr) - : QThread(parent) { - url = u; - headers = h; - } + RequestThread(Type type = Type::Get, QObject* parent = nullptr); + RequestThread(const std::string& url, const std::vector<std::string>& headers = {}, + const std::string& data = "", Type type = Type::Get, QObject* parent = nullptr); + ~RequestThread(); + + void SetUrl(const std::string& url); + void SetHeaders(const std::vector<std::string>& headers); + void SetData(const std::string& data); + void SetType(Type type); + + void Stop(); signals: void ReceivedData(const QByteArray& ba); protected: - void run() override { emit ReceivedData(Get(url, headers)); } - - std::string url; - std::vector<std::string> headers; -}; - -class PostThread : public QThread { - Q_OBJECT + void run() override; + static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata); -public: - PostThread(const std::string& u, const std::string& d, const std::vector<std::string>& h = {}, - QObject* parent = nullptr) - : QThread(parent) { - url = u; - data = d; - headers = h; - } + std::string url_; + std::string data_; + std::vector<std::string> headers_; + Type type_; -signals: - void ReceivedData(const QByteArray& ba); + /* these are passed to the write callback */ + QByteArray array_; + bool cancelled_ = false; -protected: - void run() override { emit ReceivedData(Post(url, data, headers)); } - - std::string url; - std::string data; - std::vector<std::string> headers; + /* don't fuck this up */ + std::mutex callback_data_mutex_; }; } // namespace HTTP