lssa

List similar artists.


lssa

/

src

/

ob

/

belle.hh

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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
/*
                                    88888888
                                  888888888888
                                 88888888888888
                                8888888888888888
                               888888888888888888
                              888888  8888  888888
                              88888    88    88888
                              888888  8888  888888
                              88888888888888888888
                              88888888888888888888
                             8888888888888888888888
                          8888888888888888888888888888
                        88888888888888888888888888888888
                              88888888888888888888
                            888888888888888888888888
                           888888  8888888888  888888
                           888     8888  8888     888
                                   888    888

                                   OCTOBANANA

Belle
0.6.0-develop-stripped

An HTTP / Websocket library in C++17 using Boost.Beast and Boost.ASIO.
https://octobanana.com/software/belle

Licensed under the MIT License

Copyright (c) 2018-2019 Brett Robinson <https://octobanana.com/>

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.

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 OB_BELLE_HH
#define OB_BELLE_HH

#define OB_BELLE_VERSION_MAJOR 0
#define OB_BELLE_VERSION_MINOR 6
#define OB_BELLE_VERSION_PATCH 0

// Config Begin

// compile with -DOB_BELLE_CONFIG_<OPT> or
// comment out defines to alter the library

// zlib support
#ifndef OB_BELLE_CONFIG_ZLIB_OFF
#define OB_BELLE_CONFIG_ZLIB_ON
#endif // OB_BELLE_CONFIG_ZLIB_OFF

// signal support
#ifndef OB_BELLE_CONFIG_SIGNAL_OFF
#define OB_BELLE_CONFIG_SIGNAL_ON
#endif // OB_BELLE_CONFIG_SIGNAL_OFF

// ssl support
#ifndef OB_BELLE_CONFIG_SSL_OFF
#define OB_BELLE_CONFIG_SSL_ON
#endif // OB_BELLE_CONFIG_SSL_OFF

// client support
#ifndef OB_BELLE_CONFIG_CLIENT_OFF
#define OB_BELLE_CONFIG_CLIENT_ON
#endif // OB_BELLE_CONFIG_CLIENT_OFF

// Config End

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>

#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/bind_executor.hpp>

#ifdef OB_BELLE_CONFIG_ZLIB_ON
#include <zlib.h>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#endif // OB_BELLE_CONFIG_ZLIB_ON

#ifdef OB_BELLE_CONFIG_CLIENT_ON
#include <boost/asio/connect.hpp>
#include <boost/asio/read_until.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#endif // OB_BELLE_CONFIG_CLIENT_ON

#ifdef OB_BELLE_CONFIG_SSL_ON
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#endif // OB_BELLE_CONFIG_SSL_ON

#include <boost/config.hpp>

#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstddef>
#include <cstdint>
#include <csignal>

#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <array>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <regex>
#include <memory>
#include <chrono>
#include <utility>
#include <initializer_list>
#include <optional>
#include <limits>
#include <type_traits>
#include <thread>
#include <fstream>
#include <filesystem>

namespace OB::Belle
{

// aliases
namespace fs = std::filesystem;
namespace net = boost::asio;
namespace beast = boost::beast;
namespace http = boost::beast::http;

#ifdef OB_BELLE_CONFIG_SSL_ON
namespace ssl = boost::asio::ssl;
#endif // OB_BELLE_CONFIG_SSL_ON

using io = boost::asio::io_context;
using tcp = boost::asio::ip::tcp;
using error_code = boost::system::error_code;
using Method = boost::beast::http::verb;
using Status = boost::beast::http::status;
using Header = boost::beast::http::field;
using Headers = boost::beast::http::fields;

namespace Detail
{

// prototypes
inline std::string lowercase(std::string str);
inline std::vector<std::string> split(std::string const& str, std::string const& delim,
  std::size_t size = std::numeric_limits<std::size_t>::max());
inline std::string hex_encode(char const c);
inline char hex_decode(std::string const& s);
inline std::string base64_encode(std::string const& val);
inline std::string base64_decode(std::string const& val);

// string to lowercase
inline std::string lowercase(std::string str)
{
  auto const to_lower = [](char& c)
  {
    if (c >= 'A' && c <= 'Z')
    {
      c += 'a' - 'A';
    }

    return c;
  };

  for (char& c : str)
  {
    c = to_lower(c);
  }

  return str;
}

// split a string by a delimiter 'n' times
inline std::vector<std::string> split(std::string const& str,
  std::string const& delim, std::size_t size)
{
  std::vector<std::string> vtok;
  std::size_t start {0};
  auto end = str.find(delim);

  while ((size-- > 0) && (end != std::string::npos))
  {
    vtok.emplace_back(str.substr(start, end - start));
    start = end + delim.length();
    end = str.find(delim, start);
  }

  vtok.emplace_back(str.substr(start, end));

  return vtok;
}

// convert object into a string
template<typename T>
inline std::string to_string(T const& t)
{
  std::stringstream ss;
  ss << t;

  return ss.str();
}

inline std::string hex_encode(char const c)
{
  char s[3];

  if (c & 0x80)
  {
    std::snprintf(&s[0], 3, "%02X",
      static_cast<unsigned int>(c & 0xff)
    );
  }
  else
  {
    std::snprintf(&s[0], 3, "%02X",
      static_cast<unsigned int>(c)
    );
  }

  return std::string(s);
}

inline char hex_decode(std::string const& s)
{
  unsigned int n;

  std::sscanf(s.data(), "%x", &n);

  return static_cast<char>(n);
}

inline std::string base64_encode(std::string const& val)
{
  using it = boost::archive::iterators::base64_from_binary<boost::archive::iterators::transform_width<std::string::const_iterator, 6, 8>>;

  return std::string(it(val.begin()), it(val.end())).append((3 - val.size() % 3) % 3, '=');
}

inline std::string base64_decode(std::string const& val)
{
  using it = boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64<std::string::const_iterator>, 8, 6>;

  return boost::algorithm::trim_right_copy_if(std::string(it(val.begin()), it(val.end())),
    [](char const c)
    {
      return c == '\0';
    });
}

#ifdef OB_BELLE_CONFIG_SSL_ON
// TODO switch to boost::beast::ssl_stream when it moves out of experimental
template<typename Next_Layer>
class ssl_stream final : public ssl::stream_base
{
// This class (ssl_stream) is a derivative work based on Boost.Beast,
// orignal copyright below:
/*
  Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)

  Boost Software License - Version 1.0 - August 17th, 2003

  Permission is hereby granted, free of charge, to any person or organization
  obtaining a copy of the software and accompanying documentation covered by
  this license (the "Software") to use, reproduce, display, distribute,
  execute, and transmit the Software, and to prepare derivative works of the
  Software, and to permit third-parties to whom the Software is furnished to
  do so, all subject to the following:

  The copyright notices in the Software and this entire statement, including
  the above license grant, this restriction and the following disclaimer,
  must be included in all copies of the Software, in whole or in part, and
  all derivative works of the Software, unless such copies or derivative
  works are solely in the form of machine-executable object code generated by
  a source language processor.

  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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.
*/

  using stream_type = ssl::stream<Next_Layer>;

public:

  using native_handle_type = typename stream_type::native_handle_type;
  using impl_struct = typename stream_type::impl_struct;
  using next_layer_type = typename stream_type::next_layer_type;
  using lowest_layer_type = typename stream_type::lowest_layer_type;
  using executor_type = typename stream_type::executor_type;

  ssl_stream(Next_Layer&& arg, ssl::context& ctx) :
    _ptr {std::make_unique<stream_type>(std::move(arg), ctx)}
  {
  }

  executor_type get_executor() noexcept
  {
    return _ptr->get_executor();
  }

  native_handle_type native_handle()
  {
    return _ptr->native_handle();
  }

  next_layer_type const& next_layer() const
  {
    return _ptr->next_layer();
  }

  next_layer_type& next_layer()
  {
    return _ptr->next_layer();
  }

  lowest_layer_type& lowest_layer()
  {
    return _ptr->lowest_layer();
  }

  lowest_layer_type const& lowest_layer() const
  {
    return _ptr->lowest_layer();
  }

  void set_verify_mode(ssl::verify_mode v)
  {
    _ptr->set_verify_mode(v);
  }

  void set_verify_mode(ssl::verify_mode v, error_code& ec)
  {
    _ptr->set_verify_mode(v, ec);
  }

  void set_verify_depth(int depth)
  {
    _ptr->set_verify_depth(depth);
  }

  void set_verify_depth(int depth, error_code& ec)
  {
    _ptr->set_verify_depth(depth, ec);
  }

  template<typename VerifyCallback>
  void set_verify_callback(VerifyCallback callback)
  {
    _ptr->set_verify_callback(callback);
  }

  template<typename VerifyCallback>
  void set_verify_callback(VerifyCallback callback, error_code& ec)
  {
    _ptr->set_verify_callback(callback, ec);
  }

  void handshake(handshake_type type)
  {
    _ptr->handshake(type);
  }

  void handshake(handshake_type type, error_code& ec)
  {
    _ptr->handshake(type, ec);
  }

  template<typename ConstBufferSequence>
  void handshake(handshake_type type, ConstBufferSequence const& buffers)
  {
    _ptr->handshake(type, buffers);
  }

  template<typename ConstBufferSequence>
  void handshake(handshake_type type, ConstBufferSequence const& buffers, error_code& ec)
  {
    _ptr->handshake(type, buffers, ec);
  }

  template<typename HandshakeHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler, void(error_code))
  async_handshake(handshake_type type, BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler)
  {
    return _ptr->async_handshake(type, BOOST_ASIO_MOVE_CAST(HandshakeHandler)(handler));
  }

  template<typename ConstBufferSequence, typename BufferedHandshakeHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(BufferedHandshakeHandler, void (error_code, std::size_t))
  async_handshake(handshake_type type, ConstBufferSequence const& buffers,
    BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler)
  {
    return _ptr->async_handshake(type, buffers, BOOST_ASIO_MOVE_CAST(BufferedHandshakeHandler)(handler));
  }

  void shutdown()
  {
    _ptr->shutdown();
  }

  void shutdown(error_code& ec)
  {
    _ptr->shutdown(ec);
  }

  template<typename ShutdownHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(ShutdownHandler, void (error_code))
  async_shutdown(BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler)
  {
    return _ptr->async_shutdown(BOOST_ASIO_MOVE_CAST(ShutdownHandler)(handler));
  }

  template<typename ConstBufferSequence>
  std::size_t write_some(ConstBufferSequence const& buffers)
  {
    return _ptr->write_some(buffers);
  }

  template<typename ConstBufferSequence>
  std::size_t write_some(ConstBufferSequence const& buffers, error_code& ec)
  {
    return _ptr->write_some(buffers, ec);
  }

  template<typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void (error_code, std::size_t))
  async_write_some(ConstBufferSequence const& buffers,
    BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  {
    return _ptr->async_write_some(buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  }

  template<typename MutableBufferSequence>
  std::size_t read_some(MutableBufferSequence const& buffers)
  {
    return _ptr->read_some(buffers);
  }

  template<typename MutableBufferSequence>
  std::size_t read_some(MutableBufferSequence const& buffers, error_code& ec)
  {
    return _ptr->read_some(buffers, ec);
  }

  template<typename MutableBufferSequence, typename ReadHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(error_code, std::size_t))
  async_read_some(MutableBufferSequence const& buffers,
    BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  {
    return _ptr->async_read_some(buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  }

private:

  std::unique_ptr<stream_type> _ptr;
}; // class ssl_stream
#endif // OB_BELLE_CONFIG_SSL_ON

} // namespace Detail

namespace Util
{

// Ordered_Map: an insert ordered map
// enables random lookup and insert ordered iterators
// unordered map stores key value pairs
// queue holds insert ordered iterators to each key in the unordered map
template<typename K, typename V>
class Ordered_Map final
{
public:

  // map iterators
  using m_iterator = typename std::unordered_map<K, V>::iterator;
  using m_const_iterator = typename std::unordered_map<K, V>::const_iterator;

  // index iterators
  using i_iterator = typename std::deque<m_iterator>::iterator;
  using i_const_iterator = typename std::deque<m_const_iterator>::const_iterator;


  Ordered_Map()
  {
  }

  Ordered_Map(std::initializer_list<std::pair<K, V>> const& lst)
  {
    for (auto const& [key, val] : lst)
    {
      _it.emplace_back(_map.insert({key, val}).first);
    }
  }

  ~Ordered_Map()
  {
  }

  Ordered_Map& operator()(K const& k, V const& v)
  {
    auto it = _map.insert_or_assign(k, v);

    if (it.second)
    {
      _it.emplace_back(it.first);
    }

    return *this;
  }

  i_iterator operator[](std::size_t index)
  {
    return _it[index];
  }

  i_const_iterator const operator[](std::size_t index) const
  {
    return _it[index];
  }

  V& at(K const& k)
  {
    return _map.at(k);
  }

  V const& at(K const& k) const
  {
    return _map.at(k);
  }

  m_iterator find(K const& k)
  {
    return _map.find(k);
  }

  m_const_iterator find(K const& k) const
  {
    return _map.find(k);
  }

  std::size_t size() const
  {
    return _it.size();
  }

  bool empty() const
  {
    return _it.empty();
  }

  Ordered_Map& clear()
  {
    _it.clear();
    _map.clear();

    return *this;
  }

  Ordered_Map& erase(K const& k)
  {
    auto it = _map.find(k);

    if (it != _map.end())
    {
      for (auto e = _it.begin(); e < _it.end(); ++e)
      {
        if ((*e) == it)
        {
          _it.erase(e);
          break;
        }
      }

      _map.erase(it);
    }

    return *this;
  }

  i_iterator begin()
  {
    return _it.begin();
  }

  i_const_iterator begin() const
  {
    return _it.begin();
  }

  i_const_iterator cbegin() const
  {
    return _it.cbegin();
  }

  i_iterator end()
  {
    return _it.end();
  }

  i_const_iterator end() const
  {
    return _it.end();
  }

  i_const_iterator cend() const
  {
    return _it.cend();
  }

  m_iterator map_begin()
  {
    return _map.begin();
  }

  m_const_iterator map_begin() const
  {
    return _map.begin();
  }

  m_const_iterator map_cbegin() const
  {
    return _map.cbegin();
  }

  m_iterator map_end()
  {
    return _map.end();
  }

  m_const_iterator map_end() const
  {
    return _map.end();
  }

  m_const_iterator map_cend() const
  {
    return _map.cend();
  }

private:

  std::unordered_map<K, V> _map;
  std::deque<m_iterator> _it;
}; // class Ordered_Map

std::unordered_map<std::string, std::string> const mime_types
{
  {"html", "text/html"},
  {"htm", "text/html"},
  {"shtml", "text/html"},
  {"css", "text/css"},
  {"xml", "text/xml"},
  {"gif", "image/gif"},
  {"jpg", "image/jpg"},
  {"jpeg", "image/jpg"},
  {"js", "application/javascript"},
  {"atom", "application/atom+xml"},
  {"rss", "application/rss+xml"},
  {"mml", "text/mathml"},
  {"txt", "text/plain"},
  {"jad", "text/vnd.sun.j2me.app-descriptor"},
  {"wml", "text/vnd.wap.wml"},
  {"htc", "text/x-component"},
  {"png", "image/png"},
  {"tif", "image/tiff"},
  {"tiff", "image/tiff"},
  {"wbmp", "image/vnd.wap.wbmp"},
  {"ico", "image/x-icon"},
  {"jng", "image/x-jng"},
  {"bmp", "image/x-ms-bmp"},
  {"svg", "image/svg+xml"},
  {"svgz", "image/svg+xml"},
  {"webp", "image/webp"},
  {"woff", "application/font-woff"},
  {"jar", "application/java-archive"},
  {"war", "application/java-archive"},
  {"ear", "application/java-archive"},
  {"json", "application/json"},
  {"hqx", "application/mac-binhex40"},
  {"doc", "application/msword"},
  {"pdf", "application/pdf"},
  {"ps", "application/postscript"},
  {"eps", "application/postscript"},
  {"ai", "application/postscript"},
  {"rtf", "application/rtf"},
  {"m3u8", "application/vnd.apple.mpegurl"},
  {"xls", "application/vnd.ms-excel"},
  {"eot", "application/vnd.ms-fontobject"},
  {"ppt", "application/vnd.ms-powerpoint"},
  {"wmlc", "application/vnd.wap.wmlc"},
  {"kml", "application/vnd.google-earth.kml+xml"},
  {"kmz", "application/vnd.google-earth.kmz"},
  {"7z", "application/x-7z-compressed"},
  {"cco", "application/x-cocoa"},
  {"jardiff", "application/x-java-archive-diff"},
  {"jnlp", "application/x-java-jnlp-file"},
  {"run", "application/x-makeself"},
  {"pm", "application/x-perl"},
  {"pl", "application/x-perl"},
  {"pdb", "application/x-pilot"},
  {"prc", "application/x-pilot"},
  {"rar", "application/x-rar-compressed"},
  {"rpm", "application/x-redhat-package-manager"},
  {"sea", "application/x-sea"},
  {"swf", "application/x-shockwave-flash"},
  {"sit", "application/x-stuffit"},
  {"tk", "application/x-tcl"},
  {"tcl", "application/x-tcl"},
  {"crt", "application/x-x509-ca-cert"},
  {"pem", "application/x-x509-ca-cert"},
  {"der", "application/x-x509-ca-cert"},
  {"xpi", "application/x-xpinstall"},
  {"xhtml", "application/xhtml+xml"},
  {"xspf", "application/xspf+xml"},
  {"zip", "application/zip"},
  {"dll", "application/octet-stream"},
  {"exe", "application/octet-stream"},
  {"bin", "application/octet-stream"},
  {"deb", "application/octet-stream"},
  {"dmg", "application/octet-stream"},
  {"img", "application/octet-stream"},
  {"iso", "application/octet-stream"},
  {"msm", "application/octet-stream"},
  {"msp", "application/octet-stream"},
  {"msi", "application/octet-stream"},
  {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
  {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
  {"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
  {"kar", "audio/midi"},
  {"midi", "audio/midi"},
  {"mid", "audio/midi"},
  {"mp3", "audio/mpeg"},
  {"ogg", "audio/ogg"},
  {"m4a", "audio/x-m4a"},
  {"ra", "audio/x-realaudio"},
  {"3gp", "video/3gpp"},
  {"3gpp", "video/3gpp"},
  {"ts", "video/mp2t"},
  {"mp4", "video/mp4"},
  {"mpg", "video/mpeg"},
  {"mpeg", "video/mpeg"},
  {"mov", "video/quicktime"},
  {"webm", "video/webm"},
  {"flv", "video/x-flv"},
  {"m4v", "video/x-m4v"},
  {"mng", "video/x-mng"},
  {"asf", "video/x-ms-asf"},
  {"asx", "video/x-ms-asf"},
  {"wmv", "video/x-ms-wmv"},
  {"avi", "video/x-msvideo"},
};

// prototypes
inline std::string mime_type(fs::path const& path);
inline std::string url_encode(std::string const& str);
inline std::string url_decode(std::string const& str);
#ifdef OB_BELLE_CONFIG_ZLIB_ON
inline std::string zlib_encode(std::string const& val);
inline std::string zlib_decode(std::string const& val);
inline std::string gzip_encode(std::string const& val);
inline std::string gzip_decode(std::string const& val);
#endif // OB_BELLE_CONFIG_ZLIB_ON

// find the mime type of a string path
inline std::string mime_type(fs::path const& path)
{
  if (std::string ext = path.extension(); ext.size())
  {
    auto const str = Detail::lowercase(ext);

    if (mime_types.find(str) != mime_types.end())
    {
      return mime_types.at(str);
    }
  }

  return "application/octet-stream";
}

inline std::string url_encode(std::string const& str)
{
  std::string res;
  res.reserve(str.size());

  for (auto const& e : str)
  {
    if (e == ' ')
    {
      res += "+";
    }
    else if (std::isalnum(static_cast<unsigned char>(e)) ||
      e == '-' || e == '_' || e == '.' || e == '~')
    {
      res += e;
    }
    else
    {
      res += "%" + Detail::hex_encode(e);
    }
  }

  return res;
}

inline std::string url_decode(std::string const& str)
{
  std::string res;
  res.reserve(str.size());

  for (std::size_t i = 0; i < str.size(); ++i)
  {
    if (str[i] == '+')
    {
      res += " ";
    }
    else if (str[i] == '%' && i + 2 < str.size() &&
      std::isxdigit(static_cast<unsigned char>(str[i + 1])) &&
      std::isxdigit(static_cast<unsigned char>(str[i + 2])))
    {
      res += Detail::hex_decode(str.substr(i + 1, 2));
      i += 2;
    }
    else
    {
      res += str[i];
    }
  }

  return res;
}

#ifdef OB_BELLE_CONFIG_ZLIB_ON
inline std::string zlib_encode(std::string const& str)
{
  boost::iostreams::array_source src {str.data(), str.size()};
  boost::iostreams::filtering_istream is;

  is.push(boost::iostreams::zlib_compressor(MAX_WBITS));
  is.push(src);

  std::stringstream body;
  boost::iostreams::copy(is, body);

  return body.str();
}

inline std::string zlib_decode(std::string const& str)
{
  boost::iostreams::array_source src {str.data(), str.size()};
  boost::iostreams::filtering_istream is;

  is.push(boost::iostreams::zlib_decompressor(MAX_WBITS));
  is.push(src);

  std::stringstream body;
  boost::iostreams::copy(is, body);

  return body.str();
}

inline std::string gzip_encode(std::string const& str)
{
  boost::iostreams::array_source src {str.data(), str.size()};
  boost::iostreams::filtering_istream is;

  is.push(boost::iostreams::gzip_compressor());
  is.push(src);

  std::stringstream body;
  boost::iostreams::copy(is, body);

  return body.str();
}

inline std::string gzip_decode(std::string const& str)
{
  boost::iostreams::array_source src {str.data(), str.size()};
  boost::iostreams::filtering_istream is;

  is.push(boost::iostreams::gzip_decompressor());
  is.push(src);

  std::stringstream body;
  boost::iostreams::copy(is, body);

  return body.str();
}
#endif // OB_BELLE_CONFIG_ZLIB_ON

} // namespace Util

class Request final : public http::request<http::string_body>
{
  using Base = http::request<http::string_body>;

public:

  using Path = std::vector<std::string>;
  using Params = std::unordered_multimap<std::string, std::string>;

  // inherit base constructors
  using http::request<http::string_body>::message;

  // default constructor
  Request() = default;

  // copy constructor
  Request(Request const&) = default;

  // move constructor
  Request(Request&&) = default;

  // copy assignment
  Request& operator=(Request const&) = default;

  // move assignment
  Request& operator=(Request&&) = default;

  // default deconstructor
  ~Request() = default;

  Request&& move() noexcept
  {
    return std::move(*this);
  }

  // get the path
  Path& path()
  {
    return _path;
  }

  // get the query parameters
  Params& params()
  {
    return _params;
  }

  // serialize path and query parameters to the target
  void params_serialize()
  {
    std::string path {target().to_string()};

    _path.clear();
    _path.emplace_back(path);

    if (! _params.empty())
    {
      path += "?";
      auto it = _params.begin();
      for (; it != _params.end(); ++it)
      {
        path += Util::url_encode(it->first) + "=" + Util::url_encode(it->second) + "&";
      }
      path.pop_back();
    }

    target(path);
  }

  // parse the query parameters from the target
  void params_parse()
  {
    std::string path {target().to_string()};

    // separate the query params
    auto params = Detail::split(path, "?", 1);

    // set params
    if (params.size() == 2)
    {
      auto kv = Detail::split(params.at(1), "&");

      for (auto const& e : kv)
      {
        if (e.empty())
        {
          continue;
        }

        auto k_v = Detail::split(e, "=", 1);

        if (k_v.size() == 1)
        {
          _params.emplace(Util::url_decode(e), "");
        }

        else if (k_v.size() == 2)
        {
          _params.emplace(Util::url_decode(k_v.at(0)), Util::url_decode(k_v.at(1)));
        }

        continue;
      }
    }
  }

private:

  Path _path {};
  Params _params {};
}; // Request

#ifdef OB_BELLE_CONFIG_CLIENT_ON
namespace Client
{

class Http final
{
public:

  struct Status
  {
    enum
    {
      closed = 0,
      closing,
      resolving,
      connecting,
      handshaking,
      error,
      open,
      reading,
      writing,
    };
  };

  struct Session_Ctx
  {
    // http request
    Request req {};

    // http response
    http::response<http::string_body> res {};
  }; // struct Session_Ctx

  struct Error_Ctx
  {
    // error code
    error_code const& ec;
  }; // struct Error_Ctx

  // callbacks
  using fn_on_open = std::function<void(Session_Ctx&)>;
  using fn_on_read = std::function<void(Session_Ctx&)>;
  using fn_on_write = std::function<void(Session_Ctx&)>;
  using fn_on_close = std::function<void(Session_Ctx&)>;
  using fn_on_error = std::function<void(Error_Ctx&)>;

  struct Attr
  {
#ifdef OB_BELLE_CONFIG_SSL_ON
    // use ssl
    bool ssl {false};

    // ssl context
    ssl::context ssl_context {ssl::context::tlsv12_client};
#endif // OB_BELLE_CONFIG_SSL_ON

    // socket status
    int status {Status::closed};

    // socket timeout
    std::chrono::seconds timeout {10};

    // address to connect to
    std::string address {"127.0.0.1"};

    // port to connect to
    unsigned short port {8080};

    // callbacks
    fn_on_open on_open {};
    fn_on_read on_read {};
    fn_on_write on_write {};
    fn_on_close on_close {};
    fn_on_error on_error {};
  }; // struct Attr

  struct Session_Type
  {
    virtual ~Session_Type() = default;
    virtual void read() = 0;
    virtual void write(Request&&) = 0;
    virtual void close() = 0;
    virtual void error(error_code const&) = 0;
  }; // struct Session_Type

  template<typename Derived>
  class Session_Base : public Session_Type
  {
    Derived& derived()
    {
      return static_cast<Derived&>(*this);
    }

  public:

    Session_Base(net::io_context& io_, std::shared_ptr<Attr> const attr_) :
      _resolver {io_},
      _strand {io_.get_executor()},
      _timer {io_, (std::chrono::steady_clock::time_point::max)()},
      _attr {attr_}
    {
    }

    ~Session_Base()
    {
      _attr->status = Status::closed;
    }

    void cancel_timer()
    {
      // set the timer to expire immediately
      _timer.expires_at((std::chrono::steady_clock::time_point::min)());
    }

    void do_timer()
    {
      _timer.cancel();
      _timer.expires_after(_attr->timeout);

      // wait on the timer
      _timer.async_wait(
        net::bind_executor(_strand,
          [self = derived().shared_from_this()](error_code ec)
          {
            self->on_timer(ec);
          }
        )
      );
    }

    void on_timer(error_code ec_)
    {
      if (ec_ && ec_ != net::error::operation_aborted)
      {
        on_error(ec_);

        return;
      }

      // check if socket has been closed
      if (_timer.expires_at() == (std::chrono::steady_clock::time_point::min)())
      {
        return;
      }

      // check expiry
      if (_timer.expiry() <= std::chrono::steady_clock::now())
      {
        derived().do_close();

        return;
      }

      if (_close)
      {
        return;
      }
    }

    void do_resolve()
    {
      _attr->status = Status::resolving;

      // domain name server lookup
      _resolver.async_resolve(_attr->address,
        Detail::to_string(_attr->port),
        net::bind_executor(_strand,
          [self = derived().shared_from_this()]
          (error_code ec, tcp::resolver::results_type results)
          {
            self->on_resolve(ec, results);
          }
        )
      );
    }

    void on_resolve(error_code ec_, tcp::resolver::results_type results_)
    {
      if (ec_)
      {
        on_error(ec_);

        return;
      }

      _attr->status = Status::connecting;

      // connect to the endpoint
      net::async_connect(derived().socket().lowest_layer(),
        results_.begin(), results_.end(),
        net::bind_executor(_strand,
          [self = derived().shared_from_this()](error_code ec, auto)
          {
            self->derived().on_connect(ec);
          }
        )
      );
    }

    void prepare_req()
    {
      // serialize target and params
      _ctx.req.params_serialize();

      // set default user-agent header value if not present
      if (_ctx.req.find(Header::user_agent) == _ctx.req.end())
      {
        _ctx.req.set(Header::user_agent, "Belle");
      }

      // set default host header value if not present
      if (_ctx.req.find(Header::host) == _ctx.req.end())
      {
        _ctx.req.set(Header::host, _attr->address);
      }

#ifdef OB_BELLE_CONFIG_ZLIB_ON
      // set default accept-encoding header value if not present
      if (_ctx.req.find(Header::accept_encoding) == _ctx.req.end())
      {
        _ctx.req.set(Header::accept_encoding, "gzip, deflate");
      }
#endif // OB_BELLE_CONFIG_ZLIB_ON

      // prepare the payload
      _ctx.req.prepare_payload();
    }

    void do_write()
    {
      prepare_req();
      do_timer();

      _attr->status = Status::writing;

      // Send the HTTP request
      http::async_write(derived().socket(), _ctx.req,
        net::bind_executor(_strand,
          [self = derived().shared_from_this()](error_code ec, std::size_t bytes)
          {
            self->on_write(ec, bytes);
          }
        )
      );
    }

    void on_write(error_code ec_, std::size_t bytes_)
    {
      boost::ignore_unused(bytes_);

      _attr->status = Status::open;

      if (ec_)
      {
        on_error(ec_);

        return;
      }

      if (_attr->on_write)
      {
        try
        {
          // run user function
          _attr->on_write(_ctx);
        }
        catch (...)
        {
          on_error();
        }
      }
    }

    void do_read()
    {
      // clear the HTTP response
      _ctx.res = {};
      do_timer();

      _attr->status = Status::reading;

      // Receive the HTTP response
      http::async_read(derived().socket(), _buf, _ctx.res,
        net::bind_executor(_strand,
          [self = derived().shared_from_this()](error_code ec, std::size_t bytes)
          {
            self->on_read(ec, bytes);
          }
        )
      );
    }

    void on_read(error_code ec_, std::size_t bytes_)
    {
      boost::ignore_unused(bytes_);

      _attr->status = Status::open;

      if (ec_)
      {
        on_error(ec_);

        return;
      }

      if (_attr->on_read)
      {
        try
        {
#ifdef OB_BELLE_CONFIG_ZLIB_ON
          if (_ctx.res.body().size() && _ctx.res["content-encoding"] != "" &&
            _ctx.res["content-encoding"] != "identity")
          {
            if (_ctx.res["content-encoding"] == "gzip")
            {
              _ctx.res.body() = OB::Belle::Util::gzip_decode(_ctx.res.body());
            }
            else if (_ctx.res["content-encoding"] == "deflate")
            {
              _ctx.res.body() = OB::Belle::Util::zlib_decode(_ctx.res.body());
            }
          }
#endif // OB_BELLE_CONFIG_ZLIB_ON

          // run user function
          _attr->on_read(_ctx);
        }
        catch (...)
        {
          on_error();
        }
      }
    }

    void on_error(error_code const& ec_ = {})
    {
      _attr->status = Status::error;

      if (_attr->on_error)
      {
        try
        {
          // run user function
          Error_Ctx err {ec_};
          _attr->on_error(err);
        }
        catch (...)
        {
        }
      }
    }

    void read()
    {
      do_read();
    }

    void write(Request&& req_)
    {
      _ctx.req = std::move(req_);
      do_write();
    }

    void close()
    {
      derived().do_close();
    }

    void error(error_code const& ec_)
    {
      on_error(ec_);
    }

    tcp::resolver _resolver;
    net::strand<net::io_context::executor_type> _strand;
    net::steady_timer _timer;
    std::shared_ptr<Attr> const _attr;
    Session_Ctx _ctx {};
    beast::flat_buffer _buf {};
    bool _close {false};
  }; // class Session_Base

  class Session final :
    public Session_Base<Session>,
    public std::enable_shared_from_this<Session>
  {
  public:
    Session(net::io_context& io_, std::shared_ptr<Attr> attr_) :
      Session_Base<Session>(io_, attr_),
      _socket {io_}
    {
    }

    ~Session()
    {
    }

    tcp::socket& socket()
    {
      return _socket;
    }

    tcp::socket&& socket_move()
    {
      return std::move(_socket);
    }

    void run()
    {
      do_timer();
      do_resolve();
    }

    void on_connect(error_code ec_)
    {
      if (ec_)
      {
        on_error(ec_);

        return;
      }

      _attr->status = Status::open;

      if (_attr->on_open)
      {
        try
        {
          // run user function
          _attr->on_open(_ctx);
        }
        catch (...)
        {
          on_error();
        }
      }
    }

    void do_close()
    {
      _attr->status = Status::closing;

      cancel_timer();

      error_code ec;

      // shutdown the socket
      _socket.shutdown(tcp::socket::shutdown_both, ec);
      _socket.close(ec);

      _attr->status = Status::closed;

      // ignore not_connected error
      if (ec && ec != boost::system::errc::not_connected)
      {
        on_error(ec);

        return;
      }

      if (_attr->on_close)
      {
        try
        {
          // run user function
          _attr->on_close(_ctx);
        }
        catch (...)
        {
          on_error();
        }
      }

      // the connection is now closed
    }

  private:

    tcp::socket _socket;
  }; // class Session

#ifdef OB_BELLE_CONFIG_SSL_ON
  class Session_Secure final :
    public Session_Base<Session_Secure>,
    public std::enable_shared_from_this<Session_Secure>
  {
  public:
    Session_Secure(net::io_context& io_, std::shared_ptr<Attr> attr_) :
      Session_Base<Session_Secure>(io_, attr_),
      _socket {tcp::socket(io_), attr_->ssl_context}
    {
      _close = true;
    }

    ~Session_Secure()
    {
    }

    Detail::ssl_stream<tcp::socket>& socket()
    {
      return _socket;
    }

    Detail::ssl_stream<tcp::socket>&& socket_move()
    {
      return std::move(_socket);
    }

    void run()
    {
      // start the timer
      do_timer();

      // set server name indication
      // use SSL_ctrl instead of SSL_set_tlsext_host_name macro
      // to avoid old style C cast to char*
      // if (! SSL_set_tlsext_host_name(_socket.native_handle(), _attr->address.data()))
      if (! SSL_ctrl(_socket.native_handle(), SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, _attr->address.data()))
      {
        error_code ec
        {
          static_cast<int>(ERR_get_error()),
          net::error::get_ssl_category()
        };

        on_error(ec);

        return;
      }

      do_resolve();
    }

    void on_connect(error_code ec_)
    {
      if (ec_)
      {
        on_error(ec_);

        return;
      }

      do_handshake();
    }

    void do_handshake()
    {
      _attr->status = Status::handshaking;

      // perform the ssl handshake
      _socket.async_handshake(ssl::stream_base::client,
        net::bind_executor(_strand,
          [self = shared_from_this()](error_code ec)
          {
            self->on_handshake(ec);
          }
        )
      );
    }

    void on_handshake(error_code ec_)
    {
      if (ec_)
      {
        on_error(ec_);

        return;
      }

      _close = false;

      _attr->status = Status::open;

      if (_attr->on_open)
      {
        try
        {
          // run user function
          _attr->on_open(_ctx);
        }
        catch (...)
        {
          on_error();
        }
      }
    }

    void do_close()
    {
      if (_close)
      {
        return;
      }

      _attr->status = Status::closing;

      _close = true;

      // shutdown the socket
      _socket.async_shutdown(
        net::bind_executor(_strand,
          [self = shared_from_this()](error_code ec)
          {
            self->on_shutdown(ec);
          }
        )
      );
    }

    void on_shutdown(error_code ec_)
    {
      cancel_timer();

      // ignore errors
      ec_.assign(0, ec_.category());

      // close the socket
      _socket.next_layer().close(ec_);
      _attr->status = Status::closed;

      if (_attr->on_close)
      {
        try
        {
          // run user function
          _attr->on_close(_ctx);
        }
        catch (...)
        {
          on_error();
        }
      }

      // the connection is now closed
    }

  private:

    Detail::ssl_stream<tcp::socket> _socket;
  }; // class Session_Secure
#endif // OB_BELLE_CONFIG_SSL_ON

  // default constructor
  Http(net::io_context& io_) :
    _io {io_}
  {
  }

  Http(net::io_context& io_, std::string const& url_) :
    _io {io_}
  {
    url(url_);
  }

  // constructor with address and port
  Http(net::io_context& io_, std::string address_, unsigned short port_) :
    _io {io_}
  {
    _attr->address = address_;
    _attr->port = port_;
  }

#ifdef OB_BELLE_CONFIG_SSL_ON
  // constructor with address, port, and ssl
  Http(net::io_context& io_, std::string address_, unsigned short port_, bool ssl_) :
    _io {io_}
  {
    _attr->address = address_;
    _attr->port = port_;
    _attr->ssl = ssl_;
  }
#endif // OB_BELLE_CONFIG_SSL_ON

  // destructor
  ~Http()
  {
  }

  Http& url(std::string const& url_)
  {
    std::smatch rx_match;
    auto const rx_opts {std::regex::ECMAScript | std::regex::icase};

    std::regex const rx_str {"^(http|https)://(.+?)(?::(\\d*)|)$", rx_opts};
    auto const rx_flgs {std::regex_constants::match_not_null};

    if (std::regex_match(url_, rx_match, rx_str, rx_flgs))
    {
      _attr->address = rx_match[2].str();
      _attr->port = static_cast<unsigned short>(rx_match[3].str().size() ? std::stoi(rx_match[3].str()) : (rx_match[1].str().size() == 5 ? 443 : 80));
#ifdef OB_BELLE_CONFIG_SSL_ON
      _attr->ssl = rx_match[1].str().size() == 5 ? true : false;
#endif // OB_BELLE_CONFIG_SSL_ON
    }

    return *this;
  }

  // set the address to connect to
  Http& address(std::string address_)
  {
    _attr->address = address_;

    return *this;
  }

  // get the address to connect to
  std::string address()
  {
    return _attr->address;
  }

  // set the port to connect to
  Http& port(unsigned short port_)
  {
    _attr->port = port_;

    return *this;
  }

  // get the port to connect to
  unsigned short port()
  {
    return _attr->port;
  }

  // set the socket timeout
  Http& timeout(std::chrono::seconds timeout_)
  {
    _attr->timeout = timeout_;

    return *this;
  }

  // get the socket timeout
  std::chrono::seconds timeout()
  {
    return _attr->timeout;
  }

  // get the io_context
  net::io_context& io()
  {
    return _io;
  }

#ifdef OB_BELLE_CONFIG_SSL_ON
  // set ssl
  Http& ssl(bool ssl_)
  {
    _attr->ssl = ssl_;

    return *this;
  }

  // get ssl
  bool ssl()
  {
    return _attr->ssl;
  }

  // get the ssl context
  ssl::context& ssl_context()
  {
    return _attr->ssl_context;
  }

  // set the ssl context
  Http& ssl_context(ssl::context&& ctx_)
  {
    _attr->ssl_context = std::move(ctx_);

    return *this;
  }
#endif // OB_BELLE_CONFIG_SSL_ON

  Http& on_open(fn_on_open on_open_)
  {
    _attr->on_open = on_open_;

    return *this;
  }

  Http& on_read(fn_on_read on_read_)
  {
    _attr->on_read = on_read_;

    return *this;
  }

  Http& on_write(fn_on_write on_write_)
  {
    _attr->on_write = on_write_;

    return *this;
  }

  Http& on_close(fn_on_close on_close_)
  {
    _attr->on_close = on_close_;

    return *this;
  }

  Http& on_error(fn_on_error on_error_)
  {
    _attr->on_error = on_error_;

    return *this;
  }

  Http& run()
  {
#ifdef OB_BELLE_CONFIG_SSL_ON
    if (_attr->ssl)
    {
      // use secure
      auto tmp = std::make_shared<Session_Secure>(_io, _attr);
      tmp->run();
      _session = tmp.get();
    }
    else
#endif // OB_BELLE_CONFIG_SSL_ON
    {
      // use plain
      auto tmp = std::make_shared<Session>(_io, _attr);
      tmp->run();
      _session = tmp.get();
    }

    return *this;
  }

  int status()
  {
    return _attr->status;
  }

  std::string const& status_string()
  {
    return _status_string.at(static_cast<std::size_t>(_attr->status));
  }

  bool read()
  {
    if (_session)
    {
      _session->read();
    }

    return _session;
  }

  bool write(Request req_)
  {
    if (_session)
    {
      _session->write(std::move(req_));
    }

    return _session;
  }

  bool close()
  {
    if (_session)
    {
      _session->close();
    }

    return _session;
  }

  bool error(error_code const& ec_ = {})
  {
    if (_session)
    {
      _session->error(ec_);
    }

    return _session;
  }

private:

  std::vector<std::string> const _status_string {
    "closed",
    "closing",
    "resolving",
    "connecting",
    "handshaking",
    "error",
    "open",
    "reading",
    "writing",
  };

  // the io context
  net::io_context& _io;

  // hold the client attributes
  std::shared_ptr<Attr> const _attr {std::make_shared<Attr>()};

  // the session
  Session_Type* _session {nullptr};
}; // class Http

} // namespace Client
#endif // OB_BELLE_CONFIG_CLIENT_ON

#ifdef OB_BELLE_CONFIG_SIGNAL_ON
class Signal final
{
public:

  using fn_on_signal = std::function<void(error_code const&, int)>;

  Signal(net::io_context& io_) noexcept :
    _io {io_}
  {
  }

  void wait()
  {
    _set.async_wait(_callback);
  }

  Signal& on_signal(int const sig_, fn_on_signal const fn_)
  {
    _set.add(sig_);
    _event[sig_] = fn_;

    return *this;
  }

  Signal& on_signal(std::vector<int> const sig_, fn_on_signal const fn_)
  {
    for (auto const& e : sig_)
    {
      _set.add(e);
      _event[e] = fn_;
    }

    return *this;
  }

  static std::string str(int sig_)
  {
    std::string res;

    if (_str.find(sig_) != _str.end())
    {
      res = _str.at(sig_);
    }

    return res;
  }

private:

  std::function<void(Belle::error_code, int)> const _callback = [&](auto ec_, auto sig_) {
    if (_event.find(sig_) != _event.end())
    {
      _event.at(sig_)(ec_, sig_);
    }
  };

  net::io_context& _io;

  Belle::net::signal_set _set {_io};

  std::unordered_map<int, fn_on_signal> _event;

  static inline std::unordered_map<int, std::string> const _str {
    {SIGHUP, "SIGHUP"},
    {SIGINT, "SIGINT"},
    {SIGQUIT, "SIGQUIT"},
    {SIGILL, "SIGILL"},
    {SIGTRAP, "SIGTRAP"},
    {SIGABRT, "SIGABRT"},
    {SIGBUS, "SIGBUS"},
    {SIGFPE, "SIGFPE"},
    {SIGKILL, "SIGKILL"},
    {SIGUSR1, "SIGUSR1"},
    {SIGSEGV, "SIGSEGV"},
    {SIGUSR2, "SIGUSR2"},
    {SIGPIPE, "SIGPIPE"},
    {SIGALRM, "SIGALRM"},
    {SIGTERM, "SIGTERM"},
    {SIGSTKFLT, "SIGSTKFLT"},
    {SIGCHLD, "SIGCHLD"},
    {SIGCONT, "SIGCONT"},
    {SIGSTOP, "SIGSTOP"},
    {SIGTSTP, "SIGTSTP"},
    {SIGTTIN, "SIGTTIN"},
    {SIGTTOU, "SIGTTOU"},
    {SIGURG, "SIGURG"},
    {SIGXCPU, "SIGXCPU"},
    {SIGXFSZ, "SIGXFSZ"},
    {SIGVTALRM, "SIGVTALRM"},
    {SIGPROF, "SIGPROF"},
    {SIGWINCH, "SIGWINCH"},
    {SIGPOLL, "SIGPOLL"},
    {SIGPWR, "SIGPWR"},
    {SIGSYS, "SIGSYS"},
  };
}; // class Signal
#endif // OB_BELLE_CONFIG_SIGNAL_ON

} // namespace OB::Belle

#endif // OB_BELLE_HH
Back to Top