-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTTPRequest.hpp
1330 lines (1131 loc) · 53.1 KB
/
HTTPRequest.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
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// HTTPRequest
//
#ifndef HTTPREQUEST_HPP
#define HTTPREQUEST_HPP
#include <cctype>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <array>
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <system_error>
#include <type_traits>
#include <utility>
#include <vector>
#if defined(_WIN32) || defined(__CYGWIN__)
# pragma push_macro("WIN32_LEAN_AND_MEAN")
# pragma push_macro("NOMINMAX")
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif // WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
# define NOMINMAX
# endif // NOMINMAX
# include <winsock2.h>
# if _WIN32_WINNT < _WIN32_WINNT_WINXP
extern "C" char *_strdup(const char *strSource);
# define strdup _strdup
# include <wspiapi.h>
# endif // _WIN32_WINNT < _WIN32_WINNT_WINXP
# include <ws2tcpip.h>
# pragma pop_macro("WIN32_LEAN_AND_MEAN")
# pragma pop_macro("NOMINMAX")
#else
# include <errno.h>
# include <fcntl.h>
# include <netinet/in.h>
# include <netdb.h>
# include <sys/select.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <unistd.h>
#endif // defined(_WIN32) || defined(__CYGWIN__)
namespace http
{
class RequestError final: public std::logic_error
{
public:
using logic_error::logic_error;
using logic_error::operator=;
};
class ResponseError final: public std::runtime_error
{
public:
using runtime_error::runtime_error;
using runtime_error::operator=;
};
enum class InternetProtocol: std::uint8_t
{
v4,
v6
};
struct Uri final
{
std::string scheme;
std::string user;
std::string password;
std::string host;
std::string port;
std::string path;
std::string query;
std::string fragment;
};
struct Version final
{
uint16_t major;
uint16_t minor;
};
struct Status final
{
// RFC 7231, 6. Response Status Codes
enum Code: std::uint16_t
{
Continue = 100,
SwitchingProtocol = 101,
Processing = 102,
EarlyHints = 103,
Ok = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultiStatus = 207,
AlreadyReported = 208,
ImUsed = 226,
MultipleChoice = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UseProxy = 305,
TemporaryRedirect = 307,
PermanentRedirect = 308,
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
PayloadTooLarge = 413,
UriTooLong = 414,
UnsupportedMediaType = 415,
RangeNotSatisfiable = 416,
ExpectationFailed = 417,
MisdirectedRequest = 421,
UnprocessableEntity = 422,
Locked = 423,
FailedDependency = 424,
TooEarly = 425,
UpgradeRequired = 426,
PreconditionRequired = 428,
TooManyRequests = 429,
RequestHeaderFieldsTooLarge = 431,
UnavailableForLegalReasons = 451,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HttpVersionNotSupported = 505,
VariantAlsoNegotiates = 506,
InsufficientStorage = 507,
LoopDetected = 508,
NotExtended = 510,
NetworkAuthenticationRequired = 511
};
Version version;
std::uint16_t code;
std::string reason;
};
using HeaderField = std::pair<std::string, std::string>;
using HeaderFields = std::vector<HeaderField>;
struct Response final
{
Status status;
HeaderFields headerFields;
std::vector<std::uint8_t> body;
};
inline namespace detail
{
#if defined(_WIN32) || defined(__CYGWIN__)
namespace winsock
{
class ErrorCategory final: public std::error_category
{
public:
const char* name() const noexcept override
{
return "Windows Sockets API";
}
std::string message(const int condition) const override
{
switch (condition)
{
case WSA_INVALID_HANDLE: return "Specified event object handle is invalid";
case WSA_NOT_ENOUGH_MEMORY: return "Insufficient memory available";
case WSA_INVALID_PARAMETER: return "One or more parameters are invalid";
case WSA_OPERATION_ABORTED: return "Overlapped operation aborted";
case WSA_IO_INCOMPLETE: return "Overlapped I/O event object not in signaled state";
case WSA_IO_PENDING: return "Overlapped operations will complete later";
case WSAEINTR: return "Interrupted function call";
case WSAEBADF: return "File handle is not valid";
case WSAEACCES: return "Permission denied";
case WSAEFAULT: return "Bad address";
case WSAEINVAL: return "Invalid argument";
case WSAEMFILE: return "Too many open files";
case WSAEWOULDBLOCK: return "Resource temporarily unavailable";
case WSAEINPROGRESS: return "Operation now in progress";
case WSAEALREADY: return "Operation already in progress";
case WSAENOTSOCK: return "Socket operation on nonsocket";
case WSAEDESTADDRREQ: return "Destination address required";
case WSAEMSGSIZE: return "Message too long";
case WSAEPROTOTYPE: return "Protocol wrong type for socket";
case WSAENOPROTOOPT: return "Bad protocol option";
case WSAEPROTONOSUPPORT: return "Protocol not supported";
case WSAESOCKTNOSUPPORT: return "Socket type not supported";
case WSAEOPNOTSUPP: return "Operation not supported";
case WSAEPFNOSUPPORT: return "Protocol family not supported";
case WSAEAFNOSUPPORT: return "Address family not supported by protocol family";
case WSAEADDRINUSE: return "Address already in use";
case WSAEADDRNOTAVAIL: return "Cannot assign requested address";
case WSAENETDOWN: return "Network is down";
case WSAENETUNREACH: return "Network is unreachable";
case WSAENETRESET: return "Network dropped connection on reset";
case WSAECONNABORTED: return "Software caused connection abort";
case WSAECONNRESET: return "Connection reset by peer";
case WSAENOBUFS: return "No buffer space available";
case WSAEISCONN: return "Socket is already connected";
case WSAENOTCONN: return "Socket is not connected";
case WSAESHUTDOWN: return "Cannot send after socket shutdown";
case WSAETOOMANYREFS: return "Too many references";
case WSAETIMEDOUT: return "Connection timed out";
case WSAECONNREFUSED: return "Connection refused";
case WSAELOOP: return "Cannot translate name";
case WSAENAMETOOLONG: return "Name too long";
case WSAEHOSTDOWN: return "Host is down";
case WSAEHOSTUNREACH: return "No route to host";
case WSAENOTEMPTY: return "Directory not empty";
case WSAEPROCLIM: return "Too many processes";
case WSAEUSERS: return "User quota exceeded";
case WSAEDQUOT: return "Disk quota exceeded";
case WSAESTALE: return "Stale file handle reference";
case WSAEREMOTE: return "Item is remote";
case WSASYSNOTREADY: return "Network subsystem is unavailable";
case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range";
case WSANOTINITIALISED: return "Successful WSAStartup not yet performed";
case WSAEDISCON: return "Graceful shutdown in progress";
case WSAENOMORE: return "No more results";
case WSAECANCELLED: return "Call has been canceled";
case WSAEINVALIDPROCTABLE: return "Procedure call table is invalid";
case WSAEINVALIDPROVIDER: return "Service provider is invalid";
case WSAEPROVIDERFAILEDINIT: return "Service provider failed to initialize";
case WSASYSCALLFAILURE: return "System call failure";
case WSASERVICE_NOT_FOUND: return "Service not found";
case WSATYPE_NOT_FOUND: return "Class type not found";
case WSA_E_NO_MORE: return "No more results";
case WSA_E_CANCELLED: return "Call was canceled";
case WSAEREFUSED: return "Database query was refused";
case WSAHOST_NOT_FOUND: return "Host not found";
case WSATRY_AGAIN: return "Nonauthoritative host not found";
case WSANO_RECOVERY: return "This is a nonrecoverable error";
case WSANO_DATA: return "Valid name, no data record of requested type";
case WSA_QOS_RECEIVERS: return "QoS receivers";
case WSA_QOS_SENDERS: return "QoS senders";
case WSA_QOS_NO_SENDERS: return "No QoS senders";
case WSA_QOS_NO_RECEIVERS: return "QoS no receivers";
case WSA_QOS_REQUEST_CONFIRMED: return "QoS request confirmed";
case WSA_QOS_ADMISSION_FAILURE: return "QoS admission error";
case WSA_QOS_POLICY_FAILURE: return "QoS policy failure";
case WSA_QOS_BAD_STYLE: return "QoS bad style";
case WSA_QOS_BAD_OBJECT: return "QoS bad object";
case WSA_QOS_TRAFFIC_CTRL_ERROR: return "QoS traffic control error";
case WSA_QOS_GENERIC_ERROR: return "QoS generic error";
case WSA_QOS_ESERVICETYPE: return "QoS service type error";
case WSA_QOS_EFLOWSPEC: return "QoS flowspec error";
case WSA_QOS_EPROVSPECBUF: return "Invalid QoS provider buffer";
case WSA_QOS_EFILTERSTYLE: return "Invalid QoS filter style";
case WSA_QOS_EFILTERTYPE: return "Invalid QoS filter type";
case WSA_QOS_EFILTERCOUNT: return "Incorrect QoS filter count";
case WSA_QOS_EOBJLENGTH: return "Invalid QoS object length";
case WSA_QOS_EFLOWCOUNT: return "Incorrect QoS flow count";
case WSA_QOS_EUNKOWNPSOBJ: return "Unrecognized QoS object";
case WSA_QOS_EPOLICYOBJ: return "Invalid QoS policy object";
case WSA_QOS_EFLOWDESC: return "Invalid QoS flow descriptor";
case WSA_QOS_EPSFLOWSPEC: return "Invalid QoS provider-specific flowspec";
case WSA_QOS_EPSFILTERSPEC: return "Invalid QoS provider-specific filterspec";
case WSA_QOS_ESDMODEOBJ: return "Invalid QoS shape discard mode object";
case WSA_QOS_ESHAPERATEOBJ: return "Invalid QoS shaping rate object";
case WSA_QOS_RESERVED_PETYPE: return "Reserved policy QoS element type";
default: return "Unknown error (" + std::to_string(condition) + ")";
}
}
};
inline const ErrorCategory errorCategory;
class Api final
{
public:
Api()
{
WSADATA wsaData;
const auto error = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (error != 0)
throw std::system_error{error, errorCategory, "WSAStartup failed"};
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
throw std::runtime_error{"Invalid WinSock version"};
}
started = true;
}
~Api()
{
if (started) WSACleanup();
}
Api(Api&& other) noexcept:
started{std::exchange(other.started, false)}
{
}
Api& operator=(Api&& other) noexcept
{
if (&other == this) return *this;
if (started) WSACleanup();
started = std::exchange(other.started, false);
return *this;
}
private:
bool started = false;
};
}
#endif // defined(_WIN32) || defined(__CYGWIN__)
constexpr int getAddressFamily(const InternetProtocol internetProtocol)
{
return (internetProtocol == InternetProtocol::v4) ? AF_INET :
(internetProtocol == InternetProtocol::v6) ? AF_INET6 :
throw RequestError{"Unsupported protocol"};
}
class Socket final
{
public:
#if defined(_WIN32) || defined(__CYGWIN__)
using Type = SOCKET;
static constexpr Type invalid = INVALID_SOCKET;
#else
using Type = int;
static constexpr Type invalid = -1;
#endif // defined(_WIN32) || defined(__CYGWIN__)
explicit Socket(const InternetProtocol internetProtocol):
endpoint{socket(getAddressFamily(internetProtocol), SOCK_STREAM, IPPROTO_TCP)}
{
if (endpoint == invalid)
#if defined(_WIN32) || defined(__CYGWIN__)
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to create socket"};
#else
throw std::system_error{errno, std::system_category(), "Failed to create socket"};
#endif // defined(_WIN32) || defined(__CYGWIN__)
#if defined(_WIN32) || defined(__CYGWIN__)
ULONG mode = 1;
if (ioctlsocket(endpoint, FIONBIO, &mode) == SOCKET_ERROR)
{
close();
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to get socket flags"};
}
#else
const auto flags = fcntl(endpoint, F_GETFL);
if (flags == -1)
{
close();
throw std::system_error{errno, std::system_category(), "Failed to get socket flags"};
}
if (fcntl(endpoint, F_SETFL, flags | O_NONBLOCK) == -1)
{
close();
throw std::system_error{errno, std::system_category(), "Failed to set socket flags"};
}
#endif // defined(_WIN32) || defined(__CYGWIN__)
#ifdef __APPLE__
const int value = 1;
if (setsockopt(endpoint, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value)) == -1)
{
close();
throw std::system_error{errno, std::system_category(), "Failed to set socket option"};
}
#endif // __APPLE__
}
~Socket()
{
if (endpoint != invalid) close();
}
Socket(Socket&& other) noexcept:
endpoint{std::exchange(other.endpoint, invalid)}
{
}
Socket& operator=(Socket&& other) noexcept
{
if (&other == this) return *this;
if (endpoint != invalid) close();
endpoint = std::exchange(other.endpoint, invalid);
return *this;
}
void connect(const struct sockaddr* address, const socklen_t addressSize, const std::int64_t timeout)
{
#if defined(_WIN32) || defined(__CYGWIN__)
auto result = ::connect(endpoint, address, addressSize);
while (result == -1 && WSAGetLastError() == WSAEINTR)
result = ::connect(endpoint, address, addressSize);
if (result == -1)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
select(SelectType::write, timeout);
char socketErrorPointer[sizeof(int)];
socklen_t optionLength = sizeof(socketErrorPointer);
if (getsockopt(endpoint, SOL_SOCKET, SO_ERROR, socketErrorPointer, &optionLength) == SOCKET_ERROR)
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to get socket option"};
int socketError;
std::memcpy(&socketError, socketErrorPointer, sizeof(socketErrorPointer));
if (socketError != 0)
throw std::system_error{socketError, winsock::errorCategory, "Failed to connect"};
}
else
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to connect"};
}
#else
auto result = ::connect(endpoint, address, addressSize);
while (result == -1 && errno == EINTR)
result = ::connect(endpoint, address, addressSize);
if (result == -1)
{
if (errno == EINPROGRESS)
{
select(SelectType::write, timeout);
int socketError;
socklen_t optionLength = sizeof(socketError);
if (getsockopt(endpoint, SOL_SOCKET, SO_ERROR, &socketError, &optionLength) == -1)
throw std::system_error{errno, std::system_category(), "Failed to get socket option"};
if (socketError != 0)
throw std::system_error{socketError, std::system_category(), "Failed to connect"};
}
else
throw std::system_error{errno, std::system_category(), "Failed to connect"};
}
#endif // defined(_WIN32) || defined(__CYGWIN__)
}
std::size_t send(const void* buffer, const std::size_t length, const std::int64_t timeout)
{
select(SelectType::write, timeout);
#if defined(_WIN32) || defined(__CYGWIN__)
auto result = ::send(endpoint, reinterpret_cast<const char*>(buffer),
static_cast<int>(length), 0);
while (result == SOCKET_ERROR && WSAGetLastError() == WSAEINTR)
result = ::send(endpoint, reinterpret_cast<const char*>(buffer),
static_cast<int>(length), 0);
if (result == SOCKET_ERROR)
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to send data"};
#else
auto result = ::send(endpoint, reinterpret_cast<const char*>(buffer),
length, noSignal);
while (result == -1 && errno == EINTR)
result = ::send(endpoint, reinterpret_cast<const char*>(buffer),
length, noSignal);
if (result == -1)
throw std::system_error{errno, std::system_category(), "Failed to send data"};
#endif // defined(_WIN32) || defined(__CYGWIN__)
return static_cast<std::size_t>(result);
}
std::size_t recv(void* buffer, const std::size_t length, const std::int64_t timeout)
{
select(SelectType::read, timeout);
#if defined(_WIN32) || defined(__CYGWIN__)
auto result = ::recv(endpoint, reinterpret_cast<char*>(buffer),
static_cast<int>(length), 0);
while (result == SOCKET_ERROR && WSAGetLastError() == WSAEINTR)
result = ::recv(endpoint, reinterpret_cast<char*>(buffer),
static_cast<int>(length), 0);
if (result == SOCKET_ERROR)
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to read data"};
#else
auto result = ::recv(endpoint, reinterpret_cast<char*>(buffer),
length, noSignal);
while (result == -1 && errno == EINTR)
result = ::recv(endpoint, reinterpret_cast<char*>(buffer),
length, noSignal);
if (result == -1)
throw std::system_error{errno, std::system_category(), "Failed to read data"};
#endif // defined(_WIN32) || defined(__CYGWIN__)
return static_cast<std::size_t>(result);
}
private:
enum class SelectType
{
read,
write
};
void select(const SelectType type, const std::int64_t timeout)
{
fd_set descriptorSet;
FD_ZERO(&descriptorSet);
FD_SET(endpoint, &descriptorSet);
#if defined(_WIN32) || defined(__CYGWIN__)
TIMEVAL selectTimeout{
static_cast<LONG>(timeout / 1000),
static_cast<LONG>((timeout % 1000) * 1000)
};
auto count = ::select(0,
(type == SelectType::read) ? &descriptorSet : nullptr,
(type == SelectType::write) ? &descriptorSet : nullptr,
nullptr,
(timeout >= 0) ? &selectTimeout : nullptr);
while (count == SOCKET_ERROR && WSAGetLastError() == WSAEINTR)
count = ::select(0,
(type == SelectType::read) ? &descriptorSet : nullptr,
(type == SelectType::write) ? &descriptorSet : nullptr,
nullptr,
(timeout >= 0) ? &selectTimeout : nullptr);
if (count == SOCKET_ERROR)
throw std::system_error{WSAGetLastError(), winsock::errorCategory, "Failed to select socket"};
else if (count == 0)
throw ResponseError{"Request timed out"};
#else
timeval selectTimeout{
static_cast<time_t>(timeout / 1000),
static_cast<suseconds_t>((timeout % 1000) * 1000)
};
auto count = ::select(endpoint + 1,
(type == SelectType::read) ? &descriptorSet : nullptr,
(type == SelectType::write) ? &descriptorSet : nullptr,
nullptr,
(timeout >= 0) ? &selectTimeout : nullptr);
while (count == -1 && errno == EINTR)
count = ::select(endpoint + 1,
(type == SelectType::read) ? &descriptorSet : nullptr,
(type == SelectType::write) ? &descriptorSet : nullptr,
nullptr,
(timeout >= 0) ? &selectTimeout : nullptr);
if (count == -1)
throw std::system_error{errno, std::system_category(), "Failed to select socket"};
else if (count == 0)
throw ResponseError{"Request timed out"};
#endif // defined(_WIN32) || defined(__CYGWIN__)
}
void close() noexcept
{
#if defined(_WIN32) || defined(__CYGWIN__)
closesocket(endpoint);
#else
::close(endpoint);
#endif // defined(_WIN32) || defined(__CYGWIN__)
}
#if defined(__unix__) && !defined(__APPLE__) && !defined(__CYGWIN__)
static constexpr int noSignal = MSG_NOSIGNAL;
#else
static constexpr int noSignal = 0;
#endif // defined(__unix__) && !defined(__APPLE__)
Type endpoint = invalid;
};
inline char toLower(const char c) noexcept
{
return (c >= 'A' && c <= 'Z') ? c - ('A' - 'a') : c;
}
template <class T>
T toLower(const T& s)
{
T result = s;
for (auto& c : result) c = toLower(c);
return result;
}
// RFC 7230, 3.2.3. WhiteSpace
template <typename C>
constexpr bool isWhiteSpaceChar(const C c) noexcept
{
return c == 0x20 || c == 0x09; // space or tab
};
// RFC 5234, Appendix B.1. Core Rules
template <typename C>
constexpr bool isDigitChar(const C c) noexcept
{
return c >= 0x30 && c <= 0x39; // 0 - 9
}
// RFC 5234, Appendix B.1. Core Rules
template <typename C>
constexpr bool isAlphaChar(const C c) noexcept
{
return
(c >= 0x61 && c <= 0x7A) || // a - z
(c >= 0x41 && c <= 0x5A); // A - Z
}
// RFC 7230, 3.2.6. Field Value Components
template <typename C>
constexpr bool isTokenChar(const C c) noexcept
{
return c == 0x21 || // !
c == 0x23 || // #
c == 0x24 || // $
c == 0x25 || // %
c == 0x26 || // &
c == 0x27 || // '
c == 0x2A || // *
c == 0x2B || // +
c == 0x2D || // -
c == 0x2E || // .
c == 0x5E || // ^
c == 0x5F || // _
c == 0x60 || // `
c == 0x7C || // |
c == 0x7E || // ~
isDigitChar(c) ||
isAlphaChar(c);
};
// RFC 5234, Appendix B.1. Core Rules
template <typename C>
constexpr bool isVisibleChar(const C c) noexcept
{
return c >= 0x21 && c <= 0x7E;
}
// RFC 7230, Appendix B. Collected ABNF
template <typename C>
constexpr bool isObsoleteTextChar(const C c) noexcept
{
return static_cast<unsigned char>(c) >= 0x80 &&
static_cast<unsigned char>(c) <= 0xFF;
}
template <class Iterator>
Iterator skipWhiteSpaces(const Iterator begin, const Iterator end)
{
auto i = begin;
for (i = begin; i != end; ++i)
if (!isWhiteSpaceChar(*i))
break;
return i;
}
// RFC 5234, Appendix B.1. Core Rules
template <typename T, typename C, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
constexpr T digitToUint(const C c)
{
// DIGIT
return (c >= 0x30 && c <= 0x39) ? static_cast<T>(c - 0x30) : // 0 - 9
throw ResponseError{"Invalid digit"};
}
// RFC 5234, Appendix B.1. Core Rules
template <typename T, typename C, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
constexpr T hexDigitToUint(const C c)
{
// HEXDIG
return (c >= 0x30 && c <= 0x39) ? static_cast<T>(c - 0x30) : // 0 - 9
(c >= 0x41 && c <= 0x46) ? static_cast<T>(c - 0x41) + T(10) : // A - Z
(c >= 0x61 && c <= 0x66) ? static_cast<T>(c - 0x61) + T(10) : // a - z, some services send lower-case hex digits
throw ResponseError{"Invalid hex digit"};
}
// RFC 3986, 3. Syntax Components
template <class Iterator>
Uri parseUri(const Iterator begin, const Iterator end)
{
Uri result;
// RFC 3986, 3.1. Scheme
auto i = begin;
if (i == end || !isAlphaChar(*begin))
throw RequestError{"Invalid scheme"};
result.scheme.push_back(*i++);
for (; i != end && (isAlphaChar(*i) || isDigitChar(*i) || *i == '+' || *i == '-' || *i == '.'); ++i)
result.scheme.push_back(*i);
if (i == end || *i++ != ':')
throw RequestError{"Invalid scheme"};
if (i == end || *i++ != '/')
throw RequestError{"Invalid scheme"};
if (i == end || *i++ != '/')
throw RequestError{"Invalid scheme"};
// RFC 3986, 3.2. Authority
std::string authority = std::string(i, end);
// RFC 3986, 3.5. Fragment
const auto fragmentPosition = authority.find('#');
if (fragmentPosition != std::string::npos)
{
result.fragment = authority.substr(fragmentPosition + 1);
authority.resize(fragmentPosition); // remove the fragment part
}
// RFC 3986, 3.4. Query
const auto queryPosition = authority.find('?');
if (queryPosition != std::string::npos)
{
result.query = authority.substr(queryPosition + 1);
authority.resize(queryPosition); // remove the query part
}
// RFC 3986, 3.3. Path
const auto pathPosition = authority.find('/');
if (pathPosition != std::string::npos)
{
// RFC 3986, 3.3. Path
result.path = authority.substr(pathPosition);
authority.resize(pathPosition);
}
else
result.path = "/";
// RFC 3986, 3.2.1. User Information
std::string userinfo;
const auto hostPosition = authority.find('@');
if (hostPosition != std::string::npos)
{
userinfo = authority.substr(0, hostPosition);
const auto passwordPosition = userinfo.find(':');
if (passwordPosition != std::string::npos)
{
result.user = userinfo.substr(0, passwordPosition);
result.password = userinfo.substr(passwordPosition + 1);
}
else
result.user = userinfo;
result.host = authority.substr(hostPosition + 1);
}
else
result.host = authority;
// RFC 3986, 3.2.2. Host
const auto portPosition = result.host.find(':');
if (portPosition != std::string::npos)
{
// RFC 3986, 3.2.3. Port
result.port = result.host.substr(portPosition + 1);
result.host.resize(portPosition);
}
return result;
}
// RFC 7230, 2.6. Protocol Versioning
template <class Iterator>
std::pair<Iterator, Version> parseVersion(const Iterator begin, const Iterator end)
{
auto i = begin;
if (i == end || *i++ != 'H')
throw ResponseError{"Invalid HTTP version"};
if (i == end || *i++ != 'T')
throw ResponseError{"Invalid HTTP version"};
if (i == end || *i++ != 'T')
throw ResponseError{"Invalid HTTP version"};
if (i == end || *i++ != 'P')
throw ResponseError{"Invalid HTTP version"};
if (i == end || *i++ != '/')
throw ResponseError{"Invalid HTTP version"};
if (i == end)
throw ResponseError{"Invalid HTTP version"};
const auto majorVersion = digitToUint<std::uint16_t>(*i++);
if (i == end || *i++ != '.')
throw ResponseError{"Invalid HTTP version"};
if (i == end)
throw ResponseError{"Invalid HTTP version"};
const auto minorVersion = digitToUint<std::uint16_t>(*i++);
return {i, Version{majorVersion, minorVersion}};
}
// RFC 7230, 3.1.2. Status Line
template <class Iterator>
std::pair<Iterator, std::uint16_t> parseStatusCode(const Iterator begin, const Iterator end)
{
std::uint16_t result = 0;
auto i = begin;
while (i != end && isDigitChar(*i))
result = static_cast<std::uint16_t>(result * 10U) + digitToUint<std::uint16_t>(*i++);
if (std::distance(begin, i) != 3)
throw ResponseError{"Invalid status code"};
return {i, result};
}
// RFC 7230, 3.1.2. Status Line
template <class Iterator>
std::pair<Iterator, std::string> parseReasonPhrase(const Iterator begin, const Iterator end)
{
std::string result;
auto i = begin;
for (; i != end && (isWhiteSpaceChar(*i) || isVisibleChar(*i) || isObsoleteTextChar(*i)); ++i)
result.push_back(static_cast<char>(*i));
return {i, std::move(result)};
}
// RFC 7230, 3.2.6. Field Value Components
template <class Iterator>
std::pair<Iterator, std::string> parseToken(const Iterator begin, const Iterator end)
{
std::string result;
auto i = begin;
for (; i != end && isTokenChar(*i); ++i)
result.push_back(static_cast<char>(*i));
if (result.empty())
throw ResponseError{"Invalid token"};
return {i, std::move(result)};
}
// RFC 7230, 3.2. Header Fields
template <class Iterator>
std::pair<Iterator, std::string> parseFieldValue(const Iterator begin, const Iterator end)
{
std::string result;
auto i = begin;
for (; i != end && (isWhiteSpaceChar(*i) || isVisibleChar(*i) || isObsoleteTextChar(*i)); ++i)
result.push_back(static_cast<char>(*i));
// trim white spaces
result.erase(std::find_if(result.rbegin(), result.rend(), [](const char c) noexcept {
return !isWhiteSpaceChar(c);
}).base(), result.end());
return {i, std::move(result)};
}
// RFC 7230, 3.2. Header Fields
template <class Iterator>
std::pair<Iterator, std::string> parseFieldContent(const Iterator begin, const Iterator end)
{
std::string result;
auto i = begin;
for (;;)
{
const auto fieldValueResult = parseFieldValue(i, end);
i = fieldValueResult.first;
result += fieldValueResult.second;
// Handle obsolete fold as per RFC 7230, 3.2.4. Field Parsing
// Obsolete folding is known as linear white space (LWS) in RFC 2616, 2.2 Basic Rules
auto obsoleteFoldIterator = i;
if (obsoleteFoldIterator == end || *obsoleteFoldIterator++ != '\r')
break;
if (obsoleteFoldIterator == end || *obsoleteFoldIterator++ != '\n')
break;
if (obsoleteFoldIterator == end || !isWhiteSpaceChar(*obsoleteFoldIterator++))
break;
result.push_back(' ');
i = obsoleteFoldIterator;
}
return {i, std::move(result)};
}
// RFC 7230, 3.2. Header Fields
template <class Iterator>
std::pair<Iterator, HeaderField> parseHeaderField(const Iterator begin, const Iterator end)
{
auto tokenResult = parseToken(begin, end);
auto i = tokenResult.first;
auto fieldName = toLower(tokenResult.second);
if (i == end || *i++ != ':')
throw ResponseError{"Invalid header"};
i = skipWhiteSpaces(i, end);
auto valueResult = parseFieldContent(i, end);
i = valueResult.first;
auto fieldValue = std::move(valueResult.second);
if (i == end || *i++ != '\r')
throw ResponseError{"Invalid header"};
if (i == end || *i++ != '\n')
throw ResponseError{"Invalid header"};
return {i, {std::move(fieldName), std::move(fieldValue)}};
}
// RFC 7230, 3.1.2. Status Line
template <class Iterator>
std::pair<Iterator, Status> parseStatusLine(const Iterator begin, const Iterator end)
{
const auto versionResult = parseVersion(begin, end);
auto i = versionResult.first;
if (i == end || *i++ != ' ')
throw ResponseError{"Invalid status line"};
const auto statusCodeResult = parseStatusCode(i, end);
i = statusCodeResult.first;
if (i == end || *i++ != ' ')
throw ResponseError{"Invalid status line"};
auto reasonPhraseResult = parseReasonPhrase(i, end);
i = reasonPhraseResult.first;
if (i == end || *i++ != '\r')
throw ResponseError{"Invalid status line"};
if (i == end || *i++ != '\n')
throw ResponseError{"Invalid status line"};
return {i, Status{
versionResult.second,
statusCodeResult.second,
std::move(reasonPhraseResult.second)
}};
}
// RFC 7230, 4.1. Chunked Transfer Coding
template <typename T, class Iterator, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
T stringToUint(const Iterator begin, const Iterator end)
{
T result = 0;
for (auto i = begin; i != end; ++i)
result = T(10U) * result + digitToUint<T>(*i);
return result;
}
template <typename T, class Iterator, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
T hexStringToUint(const Iterator begin, const Iterator end)
{
T result = 0;
for (auto i = begin; i != end; ++i)
result = T(16U) * result + hexDigitToUint<T>(*i);