annotate include/core/http.h @ 397:811697ad826a

http: do proper global init/cleanup of libcurl this is done automagically using RAII
author Paper <paper@tflc.us>
date Fri, 07 Nov 2025 08:39:24 -0500
parents 963047512d34
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
1 #ifndef MINORI_CORE_HTTP_H_
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
2 #define MINORI_CORE_HTTP_H_
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
3
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
4 #include <QByteArray>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
5 #include <QThread>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
6
393
963047512d34 *: clang-format
Paper <paper@tflc.us>
parents: 390
diff changeset
7 #include <atomic>
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
8 #include <mutex>
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
9 #include <string>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
10 #include <vector>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
11
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
12 namespace HTTP {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
13
397
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
14 /* Global state init/quit */
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
15 void Init();
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
16 void Quit();
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
17
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
18 /* helper class that automatically init/deinit as constructors
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
19 * and destructors */
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
20 struct Initializer {
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
21 Initializer() { Init(); }
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
22 ~Initializer() { Quit(); }
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
23 };
811697ad826a http: do proper global init/cleanup of libcurl
Paper <paper@tflc.us>
parents: 393
diff changeset
24
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
25 /* calls libcurl to encode/decode */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
26 std::string UrlEncode(const std::string &data);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
27 std::string UrlDecode(const std::string &data);
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
28
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
29 std::string EncodeParamsList(std::string base, const std::map<std::string, std::string> &params);
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
30
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
31 enum class Type {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
32 Get,
389
1e5d922fe82b kitsu: implement UpdateAnimeEntry
Paper <paper@tflc.us>
parents: 369
diff changeset
33 Post,
1e5d922fe82b kitsu: implement UpdateAnimeEntry
Paper <paper@tflc.us>
parents: 369
diff changeset
34 Patch
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
35 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
36
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
37 QByteArray Request(const std::string &url, const std::vector<std::string> &headers = {}, const std::string &data = "",
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
38 Type type = Type::Get);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
39
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
40 class RequestThread final : public QThread {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
41 Q_OBJECT
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
42
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
43 public:
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
44 RequestThread(Type type = Type::Get, QObject *parent = nullptr);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
45 RequestThread(const std::string &url, const std::vector<std::string> &headers = {}, const std::string &data = "",
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
46 Type type = Type::Get, QObject *parent = nullptr);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
47 ~RequestThread();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
48
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
49 void SetUrl(const std::string &url);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
50 void SetHeaders(const std::vector<std::string> &headers);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
51 void SetData(const std::string &data);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
52 void SetType(Type type);
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
53
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
54 void Stop();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
55
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
56 signals:
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
57 void ReceivedData(const QByteArray &ba);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
58
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
59 protected:
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
60 void run() override;
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
61 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userdata);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
62
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
63 std::string url_;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
64 std::string data_;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
65 std::vector<std::string> headers_;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
66 Type type_;
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
67 void *curl_;
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
68
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
69 /* these are passed to the write callback */
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
70 QByteArray array_;
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
71 std::atomic<bool> cancelled_ = false;
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
72 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
73
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
74 } // namespace HTTP
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
75
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
76 #endif // MINORI_CORE_HTTP_H_