annotate include/core/http.h @ 390:2d3e10319112

http: optimize HTTP request thread we don't need a mutex at all, in fact all we need is an atomic boolean to signify whether the thread is cancelled. curl options are now for the most part handled by a separate function to keep them in sync between non-threaded and threaded implementations
author Paper <paper@tflc.us>
date Fri, 07 Nov 2025 07:08:57 -0500
parents 1e5d922fe82b
children 963047512d34
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
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
7 #include <mutex>
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
8 #include <string>
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
9 #include <vector>
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
10 #include <atomic>
301
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
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
14 /* calls libcurl to encode/decode */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
15 std::string UrlEncode(const std::string &data);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
16 std::string UrlDecode(const std::string &data);
317
b1f4d1867ab1 services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents: 301
diff changeset
17
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
18 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
19
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
20 enum class Type {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
21 Get,
389
1e5d922fe82b kitsu: implement UpdateAnimeEntry
Paper <paper@tflc.us>
parents: 369
diff changeset
22 Post,
1e5d922fe82b kitsu: implement UpdateAnimeEntry
Paper <paper@tflc.us>
parents: 369
diff changeset
23 Patch
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
24 };
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
25
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
26 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
27 Type type = Type::Get);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
28
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
29 class RequestThread final : public QThread {
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
30 Q_OBJECT
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
31
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
32 public:
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
33 RequestThread(Type type = Type::Get, QObject *parent = nullptr);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
34 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
35 Type type = Type::Get, QObject *parent = nullptr);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
36 ~RequestThread();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
37
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
38 void SetUrl(const std::string &url);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
39 void SetHeaders(const std::vector<std::string> &headers);
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
40 void SetData(const std::string &data);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
41 void SetType(Type type);
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 void Stop();
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
44
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
45 signals:
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
46 void ReceivedData(const QByteArray &ba);
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
47
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
48 protected:
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
49 void run() override;
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 317
diff changeset
50 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
51
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
52 std::string url_;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
53 std::string data_;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
54 std::vector<std::string> headers_;
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
55 Type type_;
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
56 void *curl_;
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
57
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
58 /* these are passed to the write callback */
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
59 QByteArray array_;
390
2d3e10319112 http: optimize HTTP request thread
Paper <paper@tflc.us>
parents: 389
diff changeset
60 std::atomic<bool> cancelled_ = false;
301
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
61 };
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 } // namespace HTTP
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
64
b1f625b0227c *: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents: 291
diff changeset
65 #endif // MINORI_CORE_HTTP_H_