-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathCurlHttpClient.cpp
More file actions
149 lines (121 loc) · 4.93 KB
/
Copy pathCurlHttpClient.cpp
File metadata and controls
149 lines (121 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "tgbot/CurlHttpClient.h"
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <span>
#include <string>
#include <variant>
namespace TgBot {
namespace {
struct RequestCancelState {
RequestCancelState(const std::atomic<bool>* const isEternalCancel,
const std::atomic<uint64_t>* const globalCancelEpoch, const uint64_t currentCancelEpoch)
: isEternalCancel(isEternalCancel)
, globalCancelEpoch(globalCancelEpoch)
, currentCancelEpoch(currentCancelEpoch) {
}
const std::atomic<bool>* const isEternalCancel = nullptr;
const std::atomic<uint64_t>* const globalCancelEpoch = nullptr;
const uint64_t currentCancelEpoch = 0;
};
} // namespace
CurlHttpClient::CurlHttpClient() {
}
CurlHttpClient::~CurlHttpClient() {
std::lock_guard<std::mutex> lock(curlHandlesMutex);
for (auto& c : curlHandles) {
curl_easy_cleanup(c.second);
}
}
static CURL* getCurlHandle(const CurlHttpClient* c_) {
CurlHttpClient* c = const_cast<CurlHttpClient*>(c_);
std::lock_guard<std::mutex> lock(c->curlHandlesMutex);
auto id = std::this_thread::get_id();
auto it = c->curlHandles.find(id);
if (it == c->curlHandles.end()) {
CURL* curl = curl_easy_init();
if (!curl) {
throw std::runtime_error("curl_easy_init() failed");
}
c->curlHandles[id] = curl;
return curl;
}
return it->second;
}
static int curlProgressCallback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal,
curl_off_t ulnow) {
const RequestCancelState* const state = static_cast<const RequestCancelState* const>(clientp);
if (state
&& ((state->isEternalCancel && state->isEternalCancel->load())
|| (state->globalCancelEpoch && state->currentCancelEpoch < state->globalCancelEpoch->load()))) {
return 1;
}
return 0;
}
static std::size_t curlWriteString(char* ptr, std::size_t size, std::size_t nmemb, void* userdata) {
static_cast<std::string*>(userdata)->append(ptr, size * nmemb);
return size * nmemb;
}
std::string CurlHttpClient::makeRequest(const std::string& url, std::span<const HttpFormField> fields) const {
CURL* curl = getCurlHandle(this);
curl_easy_reset(curl);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, _connectTimeout);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, _timeout);
curl_easy_setopt(curl, CURLOPT_PROXY, _proxyUrl);
const RequestCancelState state { &_isEternalCancel, &_cancelEpoch, _cancelEpoch.load() };
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curlProgressCallback);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &state);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_mime* mime;
curl_mimepart* part;
mime = curl_mime_init(curl);
if (!fields.empty()) {
for (const HttpFormField& field : fields) {
part = curl_mime_addpart(mime);
curl_mime_name(part, field.name.c_str());
if (const auto* file = std::get_if<HttpFile>(&field.value)) {
curl_mime_data(part, file->data.c_str(), file->data.size());
curl_mime_type(part, file->mimeType.c_str());
curl_mime_filename(part, file->fileName.c_str());
} else {
const std::string& value = std::get<std::string>(field.value);
curl_mime_data(part, value.c_str(), value.size());
curl_mime_type(part, "text/plain");
}
}
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
}
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteString);
char errbuf[CURL_ERROR_SIZE] { };
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
auto res = curl_easy_perform(curl);
curl_mime_free(mime);
if (res == CURLcode::CURLE_ABORTED_BY_CALLBACK
&& ((state.isEternalCancel && state.isEternalCancel->load())
|| (state.globalCancelEpoch && state.currentCancelEpoch < state.globalCancelEpoch->load()))) {
const size_t slashPos = url.rfind('/');
if (slashPos == std::string::npos) {
throw RequestCancelled();
}
throw RequestCancelled(url.substr(slashPos + 1));
}
// If the request did not complete correctly, show the error
// information. If no detailed error information was written to errbuf
// show the more generic information from curl_easy_strerror instead.
if (res != CURLE_OK) {
size_t len = strlen(errbuf);
std::string errmsg;
if (len) {
errmsg = std::string(errbuf) + ((errbuf[len - 1] != '\n') ? "\n" : "");
} else {
errmsg = curl_easy_strerror(res);
}
throw std::runtime_error(std::string("curl error: ") + errmsg);
}
return response;
}
} // namespace TgBot