-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_http.hpp
730 lines (588 loc) · 23.9 KB
/
simple_http.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#pragma once
#include <algorithm>
#include <curl/curl.h>
#include <functional>
#include <numeric>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
namespace SimpleHttp {
template <class... As> struct visitor : As... {
using As::operator()...;
};
template <class... As> visitor(As...) -> visitor<As...>;
template <typename Name, typename A> struct Tiny {
Tiny() noexcept(std::is_nothrow_constructible_v<A>) = default;
explicit Tiny(const A &value) noexcept(
std::is_nothrow_copy_constructible_v<A>)
: value_(value) {}
explicit Tiny(A &&value) noexcept(std::is_nothrow_move_constructible_v<A>)
: value_(std::move(value)) {}
friend std::ostream &operator<<(std::ostream &os, const Tiny &object) {
return os << object.value();
}
friend bool operator==(const Tiny &lhs, const Tiny &rhs) {
return lhs.value() == rhs.value();
}
friend bool operator<(const Tiny &lhs, const Tiny &rhs) {
return lhs.value() < rhs.value();
}
friend bool operator<=(const Tiny &lhs, const Tiny &rhs) {
return lhs.value() <= rhs.value();
}
friend bool operator>(const Tiny &lhs, const Tiny &rhs) {
return lhs.value() > rhs.value();
}
friend bool operator>=(const Tiny &lhs, const Tiny &rhs) {
return lhs.value() >= rhs.value();
}
explicit operator A &() const noexcept { return value(); }
[[nodiscard]] const A &value() const noexcept { return value_; }
[[nodiscard]] std::string to_string() const {
std::ostringstream ss;
ss << value();
return ss.str();
}
protected:
A value_;
};
#define SIMPLE_HTTP_TINY_STRING(Name) \
struct Name##Detail {}; \
using Name = Tiny<Name##Detail, std::string>;
#define SIMPLE_HTTP_TINY_int64_t(Name) \
struct Name##Detail {}; \
using Name = Tiny<Name##Detail, int64_t>;
SIMPLE_HTTP_TINY_STRING(HttpConnectionFailure)
SIMPLE_HTTP_TINY_STRING(HttpResponseBody)
SIMPLE_HTTP_TINY_STRING(HttpRequestBody)
SIMPLE_HTTP_TINY_STRING(Protcol)
SIMPLE_HTTP_TINY_STRING(Host)
SIMPLE_HTTP_TINY_STRING(PathSegment)
SIMPLE_HTTP_TINY_STRING(QueryParameterKey)
SIMPLE_HTTP_TINY_STRING(QueryParameterValue)
SIMPLE_HTTP_TINY_int64_t(HttpStatusCode)
#undef SIMPLE_HTTP_TINY_STRING
#undef SIMPLE_HTTP_TINY_int64_t
using CurlSetupCallback = std::function<void(CURL *curl)>;
inline static CurlSetupCallback NoopCurlSetupCallback = [](auto) {};
using CurlHeaderCallback = std::function<curl_slist *(curl_slist *chunk)>;
inline static CurlHeaderCallback NoopCurlHeaderCallback =
[](curl_slist *chunk) { return chunk; };
using Headers = std::unordered_map<std::string, std::string>;
inline static const std::string WHITESPACE = "\n\t\f\v\r ";
inline static std::string left_trim(const std::string &candidate) {
size_t start = candidate.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : candidate.substr(start);
}
inline static std::string right_trim(const std::string &candidate) {
size_t end = candidate.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : candidate.substr(0, end + 1);
}
inline static std::string trim(const std::string &candidate) {
return right_trim(left_trim(candidate));
}
inline static std::vector<std::string> vec(const std::string &candidate,
const char separator) {
std::vector<std::string> container;
std::stringstream ss(candidate);
std::string temp;
while (std::getline(ss, temp, separator)) {
container.push_back(trim(temp));
}
return container;
}
struct QueryParameters final {
QueryParameters() = default;
explicit QueryParameters(
std::vector<std::pair<QueryParameterKey, QueryParameterValue>> values)
: values_(std::move(values)) {}
[[nodiscard]] const std::vector<
std::pair<QueryParameterKey, QueryParameterValue>> &
values() const {
return values_;
}
[[nodiscard]] std::string to_string() const {
std::stringstream ss;
if (!values_.empty()) {
ss << "?" << values_[0].first << "=" << values_[0].second;
for (uint64_t i = 1; i < values_.size(); ++i) {
ss << "&" << values_[i].first << "=" << values_[i].second;
}
}
return ss.str();
}
private:
std::vector<std::pair<QueryParameterKey, QueryParameterValue>> values_;
};
struct HttpResponseHeaders final {
explicit HttpResponseHeaders(Headers headers)
: headers_(std::move(headers)) {}
explicit HttpResponseHeaders(const std::string &header_string)
: headers_(parse(header_string)) {}
bool operator==(const HttpResponseHeaders &rhs) const {
return headers_ == rhs.headers_;
}
bool operator!=(const HttpResponseHeaders &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os,
const HttpResponseHeaders &headers) {
const std::basic_string<char> &string = std::accumulate(
headers.value().cbegin(), headers.value().cend(), std::string(),
[](const std::string &s,
const std::pair<const std::string, std::string> &v) {
return s + (s.empty() ? std::string() : ",") + v.first + " : " +
v.second;
});
os << "[" << string << "]";
return os;
}
const Headers &value() const { return headers_; }
private:
Headers headers_;
static Headers parse(const std::string &header_string) {
std::stringstream ss(header_string);
std::string container;
Headers headers;
while (std::getline(ss, container, '\n')) {
std::size_t pos = container.find(':');
if (pos != std::string::npos) {
headers.emplace(trim(container.substr(0, pos)),
trim(container.substr(pos + 1)));
}
}
return headers;
}
};
struct HttpResponse final {
HttpStatusCode status;
HttpResponseHeaders headers;
HttpResponseBody body;
bool operator==(const HttpResponse &rhs) const {
return status == rhs.status && headers == rhs.headers && body == rhs.body;
}
bool operator!=(const HttpResponse &rhs) const { return !(rhs == *this); }
friend std::ostream &operator<<(std::ostream &os,
const HttpResponse &response) {
os << "status: " << response.status << " headers: " << response.headers
<< " body: " << response.body;
return os;
}
};
struct HttpSuccess final {
explicit HttpSuccess(HttpResponse response) : value_(std::move(response)) {}
bool operator==(const HttpSuccess &rhs) const { return value_ == rhs.value_; }
bool operator!=(const HttpSuccess &rhs) const { return !(rhs == *this); }
friend std::ostream &operator<<(std::ostream &os,
const HttpSuccess &success) {
os << success.value_;
return os;
}
[[nodiscard]] const HttpResponse &value() const { return value_; }
[[nodiscard]] const HttpStatusCode &status() const { return value_.status; }
[[nodiscard]] const HttpResponseBody &body() const { return value_.body; }
[[nodiscard]] const HttpResponseHeaders &headers() const {
return value_.headers;
}
private:
HttpResponse value_;
};
struct HttpFailure final {
explicit HttpFailure(HttpConnectionFailure value)
: value_(std::move(value)) {}
explicit HttpFailure(HttpResponse value) : value_(std::move(value)) {}
bool operator==(const HttpFailure &rhs) const { return value_ == rhs.value_; }
bool operator!=(const HttpFailure &rhs) const { return !(rhs == *this); }
friend std::ostream &operator<<(std::ostream &os,
const HttpFailure &failure) {
failure.template match<void>(
[&os](const HttpConnectionFailure &f) { os << f; },
[&os](const HttpResponse &s) { os << s; });
return os;
}
[[nodiscard]] const std::variant<HttpConnectionFailure, HttpResponse> &
value() const {
return value_;
}
template <class A>
A match(
const std::function<A(const HttpConnectionFailure &connectionFailure)>
&cFn,
const std::function<A(const HttpResponse &semanticFailure)> &sFn) const {
return std::visit(
visitor{[&cFn](const HttpConnectionFailure &c) { return cFn(c); },
[&sFn](const HttpResponse &s) { return sFn(s); }},
value_);
}
private:
std::variant<HttpConnectionFailure, HttpResponse> value_;
};
struct HttpResult final {
explicit HttpResult(std::variant<HttpFailure, HttpSuccess> value)
: value_(std::move(value)) {}
bool operator==(const HttpResult &rhs) const { return value_ == rhs.value_; }
bool operator!=(const HttpResult &rhs) const { return !(rhs == *this); }
[[nodiscard]] const std::variant<HttpFailure, HttpSuccess> &value() const {
return value_;
}
[[nodiscard]] std::optional<HttpFailure> failure() const {
return std::holds_alternative<HttpFailure>(value_)
? std::get<HttpFailure>(value_)
: std::optional<HttpFailure>{};
}
[[nodiscard]] std::optional<HttpSuccess> success() const {
return std::holds_alternative<HttpSuccess>(value_)
? std::get<HttpSuccess>(value_)
: std::optional<HttpSuccess>{};
}
template <class A>
[[nodiscard]] A
match(const std::function<A(const HttpFailure &)> failureFn,
const std::function<A(const HttpSuccess &)> successFn) const {
return std::visit(visitor{[&failureFn](const HttpFailure &failure) {
return failureFn(failure);
},
[&successFn](const HttpSuccess &success) {
return successFn(success);
}},
value_);
}
private:
std::variant<HttpFailure, HttpSuccess> value_;
};
struct PathSegments final {
PathSegments() = default;
PathSegments(PathSegments &path_segments) : value_(path_segments.value_) {}
PathSegments(PathSegments &&path_segments) noexcept
: value_(std::move(path_segments.value_)) {}
explicit PathSegments(std::vector<PathSegment> value)
: value_(std::move(value)) {}
PathSegments &operator=(const PathSegments &other) {
this->value_ = other.value_;
return *this;
}
[[nodiscard]] const std::vector<PathSegment> &value() const { return value_; }
[[nodiscard]] std::string to_string() const {
std::stringstream ss;
for (const auto &segment : value_) {
ss << "/" << segment;
}
return ss.str();
}
private:
std::vector<PathSegment> value_;
};
struct HttpUrl final {
HttpUrl() = default;
HttpUrl(Protcol protocol, Host host, PathSegments path_segments = {},
QueryParameters query_parameters = {})
: protocol_(std::move(protocol)), host_(std::move(host)),
path_segments_(std::move(path_segments)),
query_parameters_(std::move(query_parameters)), value_(to_string()) {}
explicit HttpUrl(std::string value)
: protocol_(detect_protocol(value)), value_(std::move(value)) {}
[[nodiscard]] const Protcol &protocol() const { return protocol_; }
[[nodiscard]] std::string value() const {
return value_.empty() ? to_string() : value_;
}
[[nodiscard]] HttpUrl &with_protocol(Protcol protocol) {
protocol_ = std::move(protocol);
return *this;
}
[[nodiscard]] HttpUrl &with_host(Host host) {
host_ = std::move(host);
return *this;
}
[[nodiscard]] HttpUrl &with_path_segments(const PathSegments &path_segments) {
path_segments_ = path_segments;
return *this;
}
[[nodiscard]] HttpUrl &
with_query_parameters(QueryParameters query_parameters) {
query_parameters_ = std::move(query_parameters);
return *this;
}
[[nodiscard]] const PathSegments &path_segments() const {
return path_segments_;
}
private:
Protcol protocol_;
Host host_;
PathSegments path_segments_;
QueryParameters query_parameters_;
std::string value_;
static std::string detect_protocol(const std::string &url_string) {
uint64_t i = url_string.find(':');
if (i != std::string::npos) {
return url_string.substr(0, i);
} else {
return "unknown";
}
}
[[nodiscard]] std::string to_string() const {
std::stringstream ss;
ss << protocol_ << "://" << host_ << path_segments_.to_string()
<< query_parameters_.to_string();
return trim(ss.str());
}
};
template <class A> using Predicate = std::function<bool(const A &a)>;
template <class A> Predicate<A> eq(const A &a) {
return [a](const A &other) { return a == other; };
}
template <class A> Predicate<A> between_inclusive(const A &a, const A &b) {
return [a, b](const A &other) { return other >= a && other <= b; };
}
template <class A> Predicate<A> logical_or(const A &a, const A &b) {
return [a, b](const A &other) { return other == a || other == b; };
}
// Unknown
inline static HttpStatusCode UNKNOWN = HttpStatusCode{0};
// Information Responses
inline static HttpStatusCode CONTINUE = HttpStatusCode{100};
inline static HttpStatusCode SWITCHING_PROTOCOL = HttpStatusCode{101};
inline static HttpStatusCode PROCESSING = HttpStatusCode{102};
inline static HttpStatusCode EARLY_HINTS = HttpStatusCode{103};
// Successful Responses
inline static HttpStatusCode OK = HttpStatusCode{200};
inline static HttpStatusCode CREATED = HttpStatusCode{201};
inline static HttpStatusCode ACCEPTED = HttpStatusCode{202};
inline static HttpStatusCode NON_AUTHORITATIVE_INFORMATION =
HttpStatusCode{203};
inline static HttpStatusCode NO_CONTENT = HttpStatusCode{204};
inline static HttpStatusCode RESET_CONTENT = HttpStatusCode{205};
inline static HttpStatusCode PARTIAL_CONTENT = HttpStatusCode{206};
inline static HttpStatusCode MULTI_STATUS = HttpStatusCode{207};
inline static HttpStatusCode ALREADY_REPORTED = HttpStatusCode{208};
inline static HttpStatusCode IM_USED = HttpStatusCode{226};
// Redirection Messages
inline static HttpStatusCode MULTIPLE_CHOICE = HttpStatusCode{300};
inline static HttpStatusCode MOVED_PERMANENTLY = HttpStatusCode{301};
inline static HttpStatusCode FOUND = HttpStatusCode{302};
inline static HttpStatusCode SEE_OTHER = HttpStatusCode{303};
inline static HttpStatusCode NOT_MODIFIED = HttpStatusCode{304};
inline static HttpStatusCode USE_PROXY = HttpStatusCode{305};
inline static HttpStatusCode TEMPORARY_REDIRECT = HttpStatusCode{307};
inline static HttpStatusCode PERMANENT_REDIRECT = HttpStatusCode{308};
// Client Error Responses
inline static HttpStatusCode BAD_REQUEST = HttpStatusCode{400};
inline static HttpStatusCode UNAUTHORIZED = HttpStatusCode{401};
inline static HttpStatusCode PAYMENT_REQUIRED = HttpStatusCode{402};
inline static HttpStatusCode FORBIDDEN = HttpStatusCode{403};
inline static HttpStatusCode NOT_FOUND = HttpStatusCode{404};
inline static HttpStatusCode METHOD_NOT_ALLOWED = HttpStatusCode{405};
inline static HttpStatusCode NOT_ACCEPTABLE = HttpStatusCode{406};
inline static HttpStatusCode PROXY_AUTHENTICATION_REQUIRED =
HttpStatusCode{407};
inline static HttpStatusCode REQUEST_TIMEOUT = HttpStatusCode{408};
inline static HttpStatusCode CONFLICT = HttpStatusCode{409};
inline static HttpStatusCode GONE = HttpStatusCode{410};
inline static HttpStatusCode LENGTH_REQUIRED = HttpStatusCode{411};
inline static HttpStatusCode PRECONDITION_FAILED = HttpStatusCode{412};
inline static HttpStatusCode PAYLOAD_TOO_LARGE = HttpStatusCode{413};
inline static HttpStatusCode URI_TOO_int64_t = HttpStatusCode{414};
inline static HttpStatusCode UNSUPPORTED_MEDIA_TYPE = HttpStatusCode{415};
inline static HttpStatusCode RANGE_NOT_SATISFIABLE = HttpStatusCode{416};
inline static HttpStatusCode EXPECTATION_FAILED = HttpStatusCode{417};
inline static HttpStatusCode IM_A_TEAPOT = HttpStatusCode{418};
inline static HttpStatusCode UNPROCESSABLE_ENTITY = HttpStatusCode{422};
inline static HttpStatusCode FAILED_DEPENDENCY = HttpStatusCode{424};
inline static HttpStatusCode TOO_EARLY = HttpStatusCode{425};
inline static HttpStatusCode UPGRADE_REQUIRED = HttpStatusCode{426};
inline static HttpStatusCode PRECONDITION_REQUIRED = HttpStatusCode{428};
inline static HttpStatusCode TOO_MANY_REQUESTS = HttpStatusCode{429};
inline static HttpStatusCode REQUEST_HEADER_FIELDS_TOO_LARGE =
HttpStatusCode{431};
inline static HttpStatusCode UNAVAILABLE_FOR_LEGAL_REASONS =
HttpStatusCode{451};
// Server Error Responses
inline static HttpStatusCode INTERNAL_SERVER_ERROR = HttpStatusCode{500};
inline static HttpStatusCode NOT_IMPLEMENTED = HttpStatusCode{501};
inline static HttpStatusCode BAD_GATEWAY = HttpStatusCode{502};
inline static HttpStatusCode SERVICE_UNAVAILABLE = HttpStatusCode{503};
inline static HttpStatusCode GATEWAY_TIMEOUT = HttpStatusCode{504};
inline static HttpStatusCode HTTP_VERSION_NOT_SUPPORTED = HttpStatusCode{505};
inline static HttpStatusCode VARIANT_ALSO_NEGOTIATES = HttpStatusCode{506};
inline static HttpStatusCode INSUFFICIENT_STORAGE = HttpStatusCode{507};
inline static HttpStatusCode LOOP_DETECTED = HttpStatusCode{508};
inline static HttpStatusCode NOT_EXTENDED = HttpStatusCode{510};
inline static HttpStatusCode NETWORK_AUTHENTICATION_REQUIRED =
HttpStatusCode{511};
inline static Predicate<HttpStatusCode> informational() {
return between_inclusive(CONTINUE, EARLY_HINTS);
}
inline static Predicate<HttpStatusCode> successful() {
return between_inclusive(OK, IM_USED);
}
inline static Predicate<HttpStatusCode> redirect() {
return between_inclusive(MULTIPLE_CHOICE, PERMANENT_REDIRECT);
}
inline static Predicate<HttpStatusCode> client_error() {
return between_inclusive(BAD_REQUEST, UNAVAILABLE_FOR_LEGAL_REASONS);
}
inline static Predicate<HttpStatusCode> server_error() {
return between_inclusive(INTERNAL_SERVER_ERROR,
NETWORK_AUTHENTICATION_REQUIRED);
}
struct Client final {
Client() : debug_(false), verify_(true) {}
Client &with_tls_verification(bool verify) {
verify_ = verify;
return *this;
}
Client &with_debug(bool debug) {
debug_ = debug;
return *this;
}
[[nodiscard]] HttpResult get(const HttpUrl &url,
const Headers &headers = {}) const {
return get(url, eq(OK), headers);
}
[[nodiscard]] HttpResult
get(const HttpUrl &url, const Predicate<HttpStatusCode> &successPredicate,
const Headers &headers = {}) const {
return execute(url, make_header_callback(headers), NoopCurlSetupCallback,
successPredicate);
}
[[nodiscard]] HttpResult post(const HttpUrl &url, const HttpRequestBody &body,
const Headers &headers = {}) const {
return post(url, body, eq(OK), headers);
}
[[nodiscard]] HttpResult
post(const HttpUrl &url, const HttpRequestBody &body,
const Predicate<HttpStatusCode> &successPredicate,
const Headers &headers = {}) const {
CurlSetupCallback setup = [&](CURL *curl) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.value().c_str());
};
return execute(url, make_header_callback(headers), setup, successPredicate);
}
[[nodiscard]] HttpResult put(const HttpUrl &url, const HttpRequestBody &body,
const Headers &headers = {}) const {
return put(url, body, eq(OK), headers);
}
[[nodiscard]] HttpResult
put(const HttpUrl &url, const HttpRequestBody &body,
const Predicate<HttpStatusCode> &successPredicate,
const Headers &headers = {}) const {
CurlSetupCallback setup = [&](CURL *curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.value().c_str());
};
return execute(url, make_header_callback(headers), setup, successPredicate);
}
[[nodiscard]] HttpResult del(const HttpUrl &url,
const Headers &headers = {}) const {
return del(url, eq(OK), headers);
}
[[nodiscard]] HttpResult
del(const HttpUrl &url, const Predicate<HttpStatusCode> &successPredicate,
const Headers &headers = {}) const {
CurlSetupCallback setup = [&](CURL *curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
};
return execute(url, make_header_callback(headers), setup, successPredicate);
}
[[nodiscard]] HttpResult head(const HttpUrl &url) const {
CurlSetupCallback setup = [&](CURL *curl) {
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
};
return execute(url, NoopCurlHeaderCallback, setup, eq(OK));
}
[[nodiscard]] HttpResult options(const HttpUrl &url) const {
CurlSetupCallback setup = [&](CURL *curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
};
return execute(url, NoopCurlHeaderCallback, setup, eq(OK));
}
[[nodiscard]] HttpResult trace(const HttpUrl &url) const {
CurlSetupCallback setup = [&](CURL *curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
};
return execute(url, NoopCurlHeaderCallback, setup, eq(OK));
}
[[nodiscard]] HttpResult
execute(const HttpUrl &url, const CurlHeaderCallback &curl_header_callback,
const CurlSetupCallback &curl_setup_callback,
const Predicate<HttpStatusCode> &successPredicate) const {
CurlWrapper curlWrapper{successPredicate};
curlWrapper.execute_header_callback(curl_header_callback);
curlWrapper.add_option(CURLOPT_URL, url.value().c_str());
curlWrapper.add_option(CURLOPT_VERBOSE, debug_ ? 1L : 0L);
if (url.protocol().value() == "https") {
verify_ ? curlWrapper.add_option(CURLOPT_SSL_VERIFYPEER, 1L)
: curlWrapper.add_option(CURLOPT_SSL_VERIFYPEER, 0L);
}
curlWrapper.execute_setup_callback(curl_setup_callback);
return curlWrapper.execute();
}
private:
bool debug_;
bool verify_;
static size_t write_callback(void *contents, size_t size, size_t nmemb,
void *userp) {
((std::string *)userp)->append((char *)contents, size * nmemb); // NOLINT
return size * nmemb;
}
static CurlHeaderCallback make_header_callback(const Headers &headers) {
return headers.empty()
? NoopCurlHeaderCallback
: [&headers](curl_slist *chunk) {
for (const auto &[key, value] : headers) {
chunk =
curl_slist_append(chunk, (key + ": " + value).c_str());
}
return chunk;
};
}
struct CurlWrapper final {
explicit CurlWrapper(const Predicate<HttpStatusCode> &success_predicate)
: curl_(curl_easy_init()), slist_(nullptr),
success_predicate_(success_predicate) {}
~CurlWrapper() {
curl_easy_cleanup(curl_);
curl_slist_free_all(slist_);
}
template <class A> void add_option(const CURLoption option, A value) {
curl_easy_setopt(curl_, option, value);
}
void execute_header_callback(const CurlHeaderCallback &header_callback) {
slist_ = header_callback(slist_);
}
void execute_setup_callback(const CurlSetupCallback &setup_callback) {
setup_callback(curl_);
}
[[nodiscard]] HttpResult execute() {
std::string body_buffer;
std::string header_buffer;
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &body_buffer);
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, slist_);
curl_easy_setopt(curl_, CURLOPT_HEADERDATA, &header_buffer);
CURLcode res = curl_easy_perform(curl_);
if (res != CURLE_OK) {
return HttpResult{
HttpFailure{HttpConnectionFailure{curl_easy_strerror(res)}}};
}
int64_t status_code = 0;
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &status_code);
HttpStatusCode status{status_code};
HttpResponse httpResponse =
HttpResponse{status, HttpResponseHeaders{header_buffer},
HttpResponseBody{body_buffer}};
return success_predicate_(status) ? HttpResult{HttpSuccess{httpResponse}}
: HttpResult{HttpFailure{httpResponse}};
}
private:
CURL *curl_;
curl_slist *slist_;
Predicate<HttpStatusCode> success_predicate_;
};
};
} // namespace SimpleHttp