Debian Codegraph @

modules/cloudpinyin/fetch.h

raw
1 /*
2 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7 #ifndef _CLOUDPINYIN_FETCH_H_
8 #define _CLOUDPINYIN_FETCH_H_
9
10 #include "cloudpinyin_public.h"
11 #include <algorithm>
12 #include <cstddef>
13 #include <cstdint>
14 #include <curl/curl.h>
15 #include <curl/easy.h>
16 #include <curl/multi.h>
17 #include <fcitx-utils/event.h>
18 #include <fcitx-utils/eventdispatcher.h>
19 #include <fcitx-utils/eventloopinterface.h>
20 #include <fcitx-utils/intrusivelist.h>
21 #include <functional>
22 #include <iterator>
23 #include <memory>
24 #include <mutex>
25 #include <stdexcept>
26 #include <string>
27 #include <thread>
28 #include <unordered_map>
29 #include <utility>
30 #include <vector>
31 #define MAX_HANDLE 100l
32 #define MAX_BUFFER_SIZE 2048
33
34 class CloudPinyin;
35
36 class CurlQueue : public fcitx::IntrusiveListNode {
37 public:
38 CurlQueue() : curl_(curl_easy_init()) {
39 if (!curl_) {
40 throw std::runtime_error("Failed to init CURL handle.");
41 }
42
43 // These options should be pretty safe to set, throw exception
44 // to de-init cloudpinyin.
45 const bool result =
46 (curl_easy_setopt(curl_, CURLOPT_PRIVATE, this) == CURLE_OK) &&
47 (curl_easy_setopt(curl_, CURLOPT_WRITEDATA, this) == CURLE_OK) &&
48 (curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION,
49 &CurlQueue::curlWriteFunction) == CURLE_OK) &&
50 (curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 10L) == CURLE_OK) &&
51 (curl_easy_setopt(curl_, CURLOPT_NOSIGNAL, 1L) == CURLE_OK);
52 if (!result) {
53 throw std::runtime_error("Failed setup CURL handle options.");
54 }
55 }
56
57 ~CurlQueue() override { curl_easy_cleanup(curl_); }
58
59 void release() {
60 busy_ = false;
61 data_.clear();
62 pinyin_.clear();
63 // make sure lambda is free'd
64 callback_ = CloudPinyinCallback();
65 httpCode_ = 0;
66 }
67
68 const auto &pinyin() const { return pinyin_; }
69 void setPinyin(std::string pinyin) { pinyin_ = std::move(pinyin); }
70
71 auto curl() { return curl_; }
72 void finish(CURLcode result) {
73 curlResult_ = result;
74 curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &httpCode_);
75 }
76
77 bool busy() const { return busy_; }
78 void setBusy() { busy_ = true; }
79
80 const std::vector<char> &result() { return data_; }
81
82 CloudPinyinCallback callback() { return callback_; }
83 void setCallback(CloudPinyinCallback callback) {
84 callback_ = std::move(callback);
85 }
86
87 auto httpCode() const { return httpCode_; }
88
89 private:
90 static size_t curlWriteFunction(char *ptr, size_t size, size_t nmemb,
91 void *userdata) {
92 auto *self = static_cast<CurlQueue *>(userdata);
93 return self->curlWrite(ptr, size, nmemb);
94 }
95
96 size_t curlWrite(char *ptr, size_t size, size_t nmemb) {
97 size_t realsize = size * nmemb;
98 /*
99 * We know that it isn't possible to overflow during multiplication if
100 * neither operand uses any of the most significant half of the bits in
101 * a size_t.
102 */
103
104 if ((unsigned long long)((nmemb | size) & ((unsigned long long)SIZE_MAX
105 << (sizeof(size_t) << 2))) &&
106 (realsize / size != nmemb)) {
107 return 0;
108 }
109
110 realsize = std::min(SIZE_MAX - data_.size(), realsize);
111
112 // make sure we won't be hacked
113 if (data_.size() + realsize > MAX_BUFFER_SIZE) {
114 return 0;
115 }
116
117 data_.reserve(data_.size() + realsize);
118 std::copy(ptr, ptr + realsize, std::back_inserter(data_));
119 return realsize;
120 }
121
122 bool busy_ = false;
123 CURL *curl_ = nullptr;
124 CURLcode curlResult_ = CURLE_OK;
125 long httpCode_ = 0;
126 std::vector<char> data_;
127 std::string pinyin_;
128 CloudPinyinCallback callback_;
129 };
130
131 using SetupRequestCallback = std::function<bool(CurlQueue *)>;
132
133 class FetchThread {
134 public:
135 FetchThread(CloudPinyin *cloudPinyin);
136 ~FetchThread();
137
138 // Call from main thread.
139 bool addRequest(const SetupRequestCallback &callback);
140 CurlQueue *popFinished();
141
142 private:
143 static void runThread(FetchThread *self);
144 static int curlCallback(CURL *easy, /* easy handle */
145 curl_socket_t s, /* socket */
146 int action, /* see values below */
147 void *userp, /* private callback pointer */
148 void *socketp);
149 static int curlTimerCallback(CURLM *multi, /* multi handle */
150 long timeout_ms, /* see above */
151 void *userp); /* private callback
152 pointer */
153 int curl(curl_socket_t s, /* socket */
154 int action);
155 void curlTimer(long timeout_ms);
156
157 void handleIO(int fd, fcitx::IOEventFlags flags);
158 void processMessages();
159
160 void handlePendingRequests();
161
162 void run();
163 void finished(CurlQueue *queue);
164
165 // Call from main thread.
166 void exit();
167
168 CloudPinyin *cloudPinyin_;
169 std::unique_ptr<std::thread> thread_;
170 std::unique_ptr<fcitx::EventLoop> loop_;
171 fcitx::EventDispatcher dispatcher_;
172 std::unordered_map<int, std::unique_ptr<fcitx::EventSourceIO>> events_;
173 std::unique_ptr<fcitx::EventSourceTime> timer_;
174
175 CURLM *curlm_;
176
177 CurlQueue handles_[MAX_HANDLE];
178 fcitx::IntrusiveList<CurlQueue> pendingQueue;
179 fcitx::IntrusiveList<CurlQueue> workingQueue;
180 fcitx::IntrusiveList<CurlQueue> finishingQueue;
181
182 std::mutex pendingQueueLock;
183 std::mutex finishQueueLock;
184 };
185
186 #endif // _CLOUDPINYIN_FETCH_H_