-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpclient.h
More file actions
970 lines (786 loc) · 31.7 KB
/
Copy pathhttpclient.h
File metadata and controls
970 lines (786 loc) · 31.7 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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/****************************************************************************
*
* Copyright (c) 2023, Danilo Pucci
*
* This file is part of the HTTP Client header-only library.
*
* Source Code:
* https://github.com/danilopucci/httpclient/
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so,subject to the
* following conditions:
*
* + The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* + Credit is appreciated, but not required, if you find this project
* useful enough to include in your application, product, device, etc.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <thread>
#include <chrono>
#include <iostream>
#include <string>
#include <unordered_map>
#include <regex>
#include <boost/asio/strand.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/dynamic_body.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/algorithm/string.hpp>
namespace HttpClient {
class HttpConnection;
class HttpResponse;
using HttpResponse_ptr = std::shared_ptr<HttpResponse>;
using HttpResponse_cb = std::function<void(const HttpResponse_ptr&)>;
using HttpFailure_cb = std::function<void(const HttpResponse_ptr&)>;
class HttpUrl {
public:
HttpUrl(const std::string& url_)
: url(url_)
{
parseUrl(url);
};
bool isValid() {
return valid;
}
bool isProtocolSecure() const {
return secure;
}
std::string url;
std::string target;
std::string protocol;
std::string host;
int port;
std::string path;
std::string query;
std::string fragment;
private:
void parseUrl(const std::string& url) {
static const std::regex urlRegex(R"(^(https?:\/\/)?([^\/:]+)(:\d+)?(\/.*)?$)");
valid = false;
std::smatch matches;
if (std::regex_match(url, matches, urlRegex)) {
auto& scheme = matches[1];
auto& host = matches[2];
auto& port = matches[3];
auto& arguments = matches[4];
setProtocol(scheme);
setHost(host);
setPort(port);
if (arguments.matched) {
target = arguments.str();
parsePath(arguments.str());
parseQuery(arguments.str());
parseFragment(arguments.str());
}
else {
target = "/";
}
valid = true;
}
}
void parsePath(const std::string& arguments) {
static const std::regex pathRegex(R"(/([^?#]*))");
std::smatch match;
if (std::regex_search(arguments, match, pathRegex)) {
setPath(match[1]);
}
}
void parseQuery(const std::string& arguments) {
static const std::regex queryRegex(R"(\?([^#]*))");
std::smatch match;
if (std::regex_search(arguments, match, queryRegex)) {
setQuery(match[1]);
}
}
void parseFragment(const std::string& arguments) {
static const std::regex fragmentRegex(R"(#(.*))");
std::smatch match;
if (std::regex_search(arguments, match, fragmentRegex)) {
setFragment(match[1]);
}
}
void setProtocol(const std::ssub_match& match) {
if (match.matched) {
protocol = match.str();
boost::algorithm::to_lower(protocol);
}
else {
protocol = "http://";
}
secure = protocol == "https://";
}
void setHost(const std::ssub_match& match) {
if (match.matched) {
host = match.str();
}
}
void setPort(const std::ssub_match& match) {
if (match.matched) {
port = match.str().empty() ? 0 : std::stoi(match.str().substr(1));
}
else {
if (protocol.find("https://") != std::string::npos) {
port = 443;
}
else if (protocol.find("http://") != std::string::npos) {
port = 80;
}
}
}
void setPath(const std::ssub_match& match) {
if (match.matched) {
path = match.str();
}
}
void setQuery(const std::ssub_match& match) {
if (match.matched) {
query = match.str();
}
}
void setFragment(const std::ssub_match& match) {
if (match.matched) {
fragment = match.str();
}
}
private:
bool valid;
bool secure;
};
class HttpResponse {
public:
uint32_t requestId;
int version;
int statusCode;
std::string location;
std::string contentType;
uint32_t responseTimeMs;
std::string headerData;
size_t bodySize;
std::string bodyData;
bool success;
std::string errorMessage;
private:
void buildHeaderData(const boost::beast::http::response_parser<boost::beast::http::dynamic_body>& response) {
auto& responseHeader = response.get();
statusCode = responseHeader.result_int();
version = responseHeader.version();
location = std::string(responseHeader[boost::beast::http::field::location]);
contentType = std::string(responseHeader[boost::beast::http::field::content_type]);
auto headers = responseHeader.base();
for (const auto& header : headers) {
boost::beast::string_view headerName = header.name_string();
boost::beast::string_view headerValue = header.value();
std::string headerString = std::string(headerName) + ": " + std::string(headerValue) + "\n";
headerData.insert(headerData.end(), headerString.begin(), headerString.end());
}
bodySize = 0;
if (responseHeader.has_content_length()) {
bodySize = std::stoul(std::string(responseHeader[boost::beast::http::field::content_length]));
}
}
inline void buildBodyData(const boost::beast::http::response_parser<boost::beast::http::dynamic_body>& response) {
bodyData = boost::beast::buffers_to_string(response.get().body().data());
}
void setResponseTime(uint32_t responseTimeMs_)
{
responseTimeMs = responseTimeMs_;
}
void setRequestId(uint32_t requestId_)
{
requestId = requestId_;
}
friend class HttpConnectionBase;
friend class HttpConnection;
friend class HttpsConnection;
};
class HttpConnectionBase : public std::enable_shared_from_this<HttpConnectionBase>
{
public:
HttpConnectionBase(boost::asio::any_io_executor executor, uint32_t id, HttpResponse_cb responseCallback, HttpFailure_cb failureCallback)
: resolver(executor),
stream(executor),
timer(executor),
id(id),
responseData(std::make_shared<HttpResponse>()),
responseCallback(responseCallback),
failureCallback(failureCallback)
{
setTimeout(30000);
responseData->setRequestId(id);
}
virtual ~HttpConnectionBase() {
}
inline void setTimeout(int timeout_) {
timeout = timeout_;
}
virtual void create(const boost::beast::http::request<boost::beast::http::string_body>& request_, const std::string& url, uint32_t port, bool skipBody = false)
{
timer.expires_after(std::chrono::milliseconds(timeout));
timer.async_wait(std::bind(&HttpConnectionBase::onTimeout, shared_from_this(), std::placeholders::_1));
request = request_;
connectionStart = std::chrono::steady_clock::now();
response.skip(skipBody);
resolve(url, port);
}
virtual void resolve(const std::string& url, uint32_t port)
{
resolver.async_resolve(url, std::to_string(port), std::bind(&HttpConnectionBase::onResolve, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
virtual void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::results_type results)
{
if(!error) {
connect(results);
}
else {
onError("Failed to resolve to HTTP address: " + error.message());
}
}
virtual void connect(const boost::asio::ip::tcp::resolver::results_type& results)
{
boost::asio::async_connect(stream, results.begin(), results.end(), std::bind(&HttpConnectionBase::onConnect, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
virtual void onConnect(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::results_type::iterator i)
{
if(!error) {
writeRequest();
}
else {
onError("Failed to connect to HTTP socket: " + error.message());
}
}
virtual void writeRequest()
{
boost::beast::http::async_write(stream, request, std::bind(&HttpConnectionBase::onRequestWrite, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
virtual void onRequestWrite(const boost::beast::error_code& error, std::size_t bytes_transferred)
{
if(!error) {
readHeader();
}
else {
close();
onError("Failed to write HTTP request: " + error.message());
}
}
virtual void readHeader()
{
buffer.max_size(MAX_HEADER_CHUNCK_SIZE);
boost::beast::http::async_read_header(stream, buffer, response, std::bind(&HttpConnectionBase::onReadHeader, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
virtual void onReadHeader(const boost::beast::error_code& error, std::size_t bytes_transferred)
{
if(!error || response.is_header_done()) {
responseData->setRequestId(id);
responseData->buildHeaderData(response);
readBody();
}
else {
close();
onError("Failed to read HTTP header: " + error.message());
}
}
virtual void readBody()
{
timer.expires_after(std::chrono::milliseconds(timeout));
buffer.max_size(MAX_BODY_CHUNCK_SIZE);
boost::beast::http::async_read_some(stream, buffer, response, std::bind(&HttpConnectionBase::onReadBody, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
virtual void onReadBody(const boost::beast::error_code& error, std::size_t bytes_transferred)
{
if(error && error != boost::beast::http::error::end_of_stream) {
close();
onError("Failed to read HTTP body: " + error.message());
return;
}
if(error == boost::beast::http::error::end_of_stream || response.is_done()) {
responseData->setResponseTime(calculateResponseTime());
responseData->buildBodyData(response);
responseData->success = true;
onSuccess(responseData);
close();
return;
}
readBody();
}
virtual void close()
{
timer.cancel();
boost::system::error_code ec;
stream.close(ec);
}
virtual void onShutdown(boost::system::error_code error)
{
}
void onTimeout(const boost::system::error_code& error)
{
if(!error) {
close();
onError("Failed on HTTP: timeout");
}
}
void inline onError(const std::string& reason)
{
if(failureCallback) {
if(!responseData) {
responseData = std::make_shared<HttpResponse>();
}
responseData->success = false;
responseData->errorMessage = reason;
failureCallback(responseData);
}
}
void inline onSuccess(const HttpResponse_ptr& responseData)
{
if(responseCallback) {
responseCallback(responseData);
}
}
protected:
int timeout;
uint32_t id;
boost::asio::ip::tcp::resolver resolver;
boost::asio::ip::tcp::socket stream;
boost::asio::steady_timer timer;
boost::beast::flat_buffer buffer;
boost::beast::http::request<boost::beast::http::string_body> request;
boost::beast::http::response_parser<boost::beast::http::dynamic_body> response;
std::chrono::steady_clock::time_point connectionStart;
const int MAX_HEADER_CHUNCK_SIZE = 8 * 1024;
const int MAX_BODY_CHUNCK_SIZE = 64 * 1024;
HttpResponse_ptr responseData;
HttpResponse_cb responseCallback;
HttpFailure_cb failureCallback;
uint32_t calculateResponseTime()
{
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> duration = end - connectionStart;
return static_cast<uint32_t>(duration.count());
}
};
class HttpConnection : public HttpConnectionBase
{
public:
HttpConnection(boost::asio::io_context& ioContext, uint32_t id, HttpResponse_cb responseCallback, HttpFailure_cb failureCallback)
: HttpConnectionBase(boost::asio::make_strand(ioContext), id, responseCallback, failureCallback)
{
}
};
class HttpsConnection : public HttpConnectionBase
{
public:
HttpsConnection(boost::asio::io_context& ioContext, uint32_t id, boost::asio::ssl::context& sslContext, HttpResponse_cb responseCallback, HttpFailure_cb failureCallback)
: HttpConnectionBase(boost::asio::make_strand(ioContext), id, responseCallback, failureCallback),
sslStream(stream, sslContext)
{
}
void create(const boost::beast::http::request<boost::beast::http::string_body>& request_, const std::string& url, uint32_t port, bool skipBody = false) override
{
sslStream.set_verify_mode(boost::asio::ssl::verify_peer);
sslStream.set_verify_callback([](bool, boost::asio::ssl::verify_context&) { return true; });
if (!SSL_set_tlsext_host_name(sslStream.native_handle(), url.c_str())) {
boost::beast::error_code ec2(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
onError("HTTPS error" + ec2.message());
return;
}
timer.expires_after(std::chrono::milliseconds(timeout));
timer.async_wait(std::bind(&HttpConnectionBase::onTimeout, shared_from_this(), std::placeholders::_1));
request = request_;
connectionStart = std::chrono::steady_clock::now();
response.skip(skipBody);
resolve(url, port);
}
void onConnect(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::results_type::iterator i) override
{
if (!error) {
handshake();
}
else {
onError("Failed to connect to HTTP socket: " + error.message());
}
}
void handshake()
{
auto self(shared_from_this());
sslStream.async_handshake(boost::asio::ssl::stream_base::client, [&, self](const boost::system::error_code& error) {
if(!error) {
writeRequest();
}
else {
onError("Failed SSL handshake: " + error.message());
}
});
}
void writeRequest() override
{
boost::beast::http::async_write(sslStream, request, std::bind(&HttpConnectionBase::onRequestWrite, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
void readHeader() override
{
buffer.max_size(MAX_HEADER_CHUNCK_SIZE);
boost::beast::http::async_read_header(sslStream, buffer, response, std::bind(&HttpConnectionBase::onReadHeader, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
void readBody() override
{
timer.expires_after(std::chrono::milliseconds(timeout));
buffer.max_size(MAX_BODY_CHUNCK_SIZE);
boost::beast::http::async_read_some(sslStream, buffer, response, std::bind(&HttpConnectionBase::onReadBody, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
void close() override
{
timer.cancel();
sslStream.async_shutdown(std::bind(&HttpConnectionBase::onShutdown, this->shared_from_this(), std::placeholders::_1));
}
void onShutdown(boost::system::error_code error) override
{
boost::system::error_code ec;
stream.close(ec);
}
private:
boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> sslStream;
};
class Request
{
public:
Request()
: context(), guard(boost::asio::make_work_guard(context)), requestId(1)
{
thread = std::thread([this]() {
context.run();
});
}
Request(const HttpResponse_cb& responseCallback)
: context(), guard(boost::asio::make_work_guard(context)), requestId(1), responseCallback(responseCallback)
{
thread = std::thread([this]() {
context.run();
});
}
Request(const HttpResponse_cb& responseCallback, const HttpFailure_cb& failureCallback)
: context(), guard(boost::asio::make_work_guard(context)), requestId(1), responseCallback(responseCallback), failureCallback(failureCallback)
{
thread = std::thread([this]() {
context.run();
});
}
~Request()
{
context.stop();
guard.reset();
if (thread.joinable()) {
thread.join();
}
}
inline uint32_t getRequestId()
{
return requestId;
}
inline void setTimeout(uint32_t timeout)
{
requestTimeoutMs = timeout;
}
bool connect(const std::string& url)
{
return connect(url, emptyFields);
}
bool connect(const std::string& url, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request CONNECT: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::connect);
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request CONNECT (" + url + "): " + e.what());
return false;
}
return true;
}
bool trace(const std::string& url)
{
return trace(url, emptyFields);
}
bool trace(const std::string& url, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request TRACE: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::trace);
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request TRACE (" + url + "): " + e.what());
return false;
}
return true;
}
bool options(const std::string& url)
{
return options(url, emptyFields);
}
bool options(const std::string& url, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request OPTIONS: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::options);
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request OPTIONS (" + url + "): " + e.what());
return false;
}
return true;
}
bool head(const std::string& url)
{
return head(url, emptyFields);
}
bool head(const std::string& url, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request HEAD: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::head);
const bool skipBody = true;
doRequest(httpUrl, request, skipBody);
}
catch (std::exception e) {
onError("error during HTTP request HEAD (" + url + "): " + e.what());
return false;
}
return true;
}
bool delete_(const std::string& url)
{
return delete_(url, emptyFields);
}
bool delete_(const std::string& url, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request DELETE: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::delete_);
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request DELETE (" + url + "): " + e.what());
return false;
}
return true;
}
bool get(const std::string& url)
{
return get(url, emptyFields);
}
bool get(const std::string& url, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request GET: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::get);
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request GET (" + url + "): " + e.what());
return false;
}
return true;
}
bool post(const std::string& url, const std::string& postData)
{
return post(url, postData, emptyFields);
}
bool post(const std::string& url, const std::string& postData, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request POST: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::post);
request.body() = postData;
request.prepare_payload();
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request POST (" + url + "): " + e.what());
return false;
}
return true;
}
bool patch(const std::string& url, const std::string& patchData)
{
return patch(url, patchData, emptyFields);
}
bool patch(const std::string& url, const std::string& patchData, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request PATCH: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::patch);
request.body() = patchData;
request.prepare_payload();
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request PATCH (" + url + "): " + e.what());
return false;
}
return true;
}
bool put(const std::string& url, const std::string& putData)
{
return put(url, putData, emptyFields);
}
bool put(const std::string& url, const std::string& putData, std::unordered_map<std::string, std::string>& fields)
{
HttpUrl httpUrl(url);
if (!httpUrl.isValid()) {
onError("error during HTTP request PUT: invalid URL: " + url);
return false;
}
try {
boost::beast::http::request<boost::beast::http::string_body> request = buildBasicRequest(httpUrl, fields);
setUniqueRequestId();
request.method(boost::beast::http::verb::put);
request.body() = putData;
request.prepare_payload();
doRequest(httpUrl, request);
}
catch (std::exception e) {
onError("error during HTTP request PUT (" + url + "): " + e.what());
return false;
}
return true;
}
private:
std::thread thread;
boost::asio::io_context context;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> guard;
std::unordered_map<std::string, std::string> emptyFields;
uint32_t requestId;
uint32_t requestTimeoutMs = 0;
HttpResponse_cb responseCallback;
HttpFailure_cb failureCallback;
boost::beast::http::request<boost::beast::http::string_body> buildBasicRequest(const HttpUrl& httpUrl, std::unordered_map<std::string, std::string>& fields)
{
boost::beast::http::request<boost::beast::http::string_body> request;
request.version(11);
request.prepare_payload();
request.keep_alive(false);
request.set(boost::beast::http::field::host, httpUrl.host);
for (auto& field : fields) {
request.insert(field.first, field.second);
}
request.target(httpUrl.target);
return request;
}
void setUniqueRequestId()
{
requestId++;
}
void doRequest(const HttpUrl& httpUrl, boost::beast::http::request<boost::beast::http::string_body>& request, bool skipBody = false)
{
std::shared_ptr<HttpConnectionBase> httpConnection;
if (httpUrl.isProtocolSecure()) {
boost::asio::ssl::context sslContext { boost::asio::ssl::context::tlsv12_client };
sslContext.set_default_verify_paths();
httpConnection = std::make_shared<HttpsConnection>(context, requestId, sslContext,
std::bind(&Request::requestSuccessCallback, this, std::placeholders::_1),
std::bind(&Request::requestFailureCallback, this, std::placeholders::_1));
}
else {
httpConnection = std::make_shared<HttpConnection>(context, requestId,
std::bind(&Request::requestSuccessCallback, this, std::placeholders::_1),
std::bind(&Request::requestFailureCallback, this, std::placeholders::_1));
}
if (requestTimeoutMs > 0) {
httpConnection->setTimeout(requestTimeoutMs);
}
httpConnection->create(request, httpUrl.host, httpUrl.port, skipBody);
}
void requestSuccessCallback(HttpResponse_ptr response)
{
if (responseCallback) {
responseCallback(response);
}
else {
std::cout << "HTTP response received (" << response->responseTimeMs << "ms) but Request has no responseCallback" << std::endl;
}
}
void requestFailureCallback(HttpResponse_ptr response)
{
if (failureCallback) {
failureCallback(response);
}
else {
std::cout << "HTTP failure but Request has no failureCallback. Failure reason: " << response->errorMessage << std::endl;
}
}
void onError(const std::string& reason)
{
std::cout << "Could not complete HTTP request: " << reason << std::endl;
}
};
}
#endif //HTTPCLIENT_H