-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_lib.hpp
275 lines (217 loc) · 9.34 KB
/
http_lib.hpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef HTTP_LIB_HTTP_LIB_HPP
#define HTTP_LIB_HTTP_LIB_HPP
#include <iostream>
#include <vector>
#include <unordered_map>
#include <curl/curl.h>
#include <sstream>
#include <thread>
namespace muskrat {
using post_fields_t = const std::unordered_map<std::string, std::string>&;
namespace util {
template<typename T>
inline std::string to_str(T value) { return std::to_string(value); }
inline std::string to_str(const std::string& value) { return value; }
}
class c_http_response {
public:
c_http_response(std::string_view body, std::string_view headers) {
parse_headers(headers);
m_body = body;
}
[[nodiscard]] bool is_redirect() const {
return (m_http_status == 302 || m_http_status == 301 || m_http_status == 303 || m_http_status == 307
|| m_http_status == 308);
}
[[nodiscard]] bool is_successful() const { return m_successful; }
[[nodiscard]] bool is_failed() const { return !m_successful; }
std::string body() {
return m_body;
}
std::string get_header(const std::string& header) {
return m_headers[header];
}
std::unordered_map<std::string, std::string> get_headers() {
return m_headers;
}
private:
void parse_headers(std::string_view headers) {
std::istringstream hdr_stream(headers.data());
std::string line;
while (std::getline(hdr_stream, line)) {
if (line.empty())
continue;
if (line.length() > 5 && line.substr(0, 4) == "HTTP") {
m_headers.clear();
try {
m_http_status = std::stoi(line.substr(line.find(' ') + 1, 3));
} catch (...) {
m_successful = false;
return;
}
continue;
}
}
m_successful = !(m_http_status == 404 || m_http_status == 500 || m_http_status == 419
|| m_http_status == 429);
}
bool m_successful = false;
uint16_t m_http_status = 0;
std::string m_body{};
std::unordered_map<std::string, std::string> m_headers{};
};
class c_basic_request {
public:
inline std::shared_ptr<c_http_response> post(std::string_view url);
inline std::shared_ptr<c_http_response> get(std::string_view url);
inline std::shared_ptr<c_http_response> post(std::string_view url, post_fields_t params);
c_basic_request* retry(uint32_t attempts, uint64_t delay) {
m_retry_attempts = attempts;
m_retry_delay = delay;
return this;
}
c_basic_request* with_token(std::string_view token) {
m_headers["Authentication"] = std::string("Bearer ").append(token);
return this;
}
c_basic_request* with_basic_auth(std::string_view username, std::string_view password) {
m_basic_auth.used = true;
m_basic_auth.query = std::string(username).append(":").append(password);
return this;
}
c_basic_request* with_body(std::string_view body) {
m_body = body;
return this;
}
c_basic_request* with_proxy(std::string_view proxy) {
m_proxy_path = proxy;
return this;
}
c_basic_request* add_cookie(std::string_view key, std::string_view val) {
m_cookies.append(key).append("=").append(val).append("; ");
return this;
}
c_basic_request* set_raw_cookies(std::string_view raw) {
m_cookies = raw;
return this;
}
c_basic_request* timeout(uint64_t timeout) {
m_timeout = timeout;
return this;
}
template<typename T>
c_basic_request* with_header(std::string_view name, T value) {
static_assert(std::is_fundamental<T>::value || std::is_same<T, std::string>::value,
"Header value can only be of a basic type or an STL string");
m_headers[name.data()] = util::to_str(value);
return this;
}
c_basic_request* with_headers(const std::unordered_map<std::string, std::string>& headers) {
m_headers.insert(headers.begin(), headers.end());
return this;
}
c_basic_request* follow_redirects() {
m_follow_location = true;
return this;
}
protected:
std::unordered_map<std::string, std::string> m_headers{};
uint32_t m_retry_attempts = 0;
uint64_t m_retry_delay = 0;
uint64_t m_timeout = 0;
std::string m_proxy_path{};
std::string m_cookies{};
std::string m_body{};
bool m_follow_location = false;
struct {
bool used = false;
std::string query{};
} m_basic_auth;
};
static size_t write_callback(void* contents, size_t size, size_t num_blocks, void* result) {
((std::string*)result)->append((char*)contents, size * num_blocks);
return size * num_blocks;
}
class c_http_lib : public c_basic_request {
public:
std::shared_ptr<c_http_response> attempt(std::string_view url, bool post, post_fields_t fields = {}) {
m_retry_attempts = std::max(m_retry_attempts, (uint32_t)1);
while (m_retry_attempts--) {
auto response = request(url, post, fields);
if (response->is_successful())
return response;
std::this_thread::sleep_for(std::chrono::milliseconds(std::max(m_retry_delay, (uint64_t)3000)));
}
return std::make_shared<c_http_response>("", "");
}
private:
std::shared_ptr<c_http_response> request(std::string_view url, bool post, post_fields_t fields = {}) {
auto handle = curl_easy_init();
if (!handle)
return std::make_shared<c_http_response>("", "");
struct curl_slist* header_list = nullptr;
for (const auto& field : m_headers) {
header_list = curl_slist_append(header_list,
std::string(field.first).append(": ").append(field.second).c_str());
}
std::string body{};
std::string headers{};
std::string request_string{};
std::for_each(fields.begin(), fields.end(), [&request_string](const auto& field) {
request_string.append(field.first).append("=").append(field.second).append("&");
});
if (!request_string.empty())
request_string.pop_back();
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(handle, CURLOPT_URL, url.data());
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, static_cast<int>(m_follow_location));
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &body);
curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, write_callback);
curl_easy_setopt(handle, CURLOPT_HEADERDATA, &headers);
if (m_basic_auth.used) {
curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(handle, CURLOPT_USERPWD, m_basic_auth.query.c_str());
}
if (!m_proxy_path.empty()) {
curl_easy_setopt(handle, CURLOPT_PROXY, m_proxy_path.c_str());
}
if (!m_cookies.empty()) {
curl_easy_setopt(handle, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(handle, CURLOPT_COOKIE, m_cookies.c_str());
}
if (m_timeout > 100)
curl_easy_setopt(handle, CURLOPT_TIMEOUT_MS, m_timeout);
if (post) {
curl_easy_setopt(handle, CURLOPT_POST, 1);
if (request_string.empty()) {
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, m_body.c_str());
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, m_body.length());
} else {
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, request_string.c_str());
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, request_string.length());
}
}
else
curl_easy_setopt(handle, CURLOPT_HTTPGET, 1);
curl_easy_perform(handle);
curl_easy_cleanup(handle);
if (header_list)
curl_slist_free_all(header_list);
return std::make_shared<c_http_response>(body, headers);
}
};
inline std::shared_ptr<c_http_response> c_basic_request::post(std::string_view url) {
return ((c_http_lib*)this)->attempt(url, true);
}
inline std::shared_ptr<c_http_response> c_basic_request::get(std::string_view url) {
return ((c_http_lib*)this)->attempt(url, false);
}
inline std::shared_ptr<c_http_response> c_basic_request::post(std::string_view url, post_fields_t params) {
return ((c_http_lib*)this)->attempt(url, true, params);
}
static std::shared_ptr<c_http_lib> http() {
return std::make_shared<c_http_lib>();
}
}
#endif