/usr/include/websocketpp/impl/connection_impl.hpp is in libwebsocketpp-dev 0.7.0-11.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 | /*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WEBSOCKETPP_CONNECTION_IMPL_HPP
#define WEBSOCKETPP_CONNECTION_IMPL_HPP
#include <websocketpp/processors/hybi00.hpp>
#include <websocketpp/processors/hybi07.hpp>
#include <websocketpp/processors/hybi08.hpp>
#include <websocketpp/processors/hybi13.hpp>
#include <websocketpp/processors/processor.hpp>
#include <websocketpp/common/platforms.hpp>
#include <websocketpp/common/system_error.hpp>
#include <algorithm>
#include <exception>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
namespace websocketpp {
namespace istate = session::internal_state;
template <typename config>
void connection<config>::set_termination_handler(
termination_handler new_handler)
{
m_alog.write(log::alevel::devel,
"connection set_termination_handler");
//scoped_lock_type lock(m_connection_state_lock);
m_termination_handler = new_handler;
}
template <typename config>
std::string const & connection<config>::get_origin() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_processor->get_origin(m_request);
}
template <typename config>
size_t connection<config>::get_buffered_amount() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_send_buffer_size;
}
template <typename config>
session::state::value connection<config>::get_state() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_state;
}
template <typename config>
lib::error_code connection<config>::send(std::string const & payload,
frame::opcode::value op)
{
message_ptr msg = m_msg_manager->get_message(op,payload.size());
msg->append_payload(payload);
msg->set_compressed(true);
return send(msg);
}
template <typename config>
lib::error_code connection<config>::send(void const * payload, size_t len,
frame::opcode::value op)
{
message_ptr msg = m_msg_manager->get_message(op,len);
msg->append_payload(payload,len);
return send(msg);
}
template <typename config>
lib::error_code connection<config>::send(typename config::message_type::ptr msg)
{
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection send");
}
{
scoped_lock_type lock(m_connection_state_lock);
if (m_state != session::state::open) {
return error::make_error_code(error::invalid_state);
}
}
message_ptr outgoing_msg;
bool needs_writing = false;
if (msg->get_prepared()) {
outgoing_msg = msg;
scoped_lock_type lock(m_write_lock);
write_push(outgoing_msg);
needs_writing = !m_write_flag && !m_send_queue.empty();
} else {
outgoing_msg = m_msg_manager->get_message();
if (!outgoing_msg) {
return error::make_error_code(error::no_outgoing_buffers);
}
scoped_lock_type lock(m_write_lock);
lib::error_code ec = m_processor->prepare_data_frame(msg,outgoing_msg);
if (ec) {
return ec;
}
write_push(outgoing_msg);
needs_writing = !m_write_flag && !m_send_queue.empty();
}
if (needs_writing) {
transport_con_type::dispatch(lib::bind(
&type::write_frame,
type::get_shared()
));
}
return lib::error_code();
}
template <typename config>
void connection<config>::ping(std::string const& payload, lib::error_code& ec) {
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection ping");
}
{
scoped_lock_type lock(m_connection_state_lock);
if (m_state != session::state::open) {
std::stringstream ss;
ss << "connection::ping called from invalid state " << m_state;
m_alog.write(log::alevel::devel,ss.str());
ec = error::make_error_code(error::invalid_state);
return;
}
}
message_ptr msg = m_msg_manager->get_message();
if (!msg) {
ec = error::make_error_code(error::no_outgoing_buffers);
return;
}
ec = m_processor->prepare_ping(payload,msg);
if (ec) {return;}
// set ping timer if we are listening for one
if (m_pong_timeout_handler) {
// Cancel any existing timers
if (m_ping_timer) {
m_ping_timer->cancel();
}
if (m_pong_timeout_dur > 0) {
m_ping_timer = transport_con_type::set_timer(
m_pong_timeout_dur,
lib::bind(
&type::handle_pong_timeout,
type::get_shared(),
payload,
lib::placeholders::_1
)
);
}
if (!m_ping_timer) {
// Our transport doesn't support timers
m_elog.write(log::elevel::warn,"Warning: a pong_timeout_handler is \
set but the transport in use does not support timeouts.");
}
}
bool needs_writing = false;
{
scoped_lock_type lock(m_write_lock);
write_push(msg);
needs_writing = !m_write_flag && !m_send_queue.empty();
}
if (needs_writing) {
transport_con_type::dispatch(lib::bind(
&type::write_frame,
type::get_shared()
));
}
ec = lib::error_code();
}
template<typename config>
void connection<config>::ping(std::string const & payload) {
lib::error_code ec;
ping(payload,ec);
if (ec) {
throw exception(ec);
}
}
template<typename config>
void connection<config>::handle_pong_timeout(std::string payload,
lib::error_code const & ec)
{
if (ec) {
if (ec == transport::error::operation_aborted) {
// ignore, this is expected
return;
}
m_elog.write(log::elevel::devel,"pong_timeout error: "+ec.message());
return;
}
if (m_pong_timeout_handler) {
m_pong_timeout_handler(m_connection_hdl,payload);
}
}
template <typename config>
void connection<config>::pong(std::string const& payload, lib::error_code& ec) {
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection pong");
}
{
scoped_lock_type lock(m_connection_state_lock);
if (m_state != session::state::open) {
std::stringstream ss;
ss << "connection::pong called from invalid state " << m_state;
m_alog.write(log::alevel::devel,ss.str());
ec = error::make_error_code(error::invalid_state);
return;
}
}
message_ptr msg = m_msg_manager->get_message();
if (!msg) {
ec = error::make_error_code(error::no_outgoing_buffers);
return;
}
ec = m_processor->prepare_pong(payload,msg);
if (ec) {return;}
bool needs_writing = false;
{
scoped_lock_type lock(m_write_lock);
write_push(msg);
needs_writing = !m_write_flag && !m_send_queue.empty();
}
if (needs_writing) {
transport_con_type::dispatch(lib::bind(
&type::write_frame,
type::get_shared()
));
}
ec = lib::error_code();
}
template<typename config>
void connection<config>::pong(std::string const & payload) {
lib::error_code ec;
pong(payload,ec);
if (ec) {
throw exception(ec);
}
}
template <typename config>
void connection<config>::close(close::status::value const code,
std::string const & reason, lib::error_code & ec)
{
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection close");
}
// Truncate reason to maximum size allowable in a close frame.
std::string tr(reason,0,std::min<size_t>(reason.size(),
frame::limits::close_reason_size));
scoped_lock_type lock(m_connection_state_lock);
if (m_state != session::state::open) {
ec = error::make_error_code(error::invalid_state);
return;
}
ec = this->send_close_frame(code,tr,false,close::status::terminal(code));
}
template<typename config>
void connection<config>::close(close::status::value const code,
std::string const & reason)
{
lib::error_code ec;
close(code,reason,ec);
if (ec) {
throw exception(ec);
}
}
/// Trigger the on_interrupt handler
/**
* This is thread safe if the transport is thread safe
*/
template <typename config>
lib::error_code connection<config>::interrupt() {
m_alog.write(log::alevel::devel,"connection connection::interrupt");
return transport_con_type::interrupt(
lib::bind(
&type::handle_interrupt,
type::get_shared()
)
);
}
template <typename config>
void connection<config>::handle_interrupt() {
if (m_interrupt_handler) {
m_interrupt_handler(m_connection_hdl);
}
}
template <typename config>
lib::error_code connection<config>::pause_reading() {
m_alog.write(log::alevel::devel,"connection connection::pause_reading");
return transport_con_type::dispatch(
lib::bind(
&type::handle_pause_reading,
type::get_shared()
)
);
}
/// Pause reading handler. Not safe to call directly
template <typename config>
void connection<config>::handle_pause_reading() {
m_alog.write(log::alevel::devel,"connection connection::handle_pause_reading");
m_read_flag = false;
}
template <typename config>
lib::error_code connection<config>::resume_reading() {
m_alog.write(log::alevel::devel,"connection connection::resume_reading");
return transport_con_type::dispatch(
lib::bind(
&type::handle_resume_reading,
type::get_shared()
)
);
}
/// Resume reading helper method. Not safe to call directly
template <typename config>
void connection<config>::handle_resume_reading() {
m_read_flag = true;
read_frame();
}
template <typename config>
bool connection<config>::get_secure() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_uri->get_secure();
}
template <typename config>
std::string const & connection<config>::get_host() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_uri->get_host();
}
template <typename config>
std::string const & connection<config>::get_resource() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_uri->get_resource();
}
template <typename config>
uint16_t connection<config>::get_port() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_uri->get_port();
}
template <typename config>
uri_ptr connection<config>::get_uri() const {
//scoped_lock_type lock(m_connection_state_lock);
return m_uri;
}
template <typename config>
void connection<config>::set_uri(uri_ptr uri) {
//scoped_lock_type lock(m_connection_state_lock);
m_uri = uri;
}
template <typename config>
std::string const & connection<config>::get_subprotocol() const {
return m_subprotocol;
}
template <typename config>
std::vector<std::string> const &
connection<config>::get_requested_subprotocols() const {
return m_requested_subprotocols;
}
template <typename config>
void connection<config>::add_subprotocol(std::string const & value,
lib::error_code & ec)
{
if (m_is_server) {
ec = error::make_error_code(error::client_only);
return;
}
// If the value is empty or has a non-RFC2616 token character it is invalid.
if (value.empty() || std::find_if(value.begin(),value.end(),
http::is_not_token_char) != value.end())
{
ec = error::make_error_code(error::invalid_subprotocol);
return;
}
m_requested_subprotocols.push_back(value);
}
template <typename config>
void connection<config>::add_subprotocol(std::string const & value) {
lib::error_code ec;
this->add_subprotocol(value,ec);
if (ec) {
throw exception(ec);
}
}
template <typename config>
void connection<config>::select_subprotocol(std::string const & value,
lib::error_code & ec)
{
if (!m_is_server) {
ec = error::make_error_code(error::server_only);
return;
}
if (value.empty()) {
ec = lib::error_code();
return;
}
std::vector<std::string>::iterator it;
it = std::find(m_requested_subprotocols.begin(),
m_requested_subprotocols.end(),
value);
if (it == m_requested_subprotocols.end()) {
ec = error::make_error_code(error::unrequested_subprotocol);
return;
}
m_subprotocol = value;
}
template <typename config>
void connection<config>::select_subprotocol(std::string const & value) {
lib::error_code ec;
this->select_subprotocol(value,ec);
if (ec) {
throw exception(ec);
}
}
template <typename config>
std::string const &
connection<config>::get_request_header(std::string const & key) const {
return m_request.get_header(key);
}
template <typename config>
std::string const &
connection<config>::get_request_body() const {
return m_request.get_body();
}
template <typename config>
std::string const &
connection<config>::get_response_header(std::string const & key) const {
return m_response.get_header(key);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::set_status(http::status_code::value code)
{
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}
m_response.set_status(code);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::set_status(http::status_code::value code,
std::string const & msg)
{
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}
m_response.set_status(code,msg);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::set_body(std::string const & value) {
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}
m_response.set_body(value);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::append_header(std::string const & key,
std::string const & val)
{
if (m_is_server) {
if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
// we are setting response headers for an incoming server connection
m_response.append_header(key,val);
} else {
throw exception("Call to append_header from invalid state",
error::make_error_code(error::invalid_state));
}
} else {
if (m_internal_state == istate::USER_INIT) {
// we are setting initial headers for an outgoing client connection
m_request.append_header(key,val);
} else {
throw exception("Call to append_header from invalid state",
error::make_error_code(error::invalid_state));
}
}
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::replace_header(std::string const & key,
std::string const & val)
{
if (m_is_server) {
if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
// we are setting response headers for an incoming server connection
m_response.replace_header(key,val);
} else {
throw exception("Call to replace_header from invalid state",
error::make_error_code(error::invalid_state));
}
} else {
if (m_internal_state == istate::USER_INIT) {
// we are setting initial headers for an outgoing client connection
m_request.replace_header(key,val);
} else {
throw exception("Call to replace_header from invalid state",
error::make_error_code(error::invalid_state));
}
}
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::remove_header(std::string const & key)
{
if (m_is_server) {
if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
// we are setting response headers for an incoming server connection
m_response.remove_header(key);
} else {
throw exception("Call to remove_header from invalid state",
error::make_error_code(error::invalid_state));
}
} else {
if (m_internal_state == istate::USER_INIT) {
// we are setting initial headers for an outgoing client connection
m_request.remove_header(key);
} else {
throw exception("Call to remove_header from invalid state",
error::make_error_code(error::invalid_state));
}
}
}
/// Defer HTTP Response until later
/**
* Used in the http handler to defer the HTTP response for this connection
* until later. Handshake timers will be canceled and the connection will be
* left open until `send_http_response` or an equivalent is called.
*
* Warning: deferred connections won't time out and as a result can tie up
* resources.
*
* @return A status code, zero on success, non-zero otherwise
*/
template <typename config>
lib::error_code connection<config>::defer_http_response() {
// Cancel handshake timer, otherwise the connection will time out and we'll
// close the connection before the app has a chance to send a response.
if (m_handshake_timer) {
m_handshake_timer->cancel();
m_handshake_timer.reset();
}
// Do something to signal deferral
m_http_state = session::http_state::deferred;
return lib::error_code();
}
/// Send deferred HTTP Response (exception free)
/**
* Sends an http response to an HTTP connection that was deferred. This will
* send a complete response including all headers, status line, and body
* text. The connection will be closed afterwards.
*
* @since 0.6.0
*
* @param ec A status code, zero on success, non-zero otherwise
*/
template <typename config>
void connection<config>::send_http_response(lib::error_code & ec) {
{
scoped_lock_type lock(m_connection_state_lock);
if (m_http_state != session::http_state::deferred) {
ec = error::make_error_code(error::invalid_state);
return;
}
m_http_state = session::http_state::body_written;
}
this->write_http_response(lib::error_code());
ec = lib::error_code();
}
template <typename config>
void connection<config>::send_http_response() {
lib::error_code ec;
this->send_http_response(ec);
if (ec) {
throw exception(ec);
}
}
/******** logic thread ********/
template <typename config>
void connection<config>::start() {
m_alog.write(log::alevel::devel,"connection start");
if (m_internal_state != istate::USER_INIT) {
m_alog.write(log::alevel::devel,"Start called in invalid state");
this->terminate(error::make_error_code(error::invalid_state));
return;
}
m_internal_state = istate::TRANSPORT_INIT;
// Depending on how the transport implements init this function may return
// immediately and call handle_transport_init later or call
// handle_transport_init from this function.
transport_con_type::init(
lib::bind(
&type::handle_transport_init,
type::get_shared(),
lib::placeholders::_1
)
);
}
template <typename config>
void connection<config>::handle_transport_init(lib::error_code const & ec) {
m_alog.write(log::alevel::devel,"connection handle_transport_init");
lib::error_code ecm = ec;
if (m_internal_state != istate::TRANSPORT_INIT) {
m_alog.write(log::alevel::devel,
"handle_transport_init must be called from transport init state");
ecm = error::make_error_code(error::invalid_state);
}
if (ecm) {
std::stringstream s;
s << "handle_transport_init received error: "<< ecm.message();
m_elog.write(log::elevel::rerror,s.str());
this->terminate(ecm);
return;
}
// At this point the transport is ready to read and write bytes.
if (m_is_server) {
m_internal_state = istate::READ_HTTP_REQUEST;
this->read_handshake(1);
} else {
// We are a client. Set the processor to the version specified in the
// config file and send a handshake request.
m_internal_state = istate::WRITE_HTTP_REQUEST;
m_processor = get_processor(config::client_version);
this->send_http_request();
}
}
template <typename config>
void connection<config>::read_handshake(size_t num_bytes) {
m_alog.write(log::alevel::devel,"connection read_handshake");
if (m_open_handshake_timeout_dur > 0) {
m_handshake_timer = transport_con_type::set_timer(
m_open_handshake_timeout_dur,
lib::bind(
&type::handle_open_handshake_timeout,
type::get_shared(),
lib::placeholders::_1
)
);
}
transport_con_type::async_read_at_least(
num_bytes,
m_buf,
config::connection_read_buffer_size,
lib::bind(
&type::handle_read_handshake,
type::get_shared(),
lib::placeholders::_1,
lib::placeholders::_2
)
);
}
// All exit paths for this function need to call write_http_response() or submit
// a new read request with this function as the handler.
template <typename config>
void connection<config>::handle_read_handshake(lib::error_code const & ec,
size_t bytes_transferred)
{
m_alog.write(log::alevel::devel,"connection handle_read_handshake");
lib::error_code ecm = ec;
if (!ecm) {
scoped_lock_type lock(m_connection_state_lock);
if (m_state == session::state::connecting) {
if (m_internal_state != istate::READ_HTTP_REQUEST) {
ecm = error::make_error_code(error::invalid_state);
}
} else if (m_state == session::state::closed) {
// The connection was canceled while the response was being sent,
// usually by the handshake timer. This is basically expected
// (though hopefully rare) and there is nothing we can do so ignore.
m_alog.write(log::alevel::devel,
"handle_read_handshake invoked after connection was closed");
return;
} else {
ecm = error::make_error_code(error::invalid_state);
}
}
if (ecm) {
if (ecm == transport::error::eof && m_state == session::state::closed) {
// we expect to get eof if the connection is closed already
m_alog.write(log::alevel::devel,
"got (expected) eof/state error from closed con");
return;
}
log_err(log::elevel::rerror,"handle_read_handshake",ecm);
this->terminate(ecm);
return;
}
// Boundaries checking. TODO: How much of this should be done?
if (bytes_transferred > config::connection_read_buffer_size) {
m_elog.write(log::elevel::fatal,"Fatal boundaries checking error.");
this->terminate(make_error_code(error::general));
return;
}
size_t bytes_processed = 0;
try {
bytes_processed = m_request.consume(m_buf,bytes_transferred);
} catch (http::exception &e) {
// All HTTP exceptions will result in this request failing and an error
// response being returned. No more bytes will be read in this con.
m_response.set_status(e.m_error_code,e.m_error_msg);
this->write_http_response_error(error::make_error_code(error::http_parse_error));
return;
}
// More paranoid boundaries checking.
// TODO: Is this overkill?
if (bytes_processed > bytes_transferred) {
m_elog.write(log::elevel::fatal,"Fatal boundaries checking error.");
this->terminate(make_error_code(error::general));
return;
}
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "bytes_transferred: " << bytes_transferred
<< " bytes, bytes processed: " << bytes_processed << " bytes";
m_alog.write(log::alevel::devel,s.str());
}
if (m_request.ready()) {
lib::error_code processor_ec = this->initialize_processor();
if (processor_ec) {
this->write_http_response_error(processor_ec);
return;
}
if (m_processor && m_processor->get_version() == 0) {
// Version 00 has an extra requirement to read some bytes after the
// handshake
if (bytes_transferred-bytes_processed >= 8) {
m_request.replace_header(
"Sec-WebSocket-Key3",
std::string(m_buf+bytes_processed,m_buf+bytes_processed+8)
);
bytes_processed += 8;
} else {
// TODO: need more bytes
m_alog.write(log::alevel::devel,"short key3 read");
m_response.set_status(http::status_code::internal_server_error);
this->write_http_response_error(processor::error::make_error_code(processor::error::short_key3));
return;
}
}
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,m_request.raw());
if (!m_request.get_header("Sec-WebSocket-Key3").empty()) {
m_alog.write(log::alevel::devel,
utility::to_hex(m_request.get_header("Sec-WebSocket-Key3")));
}
}
// The remaining bytes in m_buf are frame data. Copy them to the
// beginning of the buffer and note the length. They will be read after
// the handshake completes and before more bytes are read.
std::copy(m_buf+bytes_processed,m_buf+bytes_transferred,m_buf);
m_buf_cursor = bytes_transferred-bytes_processed;
m_internal_state = istate::PROCESS_HTTP_REQUEST;
// We have the complete request. Process it.
lib::error_code handshake_ec = this->process_handshake_request();
// Write a response if this is a websocket connection or if it is an
// HTTP connection for which the response has not been deferred or
// started yet by a different system (i.e. still in init state).
if (!m_is_http || m_http_state == session::http_state::init) {
this->write_http_response(handshake_ec);
}
} else {
// read at least 1 more byte
transport_con_type::async_read_at_least(
1,
m_buf,
config::connection_read_buffer_size,
lib::bind(
&type::handle_read_handshake,
type::get_shared(),
lib::placeholders::_1,
lib::placeholders::_2
)
);
}
}
// write_http_response requires the request to be fully read and the connection
// to be in the PROCESS_HTTP_REQUEST state. In some cases we can detect errors
// before the request is fully read (specifically at a point where we aren't
// sure if the hybi00 key3 bytes need to be read). This method sets the correct
// state and calls write_http_response
template <typename config>
void connection<config>::write_http_response_error(lib::error_code const & ec) {
if (m_internal_state != istate::READ_HTTP_REQUEST) {
m_alog.write(log::alevel::devel,
"write_http_response_error called in invalid state");
this->terminate(error::make_error_code(error::invalid_state));
return;
}
m_internal_state = istate::PROCESS_HTTP_REQUEST;
this->write_http_response(ec);
}
// All exit paths for this function need to call write_http_response() or submit
// a new read request with this function as the handler.
template <typename config>
void connection<config>::handle_read_frame(lib::error_code const & ec,
size_t bytes_transferred)
{
//m_alog.write(log::alevel::devel,"connection handle_read_frame");
lib::error_code ecm = ec;
if (!ecm && m_internal_state != istate::PROCESS_CONNECTION) {
ecm = error::make_error_code(error::invalid_state);
}
if (ecm) {
log::level echannel = log::elevel::rerror;
if (ecm == transport::error::eof) {
if (m_state == session::state::closed) {
// we expect to get eof if the connection is closed already
// just ignore it
m_alog.write(log::alevel::devel,"got eof from closed con");
return;
} else if (m_state == session::state::closing && !m_is_server) {
// If we are a client we expect to get eof in the closing state,
// this is a signal to terminate our end of the connection after
// the closing handshake
terminate(lib::error_code());
return;
}
} else if (ecm == error::invalid_state) {
// In general, invalid state errors in the closed state are the
// result of handlers that were in the system already when the state
// changed and should be ignored as they pose no problems and there
// is nothing useful that we can do about them.
if (m_state == session::state::closed) {
m_alog.write(log::alevel::devel,
"handle_read_frame: got invalid istate in closed state");
return;
}
} else if (ecm == transport::error::tls_short_read) {
if (m_state == session::state::closed) {
// We expect to get a TLS short read if we try to read after the
// connection is closed. If this happens ignore and exit the
// read frame path.
terminate(lib::error_code());
return;
}
echannel = log::elevel::rerror;
} else if (ecm == transport::error::action_after_shutdown) {
echannel = log::elevel::info;
}
log_err(echannel, "handle_read_frame", ecm);
this->terminate(ecm);
return;
}
// Boundaries checking. TODO: How much of this should be done?
/*if (bytes_transferred > config::connection_read_buffer_size) {
m_elog.write(log::elevel::fatal,"Fatal boundaries checking error");
this->terminate(make_error_code(error::general));
return;
}*/
size_t p = 0;
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "p = " << p << " bytes transferred = " << bytes_transferred;
m_alog.write(log::alevel::devel,s.str());
}
while (p < bytes_transferred) {
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "calling consume with " << bytes_transferred-p << " bytes";
m_alog.write(log::alevel::devel,s.str());
}
lib::error_code consume_ec;
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "Processing Bytes: " << utility::to_hex(reinterpret_cast<uint8_t*>(m_buf)+p,bytes_transferred-p);
m_alog.write(log::alevel::devel,s.str());
}
p += m_processor->consume(
reinterpret_cast<uint8_t*>(m_buf)+p,
bytes_transferred-p,
consume_ec
);
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "bytes left after consume: " << bytes_transferred-p;
m_alog.write(log::alevel::devel,s.str());
}
if (consume_ec) {
log_err(log::elevel::rerror, "consume", consume_ec);
if (config::drop_on_protocol_error) {
this->terminate(consume_ec);
return;
} else {
lib::error_code close_ec;
this->close(
processor::error::to_ws(consume_ec),
consume_ec.message(),
close_ec
);
if (close_ec) {
log_err(log::elevel::fatal, "Protocol error close frame ", close_ec);
this->terminate(close_ec);
return;
}
}
return;
}
if (m_processor->ready()) {
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "Complete message received. Dispatching";
m_alog.write(log::alevel::devel,s.str());
}
message_ptr msg = m_processor->get_message();
if (!msg) {
m_alog.write(log::alevel::devel, "null message from m_processor");
} else if (!is_control(msg->get_opcode())) {
// data message, dispatch to user
if (m_state != session::state::open) {
m_elog.write(log::elevel::warn, "got non-close frame while closing");
} else if (m_message_handler) {
m_message_handler(m_connection_hdl, msg);
}
} else {
process_control_frame(msg);
}
}
}
read_frame();
}
/// Issue a new transport read unless reading is paused.
template <typename config>
void connection<config>::read_frame() {
if (!m_read_flag) {
return;
}
transport_con_type::async_read_at_least(
// std::min wont work with undefined static const values.
// TODO: is there a more elegant way to do this?
// Need to determine if requesting 1 byte or the exact number of bytes
// is better here. 1 byte lets us be a bit more responsive at a
// potential expense of additional runs through handle_read_frame
/*(m_processor->get_bytes_needed() > config::connection_read_buffer_size ?
config::connection_read_buffer_size : m_processor->get_bytes_needed())*/
1,
m_buf,
config::connection_read_buffer_size,
m_handle_read_frame
);
}
template <typename config>
lib::error_code connection<config>::initialize_processor() {
m_alog.write(log::alevel::devel,"initialize_processor");
// if it isn't a websocket handshake nothing to do.
if (!processor::is_websocket_handshake(m_request)) {
return lib::error_code();
}
int version = processor::get_websocket_version(m_request);
if (version < 0) {
m_alog.write(log::alevel::devel, "BAD REQUEST: can't determine version");
m_response.set_status(http::status_code::bad_request);
return error::make_error_code(error::invalid_version);
}
m_processor = get_processor(version);
// if the processor is not null we are done
if (m_processor) {
return lib::error_code();
}
// We don't have a processor for this version. Return bad request
// with Sec-WebSocket-Version header filled with values we do accept
m_alog.write(log::alevel::devel, "BAD REQUEST: no processor for version");
m_response.set_status(http::status_code::bad_request);
std::stringstream ss;
std::string sep;
std::vector<int>::const_iterator it;
for (it = versions_supported.begin(); it != versions_supported.end(); it++)
{
ss << sep << *it;
sep = ",";
}
m_response.replace_header("Sec-WebSocket-Version",ss.str());
return error::make_error_code(error::unsupported_version);
}
template <typename config>
lib::error_code connection<config>::process_handshake_request() {
m_alog.write(log::alevel::devel,"process handshake request");
if (!processor::is_websocket_handshake(m_request)) {
// this is not a websocket handshake. Process as plain HTTP
m_alog.write(log::alevel::devel,"HTTP REQUEST");
// extract URI from request
m_uri = processor::get_uri_from_host(
m_request,
(transport_con_type::is_secure() ? "https" : "http")
);
if (!m_uri->get_valid()) {
m_alog.write(log::alevel::devel, "Bad request: failed to parse uri");
m_response.set_status(http::status_code::bad_request);
return error::make_error_code(error::invalid_uri);
}
if (m_http_handler) {
m_is_http = true;
m_http_handler(m_connection_hdl);
if (m_state == session::state::closed) {
return error::make_error_code(error::http_connection_ended);
}
} else {
set_status(http::status_code::upgrade_required);
return error::make_error_code(error::upgrade_required);
}
return lib::error_code();
}
lib::error_code ec = m_processor->validate_handshake(m_request);
// Validate: make sure all required elements are present.
if (ec){
// Not a valid handshake request
m_alog.write(log::alevel::devel, "Bad request " + ec.message());
m_response.set_status(http::status_code::bad_request);
return ec;
}
// Read extension parameters and set up values necessary for the end user
// to complete extension negotiation.
std::pair<lib::error_code,std::string> neg_results;
neg_results = m_processor->negotiate_extensions(m_request);
if (neg_results.first == processor::error::make_error_code(processor::error::extension_parse_error)) {
// There was a fatal error in extension parsing that should result in
// a failed connection attempt.
m_elog.write(log::elevel::info, "Bad request: " + neg_results.first.message());
m_response.set_status(http::status_code::bad_request);
return neg_results.first;
} else if (neg_results.first) {
// There was a fatal error in extension processing that is probably our
// fault. Consider extension negotiation to have failed and continue as
// if extensions were not supported
m_elog.write(log::elevel::info,
"Extension negotiation failed: " + neg_results.first.message());
} else {
// extension negotiation succeeded, set response header accordingly
// we don't send an empty extensions header because it breaks many
// clients.
if (neg_results.second.size() > 0) {
m_response.replace_header("Sec-WebSocket-Extensions",
neg_results.second);
}
}
// extract URI from request
m_uri = m_processor->get_uri(m_request);
if (!m_uri->get_valid()) {
m_alog.write(log::alevel::devel, "Bad request: failed to parse uri");
m_response.set_status(http::status_code::bad_request);
return error::make_error_code(error::invalid_uri);
}
// extract subprotocols
lib::error_code subp_ec = m_processor->extract_subprotocols(m_request,
m_requested_subprotocols);
if (subp_ec) {
// should we do anything?
}
// Ask application to validate the connection
if (!m_validate_handler || m_validate_handler(m_connection_hdl)) {
m_response.set_status(http::status_code::switching_protocols);
// Write the appropriate response headers based on request and
// processor version
ec = m_processor->process_handshake(m_request,m_subprotocol,m_response);
if (ec) {
std::stringstream s;
s << "Processing error: " << ec << "(" << ec.message() << ")";
m_alog.write(log::alevel::devel, s.str());
m_response.set_status(http::status_code::internal_server_error);
return ec;
}
} else {
// User application has rejected the handshake
m_alog.write(log::alevel::devel, "USER REJECT");
// Use Bad Request if the user handler did not provide a more
// specific http response error code.
// TODO: is there a better default?
if (m_response.get_status_code() == http::status_code::uninitialized) {
m_response.set_status(http::status_code::bad_request);
}
return error::make_error_code(error::rejected);
}
return lib::error_code();
}
template <typename config>
void connection<config>::write_http_response(lib::error_code const & ec) {
m_alog.write(log::alevel::devel,"connection write_http_response");
if (ec == error::make_error_code(error::http_connection_ended)) {
m_alog.write(log::alevel::http,"An HTTP handler took over the connection.");
return;
}
if (m_response.get_status_code() == http::status_code::uninitialized) {
m_response.set_status(http::status_code::internal_server_error);
m_ec = error::make_error_code(error::general);
} else {
m_ec = ec;
}
m_response.set_version("HTTP/1.1");
// Set server header based on the user agent settings
if (m_response.get_header("Server").empty()) {
if (!m_user_agent.empty()) {
m_response.replace_header("Server",m_user_agent);
} else {
m_response.remove_header("Server");
}
}
// have the processor generate the raw bytes for the wire (if it exists)
if (m_processor) {
m_handshake_buffer = m_processor->get_raw(m_response);
} else {
// a processor wont exist for raw HTTP responses.
m_handshake_buffer = m_response.raw();
}
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"Raw Handshake response:\n"+m_handshake_buffer);
if (!m_response.get_header("Sec-WebSocket-Key3").empty()) {
m_alog.write(log::alevel::devel,
utility::to_hex(m_response.get_header("Sec-WebSocket-Key3")));
}
}
// write raw bytes
transport_con_type::async_write(
m_handshake_buffer.data(),
m_handshake_buffer.size(),
lib::bind(
&type::handle_write_http_response,
type::get_shared(),
lib::placeholders::_1
)
);
}
template <typename config>
void connection<config>::handle_write_http_response(lib::error_code const & ec) {
m_alog.write(log::alevel::devel,"handle_write_http_response");
lib::error_code ecm = ec;
if (!ecm) {
scoped_lock_type lock(m_connection_state_lock);
if (m_state == session::state::connecting) {
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
ecm = error::make_error_code(error::invalid_state);
}
} else if (m_state == session::state::closed) {
// The connection was canceled while the response was being sent,
// usually by the handshake timer. This is basically expected
// (though hopefully rare) and there is nothing we can do so ignore.
m_alog.write(log::alevel::devel,
"handle_write_http_response invoked after connection was closed");
return;
} else {
ecm = error::make_error_code(error::invalid_state);
}
}
if (ecm) {
if (ecm == transport::error::eof && m_state == session::state::closed) {
// we expect to get eof if the connection is closed already
m_alog.write(log::alevel::devel,
"got (expected) eof/state error from closed con");
return;
}
log_err(log::elevel::rerror,"handle_write_http_response",ecm);
this->terminate(ecm);
return;
}
if (m_handshake_timer) {
m_handshake_timer->cancel();
m_handshake_timer.reset();
}
if (m_response.get_status_code() != http::status_code::switching_protocols)
{
/*if (m_processor || m_ec == error::http_parse_error ||
m_ec == error::invalid_version || m_ec == error::unsupported_version
|| m_ec == error::upgrade_required)
{*/
if (!m_is_http) {
std::stringstream s;
s << "Handshake ended with HTTP error: "
<< m_response.get_status_code();
m_elog.write(log::elevel::rerror,s.str());
} else {
// if this was not a websocket connection, we have written
// the expected response and the connection can be closed.
this->log_http_result();
if (m_ec) {
m_alog.write(log::alevel::devel,
"got to writing HTTP results with m_ec set: "+m_ec.message());
}
m_ec = make_error_code(error::http_connection_ended);
}
this->terminate(m_ec);
return;
}
this->log_open_result();
m_internal_state = istate::PROCESS_CONNECTION;
m_state = session::state::open;
if (m_open_handler) {
m_open_handler(m_connection_hdl);
}
this->handle_read_frame(lib::error_code(), m_buf_cursor);
}
template <typename config>
void connection<config>::send_http_request() {
m_alog.write(log::alevel::devel,"connection send_http_request");
// TODO: origin header?
// Have the protocol processor fill in the appropriate fields based on the
// selected client version
if (m_processor) {
lib::error_code ec;
ec = m_processor->client_handshake_request(m_request,m_uri,
m_requested_subprotocols);
if (ec) {
log_err(log::elevel::fatal,"Internal library error: Processor",ec);
return;
}
} else {
m_elog.write(log::elevel::fatal,"Internal library error: missing processor");
return;
}
// Unless the user has overridden the user agent, send generic WS++ UA.
if (m_request.get_header("User-Agent").empty()) {
if (!m_user_agent.empty()) {
m_request.replace_header("User-Agent",m_user_agent);
} else {
m_request.remove_header("User-Agent");
}
}
m_handshake_buffer = m_request.raw();
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"Raw Handshake request:\n"+m_handshake_buffer);
}
if (m_open_handshake_timeout_dur > 0) {
m_handshake_timer = transport_con_type::set_timer(
m_open_handshake_timeout_dur,
lib::bind(
&type::handle_open_handshake_timeout,
type::get_shared(),
lib::placeholders::_1
)
);
}
transport_con_type::async_write(
m_handshake_buffer.data(),
m_handshake_buffer.size(),
lib::bind(
&type::handle_send_http_request,
type::get_shared(),
lib::placeholders::_1
)
);
}
template <typename config>
void connection<config>::handle_send_http_request(lib::error_code const & ec) {
m_alog.write(log::alevel::devel,"handle_send_http_request");
lib::error_code ecm = ec;
if (!ecm) {
scoped_lock_type lock(m_connection_state_lock);
if (m_state == session::state::connecting) {
if (m_internal_state != istate::WRITE_HTTP_REQUEST) {
ecm = error::make_error_code(error::invalid_state);
} else {
m_internal_state = istate::READ_HTTP_RESPONSE;
}
} else if (m_state == session::state::closed) {
// The connection was canceled while the response was being sent,
// usually by the handshake timer. This is basically expected
// (though hopefully rare) and there is nothing we can do so ignore.
m_alog.write(log::alevel::devel,
"handle_send_http_request invoked after connection was closed");
return;
} else {
ecm = error::make_error_code(error::invalid_state);
}
}
if (ecm) {
if (ecm == transport::error::eof && m_state == session::state::closed) {
// we expect to get eof if the connection is closed already
m_alog.write(log::alevel::devel,
"got (expected) eof/state error from closed con");
return;
}
log_err(log::elevel::rerror,"handle_send_http_request",ecm);
this->terminate(ecm);
return;
}
transport_con_type::async_read_at_least(
1,
m_buf,
config::connection_read_buffer_size,
lib::bind(
&type::handle_read_http_response,
type::get_shared(),
lib::placeholders::_1,
lib::placeholders::_2
)
);
}
template <typename config>
void connection<config>::handle_read_http_response(lib::error_code const & ec,
size_t bytes_transferred)
{
m_alog.write(log::alevel::devel,"handle_read_http_response");
lib::error_code ecm = ec;
if (!ecm) {
scoped_lock_type lock(m_connection_state_lock);
if (m_state == session::state::connecting) {
if (m_internal_state != istate::READ_HTTP_RESPONSE) {
ecm = error::make_error_code(error::invalid_state);
}
} else if (m_state == session::state::closed) {
// The connection was canceled while the response was being sent,
// usually by the handshake timer. This is basically expected
// (though hopefully rare) and there is nothing we can do so ignore.
m_alog.write(log::alevel::devel,
"handle_read_http_response invoked after connection was closed");
return;
} else {
ecm = error::make_error_code(error::invalid_state);
}
}
if (ecm) {
if (ecm == transport::error::eof && m_state == session::state::closed) {
// we expect to get eof if the connection is closed already
m_alog.write(log::alevel::devel,
"got (expected) eof/state error from closed con");
return;
}
log_err(log::elevel::rerror,"handle_read_http_response",ecm);
this->terminate(ecm);
return;
}
size_t bytes_processed = 0;
// TODO: refactor this to use error codes rather than exceptions
try {
bytes_processed = m_response.consume(m_buf,bytes_transferred);
} catch (http::exception & e) {
m_elog.write(log::elevel::rerror,
std::string("error in handle_read_http_response: ")+e.what());
this->terminate(make_error_code(error::general));
return;
}
m_alog.write(log::alevel::devel,std::string("Raw response: ")+m_response.raw());
if (m_response.headers_ready()) {
if (m_handshake_timer) {
m_handshake_timer->cancel();
m_handshake_timer.reset();
}
lib::error_code validate_ec = m_processor->validate_server_handshake_response(
m_request,
m_response
);
if (validate_ec) {
log_err(log::elevel::rerror,"Server handshake response",validate_ec);
this->terminate(validate_ec);
return;
}
// Read extension parameters and set up values necessary for the end
// user to complete extension negotiation.
std::pair<lib::error_code,std::string> neg_results;
neg_results = m_processor->negotiate_extensions(m_response);
if (neg_results.first) {
// There was a fatal error in extension negotiation. For the moment
// kill all connections that fail extension negotiation.
// TODO: deal with cases where the response is well formed but
// doesn't match the options requested by the client. Its possible
// that the best behavior in this cases is to log and continue with
// an unextended connection.
m_alog.write(log::alevel::devel, "Extension negotiation failed: "
+ neg_results.first.message());
this->terminate(make_error_code(error::extension_neg_failed));
// TODO: close connection with reason 1010 (and list extensions)
}
// response is valid, connection can now be assumed to be open
m_internal_state = istate::PROCESS_CONNECTION;
m_state = session::state::open;
this->log_open_result();
if (m_open_handler) {
m_open_handler(m_connection_hdl);
}
// The remaining bytes in m_buf are frame data. Copy them to the
// beginning of the buffer and note the length. They will be read after
// the handshake completes and before more bytes are read.
std::copy(m_buf+bytes_processed,m_buf+bytes_transferred,m_buf);
m_buf_cursor = bytes_transferred-bytes_processed;
this->handle_read_frame(lib::error_code(), m_buf_cursor);
} else {
transport_con_type::async_read_at_least(
1,
m_buf,
config::connection_read_buffer_size,
lib::bind(
&type::handle_read_http_response,
type::get_shared(),
lib::placeholders::_1,
lib::placeholders::_2
)
);
}
}
template <typename config>
void connection<config>::handle_open_handshake_timeout(
lib::error_code const & ec)
{
if (ec == transport::error::operation_aborted) {
m_alog.write(log::alevel::devel,"open handshake timer cancelled");
} else if (ec) {
m_alog.write(log::alevel::devel,
"open handle_open_handshake_timeout error: "+ec.message());
// TODO: ignore or fail here?
} else {
m_alog.write(log::alevel::devel,"open handshake timer expired");
terminate(make_error_code(error::open_handshake_timeout));
}
}
template <typename config>
void connection<config>::handle_close_handshake_timeout(
lib::error_code const & ec)
{
if (ec == transport::error::operation_aborted) {
m_alog.write(log::alevel::devel,"asio close handshake timer cancelled");
} else if (ec) {
m_alog.write(log::alevel::devel,
"asio open handle_close_handshake_timeout error: "+ec.message());
// TODO: ignore or fail here?
} else {
m_alog.write(log::alevel::devel, "asio close handshake timer expired");
terminate(make_error_code(error::close_handshake_timeout));
}
}
template <typename config>
void connection<config>::terminate(lib::error_code const & ec) {
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection terminate");
}
// Cancel close handshake timer
if (m_handshake_timer) {
m_handshake_timer->cancel();
m_handshake_timer.reset();
}
terminate_status tstat = unknown;
if (ec) {
m_ec = ec;
m_local_close_code = close::status::abnormal_close;
m_local_close_reason = ec.message();
}
// TODO: does any of this need a mutex?
if (m_is_http) {
m_http_state = session::http_state::closed;
}
if (m_state == session::state::connecting) {
m_state = session::state::closed;
tstat = failed;
// Log fail result here before socket is shut down and we can't get
// the remote address, etc anymore
if (m_ec != error::http_connection_ended) {
log_fail_result();
}
} else if (m_state != session::state::closed) {
m_state = session::state::closed;
tstat = closed;
} else {
m_alog.write(log::alevel::devel,
"terminate called on connection that was already terminated");
return;
}
// TODO: choose between shutdown and close based on error code sent
transport_con_type::async_shutdown(
lib::bind(
&type::handle_terminate,
type::get_shared(),
tstat,
lib::placeholders::_1
)
);
}
template <typename config>
void connection<config>::handle_terminate(terminate_status tstat,
lib::error_code const & ec)
{
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection handle_terminate");
}
if (ec) {
// there was an error actually shutting down the connection
log_err(log::elevel::devel,"handle_terminate",ec);
}
// clean shutdown
if (tstat == failed) {
if (m_ec != error::http_connection_ended) {
if (m_fail_handler) {
m_fail_handler(m_connection_hdl);
}
}
} else if (tstat == closed) {
if (m_close_handler) {
m_close_handler(m_connection_hdl);
}
log_close_result();
} else {
m_elog.write(log::elevel::rerror,"Unknown terminate_status");
}
// call the termination handler if it exists
// if it exists it might (but shouldn't) refer to a bad memory location.
// If it does, we don't care and should catch and ignore it.
if (m_termination_handler) {
try {
m_termination_handler(type::get_shared());
} catch (std::exception const & e) {
m_elog.write(log::elevel::warn,
std::string("termination_handler call failed. Reason was: ")+e.what());
}
}
}
template <typename config>
void connection<config>::write_frame() {
//m_alog.write(log::alevel::devel,"connection write_frame");
{
scoped_lock_type lock(m_write_lock);
// Check the write flag. If true, there is an outstanding transport
// write already. In this case we just return. The write handler will
// start a new write if the write queue isn't empty. If false, we set
// the write flag and proceed to initiate a transport write.
if (m_write_flag) {
return;
}
// pull off all the messages that are ready to write.
// stop if we get a message marked terminal
message_ptr next_message = write_pop();
while (next_message) {
m_current_msgs.push_back(next_message);
if (!next_message->get_terminal()) {
next_message = write_pop();
} else {
next_message = message_ptr();
}
}
if (m_current_msgs.empty()) {
// there was nothing to send
return;
} else {
// At this point we own the next messages to be sent and are
// responsible for holding the write flag until they are
// successfully sent or there is some error
m_write_flag = true;
}
}
typename std::vector<message_ptr>::iterator it;
for (it = m_current_msgs.begin(); it != m_current_msgs.end(); ++it) {
std::string const & header = (*it)->get_header();
std::string const & payload = (*it)->get_payload();
m_send_buffer.push_back(transport::buffer(header.c_str(),header.size()));
m_send_buffer.push_back(transport::buffer(payload.c_str(),payload.size()));
}
// Print detailed send stats if those log levels are enabled
if (m_alog.static_test(log::alevel::frame_header)) {
if (m_alog.dynamic_test(log::alevel::frame_header)) {
std::stringstream general,header,payload;
general << "Dispatching write containing " << m_current_msgs.size()
<<" message(s) containing ";
header << "Header Bytes: \n";
payload << "Payload Bytes: \n";
size_t hbytes = 0;
size_t pbytes = 0;
for (size_t i = 0; i < m_current_msgs.size(); i++) {
hbytes += m_current_msgs[i]->get_header().size();
pbytes += m_current_msgs[i]->get_payload().size();
header << "[" << i << "] ("
<< m_current_msgs[i]->get_header().size() << ") "
<< utility::to_hex(m_current_msgs[i]->get_header()) << "\n";
if (m_alog.static_test(log::alevel::frame_payload)) {
if (m_alog.dynamic_test(log::alevel::frame_payload)) {
payload << "[" << i << "] ("
<< m_current_msgs[i]->get_payload().size() << ") ["<<m_current_msgs[i]->get_opcode()<<"] "
<< (m_current_msgs[i]->get_opcode() == frame::opcode::text ?
m_current_msgs[i]->get_payload() :
utility::to_hex(m_current_msgs[i]->get_payload())
)
<< "\n";
}
}
}
general << hbytes << " header bytes and " << pbytes << " payload bytes";
m_alog.write(log::alevel::frame_header,general.str());
m_alog.write(log::alevel::frame_header,header.str());
m_alog.write(log::alevel::frame_payload,payload.str());
}
}
transport_con_type::async_write(
m_send_buffer,
m_write_frame_handler
);
}
template <typename config>
void connection<config>::handle_write_frame(lib::error_code const & ec)
{
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"connection handle_write_frame");
}
bool terminal = m_current_msgs.back()->get_terminal();
m_send_buffer.clear();
m_current_msgs.clear();
// TODO: recycle instead of deleting
if (ec) {
log_err(log::elevel::fatal,"handle_write_frame",ec);
this->terminate(ec);
return;
}
if (terminal) {
this->terminate(lib::error_code());
return;
}
bool needs_writing = false;
{
scoped_lock_type lock(m_write_lock);
// release write flag
m_write_flag = false;
needs_writing = !m_send_queue.empty();
}
if (needs_writing) {
transport_con_type::dispatch(lib::bind(
&type::write_frame,
type::get_shared()
));
}
}
template <typename config>
std::vector<int> const & connection<config>::get_supported_versions() const
{
return versions_supported;
}
template <typename config>
void connection<config>::process_control_frame(typename config::message_type::ptr msg)
{
m_alog.write(log::alevel::devel,"process_control_frame");
frame::opcode::value op = msg->get_opcode();
lib::error_code ec;
std::stringstream s;
s << "Control frame received with opcode " << op;
m_alog.write(log::alevel::control,s.str());
if (m_state == session::state::closed) {
m_elog.write(log::elevel::warn,"got frame in state closed");
return;
}
if (op != frame::opcode::CLOSE && m_state != session::state::open) {
m_elog.write(log::elevel::warn,"got non-close frame in state closing");
return;
}
if (op == frame::opcode::PING) {
bool should_reply = true;
if (m_ping_handler) {
should_reply = m_ping_handler(m_connection_hdl, msg->get_payload());
}
if (should_reply) {
this->pong(msg->get_payload(),ec);
if (ec) {
log_err(log::elevel::devel,"Failed to send response pong",ec);
}
}
} else if (op == frame::opcode::PONG) {
if (m_pong_handler) {
m_pong_handler(m_connection_hdl, msg->get_payload());
}
if (m_ping_timer) {
m_ping_timer->cancel();
}
} else if (op == frame::opcode::CLOSE) {
m_alog.write(log::alevel::devel,"got close frame");
// record close code and reason somewhere
m_remote_close_code = close::extract_code(msg->get_payload(),ec);
if (ec) {
s.str("");
if (config::drop_on_protocol_error) {
s << "Received invalid close code " << m_remote_close_code
<< " dropping connection per config.";
m_elog.write(log::elevel::devel,s.str());
this->terminate(ec);
} else {
s << "Received invalid close code " << m_remote_close_code
<< " sending acknowledgement and closing";
m_elog.write(log::elevel::devel,s.str());
ec = send_close_ack(close::status::protocol_error,
"Invalid close code");
if (ec) {
log_err(log::elevel::devel,"send_close_ack",ec);
}
}
return;
}
m_remote_close_reason = close::extract_reason(msg->get_payload(),ec);
if (ec) {
if (config::drop_on_protocol_error) {
m_elog.write(log::elevel::devel,
"Received invalid close reason. Dropping connection per config");
this->terminate(ec);
} else {
m_elog.write(log::elevel::devel,
"Received invalid close reason. Sending acknowledgement and closing");
ec = send_close_ack(close::status::protocol_error,
"Invalid close reason");
if (ec) {
log_err(log::elevel::devel,"send_close_ack",ec);
}
}
return;
}
if (m_state == session::state::open) {
s.str("");
s << "Received close frame with code " << m_remote_close_code
<< " and reason " << m_remote_close_reason;
m_alog.write(log::alevel::devel,s.str());
ec = send_close_ack();
if (ec) {
log_err(log::elevel::devel,"send_close_ack",ec);
}
} else if (m_state == session::state::closing && !m_was_clean) {
// ack of our close
m_alog.write(log::alevel::devel, "Got acknowledgement of close");
m_was_clean = true;
// If we are a server terminate the connection now. Clients should
// leave the connection open to give the server an opportunity to
// initiate the TCP close. The client's timer will handle closing
// its side of the connection if the server misbehaves.
//
// TODO: different behavior if the underlying transport doesn't
// support timers?
if (m_is_server) {
terminate(lib::error_code());
}
} else {
// spurious, ignore
m_elog.write(log::elevel::devel, "Got close frame in wrong state");
}
} else {
// got an invalid control opcode
m_elog.write(log::elevel::devel, "Got control frame with invalid opcode");
// initiate protocol error shutdown
}
}
template <typename config>
lib::error_code connection<config>::send_close_ack(close::status::value code,
std::string const & reason)
{
return send_close_frame(code,reason,true,m_is_server);
}
template <typename config>
lib::error_code connection<config>::send_close_frame(close::status::value code,
std::string const & reason, bool ack, bool terminal)
{
m_alog.write(log::alevel::devel,"send_close_frame");
// check for special codes
// If silent close is set, respect it and blank out close information
// Otherwise use whatever has been specified in the parameters. If
// parameters specifies close::status::blank then determine what to do
// based on whether or not this is an ack. If it is not an ack just
// send blank info. If it is an ack then echo the close information from
// the remote endpoint.
if (config::silent_close) {
m_alog.write(log::alevel::devel,"closing silently");
m_local_close_code = close::status::no_status;
m_local_close_reason.clear();
} else if (code != close::status::blank) {
m_alog.write(log::alevel::devel,"closing with specified codes");
m_local_close_code = code;
m_local_close_reason = reason;
} else if (!ack) {
m_alog.write(log::alevel::devel,"closing with no status code");
m_local_close_code = close::status::no_status;
m_local_close_reason.clear();
} else if (m_remote_close_code == close::status::no_status) {
m_alog.write(log::alevel::devel,
"acknowledging a no-status close with normal code");
m_local_close_code = close::status::normal;
m_local_close_reason.clear();
} else {
m_alog.write(log::alevel::devel,"acknowledging with remote codes");
m_local_close_code = m_remote_close_code;
m_local_close_reason = m_remote_close_reason;
}
std::stringstream s;
s << "Closing with code: " << m_local_close_code << ", and reason: "
<< m_local_close_reason;
m_alog.write(log::alevel::devel,s.str());
message_ptr msg = m_msg_manager->get_message();
if (!msg) {
return error::make_error_code(error::no_outgoing_buffers);
}
lib::error_code ec = m_processor->prepare_close(m_local_close_code,
m_local_close_reason,msg);
if (ec) {
return ec;
}
// Messages flagged terminal will result in the TCP connection being dropped
// after the message has been written. This is typically used when servers
// send an ack and when any endpoint encounters a protocol error
if (terminal) {
msg->set_terminal(true);
}
m_state = session::state::closing;
if (ack) {
m_was_clean = true;
}
// Start a timer so we don't wait forever for the acknowledgement close
// frame
if (m_close_handshake_timeout_dur > 0) {
m_handshake_timer = transport_con_type::set_timer(
m_close_handshake_timeout_dur,
lib::bind(
&type::handle_close_handshake_timeout,
type::get_shared(),
lib::placeholders::_1
)
);
}
bool needs_writing = false;
{
scoped_lock_type lock(m_write_lock);
write_push(msg);
needs_writing = !m_write_flag && !m_send_queue.empty();
}
if (needs_writing) {
transport_con_type::dispatch(lib::bind(
&type::write_frame,
type::get_shared()
));
}
return lib::error_code();
}
template <typename config>
typename connection<config>::processor_ptr
connection<config>::get_processor(int version) const {
// TODO: allow disabling certain versions
processor_ptr p;
switch (version) {
case 0:
p = lib::make_shared<processor::hybi00<config> >(
transport_con_type::is_secure(),
m_is_server,
m_msg_manager
);
break;
case 7:
p = lib::make_shared<processor::hybi07<config> >(
transport_con_type::is_secure(),
m_is_server,
m_msg_manager,
lib::ref(m_rng)
);
break;
case 8:
p = lib::make_shared<processor::hybi08<config> >(
transport_con_type::is_secure(),
m_is_server,
m_msg_manager,
lib::ref(m_rng)
);
break;
case 13:
p = lib::make_shared<processor::hybi13<config> >(
transport_con_type::is_secure(),
m_is_server,
m_msg_manager,
lib::ref(m_rng)
);
break;
default:
return p;
}
// Settings not configured by the constructor
p->set_max_message_size(m_max_message_size);
return p;
}
template <typename config>
void connection<config>::write_push(typename config::message_type::ptr msg)
{
if (!msg) {
return;
}
m_send_buffer_size += msg->get_payload().size();
m_send_queue.push(msg);
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "write_push: message count: " << m_send_queue.size()
<< " buffer size: " << m_send_buffer_size;
m_alog.write(log::alevel::devel,s.str());
}
}
template <typename config>
typename config::message_type::ptr connection<config>::write_pop()
{
message_ptr msg;
if (m_send_queue.empty()) {
return msg;
}
msg = m_send_queue.front();
m_send_buffer_size -= msg->get_payload().size();
m_send_queue.pop();
if (m_alog.static_test(log::alevel::devel)) {
std::stringstream s;
s << "write_pop: message count: " << m_send_queue.size()
<< " buffer size: " << m_send_buffer_size;
m_alog.write(log::alevel::devel,s.str());
}
return msg;
}
template <typename config>
void connection<config>::log_open_result()
{
std::stringstream s;
int version;
if (!processor::is_websocket_handshake(m_request)) {
version = -1;
} else {
version = processor::get_websocket_version(m_request);
}
// Connection Type
s << (version == -1 ? "HTTP" : "WebSocket") << " Connection ";
// Remote endpoint address
s << transport_con_type::get_remote_endpoint() << " ";
// Version string if WebSocket
if (version != -1) {
s << "v" << version << " ";
}
// User Agent
std::string ua = m_request.get_header("User-Agent");
if (ua.empty()) {
s << "\"\" ";
} else {
// check if there are any quotes in the user agent
s << "\"" << utility::string_replace_all(ua,"\"","\\\"") << "\" ";
}
// URI
s << (m_uri ? m_uri->get_resource() : "NULL") << " ";
// Status code
s << m_response.get_status_code();
m_alog.write(log::alevel::connect,s.str());
}
template <typename config>
void connection<config>::log_close_result()
{
std::stringstream s;
s << "Disconnect "
<< "close local:[" << m_local_close_code
<< (m_local_close_reason.empty() ? "" : ","+m_local_close_reason)
<< "] remote:[" << m_remote_close_code
<< (m_remote_close_reason.empty() ? "" : ","+m_remote_close_reason) << "]";
m_alog.write(log::alevel::disconnect,s.str());
}
template <typename config>
void connection<config>::log_fail_result()
{
std::stringstream s;
int version = processor::get_websocket_version(m_request);
// Connection Type
s << "WebSocket Connection ";
// Remote endpoint address & WebSocket version
s << transport_con_type::get_remote_endpoint();
if (version < 0) {
s << " -";
} else {
s << " v" << version;
}
// User Agent
std::string ua = m_request.get_header("User-Agent");
if (ua.empty()) {
s << " \"\" ";
} else {
// check if there are any quotes in the user agent
s << " \"" << utility::string_replace_all(ua,"\"","\\\"") << "\" ";
}
// URI
s << (m_uri ? m_uri->get_resource() : "-");
// HTTP Status code
s << " " << m_response.get_status_code();
// WebSocket++ error code & reason
s << " " << m_ec << " " << m_ec.message();
m_alog.write(log::alevel::fail,s.str());
}
template <typename config>
void connection<config>::log_http_result() {
std::stringstream s;
if (processor::is_websocket_handshake(m_request)) {
m_alog.write(log::alevel::devel,"Call to log_http_result for WebSocket");
return;
}
// Connection Type
s << (m_request.get_header("host").empty() ? "-" : m_request.get_header("host"))
<< " " << transport_con_type::get_remote_endpoint()
<< " \"" << m_request.get_method()
<< " " << (m_uri ? m_uri->get_resource() : "-")
<< " " << m_request.get_version() << "\" " << m_response.get_status_code()
<< " " << m_response.get_body().size();
// User Agent
std::string ua = m_request.get_header("User-Agent");
if (ua.empty()) {
s << " \"\" ";
} else {
// check if there are any quotes in the user agent
s << " \"" << utility::string_replace_all(ua,"\"","\\\"") << "\" ";
}
m_alog.write(log::alevel::http,s.str());
}
} // namespace websocketpp
#endif // WEBSOCKETPP_CONNECTION_IMPL_HPP
|