1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=4 sw=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ASpdySession.h" // because of SoftStreamError()
#include "Http3Session.h"
#include "Http3Stream.h"
#include "Http3StreamBase.h"
#include "Http3WebTransportSession.h"
#include "Http3ConnectUDPStream.h"
#include "Http3StreamTunnel.h"
#include "Http3WebTransportStream.h"
#include "HttpConnectionUDP.h"
#include "HttpLog.h"
#include "QuicSocketControl.h"
#include "SSLServerCertVerification.h"
#include "SSLTokensCache.h"
#include "ScopedNSSTypes.h"
#include "mozilla/RandomNum.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/glean/NetwerkMetrics.h"
#include "mozilla/glean/NetwerkProtocolHttpMetrics.h"
#include "mozilla/net/DNS.h"
#include "nsHttpHandler.h"
#include "nsIHttpActivityObserver.h"
#include "nsIOService.h"
#include "nsITLSSocketControl.h"
#include "nsNetAddr.h"
#include "nsQueryObject.h"
#include "nsSocketTransportService2.h"
#include "nsThreadUtils.h"
#include "sslerr.h"
#include "WebTransportCertificateVerifier.h"
namespace mozilla::net {
extern const nsCString& TRRProviderKey();
const uint64_t HTTP3_APP_ERROR_NO_ERROR = 0x100;
// const uint64_t HTTP3_APP_ERROR_GENERAL_PROTOCOL_ERROR = 0x101;
// const uint64_t HTTP3_APP_ERROR_INTERNAL_ERROR = 0x102;
// const uint64_t HTTP3_APP_ERROR_STREAM_CREATION_ERROR = 0x103;
// const uint64_t HTTP3_APP_ERROR_CLOSED_CRITICAL_STREAM = 0x104;
// const uint64_t HTTP3_APP_ERROR_FRAME_UNEXPECTED = 0x105;
// const uint64_t HTTP3_APP_ERROR_FRAME_ERROR = 0x106;
// const uint64_t HTTP3_APP_ERROR_EXCESSIVE_LOAD = 0x107;
// const uint64_t HTTP3_APP_ERROR_ID_ERROR = 0x108;
// const uint64_t HTTP3_APP_ERROR_SETTINGS_ERROR = 0x109;
// const uint64_t HTTP3_APP_ERROR_MISSING_SETTINGS = 0x10a;
const uint64_t HTTP3_APP_ERROR_REQUEST_REJECTED = 0x10b;
const uint64_t HTTP3_APP_ERROR_REQUEST_CANCELLED = 0x10c;
// const uint64_t HTTP3_APP_ERROR_REQUEST_INCOMPLETE = 0x10d;
// const uint64_t HTTP3_APP_ERROR_EARLY_RESPONSE = 0x10e;
// const uint64_t HTTP3_APP_ERROR_CONNECT_ERROR = 0x10f;
const uint64_t HTTP3_APP_ERROR_VERSION_FALLBACK = 0x110;
// const uint32_t UDP_MAX_PACKET_SIZE = 4096;
const uint32_t MAX_PTO_COUNTS = 16;
const uint32_t TRANSPORT_ERROR_STATELESS_RESET = 20;
NS_IMPL_ADDREF_INHERITED(Http3Session, nsAHttpConnection)
NS_IMPL_RELEASE_INHERITED(Http3Session, nsAHttpConnection)
NS_INTERFACE_MAP_BEGIN(Http3Session)
NS_INTERFACE_MAP_ENTRY(nsAHttpConnection)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_CONCRETE(Http3Session)
NS_INTERFACE_MAP_END
Http3Session::Http3Session() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG(("Http3Session::Http3Session [this=%p]", this));
mCurrentBrowserId = gHttpHandler->ConnMgr()->CurrentBrowserId();
}
static nsresult RawBytesToNetAddr(uint16_t aFamily, const uint8_t* aRemoteAddr,
uint16_t remotePort, NetAddr* netAddr) {
if (aFamily == AF_INET) {
netAddr->inet.family = AF_INET;
netAddr->inet.port = htons(remotePort);
memcpy(&netAddr->inet.ip, aRemoteAddr, 4);
} else if (aFamily == AF_INET6) {
netAddr->inet6.family = AF_INET6;
netAddr->inet6.port = htons(remotePort);
memcpy(&netAddr->inet6.ip.u8, aRemoteAddr, 16);
} else {
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
nsresult Http3Session::Init(const nsHttpConnectionInfo* aConnInfo,
nsINetAddr* aSelfAddr, nsINetAddr* aPeerAddr,
HttpConnectionUDP* udpConn, uint32_t aProviderFlags,
nsIInterfaceRequestor* callbacks,
nsIUDPSocket* socket, bool aIsTunnel) {
LOG3(("Http3Session::Init %p", this));
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(udpConn);
mConnInfo = aConnInfo->Clone();
mNetAddr = aPeerAddr;
// When `isOuterConnection` is true, this Http3Session represents the *outer*
// connection between Firefox and the proxy (e.g., when using CONNECT-UDP).
//
// We track this flag for two main reasons:
// 1. To select the correct hostname during TLS negotiation on the outer
// connection.
// 2. To explicitly enable Path MTU Discovery (PMTUD) on the outer connection,
// since the outer path’s MTU must be at least as large as the inner one.
bool isOuterConnection = false;
if (!aIsTunnel) {
if (auto* proxyInfo = aConnInfo->ProxyInfo()) {
isOuterConnection = proxyInfo->IsHttp3Proxy();
}
}
// Create security control and info object for quic.
mSocketControl =
new QuicSocketControl(isOuterConnection ? aConnInfo->ProxyInfo()->Host()
: aConnInfo->GetOrigin(),
isOuterConnection ? aConnInfo->ProxyInfo()->Port()
: aConnInfo->OriginPort(),
aProviderFlags, this);
const nsCString& alpn = isOuterConnection ? aConnInfo->GetProxyNPNToken()
: aConnInfo->GetNPNToken();
NetAddr selfAddr;
MOZ_ALWAYS_SUCCEEDS(aSelfAddr->GetNetAddr(&selfAddr));
NetAddr peerAddr;
MOZ_ALWAYS_SUCCEEDS(aPeerAddr->GetNetAddr(&peerAddr));
LOG3(
("Http3Session::Init origin=%s, alpn=%s, selfAddr=%s, peerAddr=%s,"
" qpack table size=%u, max blocked streams=%u webtransport=%d "
"[this=%p]",
PromiseFlatCString(mSocketControl->GetHostName()).get(),
PromiseFlatCString(alpn).get(), selfAddr.ToString().get(),
peerAddr.ToString().get(), gHttpHandler->DefaultQpackTableSize(),
gHttpHandler->DefaultHttp3MaxBlockedStreams(),
mConnInfo->GetWebTransport(), this));
if (mConnInfo->GetWebTransport()) {
ExtState(ExtendedConnectKind::WebTransport).mStatus = NEGOTIATING;
}
if (isOuterConnection) {
ExtState(ExtendedConnectKind::ConnectUDP).mStatus = NEGOTIATING;
}
mUseNSPRForIO =
StaticPrefs::network_http_http3_use_nspr_for_io() || aIsTunnel;
uint32_t idleTimeout =
mConnInfo->GetIsTrrServiceChannel()
? StaticPrefs::network_trr_idle_timeout_for_http3_conn()
: StaticPrefs::network_http_http3_idle_timeout();
nsresult rv;
if (mUseNSPRForIO) {
rv = NeqoHttp3Conn::InitUseNSPRForIO(
mSocketControl->GetHostName(), alpn, selfAddr, peerAddr,
gHttpHandler->DefaultQpackTableSize(),
gHttpHandler->DefaultHttp3MaxBlockedStreams(),
StaticPrefs::network_http_http3_max_data(),
StaticPrefs::network_http_http3_max_stream_data(),
StaticPrefs::network_http_http3_version_negotiation_enabled(),
mConnInfo->GetWebTransport(), gHttpHandler->Http3QlogDir(),
aProviderFlags, idleTimeout, getter_AddRefs(mHttp3Connection));
} else {
rv = NeqoHttp3Conn::Init(
mSocketControl->GetHostName(), alpn, selfAddr, peerAddr,
gHttpHandler->DefaultQpackTableSize(),
gHttpHandler->DefaultHttp3MaxBlockedStreams(),
StaticPrefs::network_http_http3_max_data(),
StaticPrefs::network_http_http3_max_stream_data(),
StaticPrefs::network_http_http3_version_negotiation_enabled(),
mConnInfo->GetWebTransport(), gHttpHandler->Http3QlogDir(),
aProviderFlags, idleTimeout, socket->GetFileDescriptor(),
isOuterConnection, getter_AddRefs(mHttp3Connection));
}
if (NS_FAILED(rv)) {
return rv;
}
nsAutoCString peerId;
mSocketControl->GetPeerId(peerId);
nsTArray<uint8_t> token;
SessionCacheInfo info;
udpConn->ChangeConnectionState(ConnectionState::TLS_HANDSHAKING);
auto hasServCertHashes = [&]() -> bool {
if (!mConnInfo->GetWebTransport()) {
return false;
}
const nsTArray<RefPtr<nsIWebTransportHash>>* servCertHashes =
gHttpHandler->ConnMgr()->GetServerCertHashes(mConnInfo);
return servCertHashes && !servCertHashes->IsEmpty();
};
// See https://github.com/mozilla/neqo/issues/2442.
// We need to set ECH first before set resumption token.
auto config = mConnInfo->GetEchConfig();
if (config.IsEmpty()) {
if (StaticPrefs::security_tls_ech_grease_http3() && config.IsEmpty()) {
if ((RandomUint64().valueOr(0) % 100) >=
100 - StaticPrefs::security_tls_ech_grease_probability()) {
// Setting an empty config enables GREASE mode.
mSocketControl->SetEchConfig(config);
mEchExtensionStatus = EchExtensionStatus::kGREASE;
}
}
} else if (nsHttpHandler::EchConfigEnabled(true) && !config.IsEmpty()) {
mSocketControl->SetEchConfig(config);
mEchExtensionStatus = EchExtensionStatus::kReal;
HttpConnectionActivity activity(
mConnInfo->HashKey(), mConnInfo->GetOrigin(), mConnInfo->OriginPort(),
mConnInfo->EndToEndSSL(), !mConnInfo->GetEchConfig().IsEmpty(),
mConnInfo->IsHttp3());
gHttpHandler->ObserveHttpActivityWithArgs(
activity, NS_ACTIVITY_TYPE_HTTP_CONNECTION,
NS_HTTP_ACTIVITY_SUBTYPE_ECH_SET, PR_Now(), 0, ""_ns);
} else {
mEchExtensionStatus = EchExtensionStatus::kNotPresent;
}
// In WebTransport, when servCertHashes is specified, it indicates that the
// connection to the WebTransport server should authenticate using the
// expected certificate hash. Therefore, 0RTT should be disabled in this
// context to ensure the certificate hash is checked.
if (StaticPrefs::network_http_http3_enable_0rtt() && !hasServCertHashes() &&
NS_SUCCEEDED(SSLTokensCache::Get(peerId, token, info))) {
LOG(("Found a resumption token in the cache."));
mHttp3Connection->SetResumptionToken(token);
mSocketControl->SetSessionCacheInfo(std::move(info));
if (mHttp3Connection->IsZeroRtt()) {
LOG(("Can send ZeroRtt data"));
RefPtr<Http3Session> self(this);
mState = ZERORTT;
udpConn->ChangeConnectionState(ConnectionState::ZERORTT);
mZeroRttStarted = TimeStamp::Now();
// Let the nsHttpConnectionMgr know that the connection can accept
// transactions.
// We need to dispatch the following function to this thread so that
// it is executed after the current function. At this point a
// Http3Session is still being initialized and ReportHttp3Connection
// will try to dispatch transaction on this session therefore it
// needs to be executed after the initializationg is done.
DebugOnly<nsresult> rv = NS_DispatchToCurrentThread(
NS_NewRunnableFunction("Http3Session::ReportHttp3Connection",
[self]() { self->ReportHttp3Connection(); }));
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"NS_DispatchToCurrentThread failed");
}
}
#ifndef ANDROID
if (mState != ZERORTT) {
ZeroRttTelemetry(ZeroRttOutcome::NOT_USED);
}
#endif
// After this line, Http3Session and HttpConnectionUDP become a cycle. We put
// this line in the end of Http3Session::Init to make sure Http3Session can be
// released when Http3Session::Init early returned.
mUdpConn = udpConn;
return NS_OK;
}
void Http3Session::DoSetEchConfig(const nsACString& aEchConfig) {
LOG(("Http3Session::DoSetEchConfig %p of length %zu", this,
aEchConfig.Length()));
nsTArray<uint8_t> config;
config.AppendElements(
reinterpret_cast<const uint8_t*>(aEchConfig.BeginReading()),
aEchConfig.Length());
mHttp3Connection->SetEchConfig(config);
}
nsresult Http3Session::SendPriorityUpdateFrame(uint64_t aStreamId,
uint8_t aPriorityUrgency,
bool aPriorityIncremental) {
return mHttp3Connection->PriorityUpdate(aStreamId, aPriorityUrgency,
aPriorityIncremental);
}
// Shutdown the http3session and close all transactions.
void Http3Session::Shutdown() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
if (mTimer) {
mTimer->Cancel();
}
mTimer = nullptr;
mTimerCallback = nullptr;
bool isEchRetry = mError == mozilla::psm::GetXPCOMFromNSSError(
SSL_ERROR_ECH_RETRY_WITH_ECH);
bool isNSSError = psm::IsNSSErrorCode(-1 * NS_ERROR_GET_CODE(mError));
bool allowToRetryWithDifferentIPFamily =
mBeforeConnectedError &&
gHttpHandler->ConnMgr()->AllowToRetryDifferentIPFamilyForHttp3(mConnInfo,
mError);
LOG(("Http3Session::Shutdown %p allowToRetryWithDifferentIPFamily=%d", this,
allowToRetryWithDifferentIPFamily));
if ((mBeforeConnectedError ||
(mError == NS_ERROR_NET_HTTP3_PROTOCOL_ERROR)) &&
!isNSSError && !isEchRetry && !mConnInfo->GetWebTransport() &&
!allowToRetryWithDifferentIPFamily && !mDontExclude) {
gHttpHandler->ExcludeHttp3(mConnInfo);
if (mFirstHttpTransaction) {
mFirstHttpTransaction->DisableHttp3(false);
}
}
for (const auto& stream : mStreamTransactionHash.Values()) {
if (mBeforeConnectedError) {
// We have an error before we were connected, just restart transactions.
// The transaction restart code path will remove AltSvc mapping and the
// direct path will be used.
MOZ_ASSERT(NS_FAILED(mError));
if (isEchRetry) {
// We have to propagate this error to nsHttpTransaction, so the
// transaction will be restarted with a new echConfig.
stream->Close(mError);
} else if (isNSSError) {
stream->Close(mError);
} else {
if (allowToRetryWithDifferentIPFamily && mNetAddr) {
NetAddr addr;
mNetAddr->GetNetAddr(&addr);
gHttpHandler->ConnMgr()->SetRetryDifferentIPFamilyForHttp3(
mConnInfo, addr.raw.family);
// We want the transaction to be restarted with the same connection
// info.
stream->Transaction()->DoNotRemoveAltSvc();
// We already set the preference in SetRetryDifferentIPFamilyForHttp3,
// so we want to keep it for the next retry.
stream->Transaction()->DoNotResetIPFamilyPreference();
stream->Close(NS_ERROR_NET_RESET);
// Since Http3Session::Shutdown can be called multiple times, we set
// mDontExclude for not putting this domain into the excluded list.
mDontExclude = true;
} else {
stream->Close(NS_ERROR_NET_RESET);
}
}
} else if (!stream->HasStreamId()) {
if (NS_SUCCEEDED(mError)) {
// Connection has not been started yet. We can restart it.
stream->Transaction()->DoNotRemoveAltSvc();
}
stream->Close(NS_ERROR_NET_RESET);
} else if (stream->GetHttp3Stream() &&
stream->GetHttp3Stream()->RecvdData()) {
stream->Close(NS_ERROR_NET_PARTIAL_TRANSFER);
} else if (mError == NS_ERROR_NET_HTTP3_PROTOCOL_ERROR) {
stream->Close(NS_ERROR_NET_HTTP3_PROTOCOL_ERROR);
} else if (mError == NS_ERROR_NET_RESET) {
stream->Close(NS_ERROR_NET_RESET);
} else {
stream->Close(NS_ERROR_ABORT);
}
RemoveStreamFromQueues(stream);
if (stream->HasStreamId()) {
mStreamIdHash.Remove(stream->StreamId());
}
}
mStreamTransactionHash.Clear();
for (const auto& stream : mWebTransportSessions) {
stream->Close(NS_ERROR_ABORT);
RemoveStreamFromQueues(stream);
mStreamIdHash.Remove(stream->StreamId());
}
mWebTransportSessions.Clear();
for (const auto& stream : mTunnelStreams) {
stream->Close(NS_ERROR_ABORT);
RemoveStreamFromQueues(stream);
mStreamIdHash.Remove(stream->StreamId());
}
mTunnelStreams.Clear();
for (const auto& stream : mWebTransportStreams) {
stream->Close(NS_ERROR_ABORT);
RemoveStreamFromQueues(stream);
mStreamIdHash.Remove(stream->StreamId());
}
RefPtr<Http3StreamBase> stream;
while ((stream = mQueuedStreams.PopFront())) {
LOG(("Close remaining stream in queue:%p", stream.get()));
stream->SetQueued(false);
stream->Close(NS_ERROR_ABORT);
}
mWebTransportStreams.Clear();
}
Http3Session::~Http3Session() {
LOG3(("Http3Session::~Http3Session %p", this));
#ifndef ANDROID
EchOutcomeTelemetry();
#endif
glean::http3::request_per_conn.AccumulateSingleSample(mTransactionCount);
glean::http3::blocked_by_stream_limit_per_conn.AccumulateSingleSample(
mBlockedByStreamLimitCount);
glean::http3::trans_blocked_by_stream_limit_per_conn.AccumulateSingleSample(
mTransactionsBlockedByStreamLimitCount);
glean::http3::trans_sending_blocked_by_flow_control_per_conn
.AccumulateSingleSample(mTransactionsSenderBlockedByFlowControlCount);
if (mTrrStreams) {
mozilla::glean::networking::trr_request_count_per_conn
.Get(nsPrintfCString("%s_h3", mConnInfo->Origin()))
.Add(static_cast<int32_t>(mTrrStreams));
}
Shutdown();
}
// This function may return a socket error.
// It will not return an error if socket error is
// NS_BASE_STREAM_WOULD_BLOCK.
// A caller of this function will close the Http3 connection
// in case of an error.
// The only callers is Http3Session::RecvData.
nsresult Http3Session::ProcessInput(nsIUDPSocket* socket) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(mUdpConn);
LOG(("Http3Session::ProcessInput writer=%p [this=%p state=%d]",
mUdpConn.get(), this, mState));
if (!socket || socket->IsSocketClosed()) {
MOZ_DIAGNOSTIC_ASSERT(false, "UDP socket should still be open");
return NS_ERROR_UNEXPECTED;
}
if (mUseNSPRForIO) {
while (true) {
nsTArray<uint8_t> data;
NetAddr addr{};
// RecvWithAddr actually does not return an error.
nsresult rv = socket->RecvWithAddr(&addr, data);
MOZ_ALWAYS_SUCCEEDS(rv);
if (NS_FAILED(rv) || data.IsEmpty()) {
break;
}
rv = mHttp3Connection->ProcessInputUseNSPRForIO(addr, data);
MOZ_ALWAYS_SUCCEEDS(rv);
if (NS_FAILED(rv)) {
break;
}
LOG(("Http3Session::ProcessInput received=%zu", data.Length()));
mTotalBytesRead += static_cast<int64_t>(data.Length());
}
return NS_OK;
}
// Not using NSPR.
auto rv = mHttp3Connection->ProcessInput();
// Note: WOULD_BLOCK is handled in neqo_glue.
if (NS_FAILED(rv.result)) {
mSocketError = rv.result;
// If there was an error return from here. We do not need to set a timer,
// because we will close the connection.
return rv.result;
}
mTotalBytesRead += rv.bytes_read;
socket->AddInputBytes(rv.bytes_read);
return NS_OK;
}
nsresult Http3Session::ProcessTransactionRead(uint64_t stream_id) {
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(stream_id);
if (!stream) {
LOG(
("Http3Session::ProcessTransactionRead - stream not found "
"stream_id=0x%" PRIx64 " [this=%p].",
stream_id, this));
return NS_OK;
}
return ProcessTransactionRead(stream);
}
nsresult Http3Session::ProcessTransactionRead(Http3StreamBase* stream) {
nsresult rv = stream->WriteSegments();
if (ASpdySession::SoftStreamError(rv) || stream->Done()) {
LOG3(
("Http3Session::ProcessSingleTransactionRead session=%p stream=%p "
"0x%" PRIx64 " cleanup stream rv=0x%" PRIx32 " done=%d.\n",
this, stream, stream->StreamId(), static_cast<uint32_t>(rv),
stream->Done()));
CloseStream(stream,
(rv == NS_BINDING_RETARGETED) ? NS_BINDING_RETARGETED : NS_OK);
return NS_OK;
}
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) {
return rv;
}
return NS_OK;
}
nsresult Http3Session::ProcessEvents() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG(("Http3Session::ProcessEvents [this=%p]", this));
// We need an array to pick up header data or a resumption token.
nsTArray<uint8_t> data;
Http3Event event{};
event.tag = Http3Event::Tag::NoEvent;
nsresult rv = mHttp3Connection->GetEvent(&event, data);
if (NS_FAILED(rv)) {
LOG(("Http3Session::ProcessEvents [this=%p] rv=%" PRIx32, this,
static_cast<uint32_t>(rv)));
return rv;
}
while (event.tag != Http3Event::Tag::NoEvent) {
switch (event.tag) {
case Http3Event::Tag::HeaderReady: {
MOZ_ASSERT(mState == CONNECTED);
LOG(("Http3Session::ProcessEvents - HeaderReady"));
uint64_t id = event.header_ready.stream_id;
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(id);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - HeaderReady - stream not found "
"stream_id=0x%" PRIx64 " [this=%p].",
id, this));
break;
}
MOZ_RELEASE_ASSERT(stream->GetHttp3Stream(),
"This must be a Http3Stream");
stream->SetResponseHeaders(data, event.header_ready.fin,
event.header_ready.interim);
rv = ProcessTransactionRead(stream);
if (NS_FAILED(rv)) {
LOG(("Http3Session::ProcessEvents [this=%p] rv=%" PRIx32, this,
static_cast<uint32_t>(rv)));
return rv;
}
mUdpConn->NotifyDataRead();
break;
}
case Http3Event::Tag::DataReadable: {
MOZ_ASSERT(mState == CONNECTED);
LOG(("Http3Session::ProcessEvents - DataReadable"));
uint64_t id = event.data_readable.stream_id;
nsresult rv = ProcessTransactionRead(id);
if (NS_FAILED(rv)) {
LOG(("Http3Session::ProcessEvents [this=%p] rv=%" PRIx32, this,
static_cast<uint32_t>(rv)));
return rv;
}
break;
}
case Http3Event::Tag::DataWritable: {
MOZ_ASSERT(CanSendData());
LOG(("Http3Session::ProcessEvents - DataWritable"));
RefPtr<Http3StreamBase> stream =
mStreamIdHash.Get(event.data_writable.stream_id);
if (stream) {
StreamReadyToWrite(stream);
stream->SetBlockedByFlowControl(false);
}
} break;
case Http3Event::Tag::Reset:
LOG(("Http3Session::ProcessEvents %p - Reset", this));
ResetOrStopSendingRecvd(event.reset.stream_id, event.reset.error,
RESET);
break;
case Http3Event::Tag::StopSending:
LOG(
("Http3Session::ProcessEvents %p - StopSeniding with error "
"0x%" PRIx64,
this, event.stop_sending.error));
if (event.stop_sending.error == HTTP3_APP_ERROR_NO_ERROR) {
RefPtr<Http3StreamBase> stream =
mStreamIdHash.Get(event.data_writable.stream_id);
if (stream) {
RefPtr<Http3Stream> httpStream = stream->GetHttp3Stream();
MOZ_RELEASE_ASSERT(httpStream, "This must be a Http3Stream");
httpStream->StopSending();
}
} else {
ResetOrStopSendingRecvd(event.reset.stream_id, event.reset.error,
STOP_SENDING);
}
break;
case Http3Event::Tag::PushPromise:
LOG(("Http3Session::ProcessEvents - PushPromise"));
break;
case Http3Event::Tag::PushHeaderReady:
LOG(("Http3Session::ProcessEvents - PushHeaderReady"));
break;
case Http3Event::Tag::PushDataReadable:
LOG(("Http3Session::ProcessEvents - PushDataReadable"));
break;
case Http3Event::Tag::PushCanceled:
LOG(("Http3Session::ProcessEvents - PushCanceled"));
break;
case Http3Event::Tag::RequestsCreatable:
LOG(("Http3Session::ProcessEvents - StreamCreatable"));
ProcessPending();
break;
case Http3Event::Tag::AuthenticationNeeded:
LOG(("Http3Session::ProcessEvents - AuthenticationNeeded %d",
mAuthenticationStarted));
if (!mAuthenticationStarted) {
mAuthenticationStarted = true;
LOG(("Http3Session::ProcessEvents - AuthenticationNeeded called"));
CallCertVerification(Nothing());
}
break;
case Http3Event::Tag::ZeroRttRejected:
LOG(("Http3Session::ProcessEvents - ZeroRttRejected"));
if (mState == ZERORTT) {
mState = INITIALIZING;
mTransactionCount = 0;
Finish0Rtt(true);
#ifndef ANDROID
ZeroRttTelemetry(ZeroRttOutcome::USED_REJECTED);
#endif
}
break;
case Http3Event::Tag::ResumptionToken: {
LOG(("Http3Session::ProcessEvents - ResumptionToken"));
if (StaticPrefs::network_http_http3_enable_0rtt() && !data.IsEmpty()) {
LOG(("Got a resumption token"));
nsAutoCString peerId;
mSocketControl->GetPeerId(peerId);
if (NS_FAILED(SSLTokensCache::Put(
peerId, data.Elements(), data.Length(), mSocketControl,
PR_Now() + event.resumption_token.expire_in))) {
LOG(("Adding resumption token failed"));
}
}
} break;
case Http3Event::Tag::ConnectionConnected: {
LOG(("Http3Session::ProcessEvents - ConnectionConnected"));
bool was0RTT = mState == ZERORTT;
mState = CONNECTED;
SetSecInfo();
mSocketControl->HandshakeCompleted();
if (was0RTT) {
Finish0Rtt(false);
#ifndef ANDROID
ZeroRttTelemetry(ZeroRttOutcome::USED_SUCCEEDED);
#endif
}
OnTransportStatus(nullptr, NS_NET_STATUS_CONNECTED_TO, 0);
mUdpConn->OnConnected();
ReportHttp3Connection();
// Maybe call ResumeSend:
// In case ZeroRtt has been used and it has been rejected, 2 events will
// be received: ZeroRttRejected and ConnectionConnected. ZeroRttRejected
// that will put all transaction into mReadyForWrite queue and it will
// call MaybeResumeSend, but that will not have an effect because the
// connection is ont in CONNECTED state. When ConnectionConnected event
// is received call MaybeResumeSend to trigger reads for the
// zero-rtt-rejected transactions.
MaybeResumeSend();
} break;
case Http3Event::Tag::GoawayReceived:
LOG(("Http3Session::ProcessEvents - GoawayReceived"));
mUdpConn->SetCloseReason(ConnectionCloseReason::GO_AWAY);
mGoawayReceived = true;
break;
case Http3Event::Tag::ConnectionClosing:
LOG(("Http3Session::ProcessEvents - ConnectionClosing"));
if (NS_SUCCEEDED(mError) && !IsClosing()) {
mError = NS_ERROR_NET_HTTP3_PROTOCOL_ERROR;
CloseConnectionTelemetry(event.connection_closing.error, true);
auto isStatelessResetOrNoError = [](CloseError& aError) -> bool {
if (aError.tag == CloseError::Tag::TransportInternalErrorOther &&
aError.transport_internal_error_other._0 ==
TRANSPORT_ERROR_STATELESS_RESET) {
return true;
}
if (aError.tag == CloseError::Tag::TransportError &&
aError.transport_error._0 == 0) {
return true;
}
if (aError.tag == CloseError::Tag::PeerError &&
aError.peer_error._0 == 0) {
return true;
}
if (aError.tag == CloseError::Tag::AppError &&
aError.app_error._0 == HTTP3_APP_ERROR_NO_ERROR) {
return true;
}
if (aError.tag == CloseError::Tag::PeerAppError &&
aError.peer_app_error._0 == HTTP3_APP_ERROR_NO_ERROR) {
return true;
}
return false;
};
if (isStatelessResetOrNoError(event.connection_closing.error)) {
mError = NS_ERROR_NET_RESET;
}
if (event.connection_closing.error.tag == CloseError::Tag::EchRetry) {
mSocketControl->SetRetryEchConfig(Substring(
reinterpret_cast<const char*>(data.Elements()), data.Length()));
mError = psm::GetXPCOMFromNSSError(SSL_ERROR_ECH_RETRY_WITH_ECH);
}
}
return mError;
break;
case Http3Event::Tag::ConnectionClosed:
LOG(("Http3Session::ProcessEvents - ConnectionClosed"));
if (NS_SUCCEEDED(mError)) {
mError = NS_ERROR_NET_TIMEOUT;
mUdpConn->SetCloseReason(ConnectionCloseReason::IDLE_TIMEOUT);
CloseConnectionTelemetry(event.connection_closed.error, false);
}
mIsClosedByNeqo = true;
if (event.connection_closed.error.tag == CloseError::Tag::EchRetry) {
mSocketControl->SetRetryEchConfig(Substring(
reinterpret_cast<const char*>(data.Elements()), data.Length()));
mError = psm::GetXPCOMFromNSSError(SSL_ERROR_ECH_RETRY_WITH_ECH);
}
LOG(("Http3Session::ProcessEvents - ConnectionClosed error=%" PRIx32,
static_cast<uint32_t>(mError)));
// We need to return here and let HttpConnectionUDP close the session.
return mError;
break;
case Http3Event::Tag::EchFallbackAuthenticationNeeded: {
nsCString echPublicName(reinterpret_cast<const char*>(data.Elements()),
data.Length());
LOG(
("Http3Session::ProcessEvents - EchFallbackAuthenticationNeeded "
"echPublicName=%s",
echPublicName.get()));
if (!mAuthenticationStarted) {
mAuthenticationStarted = true;
CallCertVerification(Some(echPublicName));
}
} break;
case Http3Event::Tag::WebTransport: {
switch (event.web_transport._0.tag) {
case WebTransportEventExternal::Tag::Negotiated:
LOG(("Http3Session::ProcessEvents - WebTransport %d",
event.web_transport._0.negotiated._0));
FinishNegotiation(ExtendedConnectKind::WebTransport,
event.web_transport._0.negotiated._0);
break;
case WebTransportEventExternal::Tag::Session: {
MOZ_ASSERT(mState == CONNECTED);
uint64_t id = event.web_transport._0.session._0;
LOG(
("Http3Session::ProcessEvents - WebTransport Session "
" sessionId=0x%" PRIx64,
id));
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(id);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - WebTransport Session - "
"stream not found "
"stream_id=0x%" PRIx64 " [this=%p].",
id, this));
break;
}
MOZ_RELEASE_ASSERT(stream->GetHttp3WebTransportSession(),
"It must be a WebTransport session");
stream->SetResponseHeaders(data, false, false);
rv = stream->WriteSegments();
if (ASpdySession::SoftStreamError(rv) || stream->Done()) {
LOG3(
("Http3Session::ProcessSingleTransactionRead session=%p "
"stream=%p "
"0x%" PRIx64 " cleanup stream rv=0x%" PRIx32 " done=%d.\n",
this, stream.get(), stream->StreamId(),
static_cast<uint32_t>(rv), stream->Done()));
// We need to keep the transaction, so we can use it to remove the
// stream from mStreamTransactionHash.
nsAHttpTransaction* trans = stream->Transaction();
if (mStreamTransactionHash.Contains(trans)) {
CloseStream(stream, (rv == NS_BINDING_RETARGETED)
? NS_BINDING_RETARGETED
: NS_OK);
mStreamTransactionHash.Remove(trans);
} else {
stream->GetHttp3WebTransportSession()->TransactionIsDone(
(rv == NS_BINDING_RETARGETED) ? NS_BINDING_RETARGETED
: NS_OK);
}
break;
}
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) {
LOG(("Http3Session::ProcessEvents [this=%p] rv=%" PRIx32, this,
static_cast<uint32_t>(rv)));
return rv;
}
} break;
case WebTransportEventExternal::Tag::SessionClosed: {
uint64_t id = event.web_transport._0.session_closed.stream_id;
LOG(
("Http3Session::ProcessEvents - WebTransport SessionClosed "
" sessionId=0x%" PRIx64,
id));
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(id);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - WebTransport SessionClosed - "
"stream not found "
"stream_id=0x%" PRIx64 " [this=%p].",
id, this));
break;
}
RefPtr<Http3WebTransportSession> wt =
stream->GetHttp3WebTransportSession();
MOZ_RELEASE_ASSERT(wt, "It must be a WebTransport session");
bool cleanly = false;
// TODO we do not handle the case when a WebTransport session stream
// is closed before headers are sent.
SessionCloseReasonExternal& reasonExternal =
event.web_transport._0.session_closed.reason;
uint32_t status = 0;
nsCString reason = ""_ns;
if (reasonExternal.tag == SessionCloseReasonExternal::Tag::Error) {
status = reasonExternal.error._0;
} else if (reasonExternal.tag ==
SessionCloseReasonExternal::Tag::Status) {
status = reasonExternal.status._0;
cleanly = true;
} else {
status = reasonExternal.clean._0;
reason.Assign(reinterpret_cast<const char*>(data.Elements()),
data.Length());
cleanly = true;
}
LOG(("reason.tag=%u err=%u data=%s\n",
static_cast<uint32_t>(reasonExternal.tag), status,
reason.get()));
wt->OnSessionClosed(cleanly, status, reason);
} break;
case WebTransportEventExternal::Tag::NewStream: {
LOG(
("Http3Session::ProcessEvents - WebTransport NewStream "
"streamId=0x%" PRIx64 " sessionId=0x%" PRIx64,
event.web_transport._0.new_stream.stream_id,
event.web_transport._0.new_stream.session_id));
uint64_t sessionId = event.web_transport._0.new_stream.session_id;
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(sessionId);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - WebTransport NewStream - "
"session not found "
"sessionId=0x%" PRIx64 " [this=%p].",
sessionId, this));
break;
}
RefPtr<Http3WebTransportSession> wt =
stream->GetHttp3WebTransportSession();
if (!wt) {
break;
}
RefPtr<Http3WebTransportStream> wtStream =
wt->OnIncomingWebTransportStream(
event.web_transport._0.new_stream.stream_type,
event.web_transport._0.new_stream.stream_id);
if (!wtStream) {
break;
}
// WebTransportStream is managed by Http3Session now.
mWebTransportStreams.AppendElement(wtStream);
mWebTransportStreamToSessionMap.InsertOrUpdate(wtStream->StreamId(),
wt->StreamId());
mStreamIdHash.InsertOrUpdate(wtStream->StreamId(),
std::move(wtStream));
} break;
case WebTransportEventExternal::Tag::Datagram:
LOG(
("Http3Session::ProcessEvents - "
"WebTransportEventExternal::Tag::Datagram [this=%p]",
this));
uint64_t sessionId = event.web_transport._0.datagram.session_id;
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(sessionId);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - WebTransport Datagram - "
"session not found "
"sessionId=0x%" PRIx64 " [this=%p].",
sessionId, this));
break;
}
RefPtr<Http3WebTransportSession> wt =
stream->GetHttp3WebTransportSession();
if (!wt) {
break;
}
wt->OnDatagramReceived(std::move(data));
break;
}
} break;
case Http3Event::Tag::ConnectUdp: {
switch (event.connect_udp._0.tag) {
case ConnectUdpEventExternal::Tag::Negotiated:
LOG(("Http3Session::ProcessEvents - ConnectUdp Negotiated %d",
event.connect_udp._0.negotiated._0));
FinishNegotiation(ExtendedConnectKind::ConnectUDP,
event.connect_udp._0.negotiated._0);
break;
case ConnectUdpEventExternal::Tag::Session: {
MOZ_ASSERT(mState == CONNECTED);
uint64_t id = event.connect_udp._0.session._0;
LOG(
("Http3Session::ProcessEvents - ConnectUdp "
" streamId=0x%" PRIx64,
id));
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(id);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - ConnectUdp Session - "
"stream not found "
"stream_id=0x%" PRIx64 " [this=%p].",
id, this));
break;
}
MOZ_RELEASE_ASSERT(stream->GetHttp3ConnectUDPStream(),
"It must be a ConnectUdp session");
stream->SetResponseHeaders(data, false, false);
rv = stream->WriteSegments();
LOG(("rv=%x", static_cast<uint32_t>(rv)));
if (ASpdySession::SoftStreamError(rv) || stream->Done()) {
LOG3(
("Http3Session::ProcessSingleTransactionRead session=%p "
"stream=%p "
"0x%" PRIx64 " cleanup stream rv=0x%" PRIx32 " done=%d.\n",
this, stream.get(), stream->StreamId(),
static_cast<uint32_t>(rv), stream->Done()));
// We need to keep the transaction, so we can use it to remove the
// stream from mStreamTransactionHash.
nsAHttpTransaction* trans = stream->Transaction();
if (mStreamTransactionHash.Contains(trans)) {
CloseStream(stream, (rv == NS_BINDING_RETARGETED)
? NS_BINDING_RETARGETED
: NS_OK);
mStreamTransactionHash.Remove(trans);
} else {
stream->GetHttp3ConnectUDPStream()->TransactionIsDone(
(rv == NS_BINDING_RETARGETED) ? NS_BINDING_RETARGETED
: NS_OK);
}
break;
}
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) {
LOG(("Http3Session::ProcessEvents [this=%p] rv=%" PRIx32, this,
static_cast<uint32_t>(rv)));
return rv;
}
} break;
case ConnectUdpEventExternal::Tag::SessionClosed: {
uint64_t id = event.connect_udp._0.session_closed.stream_id;
LOG(
("Http3Session::ProcessEvents - connect_udp SessionClosed "
" sessionId=0x%" PRIx64,
id));
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(id);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - connect_udp SessionClosed - "
"stream not found "
"stream_id=0x%" PRIx64 " [this=%p].",
id, this));
break;
}
RefPtr<Http3ConnectUDPStream> connectUDPStream =
stream->GetHttp3ConnectUDPStream();
MOZ_RELEASE_ASSERT(connectUDPStream,
"It must be a ConnectUDP stream");
// TODO we do not handle the case when a ConnectUDP session stream
// is closed before headers are sent.
SessionCloseReasonExternal& reasonExternal =
event.connect_udp._0.session_closed.reason;
uint32_t status = 0;
nsCString reason = ""_ns;
if (reasonExternal.tag == SessionCloseReasonExternal::Tag::Error) {
status = reasonExternal.error._0;
} else if (reasonExternal.tag ==
SessionCloseReasonExternal::Tag::Status) {
status = reasonExternal.status._0;
} else {
status = reasonExternal.clean._0;
reason.Assign(reinterpret_cast<const char*>(data.Elements()),
data.Length());
}
LOG(("reason.tag=%u err=%u data=%s\n",
static_cast<uint32_t>(reasonExternal.tag), status,
reason.get()));
CloseStream(connectUDPStream,
status == 0 ? NS_OK : NS_ERROR_FAILURE);
} break;
case ConnectUdpEventExternal::Tag::Datagram:
LOG(("Http3Session::ProcessEvents - ConnectUdp Datagram"));
uint64_t streamId = event.connect_udp._0.datagram.session_id;
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(streamId);
if (!stream) {
LOG(
("Http3Session::ProcessEvents - ConnectUdp Datagram - "
"stream not found "
"streamId=0x%" PRIx64 " [this=%p].",
streamId, this));
break;
}
RefPtr<Http3ConnectUDPStream> tunnelStream =
stream->GetHttp3ConnectUDPStream();
if (!tunnelStream) {
break;
}
tunnelStream->OnDatagramReceived(std::move(data));
break;
}
} break;
default:
break;
}
// Delete previous content of data
data.TruncateLength(0);
rv = mHttp3Connection->GetEvent(&event, data);
if (NS_FAILED(rv)) {
LOG(("Http3Session::ProcessEvents [this=%p] rv=%" PRIx32, this,
static_cast<uint32_t>(rv)));
return rv;
}
}
return NS_OK;
} // namespace net
// This function may return a socket error.
// It will not return an error if socket error is
// NS_BASE_STREAM_WOULD_BLOCK.
// A Caller of this function will close the Http3 connection
// if this function returns an error.
// Callers are:
// 1) HttpConnectionUDP::OnQuicTimeoutExpired
// 2) HttpConnectionUDP::SendData ->
// Http3Session::SendData
nsresult Http3Session::ProcessOutput(nsIUDPSocket* socket) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(mUdpConn);
LOG(("Http3Session::ProcessOutput reader=%p, [this=%p]", mUdpConn.get(),
this));
if (!socket || socket->IsSocketClosed()) {
MOZ_DIAGNOSTIC_ASSERT(false, "UDP socket should still be open");
return NS_ERROR_UNEXPECTED;
}
if (mUseNSPRForIO) {
mSocket = socket;
nsresult rv = mHttp3Connection->ProcessOutputAndSendUseNSPRForIO(
this,
[](void* aContext, uint16_t aFamily, const uint8_t* aAddr,
uint16_t aPort, const uint8_t* aData, uint32_t aLength) {
Http3Session* self = (Http3Session*)aContext;
uint32_t written = 0;
NetAddr addr;
if (NS_FAILED(RawBytesToNetAddr(aFamily, aAddr, aPort, &addr))) {
return NS_OK;
}
LOG3(
("Http3Session::ProcessOutput sending packet with %u bytes to %s "
"port=%d [this=%p].",
aLength, addr.ToString().get(), aPort, self));
nsresult rv =
self->mSocket->SendWithAddress(&addr, aData, aLength, &written);
LOG(("Http3Session::ProcessOutput sending packet rv=%d osError=%d",
static_cast<int32_t>(rv), NS_FAILED(rv) ? PR_GetOSError() : 0));
if (NS_FAILED(rv) && (rv != NS_BASE_STREAM_WOULD_BLOCK)) {
self->mSocketError = rv;
// If there was an error that is not NS_BASE_STREAM_WOULD_BLOCK
// return from here. We do not need to set a timer, because we
// will close the connection.
return rv;
}
self->mTotalBytesWritten += aLength;
self->mLastWriteTime = PR_IntervalNow();
return NS_OK;
},
[](void* aContext, uint64_t timeout) {
Http3Session* self = (Http3Session*)aContext;
self->SetupTimer(timeout);
});
mSocket = nullptr;
return rv;
}
// Not using NSPR.
auto rv = mHttp3Connection->ProcessOutputAndSend(
this, [](void* aContext, uint64_t timeout) {
Http3Session* self = (Http3Session*)aContext;
self->SetupTimer(timeout);
});
if (rv.result == NS_BASE_STREAM_WOULD_BLOCK) {
// The OS buffer was full. Tell the UDP socket to poll for
// write-availability.
socket->EnableWritePoll();
} else if (NS_FAILED(rv.result)) {
mSocketError = rv.result;
// If there was an error return from here. We do not need to set a timer,
// because we will close the connection.
return rv.result;
}
if (rv.bytes_written != 0) {
mTotalBytesWritten += rv.bytes_written;
mLastWriteTime = PR_IntervalNow();
socket->AddOutputBytes(rv.bytes_written);
}
return NS_OK;
}
// This is only called when timer expires.
// It is called by HttpConnectionUDP::OnQuicTimeout.
// If tihs function returns an error OnQuicTimeout will handle the error
// properly and close the connection.
nsresult Http3Session::ProcessOutputAndEvents(nsIUDPSocket* socket) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(mTimerShouldTrigger);
if (Telemetry::CanRecordPrereleaseData()) {
auto now = TimeStamp::Now();
if (mTimerShouldTrigger > now) {
// See bug 1935459
glean::http3::timer_delayed.AccumulateRawDuration(0);
} else {
glean::http3::timer_delayed.AccumulateRawDuration(now -
mTimerShouldTrigger);
}
}
mTimerShouldTrigger = TimeStamp();
nsresult rv = SendData(socket);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
NS_IMPL_ISUPPORTS(Http3Session::OnQuicTimeout, nsITimerCallback, nsINamed)
Http3Session::OnQuicTimeout::OnQuicTimeout(HttpConnectionUDP* aConnection)
: mConnection(aConnection) {
MOZ_ASSERT(mConnection);
}
NS_IMETHODIMP
Http3Session::OnQuicTimeout::Notify(nsITimer* timer) {
mConnection->OnQuicTimeoutExpired();
return NS_OK;
}
NS_IMETHODIMP
Http3Session::OnQuicTimeout::GetName(nsACString& aName) {
aName.AssignLiteral("net::HttpConnectionUDP::OnQuicTimeout");
return NS_OK;
}
void Http3Session::SetupTimer(uint64_t aTimeout) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
// UINT64_MAX indicated a no-op from neqo, which only happens when a
// connection is in or going to be Closed state.
if (aTimeout == UINT64_MAX) {
return;
}
LOG3(
("Http3Session::SetupTimer to %" PRIu64 "ms [this=%p].", aTimeout, this));
// Remember the time when the timer should trigger.
mTimerShouldTrigger =
TimeStamp::Now() + TimeDuration::FromMilliseconds(aTimeout);
if (!mTimerCallback) {
// We can keep the same callback object for all our lifetime.
mTimerCallback = MakeRefPtr<OnQuicTimeout>(mUdpConn);
}
if (!mTimer) {
// This can only fail on OOM and we'd crash.
mTimer = NS_NewTimer();
}
DebugOnly<nsresult> rv = mTimer->InitWithCallback(mTimerCallback, aTimeout,
nsITimer::TYPE_ONE_SHOT);
// There is no meaningful error handling we can do here. But an error here
// should only be possible if the timer thread did already shut down.
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
bool Http3Session::AddStream(nsAHttpTransaction* aHttpTransaction,
int32_t aPriority,
nsIInterfaceRequestor* aCallbacks) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
nsHttpTransaction* trans = aHttpTransaction->QueryHttpTransaction();
bool firstStream = false;
if (!mConnection) {
// Get the connection from the first transaction.
mConnection = aHttpTransaction->Connection();
firstStream = true;
}
// Make sure we report the connectStart
auto reportConnectStart = MakeScopeExit([&] {
if (firstStream) {
OnTransportStatus(nullptr, NS_NET_STATUS_CONNECTING_TO, 0);
}
});
if (IsClosing()) {
LOG3(
("Http3Session::AddStream %p atrans=%p trans=%p session unusable - "
"resched.\n",
this, aHttpTransaction, trans));
aHttpTransaction->SetConnection(nullptr);
nsresult rv = gHttpHandler->InitiateTransaction(trans, trans->Priority());
if (NS_FAILED(rv)) {
LOG3(
("Http3Session::AddStream %p atrans=%p trans=%p failed to initiate "
"transaction (0x%" PRIx32 ").\n",
this, aHttpTransaction, trans, static_cast<uint32_t>(rv)));
}
return true;
}
aHttpTransaction->SetConnection(this);
aHttpTransaction->OnActivated();
// reset the read timers to wash away any idle time
mLastWriteTime = PR_IntervalNow();
ClassOfService cos;
if (trans) {
cos = trans->GetClassOfService();
}
Http3StreamBase* stream = nullptr;
if (trans && mConnInfo->IsHttp3ProxyConnection() && !mIsInTunnel) {
LOG3(("Http3Session::AddStream new connect-udp stream %p atrans=%p.\n",
this, aHttpTransaction));
stream = new Http3ConnectUDPStream(aHttpTransaction, this,
NS_GetCurrentThread());
} else if (trans && trans->IsForWebTransport()) {
LOG3(("Http3Session::AddStream new WeTransport session %p atrans=%p.\n",
this, aHttpTransaction));
stream = new Http3WebTransportSession(aHttpTransaction, this);
mHasWebTransportSession = true;
} else {
LOG3(("Http3Session::AddStream %p atrans=%p.\n", this, aHttpTransaction));
stream = new Http3Stream(aHttpTransaction, this, cos, mCurrentBrowserId);
}
mStreamTransactionHash.InsertOrUpdate(aHttpTransaction, RefPtr{stream});
if (mState == ZERORTT) {
if (!stream->Do0RTT()) {
LOG(("Http3Session %p will not get early data from Http3Stream %p", this,
stream));
if (!mCannotDo0RTTStreams.Contains(stream)) {
mCannotDo0RTTStreams.AppendElement(stream);
}
if (stream->GetHttp3WebTransportSession()) {
DeferIfNegotiating(ExtendedConnectKind::WebTransport, stream);
} else if (stream->GetHttp3ConnectUDPStream()) {
DeferIfNegotiating(ExtendedConnectKind::ConnectUDP, stream);
}
return true;
}
m0RTTStreams.AppendElement(stream);
}
if (stream->GetHttp3WebTransportSession()) {
if (DeferIfNegotiating(ExtendedConnectKind::WebTransport, stream)) {
return true;
}
} else if (stream->GetHttp3ConnectUDPStream()) {
if (DeferIfNegotiating(ExtendedConnectKind::ConnectUDP, stream)) {
return true;
}
}
if (!mFirstHttpTransaction && !IsConnected()) {
mFirstHttpTransaction = aHttpTransaction->QueryHttpTransaction();
LOG3(("Http3Session::AddStream first session=%p trans=%p ", this,
mFirstHttpTransaction.get()));
}
StreamReadyToWrite(stream);
return true;
}
bool Http3Session::DeferIfNegotiating(ExtendedConnectKind aKind,
Http3StreamBase* aStream) {
auto& st = ExtState(aKind);
if (st.mStatus == NEGOTIATING) {
if (!st.mWaiters.Contains(aStream)) {
LOG(("waiting for negotiation"));
st.mWaiters.AppendElement(aStream);
}
return true;
}
return false;
}
void Http3Session::FinishNegotiation(ExtendedConnectKind aKind, bool aSuccess) {
auto& st = ExtState(aKind);
if (st.mWaiters.IsEmpty()) {
st.mStatus = aSuccess ? SUCCEEDED : FAILED;
return;
}
MOZ_ASSERT(st.mStatus == NEGOTIATING);
st.mStatus = aSuccess ? SUCCEEDED : FAILED;
for (size_t i = 0; i < st.mWaiters.Length(); ++i) {
if (st.mWaiters[i]) {
mReadyForWrite.Push(st.mWaiters[i]);
}
}
st.mWaiters.Clear();
MaybeResumeSend();
}
bool Http3Session::CanReuse() {
// TODO: we assume "pooling" is disabled here, so we don't allow this session
// to be reused. "pooling" will be implemented in bug 1815735.
return CanSendData() && !(mGoawayReceived || mShouldClose) &&
!mHasWebTransportSession;
}
void Http3Session::QueueStream(Http3StreamBase* stream) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(!stream->Queued());
LOG3(("Http3Session::QueueStream %p stream %p queued.", this, stream));
stream->SetQueued(true);
mQueuedStreams.Push(stream);
}
void Http3Session::ProcessPending() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
RefPtr<Http3StreamBase> stream;
while ((stream = mQueuedStreams.PopFront())) {
LOG3(("Http3Session::ProcessPending %p stream %p woken from queue.", this,
stream.get()));
MOZ_ASSERT(stream->Queued());
stream->SetQueued(false);
mReadyForWrite.Push(stream);
}
MaybeResumeSend();
}
static void RemoveStreamFromQueue(Http3StreamBase* aStream,
nsRefPtrDeque<Http3StreamBase>& queue) {
size_t size = queue.GetSize();
for (size_t count = 0; count < size; ++count) {
RefPtr<Http3StreamBase> stream = queue.PopFront();
if (stream != aStream) {
queue.Push(stream);
}
}
}
void Http3Session::RemoveStreamFromQueues(Http3StreamBase* aStream) {
RemoveStreamFromQueue(aStream, mReadyForWrite);
RemoveStreamFromQueue(aStream, mQueuedStreams);
mSlowConsumersReadyForRead.RemoveElement(aStream);
}
// This is called by Http3Stream::OnReadSegment.
// ProcessOutput will be called in Http3Session::ReadSegment that
// calls Http3Stream::OnReadSegment.
nsresult Http3Session::TryActivating(
const nsACString& aMethod, const nsACString& aScheme,
const nsACString& aAuthorityHeader, const nsACString& aPathQuery,
const nsACString& aHeaders, uint64_t* aStreamId, Http3StreamBase* aStream) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(*aStreamId == UINT64_MAX);
LOG(("Http3Session::TryActivating [stream=%p, this=%p state=%d]", aStream,
this, mState));
if (IsClosing()) {
if (NS_FAILED(mError)) {
return mError;
}
return NS_ERROR_FAILURE;
}
if (aStream->Queued()) {
LOG3(("Http3Session::TryActivating %p stream=%p already queued.\n", this,
aStream));
return NS_BASE_STREAM_WOULD_BLOCK;
}
if (mState == ZERORTT) {
if (!aStream->Do0RTT()) {
// Stream can't do 0RTT - queue it for activation when the session
// reaches CONNECTED state via Finish0Rtt.
if (!mCannotDo0RTTStreams.Contains(aStream)) {
LOG(("Http3Session %p queuing stream %p for post-0RTT activation", this,
aStream));
mCannotDo0RTTStreams.AppendElement(aStream);
}
return NS_BASE_STREAM_WOULD_BLOCK;
}
}
nsresult rv = NS_OK;
// The order of these checks is important: Http3StreamTunnel inherits from
// Http3Stream, so a tunnel will also match conditions for a regular stream.
// Ensure we handle Http3StreamTunnel cases before generic Http3Stream logic.
if (RefPtr<Http3StreamTunnel> streamTunnel =
aStream->GetHttp3StreamTunnel()) {
rv = mHttp3Connection->Connect(aAuthorityHeader, aHeaders, aStreamId, 3,
false);
} else if (RefPtr<Http3Stream> httpStream = aStream->GetHttp3Stream()) {
rv = mHttp3Connection->Fetch(
aMethod, aScheme, aAuthorityHeader, aPathQuery, aHeaders, aStreamId,
httpStream->PriorityUrgency(), httpStream->PriorityIncremental());
} else if (RefPtr<Http3ConnectUDPStream> udpStream =
aStream->GetHttp3ConnectUDPStream()) {
if (DeferIfNegotiating(ExtendedConnectKind::ConnectUDP, aStream)) {
return NS_BASE_STREAM_WOULD_BLOCK;
}
rv = mHttp3Connection->CreateConnectUdp(aAuthorityHeader, aPathQuery,
aHeaders, aStreamId);
} else {
MOZ_RELEASE_ASSERT(aStream->GetHttp3WebTransportSession(),
"It must be a WebTransport session");
// Don't call CreateWebTransport if we are still waiting for the negotiation
// result.
if (DeferIfNegotiating(ExtendedConnectKind::WebTransport, aStream)) {
return NS_BASE_STREAM_WOULD_BLOCK;
}
rv = mHttp3Connection->CreateWebTransport(aAuthorityHeader, aPathQuery,
aHeaders, aStreamId);
}
if (NS_FAILED(rv)) {
LOG(("Http3Session::TryActivating returns error=0x%" PRIx32 "[stream=%p, "
"this=%p]",
static_cast<uint32_t>(rv), aStream, this));
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
LOG3(
("Http3Session::TryActivating %p stream=%p no room for more "
"concurrent streams\n",
this, aStream));
mTransactionsBlockedByStreamLimitCount++;
if (mQueuedStreams.GetSize() == 0) {
mBlockedByStreamLimitCount++;
}
QueueStream(aStream);
return rv;
}
// Previously we always returned NS_OK here, which caused the
// transaction to wait until the quic connection timed out
// after which it was retried without quic.
if (StaticPrefs::network_http_http3_fallback_to_h2_on_error()) {
return NS_ERROR_HTTP2_FALLBACK_TO_HTTP1;
}
return rv;
}
LOG(("Http3Session::TryActivating streamId=0x%" PRIx64
" for stream=%p [this=%p].",
*aStreamId, aStream, this));
MOZ_ASSERT(*aStreamId != UINT64_MAX);
if (mTransactionCount > 0 && mStreamIdHash.IsEmpty()) {
MOZ_ASSERT(mConnectionIdleStart);
MOZ_ASSERT(mFirstStreamIdReuseIdleConnection.isNothing());
mConnectionIdleEnd = TimeStamp::Now();
mFirstStreamIdReuseIdleConnection = Some(*aStreamId);
}
mStreamIdHash.InsertOrUpdate(*aStreamId, RefPtr{aStream});
mTransactionCount++;
return NS_OK;
}
// This is called by Http3WebTransportStream::OnReadSegment.
// TODO: this function is almost the same as TryActivating().
// We should try to reduce the duplicate code.
nsresult Http3Session::TryActivatingWebTransportStream(
uint64_t* aStreamId, Http3StreamBase* aStream) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(*aStreamId == UINT64_MAX);
LOG(
("Http3Session::TryActivatingWebTransportStream [stream=%p, this=%p "
"state=%d]",
aStream, this, mState));
if (IsClosing()) {
if (NS_FAILED(mError)) {
return mError;
}
return NS_ERROR_FAILURE;
}
if (aStream->Queued()) {
LOG3(
("Http3Session::TryActivatingWebTransportStream %p stream=%p already "
"queued.\n",
this, aStream));
return NS_BASE_STREAM_WOULD_BLOCK;
}
nsresult rv = NS_OK;
RefPtr<Http3WebTransportStream> wtStream =
aStream->GetHttp3WebTransportStream();
MOZ_RELEASE_ASSERT(wtStream, "It must be a WebTransport stream");
rv = mHttp3Connection->CreateWebTransportStream(
wtStream->SessionId(), wtStream->StreamType(), aStreamId);
if (NS_FAILED(rv)) {
LOG((
"Http3Session::TryActivatingWebTransportStream returns error=0x%" PRIx32
"[stream=%p, "
"this=%p]",
static_cast<uint32_t>(rv), aStream, this));
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
LOG3(
("Http3Session::TryActivatingWebTransportStream %p stream=%p no room "
"for more "
"concurrent streams\n",
this, aStream));
QueueStream(aStream);
return rv;
}
return rv;
}
LOG(("Http3Session::TryActivatingWebTransportStream streamId=0x%" PRIx64
" for stream=%p [this=%p].",
*aStreamId, aStream, this));
MOZ_ASSERT(*aStreamId != UINT64_MAX);
RefPtr<Http3StreamBase> session = mStreamIdHash.Get(wtStream->SessionId());
MOZ_ASSERT(session);
Http3WebTransportSession* wtSession = session->GetHttp3WebTransportSession();
MOZ_ASSERT(wtSession);
wtSession->RemoveWebTransportStream(wtStream);
// WebTransportStream is managed by Http3Session now.
mWebTransportStreams.AppendElement(wtStream);
mWebTransportStreamToSessionMap.InsertOrUpdate(*aStreamId,
session->StreamId());
mStreamIdHash.InsertOrUpdate(*aStreamId, std::move(wtStream));
return NS_OK;
}
// This is only called by Http3Stream::OnReadSegment.
// ProcessOutput will be called in Http3Session::ReadSegment that
// calls Http3Stream::OnReadSegment.
void Http3Session::CloseSendingSide(uint64_t aStreamId) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
mHttp3Connection->CloseStream(aStreamId);
}
// This is only called by Http3Stream::OnReadSegment.
// ProcessOutput will be called in Http3Session::ReadSegment that
// calls Http3Stream::OnReadSegment.
nsresult Http3Session::SendRequestBody(uint64_t aStreamId, const char* buf,
uint32_t count, uint32_t* countRead) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
nsresult rv = mHttp3Connection->SendRequestBody(
aStreamId, (const uint8_t*)buf, count, countRead);
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
mTransactionsSenderBlockedByFlowControlCount++;
} else if (NS_FAILED(rv)) {
// Ignore this error. This may happen if some events are not handled yet.
// TODO we may try to add an assertion here.
// We will pretend that sender is blocked, that will cause the caller to
// stop trying.
*countRead = 0;
rv = NS_BASE_STREAM_WOULD_BLOCK;
}
MOZ_ASSERT((*countRead != 0) || NS_FAILED(rv));
return rv;
}
void Http3Session::ResetOrStopSendingRecvd(uint64_t aStreamId, uint64_t aError,
ResetType aType) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
uint64_t sessionId = 0;
if (mWebTransportStreamToSessionMap.Get(aStreamId, &sessionId)) {
uint8_t wtError = Http3ErrorToWebTransportError(aError);
nsresult rv = GetNSResultFromWebTransportError(wtError);
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(aStreamId);
if (stream) {
if (aType == RESET) {
stream->SetRecvdReset();
}
RefPtr<Http3WebTransportStream> wtStream =
stream->GetHttp3WebTransportStream();
if (wtStream) {
CloseWebTransportStream(wtStream, rv);
}
}
RefPtr<Http3StreamBase> session = mStreamIdHash.Get(sessionId);
if (session) {
Http3WebTransportSession* wtSession =
session->GetHttp3WebTransportSession();
MOZ_ASSERT(wtSession);
if (wtSession) {
if (aType == RESET) {
wtSession->OnStreamReset(aStreamId, rv);
} else {
wtSession->OnStreamStopSending(aStreamId, rv);
}
}
}
return;
}
RefPtr<Http3StreamBase> stream = mStreamIdHash.Get(aStreamId);
if (!stream) {
return;
}
RefPtr<Http3Stream> httpStream = stream->GetHttp3Stream();
if (!httpStream) {
return;
}
// We only handle some of Http3 error as epecial, the rest are just equivalent
// to cancel.
if (aError == HTTP3_APP_ERROR_VERSION_FALLBACK) {
// We will restart the request and the alt-svc will be removed
// automatically.
// Also disable http3 we want http1.1.
httpStream->Transaction()->DisableHttp3(false);
httpStream->Transaction()->DisableSpdy();
CloseStream(stream, NS_ERROR_NET_RESET);
} else if (aError == HTTP3_APP_ERROR_REQUEST_REJECTED) {
// This request was rejected because server is probably busy or going away.
// We can restart the request using alt-svc. Without calling
// DoNotRemoveAltSvc the alt-svc route will be removed.
httpStream->Transaction()->DoNotRemoveAltSvc();
CloseStream(stream, NS_ERROR_NET_RESET);
} else {
if (httpStream->RecvdData()) {
CloseStream(stream, NS_ERROR_NET_PARTIAL_TRANSFER);
} else {
CloseStream(stream, NS_ERROR_NET_INTERRUPT);
}
}
}
void Http3Session::SetConnection(nsAHttpConnection* aConn) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
mConnection = aConn;
}
void Http3Session::GetSecurityCallbacks(nsIInterfaceRequestor** aOut) {
*aOut = nullptr;
}
// TODO
void Http3Session::OnTransportStatus(nsITransport* aTransport, nsresult aStatus,
int64_t aProgress) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
switch (aStatus) {
// These should appear only once, deliver to the first
// transaction on the session.
case NS_NET_STATUS_RESOLVING_HOST:
case NS_NET_STATUS_RESOLVED_HOST:
case NS_NET_STATUS_CONNECTING_TO:
case NS_NET_STATUS_CONNECTED_TO: {
if (!mFirstHttpTransaction) {
// if we still do not have a HttpTransaction store timings info in
// a HttpConnection.
// If some error occur it can happen that we do not have a connection.
if (mConnection) {
RefPtr<HttpConnectionBase> conn = mConnection->HttpConnection();
conn->SetEvent(aStatus);
}
} else {
mFirstHttpTransaction->OnTransportStatus(aTransport, aStatus,
aProgress);
}
if (aStatus == NS_NET_STATUS_CONNECTED_TO) {
mFirstHttpTransaction = nullptr;
}
break;
}
default:
// The other transport events are ignored here because there is no good
// way to map them to the right transaction in HTTP3. Instead, the events
// are generated again from the HTTP3 code and passed directly to the
// correct transaction.
// NS_NET_STATUS_SENDING_TO:
// This is generated by the socket transport when (part) of
// a transaction is written out
//
// There is no good way to map it to the right transaction in HTTP3,
// so it is ignored here and generated separately when the request
// is sent from Http3Stream.
// NS_NET_STATUS_WAITING_FOR:
// Created by nsHttpConnection when the request has been totally sent.
// There is no good way to map it to the right transaction in HTTP3,
// so it is ignored here and generated separately when the same
// condition is complete in Http3Stream when there is no more
// request body left to be transmitted.
// NS_NET_STATUS_RECEIVING_FROM
// Generated in Http3Stream whenever the stream reads data.
break;
}
}
bool Http3Session::IsDone() { return mState == CLOSED; }
nsresult Http3Session::Status() {
MOZ_ASSERT(false, "Http3Session::Status()");
return NS_ERROR_UNEXPECTED;
}
uint32_t Http3Session::Caps() {
MOZ_ASSERT(false, "Http3Session::Caps()");
return 0;
}
nsresult Http3Session::ReadSegments(nsAHttpSegmentReader* reader,
uint32_t count, uint32_t* countRead) {
MOZ_ASSERT(false, "Http3Session::ReadSegments()");
return NS_ERROR_UNEXPECTED;
}
nsresult Http3Session::SendData(nsIUDPSocket* socket) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG(("Http3Session::SendData [this=%p]", this));
// 1) go through all streams/transactions that are ready to write and
// write their data into quic streams (no network write yet).
// 2) call ProcessOutput that will loop until all available packets are
// written to a socket or the socket returns an error code.
// 3) if we still have streams ready to write call ResumeSend()(we may
// still have such streams because on an stream error we return earlier
// to let the error be handled).
// 4)
nsresult rv = NS_OK;
RefPtr<Http3StreamBase> stream;
nsTArray<RefPtr<Http3StreamBase>> blockedStreams;
// Step 1)
while (CanSendData() && (stream = mReadyForWrite.PopFront())) {
LOG(("Http3Session::SendData call ReadSegments from stream=%p [this=%p]",
stream.get(), this));
stream->SetInTxQueue(false);
if (stream->BlockedByFlowControl()) {
LOG(("stream %p blocked by flow control", stream.get()));
blockedStreams.AppendElement(stream);
continue;
}
rv = stream->ReadSegments();
// on stream error we return earlier to let the error be handled.
if (NS_FAILED(rv)) {
LOG3(("Http3Session::SendData %p returns error code 0x%" PRIx32, this,
static_cast<uint32_t>(rv)));
MOZ_ASSERT(rv != NS_BASE_STREAM_WOULD_BLOCK);
if (rv == NS_BASE_STREAM_WOULD_BLOCK) { // Just in case!
rv = NS_OK;
} else if (ASpdySession::SoftStreamError(rv)) {
CloseStream(stream, rv);
LOG3(("Http3Session::SendData %p soft error override\n", this));
rv = NS_OK;
} else {
break;
}
}
}
if (NS_SUCCEEDED(rv)) {
// Step 2:
// Call actual network write.
rv = ProcessOutput(socket);
}
// Step 3:
MaybeResumeSend();
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
rv = NS_OK;
}
if (NS_FAILED(rv)) {
return rv;
}
// Put the blocked streams back to the queue, since they are ready to write.
for (const auto& stream : blockedStreams) {
mReadyForWrite.Push(stream);
stream->SetInTxQueue(true);
}
rv = ProcessEvents();
// Let the connection know we sent some app data successfully.
if (stream && NS_SUCCEEDED(rv)) {
mUdpConn->NotifyDataWrite();
}
return rv;
}
void Http3Session::StreamReadyToWrite(Http3StreamBase* aStream) {
MOZ_ASSERT(aStream);
// Http3Session::StreamReadyToWrite can be called multiple times when we get
// duplicate DataWrite events from neqo at the same time. In this case, we
// only want to insert the stream in `mReadyForWrite` once.
if (aStream->IsInTxQueue()) {
return;
}
mReadyForWrite.Push(aStream);
aStream->SetInTxQueue(true);
if (CanSendData() && mConnection) {
(void)mConnection->ResumeSend();
}
}
void Http3Session::MaybeResumeSend() {
if ((mReadyForWrite.GetSize() > 0) && CanSendData() && mConnection) {
(void)mConnection->ResumeSend();
}
}
nsresult Http3Session::ProcessSlowConsumers() {
if (mSlowConsumersReadyForRead.IsEmpty()) {
return NS_OK;
}
RefPtr<Http3StreamBase> slowConsumer =
mSlowConsumersReadyForRead.ElementAt(0);
mSlowConsumersReadyForRead.RemoveElementAt(0);
nsresult rv = ProcessTransactionRead(slowConsumer);
return rv;
}
nsresult Http3Session::WriteSegments(nsAHttpSegmentWriter* writer,
uint32_t count, uint32_t* countWritten) {
MOZ_ASSERT(false, "Http3Session::WriteSegments()");
return NS_ERROR_UNEXPECTED;
}
nsresult Http3Session::RecvData(nsIUDPSocket* socket) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
// Process slow consumers.
nsresult rv = ProcessSlowConsumers();
if (NS_FAILED(rv)) {
LOG3(("Http3Session %p ProcessSlowConsumers returns 0x%" PRIx32 "\n", this,
static_cast<uint32_t>(rv)));
return rv;
}
rv = ProcessInput(socket);
if (NS_FAILED(rv)) {
return rv;
}
rv = ProcessEvents();
if (NS_FAILED(rv)) {
return rv;
}
rv = SendData(socket);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
const uint32_t HTTP3_TELEMETRY_APP_NECKO = 42;
void Http3Session::Close(nsresult aReason) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG(("Http3Session::Close [this=%p]", this));
if (NS_FAILED(mError)) {
CloseInternal(false);
} else {
mError = aReason;
// If necko closes connection, this will map to the "closing" key and the
// value HTTP3_TELEMETRY_APP_NECKO.
glean::http3::connection_close_code.Get("app_closing"_ns)
.AccumulateSingleSample(HTTP3_TELEMETRY_APP_NECKO);
CloseInternal(true);
}
if (mCleanShutdown || mIsClosedByNeqo || NS_FAILED(mSocketError)) {
// It is network-tear-down, a socker error or neqo is state CLOSED
// (it does not need to send any more packets or wait for new packets).
// We need to remove all references, so that
// Http3Session will be destroyed.
if (mTimer) {
mTimer->Cancel();
}
mTimer = nullptr;
mTimerCallback = nullptr;
mConnection = nullptr;
mUdpConn = nullptr;
mState = CLOSED;
}
if (mConnection) {
// resume sending to send CLOSE_CONNECTION frame.
(void)mConnection->ResumeSend();
}
}
void Http3Session::CloseInternal(bool aCallNeqoClose) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
if (IsClosing()) {
return;
}
LOG(("Http3Session::Closing [this=%p]", this));
if (mState != CONNECTED) {
mBeforeConnectedError = true;
}
#ifndef ANDROID
if (mState == ZERORTT) {
ZeroRttTelemetry(aCallNeqoClose ? ZeroRttOutcome::USED_CONN_CLOSED_BY_NECKO
: ZeroRttOutcome::USED_CONN_ERROR);
}
#endif
mState = CLOSING;
Shutdown();
if (aCallNeqoClose) {
mHttp3Connection->Close(HTTP3_APP_ERROR_NO_ERROR);
}
mStreamIdHash.Clear();
mStreamTransactionHash.Clear();
}
nsHttpConnectionInfo* Http3Session::ConnectionInfo() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
RefPtr<nsHttpConnectionInfo> ci;
GetConnectionInfo(getter_AddRefs(ci));
return ci.get();
}
void Http3Session::SetProxyConnectFailed() {
MOZ_ASSERT(false, "Http3Session::SetProxyConnectFailed()");
}
nsHttpRequestHead* Http3Session::RequestHead() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
MOZ_ASSERT(false,
"Http3Session::RequestHead() "
"should not be called after http/3 is setup");
return nullptr;
}
uint32_t Http3Session::Http1xTransactionCount() { return 0; }
nsresult Http3Session::TakeSubTransactions(
nsTArray<RefPtr<nsAHttpTransaction>>& outTransactions) {
return NS_OK;
}
//-----------------------------------------------------------------------------
// Pass through methods of nsAHttpConnection
//-----------------------------------------------------------------------------
nsAHttpConnection* Http3Session::Connection() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
return mConnection;
}
nsresult Http3Session::OnHeadersAvailable(nsAHttpTransaction* transaction,
nsHttpRequestHead* requestHead,
nsHttpResponseHead* responseHead,
bool* reset) {
MOZ_ASSERT(mConnection);
if (mConnection) {
return mConnection->OnHeadersAvailable(transaction, requestHead,
responseHead, reset);
}
return NS_OK;
}
bool Http3Session::IsReused() {
if (mConnection) {
return mConnection->IsReused();
}
return true;
}
nsresult Http3Session::PushBack(const char* buf, uint32_t len) {
return NS_ERROR_UNEXPECTED;
}
already_AddRefed<HttpConnectionBase> Http3Session::TakeHttpConnection() {
LOG(("Http3Session::TakeHttpConnection %p", this));
return nullptr;
}
already_AddRefed<HttpConnectionBase> Http3Session::HttpConnection() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
if (mConnection) {
return mConnection->HttpConnection();
}
return nullptr;
}
void Http3Session::CloseTransaction(nsAHttpTransaction* aTransaction,
nsresult aResult) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG3(("Http3Session::CloseTransaction %p %p 0x%" PRIx32, this, aTransaction,
static_cast<uint32_t>(aResult)));
// Generally this arrives as a cancel event from the connection manager.
// need to find the stream and call CloseStream() on it.
RefPtr<Http3StreamBase> stream = mStreamTransactionHash.Get(aTransaction);
if (!stream) {
LOG3(("Http3Session::CloseTransaction %p %p 0x%" PRIx32 " - not found.",
this, aTransaction, static_cast<uint32_t>(aResult)));
return;
}
LOG3(
("Http3Session::CloseTransaction probably a cancel. this=%p, "
"trans=%p, result=0x%" PRIx32 ", streamId=0x%" PRIx64 " stream=%p",
this, aTransaction, static_cast<uint32_t>(aResult), stream->StreamId(),
stream.get()));
CloseStream(stream, aResult);
if (mConnection) {
(void)mConnection->ResumeSend();
}
}
void Http3Session::CloseStream(Http3StreamBase* aStream, nsresult aResult) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
RefPtr<Http3WebTransportStream> wtStream =
aStream->GetHttp3WebTransportStream();
if (wtStream) {
CloseWebTransportStream(wtStream, aResult);
return;
}
RefPtr<Http3Stream> httpStream = aStream->GetHttp3Stream();
if (httpStream && !httpStream->RecvdFin() && !httpStream->RecvdReset() &&
httpStream->HasStreamId()) {
mHttp3Connection->CancelFetch(httpStream->StreamId(),
HTTP3_APP_ERROR_REQUEST_CANCELLED);
}
if ((NS_SUCCEEDED(aResult) || NS_BASE_STREAM_CLOSED == aResult) &&
mConnInfo->GetIsTrrServiceChannel()) {
// save time of last successful response
mLastTRRResponseTime = TimeStamp::Now();
mTrrStreams++;
}
aStream->Close(aResult);
CloseStreamInternal(aStream, aResult);
}
void Http3Session::CloseStreamInternal(Http3StreamBase* aStream,
nsresult aResult) {
LOG3(("Http3Session::CloseStreamInternal %p %p 0x%" PRIx32, this, aStream,
static_cast<uint32_t>(aResult)));
if (aStream->HasStreamId()) {
// We know the transaction reusing an idle connection has succeeded or
// failed.
if (mFirstStreamIdReuseIdleConnection.isSome() &&
aStream->StreamId() == *mFirstStreamIdReuseIdleConnection) {
MOZ_ASSERT(mConnectionIdleStart);
MOZ_ASSERT(mConnectionIdleEnd);
#ifndef ANDROID
if (mConnectionIdleStart) {
mozilla::glean::netwerk::http3_time_to_reuse_idle_connection
.Get(NS_SUCCEEDED(aResult) ? "succeeded"_ns : "failed"_ns)
.AccumulateRawDuration(mConnectionIdleEnd - mConnectionIdleStart);
}
#endif
mConnectionIdleStart = TimeStamp();
mConnectionIdleEnd = TimeStamp();
mFirstStreamIdReuseIdleConnection.reset();
}
mStreamIdHash.Remove(aStream->StreamId());
// Start to idle when we remove the last stream.
if (mStreamIdHash.IsEmpty()) {
mConnectionIdleStart = TimeStamp::Now();
}
}
RemoveStreamFromQueues(aStream);
if (nsAHttpTransaction* transaction = aStream->Transaction()) {
mStreamTransactionHash.Remove(transaction);
}
mWebTransportSessions.RemoveElement(aStream);
mWebTransportStreams.RemoveElement(aStream);
mTunnelStreams.RemoveElement(aStream);
// Close(NS_OK) implies that the NeqoHttp3Conn will be closed, so we can only
// do this when there is no Http3Steeam, WebTransportSession and
// WebTransportStream.
if ((mShouldClose || mGoawayReceived) && HasNoActiveStreams()) {
MOZ_ASSERT(!IsClosing());
Close(NS_OK);
}
}
void Http3Session::CloseWebTransportStream(Http3WebTransportStream* aStream,
nsresult aResult) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG3(("Http3Session::CloseWebTransportStream %p %p 0x%" PRIx32, this, aStream,
static_cast<uint32_t>(aResult)));
if (aStream && !aStream->RecvdFin() && !aStream->RecvdReset() &&
(aStream->HasStreamId())) {
mHttp3Connection->ResetStream(aStream->StreamId(),
HTTP3_APP_ERROR_REQUEST_CANCELLED);
}
aStream->Close(aResult);
CloseStreamInternal(aStream, aResult);
}
void Http3Session::ResetWebTransportStream(Http3WebTransportStream* aStream,
uint64_t aErrorCode) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG3(("Http3Session::ResetWebTransportStream %p %p 0x%" PRIx64, this, aStream,
aErrorCode));
mHttp3Connection->ResetStream(aStream->StreamId(), aErrorCode);
}
void Http3Session::StreamStopSending(Http3WebTransportStream* aStream,
uint8_t aErrorCode) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG(("Http3Session::StreamStopSending %p %p 0x%" PRIx32, this, aStream,
static_cast<uint32_t>(aErrorCode)));
mHttp3Connection->StreamStopSending(aStream->StreamId(), aErrorCode);
}
nsresult Http3Session::TakeTransport(nsISocketTransport**,
nsIAsyncInputStream**,
nsIAsyncOutputStream**) {
MOZ_ASSERT(false, "TakeTransport of Http3Session");
return NS_ERROR_UNEXPECTED;
}
WebTransportSessionBase* Http3Session::GetWebTransportSession(
nsAHttpTransaction* aTransaction) {
RefPtr<Http3StreamBase> stream = mStreamTransactionHash.Get(aTransaction);
if (!stream || !stream->GetHttp3WebTransportSession()) {
MOZ_ASSERT(false, "There must be a stream");
return nullptr;
}
RemoveStreamFromQueues(stream);
mStreamTransactionHash.Remove(aTransaction);
mWebTransportSessions.AppendElement(stream);
return stream->GetHttp3WebTransportSession();
}
bool Http3Session::IsPersistent() { return true; }
void Http3Session::DontReuse() {
LOG3(("Http3Session::DontReuse %p\n", this));
if (!OnSocketThread()) {
LOG3(("Http3Session %p not on socket thread\n", this));
nsCOMPtr<nsIRunnable> event = NewRunnableMethod(
"Http3Session::DontReuse", this, &Http3Session::DontReuse);
gSocketTransportService->Dispatch(event, NS_DISPATCH_NORMAL);
return;
}
if (mGoawayReceived || IsClosing()) {
return;
}
mShouldClose = true;
if (HasNoActiveStreams()) {
// This is a temporary workaround and should be fixed properly in Happy
// Eyeballs project. We should not exclude this domain if
// Http3Session::DontReuse is called from
// ConnectionEntry::MakeAllDontReuseExcept.
if (mUdpConn &&
mUdpConn->CloseReason() ==
ConnectionCloseReason::CLOSE_EXISTING_CONN_FOR_COALESCING) {
mDontExclude = true;
}
Close(NS_OK);
}
}
void Http3Session::CloseWebTransportConn() {
LOG3(("Http3Session::CloseWebTransportConn %p\n", this));
// We need to dispatch, since Http3Session could be released in
// HttpConnectionUDP::CloseTransaction.
gSocketTransportService->Dispatch(
NS_NewRunnableFunction("Http3Session::CloseWebTransportConn",
[self = RefPtr{this}]() {
if (self->mUdpConn) {
self->mUdpConn->CloseTransaction(
self, NS_ERROR_ABORT);
}
}),
NS_DISPATCH_NORMAL);
}
void Http3Session::CurrentBrowserIdChanged(uint64_t id) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
mCurrentBrowserId = id;
for (const auto& stream : mStreamTransactionHash.Values()) {
RefPtr<Http3Stream> httpStream = stream->GetHttp3Stream();
if (httpStream) {
httpStream->CurrentBrowserIdChanged(id);
}
}
}
// This is called by Http3Stream::OnWriteSegment.
nsresult Http3Session::ReadResponseData(uint64_t aStreamId, char* aBuf,
uint32_t aCount,
uint32_t* aCountWritten, bool* aFin) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
nsresult rv = mHttp3Connection->ReadResponseData(aStreamId, (uint8_t*)aBuf,
aCount, aCountWritten, aFin);
// This should not happen, i.e. stream must be present in neqo and in necko at
// the same time.
MOZ_ASSERT(rv != NS_ERROR_INVALID_ARG);
if (NS_FAILED(rv)) {
LOG3(("Http3Session::ReadResponseData return an error %" PRIx32
" [this=%p]",
static_cast<uint32_t>(rv), this));
// This error will be handled by neqo and the whole connection will be
// closed. We will return NS_BASE_STREAM_WOULD_BLOCK here.
*aCountWritten = 0;
*aFin = false;
rv = NS_BASE_STREAM_WOULD_BLOCK;
}
MOZ_ASSERT((*aCountWritten != 0) || aFin || NS_FAILED(rv));
return rv;
}
void Http3Session::TransactionHasDataToWrite(nsAHttpTransaction* caller) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG3(("Http3Session::TransactionHasDataToWrite %p trans=%p", this, caller));
// a trapped signal from the http transaction to the connection that
// it is no longer blocked on read.
RefPtr<Http3StreamBase> stream = mStreamTransactionHash.Get(caller);
if (!stream) {
LOG3(("Http3Session::TransactionHasDataToWrite %p caller %p not found",
this, caller));
return;
}
LOG3(("Http3Session::TransactionHasDataToWrite %p ID is 0x%" PRIx64, this,
stream->StreamId()));
StreamHasDataToWrite(stream);
}
void Http3Session::StreamHasDataToWrite(Http3StreamBase* aStream) {
if (!IsClosing()) {
StreamReadyToWrite(aStream);
} else {
LOG3(
("Http3Session::TransactionHasDataToWrite %p closed so not setting "
"Ready4Write\n",
this));
}
// NSPR poll will not poll the network if there are non system PR_FileDesc's
// that are ready - so we can get into a deadlock waiting for the system IO
// to come back here if we don't force the send loop manually.
(void)ForceSend();
}
void Http3Session::TransactionHasDataToRecv(nsAHttpTransaction* caller) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG3(("Http3Session::TransactionHasDataToRecv %p trans=%p", this, caller));
// a signal from the http transaction to the connection that it will consume
// more
RefPtr<Http3StreamBase> stream = mStreamTransactionHash.Get(caller);
if (!stream) {
LOG3(("Http3Session::TransactionHasDataToRecv %p caller %p not found", this,
caller));
return;
}
LOG3(("Http3Session::TransactionHasDataToRecv %p ID is 0x%" PRIx64 "\n", this,
stream->StreamId()));
ConnectSlowConsumer(stream);
}
void Http3Session::ConnectSlowConsumer(Http3StreamBase* stream) {
LOG3(("Http3Session::ConnectSlowConsumer %p 0x%" PRIx64 "\n", this,
stream->StreamId()));
mSlowConsumersReadyForRead.AppendElement(stream);
(void)ForceRecv();
}
bool Http3Session::TestJoinConnection(const nsACString& hostname,
int32_t port) {
return RealJoinConnection(hostname, port, true);
}
bool Http3Session::JoinConnection(const nsACString& hostname, int32_t port) {
return RealJoinConnection(hostname, port, false);
}
// TODO test
bool Http3Session::RealJoinConnection(const nsACString& hostname, int32_t port,
bool justKidding) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
if (!mConnection || !CanSendData() || mShouldClose || mGoawayReceived) {
return false;
}
nsHttpConnectionInfo* ci = ConnectionInfo();
if (ci->UsingProxy()) {
MOZ_ASSERT(false,
"RealJoinConnection should not be called when using proxy");
return false;
}
if (nsCString(hostname).EqualsIgnoreCase(ci->Origin()) &&
(port == ci->OriginPort())) {
return true;
}
nsAutoCString key(hostname);
key.Append(':');
key.Append(justKidding ? 'k' : '.');
key.AppendInt(port);
bool cachedResult;
if (mJoinConnectionCache.Get(key, &cachedResult)) {
LOG(("joinconnection [%p %s] %s result=%d cache\n", this,
ConnectionInfo()->HashKey().get(), key.get(), cachedResult));
return cachedResult;
}
nsresult rv;
bool isJoined = false;
nsCOMPtr<nsITLSSocketControl> sslSocketControl;
mConnection->GetTLSSocketControl(getter_AddRefs(sslSocketControl));
if (!sslSocketControl) {
return false;
}
bool joinedReturn = false;
if (justKidding) {
rv = sslSocketControl->TestJoinConnection(mConnInfo->GetNPNToken(),
hostname, port, &isJoined);
} else {
rv = sslSocketControl->JoinConnection(mConnInfo->GetNPNToken(), hostname,
port, &isJoined);
}
if (NS_SUCCEEDED(rv) && isJoined) {
joinedReturn = true;
}
LOG(("joinconnection [%p %s] %s result=%d lookup\n", this,
ConnectionInfo()->HashKey().get(), key.get(), joinedReturn));
mJoinConnectionCache.InsertOrUpdate(key, joinedReturn);
if (!justKidding) {
// cache a kidding entry too as this one is good for both
nsAutoCString key2(hostname);
key2.Append(':');
key2.Append('k');
key2.AppendInt(port);
if (!mJoinConnectionCache.Get(key2)) {
mJoinConnectionCache.InsertOrUpdate(key2, joinedReturn);
}
}
return joinedReturn;
}
void Http3Session::CallCertVerification(Maybe<nsCString> aEchPublicName) {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
LOG(("Http3Session::CallCertVerification [this=%p]", this));
NeqoCertificateInfo certInfo;
if (NS_FAILED(mHttp3Connection->PeerCertificateInfo(&certInfo))) {
LOG(("Http3Session::CallCertVerification [this=%p] - no cert", this));
mHttp3Connection->PeerAuthenticated(SSL_ERROR_BAD_CERTIFICATE);
mError = psm::GetXPCOMFromNSSError(SSL_ERROR_BAD_CERTIFICATE);
return;
}
if (mConnInfo->GetWebTransport()) {
// if our connection is webtransport, we might do a verification
// based on serverCertificatedHashes
const nsTArray<RefPtr<nsIWebTransportHash>>* servCertHashes =
gHttpHandler->ConnMgr()->GetServerCertHashes(mConnInfo);
if (servCertHashes && !servCertHashes->IsEmpty() &&
certInfo.certs.Length() >= 1) {
// ok, we verify based on serverCertificateHashes
mozilla::pkix::Result rv = AuthCertificateWithServerCertificateHashes(
certInfo.certs[0], *servCertHashes);
if (rv != mozilla::pkix::Result::Success) {
// ok we failed, report it back
LOG(
("Http3Session::CallCertVerification [this=%p] "
"AuthCertificateWithServerCertificateHashes failed",
this));
mHttp3Connection->PeerAuthenticated(SSL_ERROR_BAD_CERTIFICATE);
mError = psm::GetXPCOMFromNSSError(SSL_ERROR_BAD_CERTIFICATE);
return;
}
// ok, we succeded
Authenticated(0, true);
return;
}
}
Maybe<nsTArray<nsTArray<uint8_t>>> stapledOCSPResponse;
if (certInfo.stapled_ocsp_responses_present) {
stapledOCSPResponse.emplace(std::move(certInfo.stapled_ocsp_responses));
}
Maybe<nsTArray<uint8_t>> sctsFromTLSExtension;
if (certInfo.signed_cert_timestamp_present) {
sctsFromTLSExtension.emplace(std::move(certInfo.signed_cert_timestamp));
}
uint32_t providerFlags;
// the return value is always NS_OK, just ignore it.
(void)mSocketControl->GetProviderFlags(&providerFlags);
nsCString echConfig;
nsresult nsrv = mSocketControl->GetEchConfig(echConfig);
bool verifyToEchPublicName = NS_SUCCEEDED(nsrv) && !echConfig.IsEmpty() &&
aEchPublicName && !aEchPublicName->IsEmpty();
const nsACString& hostname =
verifyToEchPublicName ? *aEchPublicName : mSocketControl->GetHostName();
SECStatus rv = psm::AuthCertificateHookWithInfo(
mSocketControl, hostname, static_cast<const void*>(this),
std::move(certInfo.certs), stapledOCSPResponse, sctsFromTLSExtension,
providerFlags);
if ((rv != SECSuccess) && (rv != SECWouldBlock)) {
LOG(("Http3Session::CallCertVerification [this=%p] AuthCertificate failed",
this));
mHttp3Connection->PeerAuthenticated(SSL_ERROR_BAD_CERTIFICATE);
mError = psm::GetXPCOMFromNSSError(SSL_ERROR_BAD_CERTIFICATE);
}
}
void Http3Session::Authenticated(int32_t aError,
bool aServCertHashesSucceeded) {
LOG(("Http3Session::Authenticated error=0x%" PRIx32 " [this=%p].", aError,
this));
if ((mState == INITIALIZING) || (mState == ZERORTT)) {
if (psm::IsNSSErrorCode(aError)) {
mError = psm::GetXPCOMFromNSSError(aError);
LOG(("Http3Session::Authenticated psm-error=0x%" PRIx32 " [this=%p].",
static_cast<uint32_t>(mError), this));
} else if (StaticPrefs::
network_http_http3_disable_when_third_party_roots_found()) {
// In test, we use another perf value to override the value of
// hasThirdPartyRoots.
bool hasThirdPartyRoots =
(xpc::IsInAutomation() || PR_GetEnv("XPCSHELL_TEST_PROFILE_DIR"))
? StaticPrefs::
network_http_http3_has_third_party_roots_found_in_automation()
: !mSocketControl->IsBuiltCertChainRootBuiltInRoot();
LOG(
("Http3Session::Authenticated [this=%p, hasThirdPartyRoots=%d, "
"servCertHashesSucceeded=%d]",
this, hasThirdPartyRoots, aServCertHashesSucceeded));
// If serverCertificateHashes is used a thirdPartyRoot is legal
if (hasThirdPartyRoots && !aServCertHashesSucceeded) {
if (mFirstHttpTransaction) {
mFirstHttpTransaction->DisableHttp3(false);
}
mUdpConn->CloseTransaction(this, NS_ERROR_NET_RESET);
return;
}
}
mHttp3Connection->PeerAuthenticated(aError);
// Call OnQuicTimeoutExpired to properly process neqo events and outputs.
// We call OnQuicTimeoutExpired instead of ProcessOutputAndEvents, because
// HttpConnectionUDP must close this session in case of an error.
NS_DispatchToCurrentThread(
NewRunnableMethod("net::HttpConnectionUDP::OnQuicTimeoutExpired",
mUdpConn, &HttpConnectionUDP::OnQuicTimeoutExpired));
mUdpConn->ChangeConnectionState(ConnectionState::TRANSFERING);
}
}
void Http3Session::SetSecInfo() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
NeqoSecretInfo secInfo;
if (NS_SUCCEEDED(mHttp3Connection->GetSecInfo(&secInfo))) {
mSocketControl->SetSSLVersionUsed(secInfo.version);
mSocketControl->SetResumed(secInfo.resumed);
mSocketControl->SetNegotiatedNPN(secInfo.alpn);
mSocketControl->SetInfo(secInfo.cipher, secInfo.version, secInfo.group,
secInfo.signature_scheme, secInfo.ech_accepted);
mHandshakeSucceeded = true;
}
if (!mSocketControl->HasServerCert()) {
mSocketControl->RebuildCertificateInfoFromSSLTokenCache();
}
}
// Transport error have values from 0x0 to 0x11.
// (https://tools.ietf.org/html/draft-ietf-quic-transport-34#section-20.1)
// We will map this error to 0-16.
// 17 will capture error codes between and including 0x12 and 0x0ff. This
// error codes are not define by the spec but who know peer may sent them.
// CryptoAlerts have value 0x100 + alert code. The range of alert code is
// 0x00-0xff. (https://tools.ietf.org/html/draft-ietf-quic-tls_34#section-4.8)
// Since telemetry does not allow more than 100 bucket, we use three diffrent
// keys to map all alert codes.
const uint32_t HTTP3_TELEMETRY_TRANSPORT_INTERNAL_ERROR = 15;
const uint32_t HTTP3_TELEMETRY_TRANSPORT_END = 16;
const uint32_t HTTP3_TELEMETRY_TRANSPORT_UNKNOWN = 17;
const uint32_t HTTP3_TELEMETRY_TRANSPORT_CRYPTO_UNKNOWN = 18;
// All errors from CloseError::Tag::CryptoError will be map to 19
const uint32_t HTTP3_TELEMETRY_CRYPTO_ERROR = 19;
uint64_t GetCryptoAlertCode(nsCString& key, uint64_t error) {
if (error < 100) {
key.Append("_a"_ns);
return error;
}
if (error < 200) {
error -= 100;
key.Append("_b"_ns);
return error;
}
if (error < 256) {
error -= 200;
key.Append("_c"_ns);
return error;
}
return HTTP3_TELEMETRY_TRANSPORT_CRYPTO_UNKNOWN;
}
uint64_t GetTransportErrorCodeForTelemetry(nsCString& key, uint64_t error) {
if (error <= HTTP3_TELEMETRY_TRANSPORT_END) {
return error;
}
if (error < 0x100) {
return HTTP3_TELEMETRY_TRANSPORT_UNKNOWN;
}
return GetCryptoAlertCode(key, error - 0x100);
}
// Http3 error codes are 0x100-0x110.
// (https://tools.ietf.org/html/draft-ietf-quic-http-33#section-8.1)
// The mapping is described below.
// 0x00-0x10 mapped to 0-16
// 0x11-0xff mapped to 17
// 0x100-0x110 mapped to 18-36
// 0x111-0x1ff mapped to 37
// 0x200-0x202 mapped to 38-40
// Others mapped to 41
const uint32_t HTTP3_TELEMETRY_APP_UNKNOWN_1 = 17;
const uint32_t HTTP3_TELEMETRY_APP_START = 18;
// Values between 0x111 and 0x1ff are no definded and will be map to 18.
const uint32_t HTTP3_TELEMETRY_APP_UNKNOWN_2 = 37;
// Error codes between 0x200 and 0x202 are related to qpack.
// (https://tools.ietf.org/html/draft-ietf-quic-qpack-20#section-6)
// They will be mapped to 19-21
const uint32_t HTTP3_TELEMETRY_APP_QPACK_START = 38;
// Values greater or equal to 0x203 are no definded and will be map to 41.
const uint32_t HTTP3_TELEMETRY_APP_UNKNOWN_3 = 41;
uint64_t GetAppErrorCodeForTelemetry(uint64_t error) {
if (error <= 0x10) {
return error;
}
if (error <= 0xff) {
return HTTP3_TELEMETRY_APP_UNKNOWN_1;
}
if (error <= 0x110) {
return error - 0x100 + HTTP3_TELEMETRY_APP_START;
}
if (error < 0x200) {
return HTTP3_TELEMETRY_APP_UNKNOWN_2;
}
if (error <= 0x202) {
return error - 0x200 + HTTP3_TELEMETRY_APP_QPACK_START;
}
return HTTP3_TELEMETRY_APP_UNKNOWN_3;
}
void Http3Session::CloseConnectionTelemetry(CloseError& aError, bool aClosing) {
uint64_t value = 0;
nsCString key = EmptyCString();
switch (aError.tag) {
case CloseError::Tag::TransportInternalError:
key = "transport_internal"_ns;
value = HTTP3_TELEMETRY_TRANSPORT_INTERNAL_ERROR;
break;
case CloseError::Tag::TransportInternalErrorOther:
key = "transport_other"_ns;
value = aError.transport_internal_error_other._0;
break;
case CloseError::Tag::TransportError:
key = "transport"_ns;
value = GetTransportErrorCodeForTelemetry(key, aError.transport_error._0);
break;
case CloseError::Tag::CryptoError:
key = "transport"_ns;
value = HTTP3_TELEMETRY_CRYPTO_ERROR;
break;
case CloseError::Tag::CryptoAlert:
key = "transport_crypto_alert"_ns;
value = GetCryptoAlertCode(key, aError.crypto_alert._0);
break;
case CloseError::Tag::PeerAppError:
key = "peer_app"_ns;
value = GetAppErrorCodeForTelemetry(aError.peer_app_error._0);
break;
case CloseError::Tag::PeerError:
key = "peer_transport"_ns;
value = GetTransportErrorCodeForTelemetry(key, aError.peer_error._0);
break;
case CloseError::Tag::AppError:
key = "app"_ns;
value = GetAppErrorCodeForTelemetry(aError.app_error._0);
break;
case CloseError::Tag::EchRetry:
key = "transport_crypto_alert"_ns;
value = 100;
}
MOZ_DIAGNOSTIC_ASSERT(value <= 100);
key.Append(aClosing ? "_closing"_ns : "_closed"_ns);
glean::http3::connection_close_code.Get(key).AccumulateSingleSample(value);
Http3Stats stats{};
mHttp3Connection->GetStats(&stats);
if (stats.packets_tx > 0) {
unsigned long loss = (stats.lost * 10000) / stats.packets_tx;
glean::http3::loss_ratio.AccumulateSingleSample(loss);
glean::http3::late_ack.EnumGet(glean::http3::LateAckLabel::eAck)
.AccumulateSingleSample(stats.late_ack);
glean::http3::late_ack.EnumGet(glean::http3::LateAckLabel::ePto)
.AccumulateSingleSample(stats.pto_ack);
unsigned long late_ack_ratio = (stats.late_ack * 10000) / stats.packets_tx;
unsigned long pto_ack_ratio = (stats.pto_ack * 10000) / stats.packets_tx;
glean::http3::late_ack_ratio.EnumGet(glean::http3::LateAckRatioLabel::eAck)
.AccumulateSingleSample(late_ack_ratio);
glean::http3::late_ack_ratio.EnumGet(glean::http3::LateAckRatioLabel::ePto)
.AccumulateSingleSample(pto_ack_ratio);
for (uint32_t i = 0; i < MAX_PTO_COUNTS; i++) {
nsAutoCString key;
key.AppendInt(i);
glean::http3::counts_pto.Get(key).AccumulateSingleSample(
stats.pto_counts[i]);
}
glean::http3::drop_dgrams.AccumulateSingleSample(stats.dropped_rx);
glean::http3::saved_dgrams.AccumulateSingleSample(stats.saved_datagrams);
}
glean::http3::received_sent_dgrams
.EnumGet(glean::http3::ReceivedSentDgramsLabel::eReceived)
.AccumulateSingleSample(stats.packets_rx);
glean::http3::received_sent_dgrams
.EnumGet(glean::http3::ReceivedSentDgramsLabel::eSent)
.AccumulateSingleSample(stats.packets_tx);
if (aClosing) {
RefPtr<nsHttpConnectionInfo> ci;
GetConnectionInfo(getter_AddRefs(ci));
if (ci && ci->GetIsTrrServiceChannel() && !mLastTRRResponseTime.IsNull() &&
(mGoawayReceived ||
(aError.tag == CloseError::Tag::PeerAppError &&
aError.peer_app_error._0 == HTTP3_APP_ERROR_NO_ERROR))) {
// Record telemetry keyed by TRR provider.
glean::network::trr_idle_close_time_h3.Get(TRRProviderKey())
.AccumulateRawDuration(TimeStamp::Now() - mLastTRRResponseTime);
mLastTRRResponseTime = TimeStamp();
}
}
}
void Http3Session::Finish0Rtt(bool aRestart) {
for (size_t i = 0; i < m0RTTStreams.Length(); ++i) {
if (m0RTTStreams[i]) {
if (aRestart) {
// When we need to restart transactions remove them from all lists.
if (m0RTTStreams[i]->HasStreamId()) {
mStreamIdHash.Remove(m0RTTStreams[i]->StreamId());
}
RemoveStreamFromQueues(m0RTTStreams[i]);
// The stream is ready to write again.
mReadyForWrite.Push(m0RTTStreams[i]);
}
m0RTTStreams[i]->Finish0RTT(aRestart);
}
}
for (size_t i = 0; i < mCannotDo0RTTStreams.Length(); ++i) {
if (mCannotDo0RTTStreams[i]) {
mReadyForWrite.Push(mCannotDo0RTTStreams[i]);
}
}
m0RTTStreams.Clear();
mCannotDo0RTTStreams.Clear();
MaybeResumeSend();
}
void Http3Session::ReportHttp3Connection() {
if (CanSendData() && !mHttp3ConnectionReported) {
mHttp3ConnectionReported = true;
gHttpHandler->ConnMgr()->ReportHttp3Connection(mUdpConn);
MaybeResumeSend();
}
}
#ifndef ANDROID
void Http3Session::EchOutcomeTelemetry() {
MOZ_ASSERT(OnSocketThread(), "not on socket thread");
glean::http3::EchOutcomeLabel label;
switch (mEchExtensionStatus) {
case EchExtensionStatus::kNotPresent:
label = glean::http3::EchOutcomeLabel::eNone;
break;
case EchExtensionStatus::kGREASE:
label = glean::http3::EchOutcomeLabel::eGrease;
break;
case EchExtensionStatus::kReal:
label = glean::http3::EchOutcomeLabel::eReal;
break;
}
glean::http3::ech_outcome.EnumGet(label).AccumulateSingleSample(
mHandshakeSucceeded ? 0 : 1);
}
void Http3Session::ZeroRttTelemetry(ZeroRttOutcome aOutcome) {
nsAutoCString key;
switch (aOutcome) {
case USED_SUCCEEDED:
key = "succeeded"_ns;
break;
case USED_REJECTED:
key = "rejected"_ns;
break;
case USED_CONN_ERROR:
key = "conn_error"_ns;
break;
case USED_CONN_CLOSED_BY_NECKO:
key = "conn_closed_by_necko"_ns;
break;
default:
break;
}
if (key.IsEmpty()) {
mozilla::glean::netwerk::http3_0rtt_state.Get("not_used"_ns).Add(1);
} else {
MOZ_ASSERT(mZeroRttStarted);
mozilla::TimeStamp zeroRttEnded = mozilla::TimeStamp::Now();
mozilla::glean::netwerk::http3_0rtt_state_duration.Get(key)
.AccumulateRawDuration(zeroRttEnded - mZeroRttStarted);
mozilla::glean::netwerk::http3_0rtt_state.Get(key).Add(1);
}
}
#endif
nsresult Http3Session::GetTransactionTLSSocketControl(
nsITLSSocketControl** tlsSocketControl) {
NS_IF_ADDREF(*tlsSocketControl = mSocketControl);
return NS_OK;
}
PRIntervalTime Http3Session::LastWriteTime() { return mLastWriteTime; }
//=========================================================================
// WebTransport
//=========================================================================
nsresult Http3Session::CloseWebTransport(uint64_t aSessionId, uint32_t aError,
const nsACString& aMessage) {
return mHttp3Connection->CloseWebTransport(aSessionId, aError, aMessage);
}
nsresult Http3Session::CreateWebTransportStream(
uint64_t aSessionId, WebTransportStreamType aStreamType,
uint64_t* aStreamId) {
return mHttp3Connection->CreateWebTransportStream(aSessionId, aStreamType,
aStreamId);
}
void Http3Session::SendDatagram(Http3WebTransportSession* aSession,
nsTArray<uint8_t>& aData,
uint64_t aTrackingId) {
nsresult rv = mHttp3Connection->WebTransportSendDatagram(aSession->StreamId(),
aData, aTrackingId);
LOG(("Http3Session::SendDatagram %p res=%" PRIx32, this,
static_cast<uint32_t>(rv)));
if (!aTrackingId) {
return;
}
switch (rv) {
case NS_OK:
aSession->OnOutgoingDatagramOutCome(
aTrackingId, WebTransportSessionEventListener::DatagramOutcome::SENT);
break;
case NS_ERROR_NOT_AVAILABLE:
aSession->OnOutgoingDatagramOutCome(
aTrackingId, WebTransportSessionEventListener::DatagramOutcome::
DROPPED_TOO_MUCH_DATA);
break;
default:
aSession->OnOutgoingDatagramOutCome(
aTrackingId,
WebTransportSessionEventListener::DatagramOutcome::UNKNOWN);
break;
}
}
uint64_t Http3Session::MaxDatagramSize(uint64_t aSessionId) {
uint64_t size = 0;
(void)mHttp3Connection->WebTransportMaxDatagramSize(aSessionId, &size);
return size;
}
void Http3Session::SendHTTPDatagram(uint64_t aStreamId,
nsTArray<uint8_t>& aData,
uint64_t aTrackingId) {
LOG(("Http3Session::SendHTTPDatagram %p length=%zu aTrackingId=%" PRIx64,
this, aData.Length(), aTrackingId));
(void)mHttp3Connection->ConnectUdpSendDatagram(aStreamId, aData, aTrackingId);
}
void Http3Session::SetSendOrder(Http3StreamBase* aStream,
Maybe<int64_t> aSendOrder) {
if (!IsClosing()) {
nsresult rv = mHttp3Connection->WebTransportSetSendOrder(
aStream->StreamId(), aSendOrder);
MOZ_ASSERT(NS_SUCCEEDED(rv));
(void)rv;
}
}
Http3Stats Http3Session::GetStats() {
if (!mHttp3Connection) {
return Http3Stats();
}
Http3Stats stats{};
mHttp3Connection->GetStats(&stats);
return stats;
}
already_AddRefed<HttpConnectionUDP> Http3Session::CreateTunnelStream(
nsAHttpTransaction* aHttpTransaction, nsIInterfaceRequestor* aCallbacks) {
LOG(("Http3Session::CreateTunnelStream %p aHttpTransaction=%p", this,
aHttpTransaction));
RefPtr<Http3StreamBase> stream =
new Http3ConnectUDPStream(aHttpTransaction, this, NS_GetCurrentThread());
mStreamTransactionHash.InsertOrUpdate(aHttpTransaction, RefPtr{stream});
StreamHasDataToWrite(stream);
RefPtr<HttpConnectionUDP> conn =
stream->GetHttp3ConnectUDPStream()->CreateUDPConnection(aCallbacks);
return conn.forget();
}
void Http3Session::FinishTunnelSetup(nsAHttpTransaction* aTransaction) {
LOG(("Http3Session::FinishTunnelSetup %p aHttpTransaction=%p", this,
aTransaction));
RefPtr<Http3StreamBase> stream = mStreamTransactionHash.Get(aTransaction);
if (!stream || !stream->GetHttp3ConnectUDPStream()) {
MOZ_ASSERT(false, "There must be a stream");
return;
}
RemoveStreamFromQueues(stream);
mStreamTransactionHash.Remove(aTransaction);
mTunnelStreams.AppendElement(stream);
}
already_AddRefed<nsHttpConnection> Http3Session::CreateTunnelStream(
nsAHttpTransaction* aHttpTransaction, nsIInterfaceRequestor* aCallbacks,
PRIntervalTime aRtt, bool aIsExtendedCONNECT) {
LOG(("Http3Session::CreateTunnelStream %p aHttpTransaction=%p", this,
aHttpTransaction));
RefPtr<Http3StreamBase> stream =
new Http3StreamTunnel(aHttpTransaction, this, mCurrentBrowserId);
mStreamTransactionHash.InsertOrUpdate(aHttpTransaction, RefPtr{stream});
StreamHasDataToWrite(stream);
RefPtr<nsHttpConnection> conn =
stream->GetHttp3StreamTunnel()->CreateHttpConnection(aCallbacks, aRtt,
aIsExtendedCONNECT);
return conn.forget();
}
} // namespace mozilla::net
|