Mercurial > minori
annotate 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 |
| rev | line source |
|---|---|
|
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
1 #ifndef MINORI_CORE_HTTP_H_ |
|
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
2 #define MINORI_CORE_HTTP_H_ |
| 75 | 3 |
| 77 | 4 #include <QByteArray> |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
5 #include <QThread> |
| 291 | 6 |
| 75 | 7 #include <string> |
| 8 #include <vector> | |
| 291 | 9 #include <mutex> |
| 75 | 10 |
| 11 namespace HTTP { | |
| 12 | |
| 291 | 13 enum class Type { |
| 14 Get, | |
| 15 Post | |
| 16 }; | |
| 75 | 17 |
| 291 | 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 { | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
21 Q_OBJECT |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
22 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
23 public: |
| 291 | 24 RequestThread(Type type = Type::Get, QObject* parent = nullptr); |
| 25 RequestThread(const std::string& url, const std::vector<std::string>& headers = {}, | |
| 26 const std::string& data = "", Type type = Type::Get, QObject* parent = nullptr); | |
| 27 ~RequestThread(); | |
| 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(); | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
35 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
36 signals: |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
37 void ReceivedData(const QByteArray& ba); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
38 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
39 protected: |
| 291 | 40 void run() override; |
| 41 static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userdata); | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
42 |
| 291 | 43 std::string url_; |
| 44 std::string data_; | |
| 45 std::vector<std::string> headers_; | |
| 46 Type type_; | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
47 |
| 291 | 48 /* these are passed to the write callback */ |
| 49 QByteArray array_; | |
| 50 bool cancelled_ = false; | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
51 |
| 291 | 52 /* don't fuck this up */ |
| 53 std::mutex callback_data_mutex_; | |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
54 }; |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
100
diff
changeset
|
55 |
| 76 | 56 } // namespace HTTP |
| 75 | 57 |
|
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
58 #endif // MINORI_CORE_HTTP_H_ |
