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
|
/** @file
*
* A brief file description
*
* @section license License
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "HTTP.h"
#include "XPACK.h"
#include "QPACK.h"
#include "tscore/ink_defs.h"
#include "tscore/ink_memory.h"
#define QPACKDebug(fmt, ...) Debug("qpack", "[%s] " fmt, this->_qc->cids().data(), ##__VA_ARGS__)
#define QPACKDTDebug(fmt, ...) Debug("qpack", "" fmt, ##__VA_ARGS__)
// qpack-05 Appendix A.
const QPACK::Header QPACK::StaticTable::STATIC_HEADER_FIELDS[] = {
{":authority", ""},
{":path", "/"},
{"age", "0"},
{"content-disposition", ""},
{"content-length", "0"},
{"cookie", ""},
{"date", ""},
{"etag", ""},
{"if-modified-since", ""},
{"if-none-match", ""},
{"last-modified", ""},
{"link", ""},
{"location", ""},
{"referer", ""},
{"set-cookie", ""},
{":method", "CONNECT"},
{":method", "DELETE"},
{":method", "GET"},
{":method", "HEAD"},
{":method", "OPTIONS"},
{":method", "POST"},
{":method", "PUT"},
{":scheme", "http"},
{":scheme", "https"},
{":status", "103"},
{":status", "200"},
{":status", "304"},
{":status", "404"},
{":status", "503"},
{"accept", "*/*"},
{"accept", "application/dns-message"},
{"accept-encoding", "gzip, deflate, br"},
{"accept-ranges", "bytes"},
{"access-control-allow-headers", "cache-control"},
{"access-control-allow-headers", "content-type"},
{"access-control-allow-origin", "*"},
{"cache-control", "max-age=0"},
{"cache-control", "max-age=2592000"},
{"cache-control", "max-age=604800"},
{"cache-control", "no-cache"},
{"cache-control", "no-store"},
{"cache-control", "public, max-age=31536000"},
{"content-encoding", "br"},
{"content-encoding", "gzip"},
{"content-type", "application/dns-message"},
{"content-type", "application/javascript"},
{"content-type", "application/json"},
{"content-type", "application/x-www-form-urlencoded"},
{"content-type", "image/gif"},
{"content-type", "image/jpeg"},
{"content-type", "image/png"},
{"content-type", "text/css"},
{"content-type", "text/html; charset=utf-8"},
{"content-type", "text/plain"},
{"content-type", "text/plain;charset=utf-8"},
{"range", "bytes=0-"},
{"strict-transport-security", "max-age=31536000"},
{"strict-transport-security", "max-age=31536000; includesubdomains"},
{"strict-transport-security", "max-age=31536000; includesubdomains; preload"},
{"vary", "accept-encoding"},
{"vary", "origin"},
{"x-content-type-options", "nosniff"},
{"x-xss-protection", "1; mode=block"},
{":status", "100"},
{":status", "204"},
{":status", "206"},
{":status", "302"},
{":status", "400"},
{":status", "403"},
{":status", "421"},
{":status", "425"},
{":status", "500"},
{"accept-language", ""},
{"access-control-allow-credentials", "FALSE"},
{"access-control-allow-credentials", "TRUE"},
{"access-control-allow-headers", "*"},
{"access-control-allow-methods", "get"},
{"access-control-allow-methods", "get, post, options"},
{"access-control-allow-methods", "options"},
{"access-control-expose-headers", "content-length"},
{"access-control-request-headers", "content-type"},
{"access-control-request-method", "get"},
{"access-control-request-method", "post"},
{"alt-svc", "clear"},
{"authorization", ""},
{"content-security-policy", "script-src 'none'; object-src 'none'; base-uri 'none'"},
{"early-data", "1"},
{"expect-ct", ""},
{"forwarded", ""},
{"if-range", ""},
{"origin", ""},
{"purpose", "prefetch"},
{"server", ""},
{"timing-allow-origin", "*"},
{"upgrade-insecure-requests", "1"},
{"user-agent", ""},
{"x-forwarded-for", ""},
{"x-frame-options", "deny"},
{"x-frame-options", "sameorigin"}};
QPACK::QPACK(QUICConnection *qc, uint32_t max_header_list_size, uint16_t max_table_size, uint16_t max_blocking_streams)
: QUICApplication(qc),
_dynamic_table(max_table_size),
_max_header_list_size(max_header_list_size),
_max_table_size(max_table_size),
_max_blocking_streams(max_blocking_streams)
{
SET_HANDLER(&QPACK::event_handler);
this->_encoder_stream_sending_instructions = new_MIOBuffer(BUFFER_SIZE_INDEX_1K);
this->_decoder_stream_sending_instructions = new_MIOBuffer(BUFFER_SIZE_INDEX_1K);
this->_encoder_stream_sending_instructions_reader = this->_encoder_stream_sending_instructions->alloc_reader();
this->_decoder_stream_sending_instructions_reader = this->_decoder_stream_sending_instructions->alloc_reader();
}
QPACK::~QPACK() {}
void
QPACK::on_new_stream(QUICStream &stream)
{
auto *info = new QUICStreamVCAdapter::IOInfo(stream);
switch (stream.direction()) {
case QUICStreamDirection::BIDIRECTIONAL:
// ink_assert(!"QPACK does not use bidirectional streams");
// QPACK offline interop uses stream 0 as a encoder stream.
info->setup_write_vio(this);
info->setup_read_vio(this);
break;
case QUICStreamDirection::SEND:
info->setup_write_vio(this);
break;
case QUICStreamDirection::RECEIVE:
info->setup_read_vio(this);
break;
default:
ink_assert(false);
break;
}
stream.set_io_adapter(&info->adapter);
}
int
QPACK::event_handler(int event, Event *data)
{
VIO *vio = reinterpret_cast<VIO *>(data);
int ret;
switch (event) {
case VC_EVENT_READ_READY:
ret = this->_on_read_ready(vio);
break;
case VC_EVENT_READ_COMPLETE:
ret = EVENT_DONE;
break;
case VC_EVENT_WRITE_READY:
ret = this->_on_write_ready(vio);
break;
case VC_EVENT_WRITE_COMPLETE:
ret = EVENT_DONE;
break;
default:
ret = EVENT_DONE;
}
return ret;
}
int
QPACK::encode(uint64_t stream_id, HTTPHdr &header_set, MIOBuffer *header_block, uint64_t &header_block_len)
{
if (!header_block) {
return -1;
}
uint16_t base_index = this->_largest_known_received_index;
// Compress headers and record the largest reference
uint16_t referred_index = 0;
uint16_t largest_reference = 0;
uint16_t smallest_reference = 0;
IOBufferBlock *compressed_headers = new_IOBufferBlock();
compressed_headers->alloc(BUFFER_SIZE_INDEX_2K);
for (auto &field : header_set) {
int ret = this->_encode_header(field, base_index, compressed_headers, referred_index);
largest_reference = std::max(largest_reference, referred_index);
smallest_reference = std::min(smallest_reference, referred_index);
if (ret < 0) {
compressed_headers->free();
return ret;
}
}
struct EntryReference eref = {smallest_reference, largest_reference};
this->_references.emplace(stream_id, eref);
// Make an IOBufferBlock for Header Data Prefix
IOBufferBlock *header_data_prefix = new_IOBufferBlock();
header_data_prefix->alloc(BUFFER_SIZE_INDEX_128);
this->_encode_prefix(largest_reference, base_index, header_data_prefix);
header_block->append_block(header_data_prefix);
header_block_len += header_data_prefix->size();
header_block->append_block(compressed_headers);
header_block_len += compressed_headers->size();
return 0;
}
int
QPACK::decode(uint64_t stream_id, const uint8_t *header_block, size_t header_block_len, HTTPHdr &hdr, Continuation *cont,
EThread *thread)
{
if (!cont || !header_block) {
return -1;
}
if (this->_invalid) {
thread->schedule_imm(cont, QPACK_EVENT_DECODE_FAILED, nullptr);
return -1;
}
uint64_t tmp = 0;
int64_t ret = xpack_decode_integer(tmp, header_block, header_block + header_block_len, 8);
if (ret < 0 && tmp > 0xFFFF) {
return -1;
}
uint16_t largest_reference = tmp;
if (this->_dynamic_table.largest_index() < largest_reference) {
// Blocked
if (this->_add_to_blocked_list(
new DecodeRequest(largest_reference, thread, cont, stream_id, header_block, header_block_len, hdr))) {
return 1;
} else {
// Number of blocked streams exceed the limit
return -2;
}
}
this->_decode(thread, cont, stream_id, header_block, header_block_len, hdr);
return 0;
}
void
QPACK::set_encoder_stream(QUICStreamId id)
{
this->_encoder_stream_id = id;
}
void
QPACK::set_decoder_stream(QUICStreamId id)
{
this->_decoder_stream_id = id;
}
void
QPACK::update_max_header_list_size(uint32_t max_header_list_size)
{
this->_max_header_list_size = max_header_list_size;
}
void
QPACK::update_max_table_size(uint16_t max_table_size)
{
this->_max_table_size = max_table_size;
}
void
QPACK::update_max_blocking_streams(uint16_t max_blocking_streams)
{
this->_max_blocking_streams = max_blocking_streams;
}
int
QPACK::_encode_prefix(uint16_t largest_reference, uint16_t base_index, IOBufferBlock *prefix)
{
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(prefix->end()),
reinterpret_cast<uint8_t *>(prefix->end() + prefix->write_avail()), largest_reference, 8)) < 0) {
return -1;
}
prefix->fill(ret);
uint16_t delta;
prefix->end()[0] = 0x0;
if (base_index < largest_reference) {
prefix->end()[0] |= 0x80;
delta = largest_reference - base_index;
} else {
delta = base_index - largest_reference;
}
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(prefix->end()),
reinterpret_cast<uint8_t *>(prefix->end() + prefix->write_avail()), delta, 7)) < 0) {
return -2;
}
prefix->fill(ret);
QPACKDebug("Encoded Header Data Prefix: largest_ref=%d, base_index=%d, delta=%d", largest_reference, base_index, delta);
return 0;
}
int
QPACK::_encode_header(const MIMEField &field, uint16_t base_index, IOBufferBlock *compressed_header, uint16_t &referred_index)
{
Arena arena;
int name_len;
const char *name = field.name_get(&name_len);
char *lowered_name = arena.str_store(name, name_len);
for (int i = 0; i < name_len; i++) {
lowered_name[i] = ParseRules::ink_tolower(lowered_name[i]);
}
int value_len;
const char *value = field.value_get(&value_len);
// TODO Set never_index flag on/off according to encoding headers
bool never_index = false;
// Find from tables, and insert / duplicate a entry prior to encode it
LookupResult lookup_result_static;
LookupResult lookup_result_dynamic;
lookup_result_static = StaticTable::lookup(lowered_name, name_len, value, value_len);
if (lookup_result_static.match_type != LookupResult::MatchType::EXACT) {
lookup_result_dynamic = this->_dynamic_table.lookup(lowered_name, name_len, value, value_len);
if (lookup_result_dynamic.match_type == LookupResult::MatchType::EXACT) {
if (this->_dynamic_table.should_duplicate(lookup_result_dynamic.index)) {
// Duplicate an entry and use the new entry
uint16_t current_index = lookup_result_dynamic.index;
lookup_result_dynamic = this->_dynamic_table.duplicate_entry(current_index);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_duplicate(current_index);
QPACKDebug("Wrote Duplicate: current_index=%d", current_index);
this->_dynamic_table.ref_entry(current_index);
}
}
} else if (lookup_result_static.match_type == LookupResult::MatchType::NAME) {
if (never_index) {
// Name in static table is always available. Do nothing.
} else {
// Insert both the name and the value
lookup_result_dynamic = this->_dynamic_table.insert_entry(lowered_name, name_len, value, value_len);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_insert_with_name_ref(lookup_result_static.index, false, value, value_len);
QPACKDebug("Wrote Insert With Name Ref: index=%u, dynamic_table=%d value=%.*s", lookup_result_static.index, false,
value_len, value);
}
}
} else if (lookup_result_dynamic.match_type == LookupResult::MatchType::NAME) {
if (never_index) {
if (this->_dynamic_table.should_duplicate(lookup_result_dynamic.index)) {
// Duplicate an entry and use the new entry
uint16_t current_index = lookup_result_dynamic.index;
lookup_result_dynamic = this->_dynamic_table.duplicate_entry(current_index);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_duplicate(current_index);
QPACKDebug("Wrote Duplicate: current_index=%d", current_index);
this->_dynamic_table.ref_entry(current_index);
}
}
} else {
if (this->_dynamic_table.should_duplicate(lookup_result_dynamic.index)) {
// Duplicate an entry and use the new entry
uint16_t current_index = lookup_result_dynamic.index;
lookup_result_dynamic = this->_dynamic_table.duplicate_entry(current_index);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_duplicate(current_index);
QPACKDebug("Wrote Duplicate: current_index=%d", current_index);
this->_dynamic_table.ref_entry(current_index);
}
} else {
// Insert both the name and the value
uint16_t current_index = lookup_result_dynamic.index;
lookup_result_dynamic = this->_dynamic_table.insert_entry(lowered_name, name_len, value, value_len);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_insert_with_name_ref(current_index, true, value, value_len);
QPACKDebug("Wrote Insert With Name Ref: index=%u, dynamic_table=%d, value=%.*s", current_index, true, value_len, value);
}
}
}
} else {
if (never_index) {
// Insert only the name
lookup_result_dynamic = this->_dynamic_table.insert_entry(lowered_name, name_len, "", 0);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_insert_without_name_ref(lowered_name, name_len, "", 0);
QPACKDebug("Wrote Insert Without Name Ref: name=%.*s value=%.*s", name_len, lowered_name, 0, "");
}
} else {
// Insert both the name and the value
lookup_result_dynamic = this->_dynamic_table.insert_entry(lowered_name, name_len, value, value_len);
if (lookup_result_dynamic.match_type != LookupResult::MatchType::NONE) {
this->_write_insert_without_name_ref(lowered_name, name_len, value, value_len);
QPACKDebug("Wrote Insert Without Name Ref: name=%.*s value=%.*s", name_len, lowered_name, value_len, value);
}
}
}
}
// Encode
if (lookup_result_static.match_type == LookupResult::MatchType::EXACT) {
this->_encode_indexed_header_field(lookup_result_static.index, base_index, false, compressed_header);
QPACKDebug("Encoded Indexed Header Field: abs_index=%d, base_index=%d, dynamic_table=%d", lookup_result_static.index,
base_index, false);
referred_index = 0;
} else if (lookup_result_dynamic.match_type == LookupResult::MatchType::EXACT) {
if (lookup_result_dynamic.index < this->_largest_known_received_index) {
this->_encode_indexed_header_field(lookup_result_dynamic.index, base_index, true, compressed_header);
QPACKDebug("Encoded Indexed Header Field: abs_index=%d, base_index=%d, dynamic_table=%d", lookup_result_dynamic.index,
base_index, true);
} else {
this->_encode_indexed_header_field_with_postbase_index(lookup_result_dynamic.index, base_index, never_index,
compressed_header);
QPACKDebug("Encoded Indexed Header With Postbase Index: abs_index=%d, base_index=%d, never_index=%d",
lookup_result_dynamic.index, base_index, never_index);
}
this->_dynamic_table.ref_entry(lookup_result_dynamic.index);
referred_index = lookup_result_dynamic.index;
} else if (lookup_result_static.match_type == LookupResult::MatchType::NAME) {
this->_encode_literal_header_field_with_name_ref(lookup_result_static.index, false, base_index, value, value_len, never_index,
compressed_header);
QPACKDebug(
"Encoded Literal Header Field With Name Ref: abs_index=%d, base_index=%d, dynamic_table=%d, value=%.*s, never_index=%d",
lookup_result_static.index, base_index, false, value_len, value, never_index);
referred_index = 0;
} else if (lookup_result_dynamic.match_type == LookupResult::MatchType::NAME) {
if (lookup_result_dynamic.index <= this->_largest_known_received_index) {
this->_encode_literal_header_field_with_name_ref(lookup_result_dynamic.index, true, base_index, value, value_len, never_index,
compressed_header);
QPACKDebug(
"Encoded Literal Header Field With Name Ref: abs_index=%d, base_index=%d, dynamic_table=%d, value=%.*s, never_index=%d",
lookup_result_dynamic.index, base_index, true, value_len, value, never_index);
} else {
this->_encode_literal_header_field_with_postbase_name_ref(lookup_result_dynamic.index, base_index, value, value_len,
never_index, compressed_header);
QPACKDebug("Encoded Literal Header Field With Postbase Name Ref: abs_index=%d, base_index=%d, value=%.*s, never_index=%d",
lookup_result_dynamic.index, base_index, value_len, value, never_index);
}
this->_dynamic_table.ref_entry(lookup_result_dynamic.index);
referred_index = lookup_result_dynamic.index;
} else {
this->_encode_literal_header_field_without_name_ref(lowered_name, name_len, value, value_len, never_index, compressed_header);
QPACKDebug("Encoded Literal Header Field Without Name Ref: name=%.*s, value=%.*s, never_index=%d", name_len, lowered_name,
value_len, value, never_index);
}
return 0;
}
int
QPACK::_encode_indexed_header_field(uint16_t index, uint16_t base_index, bool dynamic_table, IOBufferBlock *compressed_header)
{
char *buf = compressed_header->end();
char *buf_end = buf + compressed_header->write_avail();
int written = 0;
// Indexed Header Field
buf[0] = 0x80;
// References static table or not
if (dynamic_table) {
// Use relative index if we refer Dynamic Table
index = this->_calc_relative_index_from_absolute_index(base_index, index);
} else {
buf[0] |= 0x40;
}
// Index
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 6)) <
0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
compressed_header->fill(written);
return 0;
}
int
QPACK::_encode_indexed_header_field_with_postbase_index(uint16_t index, uint16_t base_index, bool never_index,
IOBufferBlock *compressed_header)
{
char *buf = compressed_header->end();
char *buf_end = buf + compressed_header->write_avail();
int written = 0;
// Indexed Header Field with Post-Base Index
buf[0] = 0x10;
// Index
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end),
this->_calc_postbase_index_from_absolute_index(base_index, index), 4)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
compressed_header->fill(written);
return 0;
}
int
QPACK::_encode_literal_header_field_with_name_ref(uint16_t index, bool dynamic_table, uint16_t base_index, const char *value,
int value_len, bool never_index, IOBufferBlock *compressed_header)
{
char *buf = compressed_header->end();
char *buf_end = buf + compressed_header->write_avail();
int written = 0;
// Literal Header Field With Name Reference
buf[0] = 0x40;
if (never_index) {
buf[0] |= 0x20;
}
// References static table or not
if (dynamic_table) {
// Use relative index if we refer Dynamic Table
index = this->_calc_relative_index_from_absolute_index(base_index, index);
} else {
buf[0] |= 0x10;
}
// Index
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 4)) <
0) {
return ret;
}
written += ret;
// Value
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value,
value_len)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
compressed_header->fill(written);
return 0;
}
int
QPACK::_encode_literal_header_field_without_name_ref(const char *name, int name_len, const char *value, int value_len,
bool never_index, IOBufferBlock *compressed_header)
{
char *buf = compressed_header->end();
char *buf_end = buf + compressed_header->write_avail();
int written = 0;
// Literal Header Field Without Name Reference
buf[0] = 0x20;
if (never_index) {
buf[0] |= 0x10;
}
// Name
int ret;
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), name, name_len,
3)) < 0) {
return ret;
}
written += ret;
// Value
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
7)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
compressed_header->fill(written);
return 0;
}
int
QPACK::_encode_literal_header_field_with_postbase_name_ref(uint16_t index, uint16_t base_index, const char *value, int value_len,
bool never_index, IOBufferBlock *compressed_header)
{
char *buf = compressed_header->end();
char *buf_end = buf + compressed_header->write_avail();
int written = 0;
// Literal Header Field With Post-Base Name Reference
buf[0] = 0x00;
if (never_index) {
buf[0] |= 0x08;
}
// Index
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end),
this->_calc_postbase_index_from_absolute_index(base_index, index), 3)) < 0) {
return ret;
}
written += ret;
// Value
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
7)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
compressed_header->fill(written);
return 0;
}
int
QPACK::_decode_indexed_header_field(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr, uint32_t &header_len)
{
// Read index field
int len = 0;
uint64_t index;
int ret = xpack_decode_integer(index, buf, buf + buf_len, 6);
if (ret < 0) {
return -1;
}
len += ret;
// Lookup a table
const char *name = nullptr;
int name_len = 0;
const char *value = nullptr;
int value_len = 0;
QPACK::LookupResult result;
if (buf[0] & 0x40) { // Static table
result = StaticTable::lookup(index, &name, &name_len, &value, &value_len);
} else { // Dynamic table
result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_relative_index(base_index, index), &name, &name_len,
&value, &value_len);
}
if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
return -1;
}
// Create and attach a header
this->_attach_header(hdr, name, name_len, value, value_len, false);
header_len = name_len + value_len;
QPACKDebug("Decoded Indexed Header Field: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index, result.index, name_len,
name, value_len, value);
return len;
}
int
QPACK::_decode_literal_header_field_with_name_ref(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr,
uint32_t &header_len)
{
int read_len = 0;
// Never index field
bool never_index = false;
if (buf[0] & 0x20) {
never_index = true;
}
// Read name index field
uint64_t index;
int ret = xpack_decode_integer(index, buf, buf + buf_len, 4);
if (ret < 0) {
return -1;
}
read_len += ret;
// Lookup the name
const char *name = nullptr;
int name_len = 0;
const char *dummy = nullptr;
int dummy_len = 0;
QPACK::LookupResult result;
if (buf[0] & 0x10) { // Static table
result = StaticTable::lookup(index, &name, &name_len, &dummy, &dummy_len);
} else { // Dynamic table
result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_relative_index(base_index, index), &name, &name_len,
&dummy, &dummy_len);
}
if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
return -1;
}
// Read value
Arena arena;
char *value;
uint64_t value_len;
if ((ret = xpack_decode_string(arena, &value, value_len, buf + read_len, buf + buf_len, 7)) < 0) {
return -1;
}
read_len += ret;
// Create and attach a header
this->_attach_header(hdr, name, name_len, value, value_len, never_index);
header_len = name_len + value_len;
QPACKDebug("Decoded Literal Header Field With Name Ref: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
result.index, name_len, name, static_cast<int>(value_len), value);
return read_len;
}
int
QPACK::_decode_literal_header_field_without_name_ref(const uint8_t *buf, size_t buf_len, HTTPHdr &hdr, uint32_t &header_len)
{
int read_len = 0;
// Never index field
bool never_index = false;
if (buf[0] & 0x10) {
never_index = true;
}
// Read name and value
Arena arena;
int64_t ret;
char *name;
uint64_t name_len;
if ((ret = xpack_decode_string(arena, &name, name_len, buf, buf + buf_len, 3)) < 0) {
return -1;
}
read_len += ret;
char *value;
uint64_t value_len;
if ((ret = xpack_decode_string(arena, &value, value_len, buf + read_len, buf + buf_len, 7)) < 0) {
return -1;
}
read_len += ret;
// Create and attach a header
this->_attach_header(hdr, name, name_len, value, value_len, never_index);
header_len = name_len + value_len;
QPACKDebug("Decoded Literal Header Field Without Name Ref: name=%.*s, value=%.*s", static_cast<uint16_t>(name_len), name,
static_cast<uint16_t>(value_len), value);
return read_len;
}
int
QPACK::_decode_indexed_header_field_with_postbase_index(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr,
uint32_t &header_len)
{
// Read index field
int len = 0;
uint64_t index;
int ret = xpack_decode_integer(index, buf, buf + buf_len, 4);
if (ret < 0) {
return -1;
}
len += ret;
// Lookup a table
const char *name = nullptr;
int name_len = 0;
const char *value = nullptr;
int value_len = 0;
QPACK::LookupResult result;
result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_postbase_index(base_index, index), &name, &name_len, &value,
&value_len);
if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
return -1;
}
// Create and attach a header
this->_attach_header(hdr, name, name_len, value, value_len, false);
header_len = name_len + value_len;
QPACKDebug("Decoded Indexed Header Field With Postbase Index: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
result.index, name_len, name, value_len, value);
return len;
}
int
QPACK::_decode_literal_header_field_with_postbase_name_ref(int16_t base_index, const uint8_t *buf, size_t buf_len, HTTPHdr &hdr,
uint32_t &header_len)
{
int read_len = 0;
// Never index field
bool never_index = false;
if (buf[0] & 0x08) {
never_index = true;
}
// Read name index field
uint64_t index;
int ret = xpack_decode_integer(index, buf, buf + buf_len, 3);
if (ret < 0) {
return -1;
}
read_len += ret;
// Lookup the name
const char *name = nullptr;
int name_len = 0;
const char *dummy = nullptr;
int dummy_len = 0;
QPACK::LookupResult result;
result = this->_dynamic_table.lookup(this->_calc_absolute_index_from_postbase_index(base_index, index), &name, &name_len, &dummy,
&dummy_len);
if (result.match_type != QPACK::LookupResult::MatchType::EXACT) {
return -1;
}
// Read value
Arena arena;
char *value;
uint64_t value_len;
if ((ret = xpack_decode_string(arena, &value, value_len, buf + read_len, buf + buf_len, 7)) < 0) {
return -1;
}
read_len += ret;
// Create and attach a header
this->_attach_header(hdr, name, name_len, value, value_len, never_index);
header_len = name_len + value_len;
QPACKDebug("Decoded Literal Header Field With Postbase Name Ref: base_index=%d, abs_index=%d, name=%.*s, value=%.*s", base_index,
static_cast<uint16_t>(index), name_len, name, static_cast<int>(value_len), value);
return read_len;
}
int
QPACK::_decode_header(const uint8_t *header_block, size_t header_block_len, HTTPHdr &hdr)
{
const uint8_t *pos = header_block;
size_t remain_len = header_block_len;
int64_t ret;
// Decode Header Data Prefix
uint64_t tmp;
if ((ret = xpack_decode_integer(tmp, pos, pos + remain_len, 8)) < 0 && tmp > 0xFFFF) {
return -1;
}
pos += ret;
uint16_t largest_reference = tmp;
uint64_t delta_base_index;
uint16_t base_index;
if ((ret = xpack_decode_integer(delta_base_index, pos, pos + remain_len, 7)) < 0 && delta_base_index < 0xFFFF) {
return -2;
}
if (pos[0] & 0x80) {
if (delta_base_index == 0) {
return -3;
}
base_index = largest_reference - delta_base_index;
} else {
base_index = largest_reference + delta_base_index;
}
pos += ret;
uint32_t decoded_header_list_size = 0;
// Decode Instructions
while (pos < header_block + header_block_len) {
uint32_t header_len = 0;
if (pos[0] & 0x80) { // Index Header Field
ret = this->_decode_indexed_header_field(base_index, pos, remain_len, hdr, header_len);
} else if (pos[0] & 0x40) { // Literal Header Field With Name Reference
ret = this->_decode_literal_header_field_with_name_ref(base_index, pos, remain_len, hdr, header_len);
} else if (pos[0] & 0x20) { // Literal Header Field Without Name Reference
ret = this->_decode_literal_header_field_without_name_ref(pos, remain_len, hdr, header_len);
} else if (pos[0] & 0x10) { // Indexed Header Field With Post-Base Index
ret = this->_decode_indexed_header_field_with_postbase_index(base_index, pos, remain_len, hdr, header_len);
} else { // Literal Header Field With Post-Base Name Reference
ret = this->_decode_literal_header_field_with_postbase_name_ref(base_index, pos, remain_len, hdr, header_len);
}
if (ret < 0) {
break;
}
decoded_header_list_size += header_len;
if (decoded_header_list_size > this->_max_header_list_size) {
ret = -2;
break;
}
pos += ret;
}
return ret;
}
void
QPACK::_decode(EThread *ethread, Continuation *cont, uint64_t stream_id, const uint8_t *header_block, size_t header_block_len,
HTTPHdr &hdr)
{
int event;
int res = this->_decode_header(header_block, header_block_len, hdr);
if (res < 0) {
event = QPACK_EVENT_DECODE_FAILED;
QPACKDebug("decoding header failed (%d)", res);
} else {
event = QPACK_EVENT_DECODE_COMPLETE;
this->_write_header_acknowledgement(stream_id);
}
ethread->schedule_imm(cont, event, &hdr);
}
bool
QPACK::_add_to_blocked_list(DecodeRequest *decode_request)
{
if (this->_blocked_list.count() >= this->_max_blocking_streams) {
return false;
}
this->_blocked_list.append(decode_request);
return true;
}
void
QPACK::_update_largest_known_received_index_by_insert_count(uint16_t insert_count)
{
this->_largest_known_received_index += insert_count;
}
void
QPACK::_update_largest_known_received_index_by_stream_id(uint64_t stream_id)
{
uint16_t largest_ref_index = this->_references[stream_id].largest;
if (largest_ref_index > this->_largest_known_received_index) {
this->_largest_known_received_index = largest_ref_index;
}
}
void
QPACK::_update_reference_counts(uint64_t stream_id)
{
uint16_t smallest_ref_index = this->_references[stream_id].smallest;
if (smallest_ref_index) {
this->_dynamic_table.unref_entry(smallest_ref_index);
}
}
void
QPACK::_resume_decode()
{
DecodeRequest *r = this->_blocked_list.head();
while (r) {
if (this->_largest_known_received_index >= r->largest_reference()) {
this->_decode(r->thread(), r->continuation(), r->stream_id(), r->header_block(), r->header_block_len(), r->hdr());
DecodeRequest *tmp = r;
r = DecodeRequest::Linkage::next_ptr(r);
this->_blocked_list.erase(tmp);
delete tmp;
} else {
r = DecodeRequest::Linkage::next_ptr(r);
}
}
}
void
QPACK::_abort_decode()
{
this->_invalid = true;
DecodeRequest *r = this->_blocked_list.head();
while (r) {
if (this->_largest_known_received_index >= r->largest_reference()) {
r->thread()->schedule_imm(r->continuation(), QPACK_EVENT_DECODE_FAILED, nullptr);
DecodeRequest *tmp = r;
r = DecodeRequest::Linkage::next_ptr(r);
this->_blocked_list.erase(tmp);
delete tmp;
} else {
r = DecodeRequest::Linkage::next_ptr(r);
}
}
}
int
QPACK::_on_read_ready(VIO *vio)
{
int nread = 0;
QUICStreamId stream_id = static_cast<QUICStreamVCAdapter *>(vio->vc_server)->stream().id();
if (stream_id == this->_decoder_stream_id) {
nread = this->_on_decoder_stream_read_ready(*vio->get_reader());
} else if (stream_id == this->_encoder_stream_id) {
nread = this->_on_encoder_stream_read_ready(*vio->get_reader());
} else {
ink_assert(!"The stream ID must match either encoder stream id or decoder stream id");
}
vio->ndone += nread;
return EVENT_DONE;
}
int
QPACK::_on_write_ready(VIO *vio)
{
QUICStreamId stream_id = static_cast<QUICStreamVCAdapter *>(vio->vc_server)->stream().id();
if (stream_id == this->_decoder_stream_id) {
return this->_on_decoder_write_ready(*vio->get_writer());
} else if (stream_id == this->_encoder_stream_id) {
return this->_on_encoder_write_ready(*vio->get_writer());
} else {
ink_assert(!"The stream ID must match either decoder stream id or decoder stream id");
return EVENT_DONE;
}
}
int
QPACK::_on_decoder_stream_read_ready(IOBufferReader &reader)
{
if (reader.is_read_avail_more_than(0)) {
uint8_t buf;
reader.memcpy(&buf, 1);
if (buf & 0x80) { // Header Acknowledgement
uint64_t stream_id;
if (this->_read_header_acknowledgement(reader, stream_id) >= 0) {
QPACKDebug("Received Header Acknowledgement: stream_id=%" PRIu64, stream_id);
this->_update_largest_known_received_index_by_stream_id(stream_id);
this->_update_reference_counts(stream_id);
this->_references.erase(stream_id);
}
} else if (buf & 0x40) { // Stream Cancellation
uint64_t stream_id;
if (this->_read_stream_cancellation(reader, stream_id) >= 0) {
QPACKDebug("Received Stream Cancellation: stream_id=%" PRIu64, stream_id);
this->_update_reference_counts(stream_id);
this->_references.erase(stream_id);
}
} else { // Table State Synchronize
uint16_t insert_count;
if (this->_read_table_state_synchronize(reader, insert_count) >= 0) {
QPACKDebug("Received Table State Synchronize: inserted_count=%d", insert_count);
this->_update_largest_known_received_index_by_insert_count(insert_count);
}
}
}
return EVENT_DONE;
}
int
QPACK::_on_encoder_stream_read_ready(IOBufferReader &reader)
{
while (reader.is_read_avail_more_than(0)) {
uint8_t buf;
reader.memcpy(&buf, 1);
if (buf & 0x80) { // Insert With Name Reference
bool is_static;
uint16_t index;
Arena arena;
char *value;
uint16_t value_len;
if (this->_read_insert_with_name_ref(reader, is_static, index, arena, &value, value_len) < 0) {
this->_abort_decode();
return EVENT_DONE;
}
QPACKDebug("Received Insert With Name Ref: is_static=%d, index=%d, value=%.*s", is_static, index, value_len, value);
this->_dynamic_table.insert_entry(is_static, index, value, value_len);
} else if (buf & 0x40) { // Insert Without Name Reference
Arena arena;
char *name;
uint16_t name_len;
char *value;
uint16_t value_len;
if (this->_read_insert_without_name_ref(reader, arena, &name, name_len, &value, value_len) < 0) {
this->_abort_decode();
return EVENT_DONE;
}
QPACKDebug("Received Insert Without Name Ref: name=%.*s, value=%.*s", name_len, name, value_len, value);
this->_dynamic_table.insert_entry(name, name_len, value, value_len);
} else if (buf & 0x20) { // Dynamic Table Size Update
uint16_t max_size;
if (this->_read_dynamic_table_size_update(reader, max_size) < 0) {
this->_abort_decode();
return EVENT_DONE;
}
QPACKDebug("Received Dynamic Table Size Update: max_size=%d", max_size);
this->_dynamic_table.update_size(max_size);
} else { // Duplicates
uint16_t index;
if (this->_read_duplicate(reader, index) < 0) {
this->_abort_decode();
return EVENT_DONE;
}
QPACKDebug("Received Duplicate: index=%d", index);
this->_dynamic_table.duplicate_entry(index);
}
this->_resume_decode();
}
return EVENT_DONE;
}
int
QPACK::_on_decoder_write_ready(MIOBuffer &writer)
{
int64_t written_len = writer.write(this->_decoder_stream_sending_instructions_reader, INT64_MAX);
this->_decoder_stream_sending_instructions_reader->consume(written_len);
return written_len;
}
int
QPACK::_on_encoder_write_ready(MIOBuffer &writer)
{
int64_t written_len = writer.write(this->_encoder_stream_sending_instructions_reader, INT64_MAX);
this->_encoder_stream_sending_instructions_reader->consume(written_len);
return written_len;
}
size_t
QPACK::estimate_header_block_size(const HTTPHdr &hdr)
{
// FIXME Estimate it
return 128 * 1024 * 1024;
}
const QPACK::LookupResult
QPACK::StaticTable::lookup(uint16_t index, const char **name, int *name_len, const char **value, int *value_len)
{
const Header &header = STATIC_HEADER_FIELDS[index];
*name = header.name;
*name_len = header.name_len;
*value = header.value;
*value_len = header.value_len;
return {index, QPACK::LookupResult::MatchType::EXACT};
}
const QPACK::LookupResult
QPACK::StaticTable::lookup(const char *name, int name_len, const char *value, int value_len)
{
QPACK::LookupResult::MatchType match_type = QPACK::LookupResult::MatchType::NONE;
uint16_t i = 0;
uint16_t candidate_index = 0;
int n = countof(STATIC_HEADER_FIELDS);
for (; i < n; ++i) {
const Header &h = STATIC_HEADER_FIELDS[i];
if (h.name_len == name_len) {
if (memcmp(name, h.name, name_len) == 0) {
candidate_index = i;
if (value_len == h.value_len && memcmp(value, h.value, value_len) == 0) {
// Exact match
match_type = QPACK::LookupResult::MatchType::EXACT;
break;
} else {
// Name match -- Keep it for no exact matches
match_type = QPACK::LookupResult::MatchType::NAME;
}
}
}
}
return {candidate_index, match_type};
}
uint16_t
QPACK::_calc_absolute_index_from_relative_index(uint16_t base_index, uint16_t relative_index)
{
return base_index - relative_index;
}
uint16_t
QPACK::_calc_absolute_index_from_postbase_index(uint16_t base_index, uint16_t postbase_index)
{
return base_index + postbase_index + 1;
}
uint16_t
QPACK::_calc_relative_index_from_absolute_index(uint16_t base_index, uint16_t absolute_index)
{
return base_index - absolute_index;
}
uint16_t
QPACK::_calc_postbase_index_from_absolute_index(uint16_t base_index, uint16_t absolute_index)
{
return absolute_index - base_index - 1;
}
void
QPACK::_attach_header(HTTPHdr &hdr, const char *name, int name_len, const char *value, int value_len, bool never_index)
{
// TODO If never_index is true, we need to mark this header as sensitive to not index the header when passing it to the other side
MIMEField *new_field = hdr.field_create(name, name_len);
new_field->value_set(hdr.m_heap, hdr.m_mime, value, value_len);
hdr.field_attach(new_field);
}
//
// DynamicTable
//
QPACK::DynamicTable::DynamicTable(uint16_t size) : _available(size), _max_entries(size), _storage(new DynamicTableStorage(size))
{
QPACKDTDebug("Dynamic table size: %u", size);
this->_entries = static_cast<struct DynamicTableEntry *>(ats_malloc(sizeof(struct DynamicTableEntry) * size));
this->_entries_head = size - 1;
this->_entries_tail = size - 1;
}
QPACK::DynamicTable::~DynamicTable()
{
if (this->_storage) {
delete this->_storage;
this->_storage = nullptr;
}
if (this->_entries) {
delete this->_entries;
this->_entries = nullptr;
}
}
const QPACK::LookupResult
QPACK::DynamicTable::lookup(uint16_t index, const char **name, int *name_len, const char **value, int *value_len)
{
// ink_assert(index >= this->_entries[(this->_entries_tail + 1) % this->_max_entries].index);
// ink_assert(index <= this->_entries[this->_entries_head].index);
uint16_t pos = (this->_entries_head + (index - this->_entries[this->_entries_head].index)) % this->_max_entries;
*name_len = this->_entries[pos].name_len;
*value_len = this->_entries[pos].value_len;
this->_storage->read(this->_entries[pos].offset, name, *name_len, value, *value_len);
return {index, QPACK::LookupResult::MatchType::EXACT};
}
const QPACK::LookupResult
QPACK::DynamicTable::lookup(const char *name, int name_len, const char *value, int value_len)
{
QPACK::LookupResult::MatchType match_type = QPACK::LookupResult::MatchType::NONE;
uint16_t i = this->_entries_tail + 1;
int end = this->_entries_head;
uint16_t candidate_index = 0;
const char *tmp_name = nullptr;
const char *tmp_value = nullptr;
// DynamicTable is empty
if (this->_entries_inserted == 0) {
return {candidate_index, match_type};
}
// TODO Use a tree for better performance
for (; i <= end; i = (i + 1) % this->_max_entries) {
if (name_len != 0 && this->_entries[i].name_len == name_len) {
this->_storage->read(this->_entries[i].offset, &tmp_name, this->_entries[i].name_len, &tmp_value,
this->_entries[i].value_len);
if (memcmp(name, tmp_name, name_len) == 0) {
candidate_index = this->_entries[i].index;
if (value_len == this->_entries[i].value_len && memcmp(value, tmp_value, value_len) == 0) {
// Exact match
match_type = QPACK::LookupResult::MatchType::EXACT;
break;
} else {
// Name match -- Keep it for no exact matches
match_type = QPACK::LookupResult::MatchType::NAME;
}
}
}
}
return {candidate_index, match_type};
}
const QPACK::LookupResult
QPACK::DynamicTable::insert_entry(bool is_static, uint16_t index, const char *value, uint16_t value_len)
{
const char *name;
int name_len;
const char *dummy;
int dummy_len;
if (is_static) {
StaticTable::lookup(index, &name, &name_len, &dummy, &dummy_len);
} else {
this->lookup(index, &name, &name_len, &dummy, &dummy_len);
}
return this->insert_entry(name, name_len, value, value_len);
}
const QPACK::LookupResult
QPACK::DynamicTable::insert_entry(const char *name, uint16_t name_len, const char *value, uint16_t value_len)
{
if (this->_max_entries == 0) {
return {UINT16_C(0), QPACK::LookupResult::MatchType::NONE};
}
// Check if we can make enough space to insert a new entry
uint16_t required_len = name_len + value_len;
uint16_t available = this->_available;
uint16_t tail = (this->_entries_tail + 1) % this->_max_entries;
while (available < required_len) {
if (this->_entries[tail].ref_count) {
break;
}
available += this->_entries[tail].name_len + this->_entries[tail].value_len;
tail = (tail + 1) % this->_max_entries;
}
if (available < required_len) {
// We can't insert a new entry because some stream(s) refer an entry that need to be evicted
return {UINT16_C(0), QPACK::LookupResult::MatchType::NONE};
}
// Evict
if (this->_available != available) {
QPACKDTDebug("Evict entries: from %u to %u", this->_entries[(this->_entries_tail + 1) % this->_max_entries].index,
this->_entries[tail - 1].index);
this->_available = available;
this->_entries_tail = tail - 1;
QPACKDTDebug("Available size: %u", this->_available);
}
// Insert
this->_entries_head = (this->_entries_head + 1) % this->_max_entries;
this->_entries[this->_entries_head] = {++this->_entries_inserted, this->_storage->write(name, name_len, value, value_len),
name_len, value_len, 0};
this->_available -= required_len;
QPACKDTDebug("Insert Entry: entry=%u, index=%u, size=%u", this->_entries_head, this->_entries_inserted, name_len + value_len);
QPACKDTDebug("Available size: %u", this->_available);
return {this->_entries_inserted, value_len ? LookupResult::MatchType::EXACT : LookupResult::MatchType::NAME};
}
const QPACK::LookupResult
QPACK::DynamicTable::duplicate_entry(uint16_t current_index)
{
const char *name;
int name_len;
const char *value;
int value_len;
char *duped_name;
char *duped_value;
this->lookup(current_index, &name, &name_len, &value, &value_len);
// We need to dup name and value to avoid memcpy-param-overlap
duped_name = ats_strndup(name, name_len);
duped_value = ats_strndup(value, value_len);
const LookupResult result = this->insert_entry(duped_name, name_len, duped_value, value_len);
ats_free(duped_name);
ats_free(duped_value);
return result;
}
bool
QPACK::DynamicTable::should_duplicate(uint16_t index)
{
// TODO: Check whether a specified entry should be duplicated
// Just return false for now
return false;
}
void
QPACK::DynamicTable::update_size(uint16_t max_size)
{
// TODO Implement it
}
void
QPACK::DynamicTable::ref_entry(uint16_t index)
{
uint16_t pos = (this->_entries_head + (index - this->_entries[this->_entries_head].index)) % this->_max_entries;
++this->_entries[pos].ref_count;
}
void
QPACK::DynamicTable::unref_entry(uint16_t index)
{
uint16_t pos = (this->_entries_head + (index - this->_entries[this->_entries_head].index)) % this->_max_entries;
--this->_entries[pos].ref_count;
}
uint16_t
QPACK::DynamicTable::largest_index() const
{
return this->_entries_inserted;
}
int
QPACK::_write_insert_with_name_ref(uint16_t index, bool dynamic, const char *value, uint16_t value_len)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_2K);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Insert With Name Reference
buf[0] = 0x80;
// References static table or not
if (!dynamic) {
buf[0] |= 0x40;
}
// Name Index
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 6)) <
0) {
return ret;
}
written += ret;
// Value
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
7)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_write_insert_without_name_ref(const char *name, int name_len, const char *value, uint16_t value_len)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_2K);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Insert Without Name Reference
buf[0] = 0x40;
// Name
int ret;
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), name, name_len,
5)) < 0) {
return ret;
}
written += ret;
// Value
if ((ret = xpack_encode_string(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), value, value_len,
7)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_write_duplicate(uint16_t index)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_2K);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Index
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), index, 5)) <
0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_write_dynamic_table_size_update(uint16_t max_size)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Dynamic Table Size Update
buf[0] = 0x20;
// Max Size
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), max_size, 5)) <
0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_write_table_state_synchronize(uint16_t insert_count)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Insert Count
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), insert_count,
6)) < 0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_write_header_acknowledgement(uint64_t stream_id)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Header Acknowledgement
buf[0] = 0x80;
// Stream ID
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), stream_id, 7)) <
0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_write_stream_cancellation(uint64_t stream_id)
{
IOBufferBlock *instruction = new_IOBufferBlock();
instruction->alloc(TS_IOBUFFER_SIZE_INDEX_128);
char *buf = instruction->end();
char *buf_end = buf + instruction->write_avail();
int written = 0;
// Stream Cancellation
buf[0] = 0x40;
// Stream ID
int ret;
if ((ret = xpack_encode_integer(reinterpret_cast<uint8_t *>(buf + written), reinterpret_cast<uint8_t *>(buf_end), stream_id, 7)) <
0) {
return ret;
}
written += ret;
// Finalize and Schedule to send
instruction->fill(written);
this->_encoder_stream_sending_instructions->append_block(instruction);
return 0;
}
int
QPACK::_read_insert_with_name_ref(IOBufferReader &reader, bool &is_static, uint16_t &index, Arena &arena, char **value,
uint16_t &value_len)
{
size_t read_len = 0;
int ret;
uint8_t input[16384];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
// S flag
is_static = input[0] & 0x40;
// Name Index
uint64_t tmp;
if ((ret = xpack_decode_integer(tmp, input, input + input_len, 6)) < 0 && tmp > 0xFFFF) {
return -1;
}
index = tmp;
read_len += ret;
// Value
if ((ret = xpack_decode_string(arena, value, tmp, input + read_len, input + input_len, 7)) < 0 && tmp > 0xFF) {
return -1;
}
value_len = tmp;
read_len += ret;
reader.consume(read_len);
return 0;
}
int
QPACK::_read_insert_without_name_ref(IOBufferReader &reader, Arena &arena, char **name, uint16_t &name_len, char **value,
uint16_t &value_len)
{
size_t read_len = 0;
int ret;
uint8_t input[16384];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
// Name
uint64_t tmp;
if ((ret = xpack_decode_string(arena, name, tmp, input, input + input_len, 5)) < 0 && tmp > 0xFFFF) {
return -1;
}
name_len = tmp;
read_len += ret;
// Value
if ((ret = xpack_decode_string(arena, value, tmp, input + read_len, input + input_len, 7)) < 0 && tmp > 0xFFFF) {
return -1;
}
value_len = tmp;
read_len += ret;
reader.consume(read_len);
return 0;
}
int
QPACK::_read_duplicate(IOBufferReader &reader, uint16_t &index)
{
size_t read_len = 0;
int ret;
uint8_t input[16];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
// Index
uint64_t tmp;
if ((ret = xpack_decode_integer(tmp, input, input + input_len, 5)) < 0 && tmp > 0xFFFF) {
return -1;
}
index = tmp;
read_len += ret;
reader.consume(read_len);
return 0;
}
int
QPACK::_read_dynamic_table_size_update(IOBufferReader &reader, uint16_t &max_size)
{
size_t read_len = 0;
int ret;
uint8_t input[16];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
uint64_t tmp;
// Max Size
if ((ret = xpack_decode_integer(tmp, input, input + input_len, 5)) < 0 && tmp > 0xFFFF) {
return -1;
}
max_size = tmp;
read_len += ret;
reader.consume(read_len);
return 0;
}
int
QPACK::_read_table_state_synchronize(IOBufferReader &reader, uint16_t &insert_count)
{
size_t read_len = 0;
int ret;
uint8_t input[16];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
uint64_t tmp;
// Insert Count
if ((ret = xpack_decode_integer(tmp, input, input + input_len, 6)) < 0 && tmp > 0xFFFF) {
return -1;
}
insert_count = tmp;
read_len += ret;
reader.consume(read_len);
return 0;
}
int
QPACK::_read_header_acknowledgement(IOBufferReader &reader, uint64_t &stream_id)
{
size_t read_len = 0;
int ret;
uint8_t input[16];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
// Stream ID
// FIXME xpack_decode_integer does not support uint64_t
if ((ret = xpack_decode_integer(stream_id, input, input + input_len, 7)) < 0) {
return -1;
}
read_len += ret;
reader.consume(read_len);
return 0;
}
int
QPACK::_read_stream_cancellation(IOBufferReader &reader, uint64_t &stream_id)
{
size_t read_len = 0;
int ret;
uint8_t input[16];
uint8_t *p = reinterpret_cast<uint8_t *>(reader.memcpy(input, sizeof(input)));
int input_len = p - input;
// Stream ID
// FIXME xpack_decode_integer does not support uint64_t
if ((ret = xpack_decode_integer(stream_id, input, input + input_len, 6)) < 0) {
return -1;
}
read_len += ret;
reader.consume(read_len);
return 0;
}
//
// DynamicTableStorage
//
QPACK::DynamicTableStorage::DynamicTableStorage(uint16_t size) : _head(size * 2 - 1), _tail(size * 2 - 1)
{
this->_data_size = size * 2;
this->_data = reinterpret_cast<uint8_t *>(ats_malloc(this->_data_size));
this->_overwrite_threshold = size;
}
QPACK::DynamicTableStorage::~DynamicTableStorage()
{
ats_free(this->_data);
this->_data = nullptr;
}
void
QPACK::DynamicTableStorage::read(uint16_t offset, const char **name, uint16_t name_len, const char **value,
uint16_t value_len) const
{
*name = reinterpret_cast<const char *>(this->_data + offset);
*value = reinterpret_cast<const char *>(this->_data + offset + name_len);
}
uint16_t
QPACK::DynamicTableStorage::write(const char *name, uint16_t name_len, const char *value, uint16_t value_len)
{
uint16_t offset = (this->_head + 1) % this->_data_size;
memcpy(this->_data + offset, name, name_len);
memcpy(this->_data + offset + name_len, value, value_len);
this->_head = (this->_head + (name_len + value_len)) % this->_data_size;
if (this->_head > this->_overwrite_threshold) {
this->_head = 0;
}
return offset;
}
void
QPACK::DynamicTableStorage::erase(uint16_t name_len, uint16_t value_len)
{
this->_tail = (this->_tail + (name_len + value_len)) % this->_data_size;
}
|