-
Notifications
You must be signed in to change notification settings - Fork 2
/
requests.hpp
617 lines (494 loc) · 17.2 KB
/
requests.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#pragma once
#include <bitset>
#include <map>
#include <regex>
#include <string>
#include <string_view>
#include <curl/curl.h>
#ifdef REQUESTS_WITH_NLOHMANN_JSON
#include "nlohmann.hpp"
#endif // REQUESTS_WITH_NLOHMANN_JSON
namespace requests {
// Requested URL.
struct url
{
/* Origin */
std::string scheme; // http or https (or empty)
std::string host; // Can't be empty, if port or scheme not empty
std::string port; // In range [0, 65535]
/* Resource */
std::string path; // With leading '/' (if not empty)
std::string query; // Without '?'
std::string fragment; // Without '#'
/* Create URL from string */
url(const char *str) noexcept : url(std::string_view{str}) {}
url(const std::string &str) noexcept : url(std::string_view{str}) {}
url(std::string_view url = "") noexcept
{
std::cmatch match;
// Starts with http(s) followed by :// in any case
std::regex rscheme("^(https?)://", std::regex_constants::icase);
// Find scheme
if (std::regex_search(url.data(), match, rscheme))
{
// Get scheme without ://
scheme = match.str(1);
// Convert scheme to lowercase
std::ranges::transform(scheme, scheme.begin(), [](char c){ return std::tolower(c); });
// Remove found
url.remove_prefix(match.str().size());
}
/* Regex helpers */
const std::string hostnumber {"(([0-9]{1,3}\\.){3}[0-9]{1,3})"};
const std::string toplabel {"([a-z][-a-z0-9]*[a-z0-9])"};
const std::string domainlabel {"([a-z0-9][-a-z0-9]*[a-z0-9])"};
const std::string hostname {"((" + domainlabel + "\\.)*" + toplabel + ")"};
// Starts with valid host
std::regex rhost(
"^(" + hostname + "|" + hostnumber + ")",
std::regex_constants::icase | std::regex_constants::nosubs
);
// Find host
if (std::regex_search(url.data(), match, rhost))
{
// Get host
host = match.str();
// Convert host to lowercase
std::ranges::transform(host, host.begin(), [](char c){ return std::tolower(c); });
// Remove found
url.remove_prefix(match.str().size());
}
// Starts with optional port
std::regex rport("^(:([0-9]{1,5}))?");
// Find port
std::regex_search(url.data(), match, rport);
if (!match.str().empty())
{
// Get specified port
port = match.str(2);
// Remove found
url.remove_prefix(match.str().size());
}
/* Regex helpers */
const std::string escape {"(%[0-9a-f]{2})"};
const std::string hsegment {"(([-$_.+;:@&=!*'(),a-z0-9]|" + escape + ")*)"};
// Starts with optional path
std::regex rpath(
"^(/" + hsegment + ")*",
std::regex_constants::icase | std::regex_constants::nosubs
);
// Find path
std::regex_search(url.data(), match, rpath);
// Get specified path or ""
path = match.str();
// Remove found
url.remove_prefix(match.str().size());
// Starts with optional query
std::regex rquery("^(?:\\?" + hsegment + ")?", std::regex_constants::icase);
// Find query
std::regex_search(url.data(), match, rquery);
// Get specified query or ""
query = match.str(1);
// Remove found
url.remove_prefix(match.str().size());
// Starts with optional query
std::regex rfragment("^(?:#" + hsegment + ")?", std::regex_constants::icase);
// Find query
std::regex_search(url.data(), match, rfragment);
// Get specified query or ""
fragment = match.str(1);
// Remove found
url.remove_prefix(match.str().size());
}
// [<scheme>://]<host>[:<port>]
std::string origin() const noexcept
{
if (host.empty()) { return ""; }
std::string host_port = host;
if (!port.empty()) { host_port += ":" + port; }
if (scheme.empty()) { return host_port; }
return scheme + "://" + host_port;
}
// <path>[?<query>][#<fragment>]
std::string resource() const noexcept
{
std::string res = path;
if (path.empty() && (!query.empty() || !fragment.empty())) { res += "/"; }
if (!query.empty()) { res += "?" + query; }
if (!fragment.empty()) { res += "#" + fragment; }
return res;
}
// Return URL string
std::string to_string() const noexcept { return origin() + resource(); }
};
// URL query part (?)
struct query : std::map<std::string, std::string>
{
using std::map<std::string, std::string>::map;
};
// URL fragment part (#)
struct fragment : std::string
{
using std::string::string;
};
std::string urlencoded(const std::map<std::string, std::string> &kv)
{
std::string res;
for (const auto &[k, v] : kv)
{
res += k + "=" + v + "&";
}
if (!res.empty()) { res.pop_back(); }
return res;
}
std::map<std::string, std::string> urldecoded(std::string_view str)
{
std::map<std::string, std::string> res;
std::string k, v;
bool processing_k = true;
for (const auto &c : str)
{
if (c == '=') { processing_k = false; continue; }
if (c == '&') { res[k] = v; k = v = ""; processing_k = true; continue; }
if (processing_k) { k.push_back(c); }
else { v.push_back(c); }
}
if (!k.empty()) { res[k] = v; }
return res;
}
// Basic authorization
struct auth
{
std::string username;
std::string password;
std::string to_base64() const noexcept
{
constexpr std::string_view base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string msg = username + ":" + password;
std::string_view str = msg;
std::string res; res.reserve(str.size() * 1.33);
while (str.size() >= 3)
{
std::string bits; bits.reserve(24);
for (size_t i = 0; i < 3; ++i)
{
bits += std::bitset<8>{static_cast<unsigned long long>(str[i])}.to_string();
}
for (size_t i = 0; i < 4; ++i)
{
res += base64_chars[std::bitset<6>(bits, i*6).to_ulong()];
}
str.remove_prefix(3);
}
if (str.size() == 2)
{
std::string bits; bits.reserve(18);
for (size_t i = 0; i < 2; ++i)
{
bits += std::bitset<8>{static_cast<unsigned long long>(str[i])}.to_string();
}
bits += "00";
for (size_t i = 0; i < 3; ++i)
{
res += base64_chars[std::bitset<6>(bits, i*6).to_ulong()];
}
res += "=";
}
else if (str.size() == 1)
{
std::string bits {std::bitset<8>{static_cast<unsigned long long>(str[0])}.to_string() + "0000"};
for (size_t i = 0; i < 2; ++i)
{
res += base64_chars[std::bitset<6>(bits, i*6).to_ulong()];
}
res += "==";
}
return res;
}
};
// Bearer token authorization
struct bearer
{
std::string token;
};
// Single HTTP-header
struct header
{
std::string name;
std::string value;
std::string to_string() const { return name + ": " + value; }
static header parse(std::string_view str)
{
header res;
std::cmatch match;
std::regex r("(.*): (.*)");
if (std::regex_search(str.data(), match, r))
{
res.name = match.str(1);
res.value = match.str(2);
}
return res;
}
};
namespace comparators {
struct case_insensitive_less
{
constexpr bool operator()(std::string_view lhs, std::string_view rhs) const noexcept
{
if (lhs.size() < rhs.size()) { return true; }
if (lhs.size() > rhs.size()) { return false; }
for (size_t i = 0; i < lhs.size(); ++i)
{
if (std::tolower(lhs[i]) < std::tolower(rhs[i])) { return true; }
if (std::tolower(lhs[i]) > std::tolower(rhs[i])) { return false; }
}
return false;
}
};
} // namespace Comparators
// Multiple HTTP-headers
struct headers : std::map<std::string, std::string, comparators::case_insensitive_less>
{
using std::map<std::string, std::string, comparators::case_insensitive_less>::map;
void insert(const header &h) { map::insert({h.name, h.value}); }
};
// URL-encoded data
struct data : std::map<std::string, std::string>
{
using std::map<std::string, std::string>::map;
};
#ifdef REQUESTS_WITH_NLOHMANN_JSON
// JSON data
using json = nlohmann::json;
#else
// JSON data
struct json : std::string
{
using std::string::string;
};
#endif // REQUESTS_WITH_NLOHMANN_JSON
// Plain text
struct text : std::string
{
using std::string::string;
};
namespace concepts {
// One of Request's options
template<typename T>
concept option = std::same_as<T, auth> ||
std::same_as<T, data> ||
std::same_as<T, fragment> ||
std::same_as<T, header> ||
std::same_as<T, headers> ||
std::same_as<T, json> ||
std::same_as<T, query> ||
std::same_as<T, text>;
} // namespace concepts
// Request's method
enum class method {
DELETE,
GET,
HEAD,
POST,
PUT,
OPTIONS,
PATCH,
};
struct request
{
requests::method method;
requests::url target;
requests::headers headers;
std::string body;
/* Helper setters */
template<concepts::option T, concepts::option ...Ts>
void set(const T &t, const Ts & ...ts) noexcept { set(t); set(ts...); }
void set() noexcept {}
void set(const requests::headers &hs) noexcept { headers = hs; }
void set(const query &q) noexcept { target.query = urlencoded(q); }
void set(const fragment &f) noexcept { target.fragment = f; }
void set(const auth &a) noexcept { headers["authorization"] = "Basic " + a.to_base64(); }
void set(const bearer &b) noexcept { headers["authorization"] = "Bearer " + b.token; }
void set(const header &h) noexcept { headers[h.name] = h.value; }
void set(const text &t) noexcept { body = t; headers["content-type"] = "text/plain"; }
void set(const data &d) noexcept
{
body = urlencoded(d);
headers["content-type"] = "application/x-www-form-urlencoded";
}
void set(const json &j) noexcept
{
#ifdef REQUESTS_WITH_NLOHMANN_JSON
body = nlohmann::to_string(j);
#else
body = j;
#endif // REQUESTS_WITH_NLOHMANN_JSON
headers["content-type"] = "application/json";
}
};
struct response
{
unsigned status_code = 0;
std::string reason;
requests::headers headers;
std::string text;
#ifdef REQUESTS_WITH_NLOHMANN_JSON
requests::json json() const { return nlohmann::json::parse(text); }
#endif // REQUESTS_WITH_NLOHMANN_JSON
};
namespace detail {
size_t write_callback(char *buffer, size_t size, size_t nitems, void *r)
{
static_cast<response *>(r)->text.append(static_cast<char*>(buffer), size * nitems);
return size * nitems;
}
size_t header_callback(char *buffer, size_t size, size_t nitems, void *r)
{
std::string_view str(buffer, size * nitems - 2);
if (str.starts_with("HTTP"))
{
static_cast<response *>(r)->reason = str.substr(13);
}
else if (!str.empty())
{
static_cast<response *>(r)->headers.insert(header::parse(str));
}
return size * nitems;
}
class curl_holder
{
public:
static curl_holder & get()
{
static curl_holder ch;
return ch;
}
curl_holder(const curl_holder &) = delete;
void operator=(const curl_holder &) = delete;
CURL * handler() const { return handler_; }
~curl_holder()
{
curl_easy_cleanup(handler_);
curl_global_cleanup();
}
private:
curl_holder()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
handler_ = curl_easy_init();
auto info = curl_version_info(CURLVERSION_NOW);
std::string version = "curl/" + std::string{info->version};
curl_easy_setopt(handler_, CURLOPT_USERAGENT, version.c_str());
curl_easy_setopt(handler_, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(handler_, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handler_, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(handler_, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(handler_, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(handler_, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
}
CURL *handler_ = nullptr;
};
} // namespace detail
// Single connection session
struct session
{
url origin; // scheme, host and port
headers common_headers = {}; // Added to each request
response send(request r)
{
response res;
/* Update info in the request */
for (const auto &[h, v] : common_headers) { r.headers[h] = v; }
r.headers["host"] = origin.host;
CURL *curl = detail::curl_holder::get().handler();
switch (r.method)
{
case method::DELETE: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); break;
case method::GET: curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); break;
case method::HEAD: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD"); break;
case method::POST:
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, r.body.c_str());
break;
case method::PUT: curl_easy_setopt(curl, CURLOPT_PUT, 1L); break;
case method::OPTIONS: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS"); break;
case method::PATCH: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); break;
}
std::string url = origin.origin() + r.target.resource();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_slist *curl_headers = nullptr;
for (const auto &[h, v] : r.headers)
{
curl_headers = curl_slist_append(curl_headers, (h + ": " + v).c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &res);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
curl_easy_perform(curl);
curl_slist_free_all(curl_headers);
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
res.status_code = static_cast<unsigned>(response_code);
return res;
}
template<concepts::option ...Args>
response delet(const url &target, const Args & ...args)
{
return send(construct_request(method::DELETE, target, args...));
}
template<concepts::option ...Args>
response get(const url &target, const Args & ...args)
{
return send(construct_request(method::GET, target, args...));
}
template<concepts::option ...Args>
response head(const url &target, const Args & ...args)
{
return send(construct_request(method::HEAD, target, args...));
}
template<concepts::option ...Args>
response options(const url &target, const Args & ...args)
{
return send(construct_request(method::OPTIONS, target, args...));
}
template<concepts::option ...Args>
response patch(const url &target, const Args & ...args)
{
return send(construct_request(method::PATCH, target, args...));
}
template<concepts::option ...Args>
response post(const url &target, const Args & ...args)
{
return send(construct_request(method::POST, target, args...));
}
template<concepts::option ...Args>
response put(const url &target, const Args & ...args)
{
return send(construct_request(method::PUT, target, args...));
}
private:
template<concepts::option ...Args>
request construct_request(method m, const url &target, const Args & ...args) const
{
request r{ m, target, {{"user-agent", "requests"}}, "" };
r.set(args...);
return r;
}
};
template<concepts::option ...Args>
response delet(const url &url, const Args &...args) { return session{url}.delet(url, args...); }
template<concepts::option ...Args>
response get(const url &url, const Args &...args) { return session{url}.get(url, args...); }
template<concepts::option ...Args>
response head(const url &url, const Args &...args) { return session{url}.head(url, args...); }
template<concepts::option ...Args>
response options(const url &url, const Args &...args) { return session{url}.options(url, args...); }
template<concepts::option ...Args>
response patch(const url &url, const Args &...args) { return session{url}.patch(url, args...); }
template<concepts::option ...Args>
response post(const url &url, const Args &...args) { return session{url}.post(url, args...); }
template<concepts::option ...Args>
response put(const url &url, const Args & ...args) { return session{url}.put(url, args...); }
} // namespace requests