Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 290:9347e2eaf6e5 | 291:9a88e1725fd2 |
|---|---|
| 1 #ifndef MINORI_CORE_HTTP_H_ | 1 #ifndef MINORI_CORE_HTTP_H_ |
| 2 #define MINORI_CORE_HTTP_H_ | 2 #define MINORI_CORE_HTTP_H_ |
| 3 | 3 |
| 4 #include <QByteArray> | 4 #include <QByteArray> |
| 5 #include <QThread> | 5 #include <QThread> |
| 6 | |
| 6 #include <string> | 7 #include <string> |
| 7 #include <vector> | 8 #include <vector> |
| 9 #include <mutex> | |
| 8 | 10 |
| 9 namespace HTTP { | 11 namespace HTTP { |
| 10 | 12 |
| 11 QByteArray Get(const std::string& url, const std::vector<std::string>& headers = {}); | 13 enum class Type { |
| 12 QByteArray Post(const std::string& url, const std::string& data, const std::vector<std::string>& headers = {}); | 14 Get, |
| 15 Post | |
| 16 }; | |
| 13 | 17 |
| 14 class GetThread : public QThread { | 18 QByteArray Request(const std::string& url, const std::vector<std::string>& headers = {}, const std::string& data = "", Type type = Type::Get); |
| 19 | |
| 20 class RequestThread final : public QThread { | |
| 15 Q_OBJECT | 21 Q_OBJECT |
| 16 | 22 |
| 17 public: | 23 public: |
| 18 GetThread(const std::string& u, const std::vector<std::string>& h = {}, QObject* parent = nullptr) | 24 RequestThread(Type type = Type::Get, QObject* parent = nullptr); |
| 19 : QThread(parent) { | 25 RequestThread(const std::string& url, const std::vector<std::string>& headers = {}, |
| 20 url = u; | 26 const std::string& data = "", Type type = Type::Get, QObject* parent = nullptr); |
| 21 headers = h; | 27 ~RequestThread(); |
| 22 } | 28 |
| 29 void SetUrl(const std::string& url); | |
| 30 void SetHeaders(const std::vector<std::string>& headers); | |
| 31 void SetData(const std::string& data); | |
| 32 void SetType(Type type); | |
| 33 | |
| 34 void Stop(); | |
| 23 | 35 |
| 24 signals: | 36 signals: |
| 25 void ReceivedData(const QByteArray& ba); | 37 void ReceivedData(const QByteArray& ba); |
| 26 | 38 |
| 27 protected: | 39 protected: |
| 28 void run() override { emit ReceivedData(Get(url, headers)); } | 40 void run() override; |
| 41 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata); | |
| 29 | 42 |
| 30 std::string url; | 43 std::string url_; |
| 31 std::vector<std::string> headers; | 44 std::string data_; |
| 32 }; | 45 std::vector<std::string> headers_; |
| 46 Type type_; | |
| 33 | 47 |
| 34 class PostThread : public QThread { | 48 /* these are passed to the write callback */ |
| 35 Q_OBJECT | 49 QByteArray array_; |
| 50 bool cancelled_ = false; | |
| 36 | 51 |
| 37 public: | 52 /* don't fuck this up */ |
| 38 PostThread(const std::string& u, const std::string& d, const std::vector<std::string>& h = {}, | 53 std::mutex callback_data_mutex_; |
| 39 QObject* parent = nullptr) | |
| 40 : QThread(parent) { | |
| 41 url = u; | |
| 42 data = d; | |
| 43 headers = h; | |
| 44 } | |
| 45 | |
| 46 signals: | |
| 47 void ReceivedData(const QByteArray& ba); | |
| 48 | |
| 49 protected: | |
| 50 void run() override { emit ReceivedData(Post(url, data, headers)); } | |
| 51 | |
| 52 std::string url; | |
| 53 std::string data; | |
| 54 std::vector<std::string> headers; | |
| 55 }; | 54 }; |
| 56 | 55 |
| 57 } // namespace HTTP | 56 } // namespace HTTP |
| 58 | 57 |
| 59 #endif // MINORI_CORE_HTTP_H_ | 58 #endif // MINORI_CORE_HTTP_H_ |
