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
|
//
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/url
//
#include "test_suite.hpp"
#include <boost/container/string.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/core/ignore_unused.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/url/string_view.hpp>
#include <boost/url/grammar/string_token.hpp>
//[snippet_headers_1
#include <boost/url.hpp>
//]
#include <algorithm>
#include <iostream>
#include <vector>
#include <cctype>
//[snippet_headers_3
#include <boost/url.hpp>
using namespace boost::urls;
//]
#include <iostream>
#ifdef assert
#undef assert
#endif
#define assert BOOST_TEST
void
using_url_views()
{
{
//[snippet_accessing_1
url_view u( "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor" );
assert(u.scheme() == "https");
assert(u.authority().buffer() == "user:pass@example.com:443");
assert(u.userinfo() == "user:pass");
assert(u.user() == "user");
assert(u.password() == "pass");
assert(u.host() == "example.com");
assert(u.port() == "443");
assert(u.path() == "/path/to/my-file.txt");
assert(u.query() == "id=42&name=John Doe Jingleheimer-Schmidt");
assert(u.fragment() == "page anchor");
//]
}
//[code_urls_parsing_1
boost::core::string_view s = "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor";
//]
{
//[code_urls_parsing_2
boost::system::result<url_view> r = parse_uri( s );
//]
//[snippet_parsing_3
url_view u = r.value();
//]
boost::ignore_unused(u);
}
{
boost::system::result<url_view> r = parse_uri( s );
//[snippet_parsing_4
url_view u = *r;
//]
boost::ignore_unused(u);
}
url_view u( s );
//[snippet_accessing_1b
for (auto seg: u.segments())
std::cout << seg << "\n";
std::cout << "\n";
for (auto param: u.params())
std::cout << param.key << ": " << param.value << "\n";
std::cout << "\n";
//]
{
//[snippet_token_1
std::string h = u.host();
assert(h == "example.com");
//]
boost::ignore_unused(h);
}
{
//[snippet_token_2
std::string h = "host: ";
u.host(string_token::append_to(h));
assert(h == "host: example.com");
//]
boost::ignore_unused(h);
}
{
//[snippet_accessing_2a
url_view u1 = parse_uri( "http://www.example.com" ).value();
assert(u1.fragment().empty());
assert(!u1.has_fragment());
//]
boost::ignore_unused(u1);
}
{
//[snippet_accessing_2b
url_view u2 = parse_uri( "http://www.example.com/#" ).value();
assert(u2.fragment().empty());
assert(u2.has_fragment());
//]
}
{
//[snippet_accessing_3a
url_view u1 = parse_uri( "http://www.example.com" ).value();
std::cout << "has fragment 1 : " << u1.has_fragment() << "\n";
std::cout << "fragment 1 : " << u1.fragment() << "\n\n";
//]
//[snippet_accessing_3b
url_view u2 = parse_uri( "http://www.example.com/#" ).value();
std::cout << "has fragment 2 : " << u2.has_fragment() << "\n";
std::cout << "fragment 2 : " << u2.fragment() << "\n\n";
//]
}
{
//[snippet_accessing_4
std::cout <<
"url : " << u << "\n"
"scheme : " << u.scheme() << "\n"
"authority : " << u.encoded_authority() << "\n"
"userinfo : " << u.encoded_userinfo() << "\n"
"user : " << u.encoded_user() << "\n"
"password : " << u.encoded_password() << "\n"
"host : " << u.encoded_host() << "\n"
"port : " << u.port() << "\n"
"path : " << u.encoded_path() << "\n"
"query : " << u.encoded_query() << "\n"
"fragment : " << u.encoded_fragment() << "\n";
//]
}
{
//[snippet_decoding_1
decode_view dv("id=42&name=John%20Doe%20Jingleheimer%2DSchmidt");
std::cout << dv << "\n";
//]
}
{
url u1 = u;
url u2 = u;
//[snippet_decoding_2
u1.set_host(u2.host());
//]
std::cout << u1 << "\n";
}
{
//[snippet_decoding_3
boost::filesystem::path p;
for (auto seg: u.segments())
p.append(seg.begin(), seg.end());
std::cout << "path: " << p << "\n";
//]
}
// transparent std::equal_to<> required
#if BOOST_CXX_VERSION >= 201402L && !defined(BOOST_CLANG)
{
auto handle_route = [](
std::vector<std::string> const&,
url_view)
{};
//[snippet_decoding_4a
auto match = [](
std::vector<std::string> const& route,
url_view u)
{
auto segs = u.segments();
if (route.size() != segs.size())
return false;
return std::equal(
route.begin(),
route.end(),
segs.begin());
};
//]
//[snippet_decoding_4b
std::vector<std::string> route =
{"community", "reviews.html"};
if (match(route, u))
{
handle_route(route, u);
}
//]
}
#endif
{
//[snippet_compound_elements_1
segments_encoded_view segs = u.encoded_segments();
for( auto v : segs )
{
std::cout << v << "\n";
}
//]
}
{
//[snippet_encoded_compound_elements_1
segments_encoded_view segs = u.encoded_segments();
for( pct_string_view v : segs )
{
decode_view dv = *v;
std::cout << dv << "\n";
}
//]
}
{
//[snippet_encoded_compound_elements_2
params_encoded_view params_ref = u.encoded_params();
for( auto v : params_ref )
{
decode_view dk(v.key);
decode_view dv(v.value);
std::cout <<
"key = " << dk <<
", value = " << dv << "\n";
}
//]
}
}
void
using_urls()
{
boost::core::string_view s = "https://user:pass@www.example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe#page%20anchor";
//[snippet_quicklook_modifying_1
url u = parse_uri( s ).value();
//]
//[snippet_quicklook_modifying_1b
static_url<1024> su = parse_uri( s ).value();
//]
(void)su;
//[snippet_quicklook_modifying_2
u.set_scheme( "https" );
//]
//[snippet_quicklook_modifying_3
u.set_scheme_id( scheme::https ); // equivalent to u.set_scheme( "https" );
//]
//[snippet_quicklook_modifying_4
u.set_host_ipv4( ipv4_address( "192.168.0.1" ) )
.set_port_number( 8080 )
.remove_userinfo();
std::cout << u << "\n";
//]
//[snippet_quicklook_modifying_5
params_ref p = u.params();
p.replace(p.find("name"), {"name", "John Doe"});
std::cout << u << "\n";
//]
}
void
parsing_urls()
{
{
auto handle_error = [](boost::system::error_code e)
{
boost::ignore_unused(e);
};
//[snippet_parsing_url_1
boost::system::result< url > ru = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
if ( ru )
{
url u = *ru;
assert(u.encoded_path() == "/path/to/file.txt");
}
else
{
boost::system::error_code e = ru.error();
handle_error(e);
}
//]
}
{
auto handle_error = [](boost::system::system_error& e)
{
boost::ignore_unused(e);
};
//[snippet_parsing_url_1b
try
{
url u = parse_uri_reference( "https://www.example.com/path/to/file.txt" ).value();
assert(u.encoded_path() == "/path/to/file.txt");
}
catch (boost::system::system_error &e)
{
handle_error(e);
}
//]
}
{
//[snippet_parsing_url_1b2
boost::system::result< url > ru = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
if ( ru.has_value() )
{
url u = *ru;
assert(u.encoded_path() == "/path/to/file.txt");
}
//]
boost::ignore_unused(ru);
}
{
//[snippet_parsing_url_1bb
url u = parse_uri_reference( "https://www.example.com/path/to/file.txt" ).value();
assert(u.encoded_path() == "/path/to/file.txt");
//]
boost::ignore_unused(u);
}
{
//[snippet_parsing_url_1bc
boost::system::result< url > rv = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
static_assert( std::is_convertible< boost::system::result< url_view >, boost::system::result< url > >::value, "" );
//]
boost::ignore_unused(rv);
}
{
//[snippet_parsing_url_1bd
boost::system::result< static_url<1024> > rv = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
static_assert( std::is_convertible< boost::system::result< static_url<1024> >, boost::system::result< url > >::value, "" );
//]
boost::ignore_unused(rv);
}
{
//[snippet_parsing_url_1c
boost::system::result< url_view > r0 = parse_relative_ref( "/path/to/file.txt" );
assert( r0.has_value() );
//]
//[snippet_parsing_url_1d
boost::system::result< url_view > r1 = parse_uri( "https://www.example.com" );
assert( r1.has_value() );
url dest;
resolve(r1.value(), r0.value(), dest);
assert(dest.buffer() == "https://www.example.com/path/to/file.txt");
//]
boost::ignore_unused(dest);
}
//[snippet_parsing_url_2
// This will hold our copy
std::shared_ptr<url_view const> sp;
{
std::string s = "/path/to/file.txt";
// result::value() will throw an exception if an error occurs
url_view u = parse_relative_ref( s ).value();
// create a copy with ownership and string lifetime extension
sp = u.persist();
// At this point the string goes out of scope
}
// but `*sp` remains valid since it has its own copy
std::cout << *sp << "\n";
//]
{
//[snippet_parsing_url_3
// This will hold our mutable copy
url v;
{
std::string s = "/path/to/file.txt";
// result::value() will throw an exception if an error occurs
v = parse_relative_ref(s).value();
// At this point the string goes out of scope
}
// but `v` remains valid since it has its own copy
std::cout << v << "\n";
// and it's mutable
v.set_fragment("anchor");
// path/to/file.txt#anchor
std::cout << v << "\n";
//]
}
}
void
parsing_components()
{
{
//[snippet_components_2a
url_view u( "https://www.ietf.org/rfc/rfc2396.txt" );
assert(u.scheme() == "https");
assert(u.host() == "www.ietf.org");
assert(u.path() == "/rfc/rfc2396.txt");
//]
}
{
//[snippet_components_2b
url_view u( "ftp://ftp.is.co.za/rfc/rfc1808.txt" );
assert(u.scheme() == "ftp");
assert(u.host() == "ftp.is.co.za");
assert(u.path() == "/rfc/rfc1808.txt");
//]
}
{
//[snippet_components_2c
url_view u( "mailto:John.Doe@example.com" );
assert(u.scheme() == "mailto");
assert(u.path() == "John.Doe@example.com");
//]
}
{
//[snippet_components_2d
url_view u( "urn:isbn:096139210x" );
assert(u.scheme() == "urn");
assert(u.path() == "isbn:096139210x");
//]
}
{
//[snippet_components_2e
url_view u( "magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36" );
assert(u.scheme() == "magnet");
assert(u.path() == "");
assert(u.query() == "xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36");
//]
}
}
void
formatting_components()
{
// I have spent a lot of time on this and have no
// idea how to fix this bug in GCC 4.8 and GCC 5.0
// without help from the pros.
#if !BOOST_WORKAROUND( BOOST_GCC_VERSION, < 60000 )
{
//[snippet_format_1
url u = format("{}://{}:{}/rfc/{}", "https", "www.ietf.org", 80, "rfc2396.txt");
assert(u.buffer() == "https://www.ietf.org:80/rfc/rfc2396.txt");
//]
}
{
//[snippet_format_2
url u = format("https://{}/{}", "www.boost.org", "Hello world!");
assert(u.buffer() == "https://www.boost.org/Hello%20world!");
//]
}
{
//[snippet_format_3a
url u = format("{}:{}", "mailto", "someone@example.com");
assert(u.buffer() == "mailto:someone@example.com");
assert(u.scheme() == "mailto");
assert(u.path() == "someone@example.com");
//]
}
{
//[snippet_format_3b
url u = format("{}{}", "mailto:", "someone@example.com");
assert(u.buffer() == "mailto%3Asomeone@example.com");
assert(!u.has_scheme());
assert(u.path() == "mailto:someone@example.com");
assert(u.encoded_path() == "mailto%3Asomeone@example.com");
//]
}
{
//[snippet_format_4
static_url<50> u;
format_to(u, "{}://{}:{}/rfc/{}", "https", "www.ietf.org", 80, "rfc2396.txt");
assert(u.buffer() == "https://www.ietf.org:80/rfc/rfc2396.txt");
//]
}
{
//[snippet_format_5a
url u = format("{0}://{2}:{1}/{3}{4}{3}", "https", 80, "www.ietf.org", "abra", "cad");
assert(u.buffer() == "https://www.ietf.org:80/abracadabra");
//]
}
{
//[snippet_format_5b
url u = format("https://example.com/~{username}", arg("username", "mark"));
assert(u.buffer() == "https://example.com/~mark");
//]
}
{
//[snippet_format_5c
boost::core::string_view fmt = "{scheme}://{host}:{port}/{dir}/{file}";
url u = format(fmt, {{"scheme", "https"}, {"port", 80}, {"host", "example.com"}, {"dir", "path/to"}, {"file", "file.txt"}});
assert(u.buffer() == "https://example.com:80/path/to/file.txt");
//]
}
#endif
}
void
parsing_scheme()
{
{
//[snippet_parsing_scheme_1
url_view u("mailto:name@email.com" );
assert( u.has_scheme() );
assert( u.scheme() == "mailto" );
//]
boost::ignore_unused(u);
}
{
//[snippet_parsing_scheme_3
url_view u("file://host/path/to/file" );
assert( u.scheme_id() == scheme::file );
//]
boost::ignore_unused(u);
}
}
void
parsing_authority()
{
{
//[snippet_parsing_authority_1
url_view u( "https:///path/to_resource" );
assert( u.authority().buffer() == "");
assert( u.has_authority() );
assert( u.path() == "/path/to_resource" );
//]
}
{
//[snippet_parsing_authority_2
url_view u("https://www.boost.org" );
std::cout << "scheme: " << u.scheme() << "\n"
"has authority: " << u.has_authority() << "\n"
"authority: " << u.authority() << "\n"
"path: " << u.path() << "\n";
//]
}
{
//[snippet_parsing_authority_3a
url_view u( "https://www.boost.org/users/download/" );
assert(u.has_authority());
authority_view a = u.authority();
//]
//[snippet_parsing_authority_3b
assert(a.host() == "www.boost.org");
//]
}
{
//[snippet_parsing_authority_4
url_view u( "https://www.boost.org/" );
std::cout << "scheme: " << u.scheme() << "\n"
"has authority: " << u.has_authority() << "\n"
"authority: " << u.authority() << "\n"
"path: " << u.path() << "\n";
//]
}
{
//[snippet_parsing_authority_5
url_view u( "mailto:John.Doe@example.com" );
std::cout << "scheme: " << u.scheme() << "\n"
"has authority: " << u.has_authority() << "\n"
"authority: " << u.authority() << "\n"
"path: " << u.path() << "\n";
//]
}
{
//[snippet_parsing_authority_6
url_view u( "mailto://John.Doe@example.com" );
std::cout << u << "\n"
"scheme: " << u.scheme() << "\n"
"has authority: " << u.has_authority() << "\n"
"authority: " << u.authority() << "\n"
"path: " << u.path() << "\n";
//]
}
{
//[snippet_parsing_authority_7
url_view u( "https://john.doe@www.example.com:123/forum/questions/" );
std::cout << "scheme: " << u.scheme() << "\n"
"has authority: " << u.has_authority() << "\n"
"authority: " << u.authority() << "\n"
"host: " << u.host() << "\n"
"userinfo: " << u.userinfo() << "\n"
"port: " << u.port() << "\n"
"path: " << u.path() << "\n";
//]
}
{
//[snippet_parsing_authority_8
url_view u( "https://john.doe@www.example.com:123/forum/questions/" );
assert(u.host() == "www.example.com");
assert(u.port() == "123");
//]
}
{
//[snippet_parsing_authority_9
url_view u( "https://john.doe@192.168.2.1:123/forum/questions/" );
assert(u.host() == "192.168.2.1");
assert(u.port() == "123");
//]
}
{
//[snippet_parsing_authority_9b
url_view u( "https://www.example.com" );
assert(u.host() == "www.example.com");
assert(u.host() == u.encoded_host());
//]
}
{
struct resolve_f {
boost::core::string_view
operator()(boost::core::string_view h)
{
return h;
}
} resolve;
struct write_request_f {
void operator()(boost::core::string_view) {}
void operator()(ipv4_address) {}
void operator()(ipv6_address) {}
} write_request;
//[snippet_parsing_authority_10
url_view u( "https://www.boost.org/users/download/" );
switch (u.host_type())
{
case host_type::name:
write_request(resolve(u.host()));
break;
case host_type::ipv4:
write_request(u.host_ipv4_address());
break;
case host_type::ipv6:
write_request(u.host_ipv6_address());
break;
default:
break;
}
//]
}
{
//[snippet_parsing_authority_10a
url_view u( "https:///path/to_resource" );
assert( u.has_authority() );
assert( u.authority().buffer().empty() );
assert( u.path() == "/path/to_resource" );
//]
}
{
//[snippet_parsing_authority_10b
url_view u( "https://www.boost.org" );
assert( u.host() == "www.boost.org" );
assert( u.path().empty() );
//]
}
{
//[snippet_parsing_authority_10c
url_view u( "https://www.boost.org/users/download/" );
assert( u.host() == "www.boost.org" );
assert( u.path() == "/users/download/" );
//]
}
{
//[snippet_parsing_authority_10d
url_view u( "https://www.boost.org/" );
assert( u.host() == "www.boost.org" );
assert( u.path() == "/" );
//]
}
{
//[snippet_parsing_authority_10e
url_view u( "mailto:John.Doe@example.com" );
assert( !u.has_authority() );
assert( u.path() == "John.Doe@example.com" );
//]
}
{
//[snippet_parsing_authority_10f
url_view u( "mailto://John.Doe@example.com" );
assert( u.authority().buffer() == "John.Doe@example.com" );
assert( u.path().empty() );
//]
}
{
//[snippet_parsing_authority_11a
url_view u( "https://john.doe@www.example.com:123/forum/questions/" );
assert(u.userinfo() == "john.doe");
assert(u.port() == "123");
//]
}
{
//[snippet_parsing_authority_11b
url_view u( "https://john.doe:123456@www.somehost.com/forum/questions/" );
assert(u.userinfo() == "john:doe");
assert(u.user() == "john");
assert(u.password() == "doe");
//]
}
{
//[snippet_parsing_authority_12
authority_view a = parse_authority( "www.example.com:80" ).value();
assert(!a.has_userinfo());
assert(a.host() == "www.example.com");
assert(a.port() == "80");
//]
}
{
//[snippet_parsing_authority_13
authority_view a = parse_authority( "user:pass@www.example.com:443" ).value();
assert(a.userinfo() == "user:pass");
assert(a.user() == "user");
assert(a.password() == "pass");
assert(a.host() == "www.example.com");
assert(a.port() == "443");
//]
}
}
void
parsing_path()
{
{
//[snippet_parsing_path_0
url_view u("http://www.example.com/path/to/file.txt");
assert(u.path() == "/path/to/file.txt");
segments_view segs = u.segments();
auto it = segs.begin();
assert( *it++ == "path" );
assert( *it++ == "to" );
assert( *it == "file.txt" );
//]
boost::ignore_unused(it);
}
{
//[snippet_parsing_path_1
url_view u("https://www.boost.org/doc/the%20libs/");
assert(u.path() == "/doc/the libs/");
assert(u.encoded_path() == "/doc/the%20libs/");
//]
//[snippet_parsing_path_1_b
std::cout << u.encoded_segments().size() << " segments\n";
for (auto seg: u.encoded_segments())
std::cout << "segment: " << seg << "\n";
//]
}
{
//[snippet_parsing_path_2
url_view u("https://www.boost.org/doc/libs");
std::cout << u.segments().size() << " segments\n";
for (auto seg: u.segments())
std::cout << "segment: " << seg << "\n";
//]
}
{
//[snippet_parsing_path_3
url_view u("https://www.boost.org");
assert(u.path().empty());
assert(u.encoded_path().empty());
//]
}
{
//[snippet_parsing_path_3a
assert( url_view("urn:isbn:096139210x").path() == "isbn:096139210x" );
//]
}
{
//[snippet_parsing_path_4
/*
url_view u("https://www.boost.org//doc///libs");
std::cout << u << "\n"
"path: " << u.encoded_path() << "\n"
"encoded segments: " << u.encoded_segments() << "\n"
"segments: " << u.segments() << "\n";
std::cout << u.encoded_segments().size() << " segments\n";
for (auto seg: u.encoded_segments())
std::cout << "segment: " << seg << "\n";
*/
//]
}
{
{
//[snippet_parsing_path_5_a
boost::core::string_view s = "https://www.boost.org";
url_view u(s);
std::cout << u << "\n"
<< "path: " << u.encoded_host() << "\n"
<< "path: " << u.encoded_path() << "\n"
<< "segments: " << u.encoded_segments().size() << "\n";
//]
}
{
//[snippet_parsing_path_5_b
boost::core::string_view s = "https://www.boost.org/";
url_view u(s);
std::cout << u << "\n"
<< "host: " << u.encoded_host() << "\n"
<< "path: " << u.encoded_path() << "\n"
<< "segments: " << u.encoded_segments().size() << "\n";
//]
}
{
//[snippet_parsing_path_5_c
boost::core::string_view s = "https://www.boost.org//";
url_view u(s);
std::cout << u << "\n"
<< "host: " << u.encoded_host() << "\n"
<< "path: " << u.encoded_path() << "\n"
<< "segments: " << u.encoded_segments().size() << "\n";
//]
}
}
{
//[snippet_parsing_path_6
url_view u("https://www.boost.org//doc/libs/");
std::cout << u << "\n"
"authority: " << u.encoded_authority() << "\n"
"path: " << u.encoded_path() << "\n";
std::cout << u.encoded_segments().size() << " segments\n";
for (auto seg: u.encoded_segments())
std::cout << "segment: " << seg << "\n";
//]
}
{
//[snippet_parsing_path_7
url_view u("https://doc/libs/");
std::cout << u << "\n"
"authority: " << u.encoded_authority() << "\n"
"path: " << u.encoded_path() << "\n";
std::cout << u.encoded_segments().size() << " segments\n";
for (auto seg: u.encoded_segments())
std::cout << "segment: " << seg << "\n";
//]
}
{
//[snippet_parsing_path_8
url_view u("https://www.boost.org/doc@folder/libs:boost");
std::cout << u << "\n"
"authority: " << u.encoded_authority() << "\n"
"path: " << u.encoded_path() << "\n";
std::cout << u.encoded_segments().size() << " segments\n";
for (auto seg: u.encoded_segments())
std::cout << "segment: " << seg << "\n";
//]
}
{
//[snippet_parsing_path_9
/*
segments_view segs = parse_path("/doc/libs").value();
assert( segs.size() == 2 );
*/
//]
//boost::ignore_unused(segs);
}
{
//[snippet_parsing_path_use_case_1
url_view u("https://www.boost.org/doc/libs/");
assert(u.host() == "www.boost.org");
assert(u.path() == "/doc/libs/");
//]
boost::ignore_unused(u);
}
{
//[snippet_parsing_path_use_case_2
assert( parse_uri("https://www.boost.org").has_value() );
assert( parse_uri("https://www.boost.org/").has_value() );
assert( parse_uri("https://www.boost.org//").has_value() );
//]
}
{
//[snippet_parsing_path_use_case_3
assert( url_view("https://www.boost.org").path().empty() );
//]
}
{
//[snippet_parsing_path_use_case_4
url_view u("https://www.boost.org/doc@folder/libs:boost");
assert(u.path() == "/doc@folder/libs:boost");
//]
boost::ignore_unused(u);
}
{
//[snippet_parsing_path_use_case_5
url_view u("https://www.boost.org/doc/libs/");
segments_view segs = u.segments();
auto it = segs.begin();
assert(*it++ == "doc");
assert(*it++ == "libs");
assert(*it == "");
//]
boost::ignore_unused(it);
}
{
//[snippet_parsing_path_use_case_6
url_view u("https://www.boost.org//doc///libs");
segments_view segs = u.segments();
auto it = segs.begin();
assert(*it++ == "");
assert(*it++ == "doc");
assert(*it++ == "");
assert(*it++ == "");
assert(*it == "libs");
//]
boost::ignore_unused(it);
}
{
//[snippet_parsing_path_use_case_7
url_view u("https://www.boost.org//doc/libs/");
segments_view segs = u.segments();
auto it = segs.begin();
assert(*it++ == "");
assert(*it++ == "doc");
assert(*it++ == "libs");
assert(*it == "");
//]
boost::ignore_unused(it);
}
{
//[snippet_parsing_path_use_case_8
url_view u("https://doc/libs/");
assert(u.host() == "doc");
segments_view segs = u.segments();
auto it = segs.begin();
assert(*it++ == "libs");
assert(*it == "");
//]
boost::ignore_unused(it);
}
}
void
parsing_query()
{
{
//[snippet_parsing_query_0
url_view u("https://www.example.com/get-customer.php?id=409&name=Joe&individual");
assert( u.query() == "id=409&name=Joe&individual" );
//]
}
{
//[snippet_parsing_query_1
url_view u("https://www.example.com/get-customer.php?id=409&name=Joe&individual");
params_view ps = u.params();
assert(ps.size() == 3);
//]
//[snippet_parsing_query_1a
auto it = ps.begin();
assert((*it).key == "id");
assert((*it).value == "409");
++it;
assert((*it).key == "name");
assert((*it).value == "Joe");
++it;
assert((*it).key == "individual");
assert(!(*it).has_value);
//]
boost::ignore_unused(it);
}
{
//[snippet_parsing_query_2
url_view u("https://www.example.com/get-customer.php?key-1=value-1&key-2=&key-3&&=value-2");
std::cout << u << "\n"
"has query: " << u.has_query() << "\n"
"encoded query: " << u.encoded_query() << "\n"
"query: " << u.query() << "\n";
std::cout << u.encoded_params().size() << " parameters\n";
for (auto p: u.encoded_params())
{
if (p.has_value)
{
std::cout <<
"parameter: <" << p.key <<
", " << p.value << ">\n";
} else {
std::cout << "parameter: " << p.key << "\n";
}
}
//]
}
{
//[snippet_parsing_query_3
url_view u("https://www.example.com/get-customer.php?email=joe@email.com&code=a:2@/!");
std::cout << u << "\n"
"has query: " << u.has_query() << "\n"
"encoded query: " << u.encoded_query() << "\n"
"query: " << u.query() << "\n";
std::cout << u.encoded_params().size() << " parameters\n";
for (auto p: u.encoded_params())
{
if (p.has_value)
{
std::cout <<
"parameter: <" << p.key <<
", " << p.value << ">\n";
} else {
std::cout << "parameter: " << p.key << "\n";
}
}
//]
}
{
//[snippet_parsing_query_4
url_view u("https://www.example.com/get-customer.php?name=joe");
std::cout << u << "\n"
"query: " << u.query() << "\n";
//]
}
{
//[snippet_parsing_query_5
assert(url_view("https://www.example.com/get-customer.php?").has_query());
assert(!url_view("https://www.example.com/get-customer.php").has_query());
//]
}
{
//[snippet_parsing_query_6
url_view u("https://www.example.com/get-customer.php?name=John%20Doe");
std::cout << u << "\n"
"has query: " << u.has_query() << "\n"
"encoded query: " << u.encoded_query() << "\n"
"query: " << u.query() << "\n";
//]
}
{
//[snippet_parsing_query_7
url_view u("https://www.example.com/get-customer.php?name=John%26Doe");
assert(u.query() == "name=John&Doe");
//]
}
{
//[snippet_parsing_query_8
url_view u("https://www.example.com/get-customer.php?key-1=value-1&key-2=&key-3&&=value-4");
params_view ps = u.params();
assert(ps.size() == 5);
//]
//[snippet_parsing_query_8a
auto it = ps.begin();
assert((*it).key == "key-1");
assert((*it).value == "value-1");
//]
//[snippet_parsing_query_8b
++it;
assert((*it).key == "key-2");
assert((*it).value == "");
assert((*it).has_value);
//]
//[snippet_parsing_query_8c
++it;
assert((*it).key == "key-3");
assert(!(*it).has_value);
//]
//[snippet_parsing_query_8d
++it;
assert((*it).key == "");
assert((*it).value == "value-4");
//]
}
{
//[snippet_parsing_query_9
url_view u("https://www.example.com/get-customer.php?email=joe@email.com&code=a:2@/!");
assert(u.has_query());
//]
}
}
void
parsing_fragment()
{
{
//[snippet_parsing_fragment_1
url_view u("https://www.example.com/index.html#section%202");
std::cout << u << "\n"
"has fragment: " << u.has_fragment() << "\n"
"fragment: " << u.fragment() << "\n"
"encoded fragment: " << u.encoded_fragment() << "\n";
//]
}
{
//[snippet_parsing_fragment_2_a
url_view u("https://www.example.com/index.html#");
std::cout << u << "\n"
"has fragment: " << u.has_fragment() << "\n"
"fragment: " << u.fragment() << "\n";
//]
}
{
//[snippet_parsing_fragment_2_b
url_view u("https://www.example.com/index.html");
std::cout << u << "\n"
"has fragment: " << u.has_fragment() << "\n"
"fragment: " << u.fragment() << "\n";
//]
}
{
//[snippet_parsing_fragment_3
url_view u("https://www.example.com/index.html#code%20:a@b?c/d");
std::cout << u << "\n"
"has fragment: " << u.has_fragment() << "\n"
"fragment: " << u.fragment() << "\n";
//]
}
{
//[snippet_parsing_fragment_4
url_view u("https://www.example.com/index.html#section2");
assert(u.fragment() == "section2");
//]
}
{
//[snippet_parsing_fragment_5
assert(url_view("https://www.example.com/index.html#").has_fragment());
assert(!url_view("https://www.example.com/index.html").has_fragment());
//]
}
{
//[snippet_parsing_fragment_6
url_view u("https://www.example.com/index.html#code%20:a@b?c/d");
assert(u.fragment() == "code :a@b?c/d");
//]
}
}
void
using_modifying()
{
{
//[snippet_modifying_1
url_view u("https://www.example.com");
url v(u);
//]
//[snippet_modifying_2
assert(v.scheme() == "https");
assert(v.has_authority());
assert(v.encoded_authority() == "www.example.com");
assert(v.encoded_path() == "");
//]
//[snippet_modifying_3
v.set_host("my website.com");
v.set_path("my file.txt");
v.set_query("id=42&name=John Doe");
assert(v.buffer() == "https://my%20website.com/my%20file.txt?id=42&name=John%20Doe");
//]
//[snippet_modifying_4
v.set_scheme("http");
assert(v.buffer() == "http://my%20website.com/my%20file.txt?id=42&name=John%20Doe");
v.set_encoded_host("www.my%20example.com");
assert(v.buffer() == "http://my%20example.com/my%20file.txt?id=42&name=John%20Doe");
//]
}
}
void
grammar_parse()
{
{
//[snippet_parse_1
// VFALCO we should not show this example
/*
boost::core::string_view s = "http:after_scheme";
const char* it = s.begin();
auto rv = grammar::parse(it, s.end(), scheme_rule() );
if( ! rv )
{
std::cout << "scheme: " << rv->scheme << '\n';
std::cout << "suffix: " << it << '\n';
}
*/
//]
}
{
//[snippet_parse_2
// VFALCO This needs refactoring
/*
boost::core::string_view s = "?key=value#anchor";
const char* it = s.begin();
boost::system::error_code ec;
if (grammar::parse(it, s.end(), ec, r1))
{
auto r2 = grammar::parse( it, s.end(), fragment_part_rule );
if( r2 )
{
std::cout << "query: " << r1.query_part << '\n';
std::cout << "fragment: " << std::get<1>(*r2.value()).encoded() << '\n';
}
}
*/
//]
}
{
//[snippet_parse_3
// VFALCO This needs refactoring
/*
boost::core::string_view s = "?key=value#anchor";
query_part_rule r1;
const char* it = s.begin();
boost::system::error_code ec;
auto r2 = grammar::parse( it, s.end(), ec, fragment_part_rule );
if( ! ec.failed() )
{
std::cout << "query: " << r1.query_part << '\n';
std::cout << "fragment: " << r2.fragment.encoded() << '\n';
}
*/
//]
}
{
//[snippet_parse_4
/* VFALCO This will be removed
boost::core::string_view s = "http://www.boost.org";
uri_rule r;
boost::system::error_code ec;
if (grammar::parse_string(s, ec, r))
{
std::cout << "scheme: " << r.scheme_part.scheme << '\n';
std::cout << "host: " << r.hier_part.authority.host.host_part << '\n';
}
*/
//]
}
}
//[snippet_customization_1
/* VFALCO This needs rewriting
struct lowercase_rule
{
boost::core::string_view str;
friend
void
tag_invoke(
grammar::parse_tag const&,
char const*& it,
char const* const end,
boost::system::error_code& ec,
lowercase_rule& t) noexcept
{
ec = {};
char const* begin = it;
while (it != end && std::islower(*it))
{
++it;
}
t.str = boost::core::string_view(begin, it);
}
};
*/
//]
void
grammar_customization()
{
{
//[snippet_customization_2
// VFALCO THIS NEEDS TO BE PORTED
/*
boost::core::string_view s = "http:somelowercase";
scheme_rule r1;
lowercase_rule r2;
boost::system::error_code ec;
if (grammar::parse_string(s, ec, r1, ':', r2))
{
std::cout << "scheme: " << r1.scheme << '\n';
std::cout << "lower: " << r2.str << '\n';
}
*/
//]
}
}
//[code_charset_1
struct CharSet
{
bool operator()( char c ) const noexcept;
// These are both optional. If either or both are left
// unspecified, a default implementation will be used.
//
char const* find_if( char const* first, char const* last ) const noexcept;
char const* find_if_not( char const* first, char const* last ) const noexcept;
};
//]
void
modifying_path()
{
{
//[snippet_modifying_path_1
url_view u("https://www.boost.org");
//]
BOOST_TEST_NOT(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 0u);
}
{
//[snippet_modifying_path_2
url_view u("https://www.boost.org/");
//]
BOOST_TEST(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 0u);
}
{
//[snippet_modifying_path_1_2
assert( !url_view("https://www.boost.org").is_path_absolute() );
assert( url_view("https://www.boost.org/").is_path_absolute() );
//]
}
{
//[snippet_modifying_path_3
url u("https://www.boost.org/./a/../b");
u.normalize();
assert(u.buffer() == "https://www.boost.org/b");
//]
BOOST_TEST(u.is_path_absolute());
BOOST_TEST_EQ(u.buffer(), "https://www.boost.org/b");
BOOST_TEST_EQ(u.encoded_segments().size(), 1u);
}
{
//[snippet_modifying_path_4
// scheme and a relative path
url_view u("https:path/to/file.txt");
//]
BOOST_TEST_EQ(u.scheme(), "https");
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST_NOT(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 3u);
}
{
//[snippet_modifying_path_5
// scheme and an absolute path
url_view u("https:/path/to/file.txt");
//]
BOOST_TEST_EQ(u.scheme(), "https");
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 3u);
}
{
//[snippet_modifying_path_6
// "//path" will be considered the authority component
url_view u("https://path/to/file.txt");
//]
BOOST_TEST_EQ(u.scheme(), "https");
BOOST_TEST(u.has_authority());
BOOST_TEST(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
}
{
//[snippet_modifying_path_4_5_6
// scheme and a relative path
url_view u1("https:path/to/file.txt");
assert(!u1.has_authority());
assert(!u1.is_path_absolute());
assert(u1.path() == "path/to/file.txt");
// scheme and an absolute path
url_view u2("https:/path/to/file.txt");
assert(!u2.has_authority());
assert(u2.is_path_absolute());
assert(u2.path() == "/path/to/file.txt");
// "//path" will be considered the authority component
url_view u3("https://path/to/file.txt");
assert(u3.has_authority());
assert(u3.authority().buffer() == "path");
assert(u3.is_path_absolute());
assert(u3.path() == "/to/file.txt");
//]
}
{
//[snippet_modifying_path_7
// only a relative path
url_view u = parse_uri_reference("path-to/file.txt").value();
//]
BOOST_TEST_NOT(u.has_scheme());
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST_NOT(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
}
{
//[snippet_modifying_path_8
// "path:" will be considered the scheme component
// instead of a substring of the first segment
url_view u = parse_uri_reference("path:to/file.txt").value();
//]
BOOST_TEST(u.has_scheme());
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST_NOT(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
}
{
//[snippet_modifying_path_7_8
// only a relative path
url_view u1 = parse_uri_reference("path-to/file.txt").value();
assert(!u1.has_scheme());
assert(u1.path() == "path-to/file.txt");
// "path:" will be considered the scheme component
// instead of a substring of the first segment
url_view u2 = parse_uri_reference("path:to/file.txt").value();
assert(u2.has_scheme());
assert(u2.path() == "to/file.txt");
//]
}
{
//[snippet_modifying_path_9
// "path" should not become the authority component
url u = parse_uri("https:path/to/file.txt").value();
u.set_encoded_path("//path/to/file.txt");
//]
BOOST_TEST_EQ(u.scheme(), "https");
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 4u);
}
{
//[snippet_modifying_path_10
// "path:to" should not make the scheme become "path:"
url u = parse_uri_reference("path-to/file.txt").value();
u.set_encoded_path("path:to/file.txt");
//]
BOOST_TEST_NOT(u.has_scheme());
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST_NOT(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 2u);
}
{
//[snippet_modifying_path_9_10
url u1("https:path/to/file.txt" );
u1.set_encoded_path("//path/to/file.txt");
assert(u1.buffer() == "https:/.//path/to/file.txt");
//]
}
{
//[snippet_modifying_path_11
// should not insert as "pathto/file.txt"
url u = parse_uri_reference("to/file.txt").value();
segments_ref segs = u.segments();
segs.insert(segs.begin(), "path");
assert(u.buffer() == "path/to/file.txt");
//]
BOOST_TEST_NOT(u.has_scheme());
BOOST_TEST_NOT(u.has_authority());
BOOST_TEST_NOT(u.is_path_absolute());
BOOST_TEST_EQ(u.encoded_segments().size(), 3u);
}
}
void
normalizing()
{
{
//[snippet_normalizing_1
url_view u1("https://www.boost.org/index.html");
url_view u2("https://www.boost.org/doc/../index.html");
assert(u1.buffer() != u2.buffer());
//]
}
{
//[snippet_normalizing_2
url_view u1("https://www.boost.org/index.html");
url_view u2("https://www.boost.org/doc/../index.html");
assert(u1 == u2);
//]
}
{
//[snippet_normalizing_3
url_view u1("https://www.boost.org/index.html");
url u2("https://www.boost.org/doc/../index.html");
assert(u1.buffer() != u2.buffer());
assert(u1 == u2);
u2.normalize();
assert(u1.buffer() == u2.buffer());
assert(u1 == u2);
//]
}
{
//[snippet_normalizing_4
url u("https://www.boost.org/doc/../%69%6e%64%65%78%20file.html");
u.normalize();
assert(u.buffer() == "https://www.boost.org/index%20file.html");
//]
}
{
//[snippet_normalizing_5
auto normalize_http_url =
[](url& u)
{
u.normalize();
if (u.port() == "80" ||
u.port().empty())
u.remove_port();
if (u.has_authority() &&
u.encoded_path().empty())
u.set_path_absolute(true);
};
url u1("https://www.boost.org");
normalize_http_url(u1);
url u2("https://www.boost.org/");
normalize_http_url(u2);
url u3("https://www.boost.org:/");
normalize_http_url(u3);
url u4("https://www.boost.org:80/");
normalize_http_url(u4);
assert(u1.buffer() == "https://www.boost.org/");
assert(u2.buffer() == "https://www.boost.org/");
assert(u3.buffer() == "https://www.boost.org/");
assert(u4.buffer() == "https://www.boost.org/");
//]
}
}
void
decode_with_token()
{
{
//[snippet_string_token_1
url_view u("http://www.example.com/my%20file.txt");
pct_string_view sv = u.encoded_path();
assert(sv == "/my%20file.txt");
std::string s = u.path();
assert(s == "/my file.txt");
//]
}
{
//[snippet_string_token_2
url_view u("http://www.example.com/my%20file.txt");
std::string s = "existing string";
u.path(string_token::assign_to(s));
assert(s == "/my file.txt");
//]
}
{
//[snippet_string_token_3
url_view u("http://www.example.com/my%20file.txt");
std::string s = "existing string";
u.path(string_token::append_to(s));
assert(s == "existing string/my file.txt");
//]
}
{
//[snippet_string_token_4
url_view u("http://www.example.com/my%20file.txt");
std::string s = "existing string";
boost::core::string_view sv = u.path(string_token::preserve_size(s));
assert(sv == "/my file.txt");
//]
}
{
#if defined(__cpp_static_assert) && __cpp_static_assert >= 201411L
//[snippet_string_token_5
static_assert(
string_token::is_token<string_token::return_string>::value);
//]
#endif
}
}
void
encoding()
{
{
//[snippet_encoding_1
std::string s = encode("hello world!", unreserved_chars);
assert(s == "hello%20world%21");
//]
}
{
//[snippet_encoding_2
encoding_opts opt;
opt.space_as_plus = true;
std::string s = encode("msg=hello world", pchars, opt);
assert(s == "msg=hello+world");
//]
}
{
//[snippet_encoding_3
std::string s;
encode("hello ", pchars, {}, string_token::assign_to(s));
encode("world", pchars, {}, string_token::append_to(s));
assert(s == "hello%20world");
//]
}
{
//[snippet_encoding_4
boost::core::string_view e = "hello world";
std::string s;
s.reserve(encoded_size(e, pchars));
encode(e, pchars, {}, string_token::assign_to(s));
assert(s == "hello%20world");
//]
}
{
//[snippet_encoding_5
boost::core::string_view e = "hello world";
std::string s;
s.resize(encoded_size(e, pchars));
encode(&s[0], s.size(), e, pchars);
assert(s == "hello%20world");
//]
}
{
//[snippet_encoding_6
pct_string_view sv = "hello%20world";
assert(sv == "hello%20world");
//]
}
{
//[snippet_encoding_7
boost::system::result<pct_string_view> rs =
make_pct_string_view("hello%20world");
assert(rs.has_value());
pct_string_view sv = rs.value();
assert(sv == "hello%20world");
//]
}
{
//[snippet_encoding_8
pct_string_view s = "path/to/file";
url u;
u.set_encoded_path(s);
assert(u.buffer() == "path/to/file");
//]
}
{
//[snippet_encoding_9
url u;
u.set_encoded_path("path/to/file");
assert(u.buffer() == "path/to/file");
//]
}
{
//[snippet_encoding_10
url_view uv("path/to/file");
url u;
u.set_encoded_path(uv.encoded_path());
assert(u.buffer() == "path/to/file");
//]
}
{
//[snippet_encoding_11
pct_string_view es("hello%20world");
assert(es == "hello%20world");
decode_view dv("hello%20world");
assert(dv == "hello world");
//]
}
{
//[snippet_encoding_12
boost::system::result<pct_string_view> rs =
make_pct_string_view("hello%20world");
assert(rs.has_value());
pct_string_view s = rs.value();
decode_view dv = *s;
assert(dv == "hello world");
//]
}
{
//[snippet_encoding_13
url_view u =
parse_relative_ref("user/john%20doe/profile%20photo.jpg").value();
std::vector<std::string> route =
{"user", "john doe", "profile photo.jpg"};
auto segs = u.encoded_segments();
auto it0 = segs.begin();
auto end0 = segs.end();
auto it1 = route.begin();
auto end1 = route.end();
while (
it0 != end0 &&
it1 != end1)
{
pct_string_view seg0 = *it0;
decode_view dseg0 = *seg0;
boost::core::string_view seg1 = *it1;
if (dseg0 == seg1)
{
++it0;
++it1;
}
else
{
break;
}
}
bool route_match = it0 == end0 && it1 == end1;
assert(route_match);
//]
}
{
//[snippet_encoding_14
pct_string_view s = "user/john%20doe/profile%20photo.jpg";
std::string buf;
buf.resize(s.decoded_size());
s.decode({}, string_token::assign_to(buf));
assert(buf == "user/john doe/profile photo.jpg");
//]
}
}
void
readme_snippets()
{
// Parse a URL. This allocates no memory. The view
// references the character buffer without taking ownership.
//
url_view uv( "https://www.example.com/path/to/file.txt?id=1001&name=John%20Doe&results=full" );
// Print the query parameters with percent-decoding applied
//
for( auto v : uv.params() )
{
std::cout << v.key << "=" << v.value << " ";
}
// Prints: id=1001 name=John Doe results=full
// Create a modifiable copy of `uv`, with ownership of the buffer
//
url u = uv;
// Change some elements in the URL
//
u.set_scheme( "http" )
.set_encoded_host( "boost.org" )
.set_encoded_path( "/index.htm" )
.remove_query()
.remove_fragment()
.params().append( {"key", "value"} );
std::cout << u;
}
//[snippet_using_static_pool_1
// VFALCO NOPE
//]
namespace boost {
namespace urls {
class snippets_test
{
public:
void
run()
{
ignore_unused(&using_url_views);
ignore_unused(&using_urls);
// parsing_urls();
parsing_components();
formatting_components();
ignore_unused(&parsing_scheme);
ignore_unused(&parsing_authority);
ignore_unused(&parsing_path);
ignore_unused(&parsing_query);
ignore_unused(&parsing_fragment);
ignore_unused(&using_modifying);
ignore_unused(&grammar_parse);
ignore_unused(&grammar_customization);
ignore_unused(&modifying_path);
normalizing();
decode_with_token();
encoding();
ignore_unused(&readme_snippets);
BOOST_TEST_PASS();
}
};
TEST_SUITE(snippets_test, "boost.url.snippets");
} // urls
} // boost
|