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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 76
diff changeset
4 #include <QByteArray>
230
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 100
diff changeset
5 #include <QThread>
291
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
6
75
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include <string>
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <vector>
291
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
9 #include <mutex>
75
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 namespace HTTP {
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12
291
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
13 enum class Type {
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
14 Get,
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
15 Post
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
16 };
75
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
291
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
18 QByteArray Request(const std::string& url, const std::vector<std::string>& headers = {}, const std::string& data = "", Type type = Type::Get);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
19
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
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
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
24 RequestThread(Type type = Type::Get, QObject* parent = nullptr);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
25 RequestThread(const std::string& url, const std::vector<std::string>& headers = {},
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
26 const std::string& data = "", Type type = Type::Get, QObject* parent = nullptr);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
27 ~RequestThread();
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
28
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
29 void SetUrl(const std::string& url);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
30 void SetHeaders(const std::vector<std::string>& headers);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
31 void SetData(const std::string& data);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
32 void SetType(Type type);
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
33
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
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
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
40 void run() override;
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
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
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
43 std::string url_;
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
44 std::string data_;
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
45 std::vector<std::string> headers_;
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
46 Type type_;
230
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 100
diff changeset
47
291
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
48 /* these are passed to the write callback */
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
49 QByteArray array_;
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
50 bool cancelled_ = false;
230
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 100
diff changeset
51
291
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
52 /* don't fuck this up */
9a88e1725fd2 *: refactor lots of stuff
Paper <paper@paper.us.eu.org>
parents: 261
diff changeset
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
3364fadc8a36 *: format source code
Paper <mrpapersonic@gmail.com>
parents: 75
diff changeset
56 } // namespace HTTP
75
d3e9310598b1 *: refactor some stuff
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
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_