pingora-core 0.9.1

Pingora's APIs and traits for the core network protocols.
Documentation
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
// Copyright 2026 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # HTTP server session for subrequests
//!
//! This server session is _very_ similar to the implementation for v1, if not
//! identical in many cases. Though in theory subrequests are HTTP version
//! agnostic in reality this means that they must interpret any version-specific
//! idiosyncracies such as Connection: upgrade headers in H1 because they
//! "stand-in" for the actual main Session when running proxy logic. As much as
//! possible they should defer downstream-specific logic to the actual downstream
//! session and act more or less as a pipe.
//!
//! The session also instantiates a [`SubrequestHandle`] that contains necessary
//! communication channels with the subrequest, to make it possible to send
//! and receive data.
//!
//! Its write calls will send `HttpTask`s to the handle channels, instead of
//! flushing to an actual underlying stream.
//!
//! Connection reuse and keep-alive are not supported because there is no
//! actual underlying stream, only transient channels per request.

use bytes::Bytes;
use http::HeaderValue;
use http::{header, header::AsHeaderName, HeaderMap, Method};
use log::{debug, trace, warn};
use pingora_error::{Error, ErrorType::*, OkOrErr, OrErr, Result};
use pingora_http::{RequestHeader, ResponseHeader};
use pingora_timeout::timeout;
use std::collections::VecDeque;
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};

use super::body::{BodyMode, BodyReader, BodyWriter, PREMATURE_BODY_END};
use crate::protocols::http::{
    body_buffer::FixedBuffer,
    server::Session as GenericHttpSession,
    subrequest::dummy::DummyIO,
    v1::common::{header_value_content_length, is_chunked_encoding_from_headers, BODY_BUF_LIMIT},
    v1::server::HttpSession as SessionV1,
    HttpTask,
};
use crate::protocols::{Digest, SocketAddr};

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
enum StreamEndState {
    #[default]
    Open,
    FinishRequired,
    Finished,
}

/// State for the cancel-safe proxy task write API.
#[derive(Default)]
struct ProxyTaskState {
    /// Tasks remain queued until `Permit::send` commits them to the channel.
    tasks: VecDeque<HttpTask>,
    /// Final `Done` is waiting for channel capacity.
    finish_in_progress: bool,
    /// End-of-stream observed from already-consumed tasks.
    stream_end: StreamEndState,
}

impl ProxyTaskState {
    fn require_finish(&mut self) {
        self.stream_end = StreamEndState::FinishRequired;
    }

    fn mark_finished(&mut self) {
        self.stream_end = StreamEndState::Finished;
    }

    fn clear_stream_end(&mut self) {
        self.stream_end = StreamEndState::Open;
    }

    fn finish_required(&self) -> bool {
        self.stream_end == StreamEndState::FinishRequired
    }

    fn finished(&self) -> bool {
        self.stream_end == StreamEndState::Finished
    }
}

/// The HTTP server session
pub struct HttpSession {
    // these are only options because we allow dropping them separately on shutdown
    tx: Option<mpsc::Sender<HttpTask>>,
    rx: Option<mpsc::Receiver<HttpTask>>,
    // Currently subrequest session is initialized via a dummy SessionV1 only
    // TODO: need to be able to indicate H2 / other HTTP versions here
    v1_inner: Box<SessionV1>,
    proxy_error: Option<oneshot::Sender<Box<Error>>>, // option to consume the sender
    read_req_header: bool,
    response_written: Option<ResponseHeader>,
    read_timeout: Option<Duration>,
    write_timeout: Option<Duration>,
    total_drain_timeout: Option<Duration>,
    body_bytes_sent: usize,
    body_bytes_read: usize,
    retry_buffer: Option<FixedBuffer>,
    body_reader: BodyReader,
    body_writer: BodyWriter,
    upgraded: bool,
    // TODO: likely doesn't need to be a separate bool when/if moving away from dummy SessionV1
    clear_request_body_headers: bool,
    digest: Option<Box<Digest>>,
    /// Whether the cancel-safe proxy task API is enabled for this session.
    /// Defaults to `false`. Toggle via [`set_proxy_tasks_enabled`](Self::set_proxy_tasks_enabled).
    proxy_tasks_enabled: bool,
    /// Cancel-safe proxy task state.
    proxy_task_state: ProxyTaskState,
}

/// A handle to the subrequest session itself to interact or read from it.
pub struct SubrequestHandle {
    /// Channel sender (for subrequest input)
    pub tx: mpsc::Sender<HttpTask>,
    /// Channel receiver (for subrequest output)
    pub rx: mpsc::Receiver<HttpTask>,
    /// Indicates when subrequest wants to start reading body input
    pub subreq_wants_body: oneshot::Receiver<()>,
    /// Any final or downstream error that was encountered while proxying
    pub subreq_proxy_error: oneshot::Receiver<Box<Error>>,
}

impl SubrequestHandle {
    /// Spawn a task to drain received HttpTasks.
    pub fn drain_tasks(mut self) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            let _tx = self.tx; // keep handle to sender alive
            while self.rx.recv().await.is_some() {}
            trace!("subrequest dropped");
        })
    }
}

impl HttpSession {
    /// Create a new http server session for a subrequest.
    /// The created session needs to call [`Self::read_request()`] first before performing
    /// any other operations.
    pub fn new_from_session(session: &GenericHttpSession) -> (Self, SubrequestHandle) {
        let v1_inner = SessionV1::new(Box::new(DummyIO::new(&session.to_h1_raw())));
        let digest = session.digest().cloned();
        // allow buffering a small number of tasks, otherwise exert backpressure
        const CHANNEL_BUFFER_SIZE: usize = 4;
        let (downstream_tx, downstream_rx) = mpsc::channel(CHANNEL_BUFFER_SIZE);
        let (upstream_tx, upstream_rx) = mpsc::channel(CHANNEL_BUFFER_SIZE);
        let (wants_body_tx, wants_body_rx) = oneshot::channel();
        let (proxy_error_tx, proxy_error_rx) = oneshot::channel();
        (
            HttpSession {
                v1_inner: Box::new(v1_inner),
                tx: Some(upstream_tx),
                rx: Some(downstream_rx),
                proxy_error: Some(proxy_error_tx),
                body_reader: BodyReader::new(Some(wants_body_tx)),
                body_writer: BodyWriter::new(),
                read_req_header: false,
                response_written: None,
                read_timeout: None,
                write_timeout: None,
                total_drain_timeout: None,
                body_bytes_sent: 0,
                body_bytes_read: 0,
                retry_buffer: None,
                upgraded: false,
                clear_request_body_headers: false,
                digest: digest.map(Box::new),
                proxy_tasks_enabled: false,
                proxy_task_state: ProxyTaskState::default(),
            },
            SubrequestHandle {
                tx: downstream_tx,
                rx: upstream_rx,
                subreq_wants_body: wants_body_rx,
                subreq_proxy_error: proxy_error_rx,
            },
        )
    }

    /// Read the request header. Return `Ok(Some(n))` where the read and parsing are successful.
    pub async fn read_request(&mut self) -> Result<Option<usize>> {
        let res = self.v1_inner.read_request().await?;
        if res.is_none() {
            // this is when h1 client closes the connection without sending data,
            // which shouldn't be the case for a subrequest session just created
            return Error::e_explain(InternalError, "no session request header provided");
        }
        self.read_req_header = true;
        if self.clear_request_body_headers {
            // indicated that we wanted to clear these headers in the past, do so now
            self.clear_request_body_headers();
        }
        Ok(res)
    }

    /// Validate the request header read. This function must be called after the request header
    /// read.
    /// # Panics
    /// this function and most other functions will panic if called before [`Self::read_request()`]
    pub fn validate_request(&self) -> Result<()> {
        self.v1_inner.validate_request()
    }

    /// Return a reference of the `RequestHeader` this session read
    /// # Panics
    /// this function and most other functions will panic if called before [`Self::read_request()`]
    pub fn req_header(&self) -> &RequestHeader {
        self.v1_inner.req_header()
    }

    /// Return a mutable reference of the `RequestHeader` this session read
    /// # Panics
    /// this function and most other functions will panic if called before [`Self::read_request()`]
    pub fn req_header_mut(&mut self) -> &mut RequestHeader {
        self.v1_inner.req_header_mut()
    }

    /// Get the header value for the given header name
    /// If there are multiple headers under the same name, the first one will be returned
    /// Use `self.req_header().header.get_all(name)` to get all the headers under the same name
    pub fn get_header(&self, name: impl AsHeaderName) -> Option<&HeaderValue> {
        self.v1_inner.get_header(name)
    }

    /// Return the method of this request. None if the request is not read yet.
    pub(super) fn get_method(&self) -> Option<&http::Method> {
        self.v1_inner.get_method()
    }

    /// Return the path of the request (i.e., the `/hello?1` of `GET /hello?1 HTTP1.1`)
    /// An empty slice will be used if there is no path or the request is not read yet
    pub(super) fn get_path(&self) -> &[u8] {
        self.v1_inner.get_path()
    }

    /// Return the host header of the request. An empty slice will be used if there is no host header
    pub(super) fn get_host(&self) -> &[u8] {
        self.v1_inner.get_host()
    }

    /// Return a string `$METHOD $PATH, Host: $HOST`. Mostly for logging and debug purpose
    pub fn request_summary(&self) -> String {
        format!(
            "{} {}, Host: {} (subrequest)",
            self.get_method().map_or("-", |r| r.as_str()),
            String::from_utf8_lossy(self.get_path()),
            String::from_utf8_lossy(self.get_host())
        )
    }

    /// Is the request a upgrade request
    pub fn is_upgrade_req(&self) -> bool {
        self.v1_inner.is_upgrade_req()
    }

    /// Get the request header as raw bytes, `b""` when the header doesn't exist
    pub fn get_header_bytes(&self, name: impl AsHeaderName) -> &[u8] {
        self.v1_inner.get_header_bytes(name)
    }

    /// Read the request body. `Ok(None)` when there is no (more) body to read.
    pub async fn read_body_bytes(&mut self) -> Result<Option<Bytes>> {
        let read = self.read_body().await?;
        Ok(read.inspect(|b| {
            self.body_bytes_read += b.len();
            if let Some(buffer) = self.retry_buffer.as_mut() {
                buffer.write_to_buffer(b);
            }
        }))
    }

    async fn do_read_body(&mut self) -> Result<Option<Bytes>> {
        self.init_body_reader();
        self.body_reader
            .read_body(self.rx.as_mut().expect("rx valid before shutdown"))
            .await
    }

    /// Read the body bytes with timeout.
    async fn read_body(&mut self) -> Result<Option<Bytes>> {
        match self.read_timeout {
            Some(t) => match timeout(t, self.do_read_body()).await {
                Ok(res) => res,
                Err(_) => Error::e_explain(
                    ReadTimedout,
                    format!("reading body, timeout: {t:?} (subrequest)"),
                ),
            },
            None => self.do_read_body().await,
        }
    }

    async fn do_drain_request_body(&mut self) -> Result<()> {
        loop {
            match self.read_body_bytes().await {
                Ok(Some(_)) => { /* continue to drain */ }
                Ok(None) => return Ok(()), // done
                Err(e) => return Err(e),
            }
        }
    }

    /// Drain the request body. `Ok(())` when there is no (more) body to read.
    pub async fn drain_request_body(&mut self) -> Result<()> {
        if self.is_body_done() {
            return Ok(());
        }
        match self.total_drain_timeout {
            Some(t) => match timeout(t, self.do_drain_request_body()).await {
                Ok(res) => res,
                Err(_) => Error::e_explain(
                    ReadTimedout,
                    format!("draining body, timeout: {t:?} (subrequest)"),
                ),
            },
            None => self.do_drain_request_body().await,
        }
    }

    /// Whether there is no (more) body to be read.
    pub fn is_body_done(&mut self) -> bool {
        self.init_body_reader();
        self.body_reader.body_done()
    }

    /// Whether the request has an empty body
    /// Because HTTP 1.1 clients have to send either `Content-Length` or `Transfer-Encoding` in order
    /// to signal the server that it will send the body, this function returns accurate results even
    /// only when the request header is just read.
    pub fn is_body_empty(&mut self) -> bool {
        self.init_body_reader();
        self.body_reader.body_empty()
    }

    /// Write the response header to the client.
    /// This function can be called more than once to send 1xx informational headers excluding 101.
    pub async fn write_response_header(&mut self, header: Box<ResponseHeader>) -> Result<()> {
        if let Some(resp) = self.response_written.as_ref() {
            if !resp.status.is_informational() || self.upgraded {
                warn!("Respond header is already sent, cannot send again (subrequest)");
                return Ok(());
            }
        }

        // XXX: don't add additional downstream headers, unlike h1, subreq is mostly treated as a pipe

        let mut upgrade_ok: Option<bool> = None;
        if header.status == 101 || !header.status.is_informational() {
            upgrade_ok = self.is_upgrade(&header);
        }

        // TODO propagate h2 end
        debug!("send response header (subrequest)");
        match self
            .tx
            .as_mut()
            .expect("tx valid before shutdown")
            .send(HttpTask::Header(header.clone(), false))
            .await
        {
            Ok(()) => {
                self.init_body_writer(&header);
                self.response_written = Some(*header);
                if let Some(true) = upgrade_ok {
                    debug!("ok upgrade handshake");
                    // For ws we use HTTP1_0 do_read_body_until_closed
                    //
                    // On ws close the initiator sends a close frame and
                    // then waits for a response from the peer, once it receives
                    // a response it closes the conn. After receiving a
                    // control frame indicating the connection should be closed,
                    // a peer discards any further data received.
                    // https://www.rfc-editor.org/rfc/rfc6455#section-1.4
                    self.upgraded = true;
                    // Now that the upgrade was successful, we need to change
                    // how we interpret the rest of the body as pass-through.
                    if self.body_reader.need_init() {
                        self.init_body_reader();
                    } else {
                        // already initialized
                        // immediately start reading the rest of the body as upgraded
                        // (in theory most upgraded requests shouldn't have any body)
                        //
                        // TODO: https://datatracker.ietf.org/doc/html/rfc9110#name-upgrade
                        // the most spec-compliant behavior is to switch interpretation
                        // after sending the former body. For now we immediately
                        // switch interpretation to match nginx behavior.
                        // TODO: this has no effect resetting the body counter of TE chunked
                        self.body_reader.convert_to_close_delimited();
                    }
                } else if upgrade_ok == Some(false) {
                    debug!("bad upgrade handshake!");
                    // continue to read body as-is, this is now just a regular request
                }
                Ok(())
            }
            Err(e) => Error::e_because(WriteError, "writing response header", e),
        }
    }

    /// Return the response header if it is already sent.
    pub fn response_written(&self) -> Option<&ResponseHeader> {
        self.response_written.as_ref()
    }

    /// `Some(true)` if the this is a successful upgrade
    /// `Some(false)` if the request is an upgrade but the response refuses it
    /// `None` if the request is not an upgrade.
    pub fn is_upgrade(&self, header: &ResponseHeader) -> Option<bool> {
        self.v1_inner.is_upgrade(header)
    }

    /// Was this request successfully turned into an upgraded connection?
    ///
    /// Both the request had to have been an `Upgrade` request
    /// and the response had to have been a `101 Switching Protocols`.
    // XXX: this should only be valid if subrequest is standing in for
    // a v1 session.
    pub fn was_upgraded(&self) -> bool {
        self.upgraded
    }

    fn init_body_writer(&mut self, header: &ResponseHeader) {
        if let Some(mode) = body_mode_for_header(
            header,
            self.get_method(),
            self.is_upgrade(header) == Some(true),
        ) {
            apply_body_mode(&mut self.body_writer, mode);
        }
    }

    /// Same as [`Self::write_response_header()`] but takes a reference.
    pub async fn write_response_header_ref(&mut self, resp: &ResponseHeader) -> Result<()> {
        self.write_response_header(Box::new(resp.clone())).await
    }

    async fn do_write_body(&mut self, buf: Bytes) -> Result<Option<usize>> {
        let written = self
            .body_writer
            .write_body(self.tx.as_mut().expect("tx valid before shutdown"), buf)
            .await;

        if let Ok(Some(num_bytes)) = written {
            self.body_bytes_sent += num_bytes;
        }

        written
    }

    /// Write response body to the client. Return `Ok(None)` when there shouldn't be more body
    /// to be written, e.g., writing more bytes than what the `Content-Length` header suggests
    pub async fn write_body(&mut self, buf: Bytes) -> Result<Option<usize>> {
        // TODO: check if the response header is written
        match self.write_timeout {
            Some(t) => match timeout(t, self.do_write_body(buf)).await {
                Ok(res) => res,
                Err(_) => Error::e_explain(WriteTimedout, format!("writing body, timeout: {t:?}")),
            },
            None => self.do_write_body(buf).await,
        }
    }

    fn maybe_force_close_body_reader(&mut self) {
        if self.upgraded && !self.body_reader.body_done() {
            // response is done, reset the request body to close
            self.body_reader.init_content_length(0);
        }
    }

    /// Signal that there is no more body to write.
    /// This call will try to flush the buffer if there is any un-flushed data.
    /// For chunked encoding response, this call will also send the last chunk.
    /// For upgraded sessions, this call will also close the reading of the client body.
    pub async fn finish(&mut self) -> Result<Option<usize>> {
        let res = self
            .body_writer
            .finish(self.tx.as_mut().expect("tx valid before shutdown"))
            .await?;

        self.maybe_force_close_body_reader();
        Ok(res)
    }

    /// Signal to error listener held by SubrequestHandle that a proxy error was encountered,
    /// and pass along what that error was.
    ///
    /// This is helpful to signal what errors were encountered outside of the proxy state machine,
    /// e.g. during subrequest request filters.
    ///
    /// Note: in the case of multiple proxy failures e.g. when caching, only the first error will
    /// be propagated (i.e. downstream error first if it goes away before upstream).
    pub fn on_proxy_failure(&mut self, e: Box<Error>) {
        // fine if handle is gone
        if let Some(sender) = self.proxy_error.take() {
            let _ = sender.send(e);
        }
    }

    /// Return how many response body bytes (application, not wire) already sent downstream
    pub fn body_bytes_sent(&self) -> usize {
        self.body_bytes_sent
    }

    /// Return how many request body bytes (application, not wire) already read from downstream
    pub fn body_bytes_read(&self) -> usize {
        self.body_bytes_read
    }

    fn is_chunked_encoding(&self) -> bool {
        is_chunked_encoding_from_headers(&self.req_header().headers)
    }

    /// Clear body-related subrequest headers.
    ///
    /// This is ok to call before the request is read; the headers will then be cleared after
    /// reading the request header.
    pub fn clear_request_body_headers(&mut self) {
        self.clear_request_body_headers = true;
        if self.read_req_header {
            let req = self.v1_inner.req_header_mut();
            req.remove_header(&header::CONTENT_LENGTH);
            req.remove_header(&header::TRANSFER_ENCODING);
            req.remove_header(&header::CONTENT_TYPE);
            req.remove_header(&header::CONTENT_ENCODING);
        }
    }

    fn init_body_reader(&mut self) {
        if self.body_reader.need_init() {
            // reset retry buffer
            if let Some(buffer) = self.retry_buffer.as_mut() {
                buffer.clear();
            }

            if self.was_upgraded() {
                // if upgraded _post_ 101 (and body was not init yet)
                // treat as upgraded body (pass through until closed)
                self.body_reader.init_close_delimited();
            } else if self.is_chunked_encoding() {
                // if chunked encoding, content-length should be ignored
                // TE is not visible at subrequest HttpTask level
                // so this means read until request closure
                self.body_reader.init_close_delimited();
            } else {
                let cl = header_value_content_length(self.get_header(header::CONTENT_LENGTH));
                match cl {
                    Some(i) => {
                        self.body_reader.init_content_length(i);
                    }
                    None => {
                        // Per RFC 9112: "Request messages are never close-delimited because they are
                        // always explicitly framed by length or transfer coding, with the absence of
                        // both implying the request ends immediately after the header section."
                        // All HTTP/1.x requests without Content-Length or Transfer-Encoding have 0 body
                        self.body_reader.init_content_length(0);
                    }
                }
            }
        }
    }

    pub fn retry_buffer_truncated(&self) -> bool {
        self.retry_buffer
            .as_ref()
            .map_or_else(|| false, |r| r.is_truncated())
    }

    pub fn enable_retry_buffering(&mut self) {
        if self.retry_buffer.is_none() {
            self.retry_buffer = Some(FixedBuffer::new(BODY_BUF_LIMIT))
        }
    }

    pub fn get_retry_buffer(&self) -> Option<Bytes> {
        self.retry_buffer.as_ref().and_then(|b| {
            if b.is_truncated() {
                None
            } else {
                b.get_buffer()
            }
        })
    }

    /// This function will (async) block forever until the client closes the connection.
    pub async fn idle(&mut self) -> Result<HttpTask> {
        let rx = self.rx.as_mut().expect("rx valid before shutdown");
        let mut task = rx
            .recv()
            .await
            .or_err(ReadError, "during HTTP idle state")?;
        // just consume empty body or done messages, the downstream channel is not a real
        // connection and only used for this one request
        while matches!(&task, HttpTask::Done)
            || matches!(&task, HttpTask::Body(b, _) if b.as_ref().is_none_or(|b| b.is_empty()))
        {
            task = rx
                .recv()
                .await
                .or_err(ReadError, "during HTTP idle state")?;
        }
        Ok(task)
    }

    /// This function will return body bytes (same as [`Self::read_body_bytes()`]), but after
    /// the client body finishes (`Ok(None)` is returned), calling this function again will block
    /// forever, same as [`Self::idle()`].
    pub async fn read_body_or_idle(&mut self, no_body_expected: bool) -> Result<Option<Bytes>> {
        if no_body_expected || self.is_body_done() {
            let read_task = self.idle().await?;
            Error::e_explain(
                ConnectError,
                format!("Sent unexpected task {read_task:?} after end of body (subrequest)"),
            )
        } else {
            self.read_body_bytes().await
        }
    }

    /// Return the raw bytes of the request header.
    pub fn get_headers_raw_bytes(&self) -> Bytes {
        self.v1_inner.get_headers_raw_bytes()
    }

    /// Close the subrequest channels, indicating that no more data will be sent
    /// or received. This is expected to be called before dropping the `Session` itself.
    pub fn shutdown(&mut self) {
        drop(self.tx.take());
        drop(self.rx.take());
    }

    /// Sets the downstream read timeout. This will trigger if we're unable
    /// to read from the subrequest channels after `timeout`.
    pub fn set_read_timeout(&mut self, timeout: Option<Duration>) {
        self.read_timeout = timeout;
    }

    /// Get the downstream read timeout.
    pub fn get_read_timeout(&self) -> Option<Duration> {
        self.read_timeout
    }

    /// Sets the downstream write timeout. This will trigger if we're unable
    /// to write to the subrequest channel after `timeout`.
    pub fn set_write_timeout(&mut self, timeout: Option<Duration>) {
        self.write_timeout = timeout;
    }

    /// Get the downstream write timeout.
    pub fn get_write_timeout(&self) -> Option<Duration> {
        self.write_timeout
    }

    /// Sets the total drain timeout.
    /// Note that the downstream read timeout still applies between body byte reads.
    pub fn set_total_drain_timeout(&mut self, timeout: Option<Duration>) {
        self.total_drain_timeout = timeout;
    }

    /// Get the downstream total drain timeout.
    pub fn get_total_drain_timeout(&self) -> Option<Duration> {
        self.total_drain_timeout
    }

    /// Return the [Digest], this is originally from the main request.
    pub fn digest(&self) -> Option<&Digest> {
        self.digest.as_deref()
    }

    /// Return a mutable [Digest] reference.
    pub fn digest_mut(&mut self) -> Option<&mut Digest> {
        self.digest.as_deref_mut()
    }

    /// Return the client (peer) address of the main request.
    pub fn client_addr(&self) -> Option<&SocketAddr> {
        self.digest()
            .and_then(|d| d.socket_digest.as_ref())
            .map(|d| d.peer_addr())?
    }

    /// Return the server (local) address of the main request.
    pub fn server_addr(&self) -> Option<&SocketAddr> {
        self.digest()
            .and_then(|d| d.socket_digest.as_ref())
            .map(|d| d.local_addr())?
    }

    /// Write a `100 Continue` response to the client.
    pub async fn write_continue_response(&mut self) -> Result<()> {
        // only send if we haven't already
        if self.response_written.is_none() {
            // size hint Some(0) because default is 8
            return self
                .write_response_header(Box::new(ResponseHeader::build(100, Some(0)).unwrap()))
                .await;
        }
        Ok(())
    }

    async fn write_non_empty_body(&mut self, data: Option<Bytes>, upgraded: bool) -> Result<()> {
        if upgraded != self.upgraded {
            if upgraded {
                panic!("Unexpected UpgradedBody task received on un-upgraded downstream session (subrequest)");
            } else {
                panic!("Unexpected Body task received on upgraded downstream session (subrequest)");
            }
        }
        let Some(d) = data else {
            return Ok(());
        };
        if d.is_empty() {
            return Ok(());
        }
        self.write_body(d).await.map_err(|e| e.into_down())?;
        Ok(())
    }

    async fn response_duplex(&mut self, task: HttpTask) -> Result<bool> {
        let end_stream = match task {
            HttpTask::Header(header, end_stream) => {
                self.write_response_header(header)
                    .await
                    .map_err(|e| e.into_down())?;
                end_stream
            }
            HttpTask::Body(data, end_stream) => {
                self.write_non_empty_body(data, false).await?;
                end_stream
            }
            HttpTask::UpgradedBody(data, end_stream) => {
                self.write_non_empty_body(data, true).await?;
                end_stream
            }
            HttpTask::Trailer(trailers) => {
                self.write_trailers(trailers).await?;
                true
            }
            HttpTask::Done => true,
            HttpTask::Failed(e) => return Err(e),
        };
        if end_stream {
            // no-op if body wasn't initialized or is finished already
            self.finish().await.map_err(|e| e.into_down())?;
        }
        Ok(end_stream || self.body_writer.finished())
    }

    // TODO: use vectored write to avoid copying
    pub async fn response_duplex_vec(&mut self, mut tasks: Vec<HttpTask>) -> Result<bool> {
        // TODO: send httptask failed on each error?
        let n_tasks = tasks.len();
        if n_tasks == 1 {
            // fallback to single operation to avoid copy
            return self.response_duplex(tasks.pop().unwrap()).await;
        }
        let mut end_stream = false;
        for task in tasks.into_iter() {
            end_stream = match task {
                HttpTask::Header(header, end_stream) => {
                    self.write_response_header(header)
                        .await
                        .map_err(|e| e.into_down())?;
                    end_stream
                }
                HttpTask::Body(data, end_stream) => {
                    self.write_non_empty_body(data, false).await?;
                    end_stream
                }
                HttpTask::UpgradedBody(data, end_stream) => {
                    self.write_non_empty_body(data, true).await?;
                    end_stream
                }
                HttpTask::Done => {
                    // write done
                    // we'll send HttpTask::Done at the end of this loop in finish
                    true
                }
                HttpTask::Trailer(trailers) => {
                    self.write_trailers(trailers).await?;
                    true
                }
                HttpTask::Failed(e) => {
                    // write failed
                    // error should also be returned when sender drops
                    return Err(e);
                }
            } || end_stream; // safe guard in case `end` in tasks flips from true to false
        }
        if end_stream {
            // no-op if body wasn't initialized or is finished already
            self.finish().await.map_err(|e| e.into_down())?;
        }
        Ok(end_stream || self.body_writer.finished())
    }

    /// Write response trailers to the client, this also closes the stream.
    pub async fn write_trailers(&mut self, trailers: Option<Box<HeaderMap>>) -> Result<()> {
        self.body_writer
            .write_trailers(
                self.tx.as_mut().expect("tx valid before shutdown"),
                trailers,
            )
            .await
    }

    // Cancel-safe proxy task API. Unlike v1's partial `AsyncWrite` state
    // machines, subrequest uses mpsc `reserve()` + synchronous `Permit::send`.

    /// Whether the cancel-safe proxy task API is enabled for this session.
    pub fn proxy_tasks_enabled(&self) -> bool {
        self.proxy_tasks_enabled
    }

    /// Enable or disable the cancel-safe proxy task API for this session.
    pub fn set_proxy_tasks_enabled(&mut self, enabled: bool) {
        self.proxy_tasks_enabled = enabled;
    }

    /// Queue a proxy task for cancel-safe writing.
    pub fn send_proxy_task(&mut self, task: HttpTask) {
        self.proxy_task_state.tasks.push_back(task);
    }

    /// Whether there are pending proxy tasks queued for writing.
    pub fn has_pending_proxy_tasks(&self) -> bool {
        self.proxy_task_state.finish_in_progress
            || self.proxy_task_state.finish_required()
            || !self.proxy_task_state.tasks.is_empty()
    }

    /// Write queued proxy tasks. Cancelling while waiting for channel capacity
    /// leaves the current task in `proxy_task_state.tasks`.
    pub async fn write_proxy_tasks(&mut self) -> Result<bool> {
        loop {
            if self.proxy_task_state.finished() {
                self.proxy_task_state.tasks.clear();
                return Ok(true);
            }

            if self.proxy_task_state.finish_in_progress {
                self.finish_proxy_task().await?;
                self.proxy_task_state.mark_finished();
                return Ok(true);
            }

            let Some(front) = self.proxy_task_state.tasks.front() else {
                break;
            };

            // Tasks with no underlying channel send: handle synchronously
            // without reserving a permit.
            match front {
                HttpTask::Done => {
                    self.proxy_task_state.tasks.pop_front();
                    self.proxy_task_state.require_finish();
                    continue;
                }
                HttpTask::Failed(_) => {
                    let HttpTask::Failed(e) = self
                        .proxy_task_state
                        .tasks
                        .pop_front()
                        .expect("queue had a Failed task at the front")
                    else {
                        unreachable!()
                    };
                    self.proxy_task_state.clear_stream_end();
                    return Err(e);
                }
                _ => {}
            }

            if let HttpTask::Header(_, header_end) = front {
                let already_sent = match self.response_written.as_ref() {
                    Some(resp) => !resp.status.is_informational() || self.upgraded,
                    None => false,
                };
                if already_sent {
                    warn!("Respond header is already sent, cannot send again (subrequest, proxy task)");
                    if *header_end {
                        self.proxy_task_state.require_finish();
                    }
                    self.proxy_task_state.tasks.pop_front();
                    continue;
                }
            }

            if let Some((upgraded_task, body_end, no_data)) = match front {
                HttpTask::Body(data, end) => {
                    Some((false, *end, data.as_ref().is_none_or(|d| d.is_empty())))
                }
                HttpTask::UpgradedBody(data, end) => {
                    Some((true, *end, data.as_ref().is_none_or(|d| d.is_empty())))
                }
                _ => None,
            } {
                if upgraded_task != self.upgraded {
                    if upgraded_task {
                        panic!("Unexpected UpgradedBody task received on un-upgraded downstream session (subrequest, proxy task)");
                    } else {
                        panic!("Unexpected Body task received on upgraded downstream session (subrequest, proxy task)");
                    }
                }

                if body_end {
                    self.proxy_task_state.require_finish();
                }

                if no_data {
                    self.proxy_task_state.tasks.pop_front();
                    continue;
                }

                match self.body_writer.body_mode {
                    BodyMode::Complete(_) => {
                        self.proxy_task_state.tasks.pop_front();
                        continue;
                    }
                    BodyMode::ContentLength(total, written) if written >= total => {
                        self.proxy_task_state.tasks.pop_front();
                        continue;
                    }
                    BodyMode::ToSelect => {
                        self.proxy_task_state.tasks.pop_front();
                        self.proxy_task_state.clear_stream_end();
                        return Error::e_explain(
                            InternalError,
                            "subrequest body proxy task before header is sent",
                        );
                    }
                    _ => {}
                }
            }

            // `reserve()` is the only cancellation point; the queued task is
            // popped only after a permit is acquired.
            let tx_ref = self
                .tx
                .as_ref()
                .ok_or_else(|| Error::explain(InternalError, "subrequest tx already shut down"))?;
            let permit = match self.write_timeout {
                Some(t) => match timeout(t, tx_ref.reserve()).await {
                    Ok(res) => res.or_err(WriteError, "subrequest channel closed")?,
                    Err(_) => {
                        return Error::e_explain(
                            WriteTimedout,
                            format!("reserving subrequest channel slot, timeout: {t:?}"),
                        );
                    }
                },
                None => tx_ref
                    .reserve()
                    .await
                    .or_err(WriteError, "subrequest channel closed")?,
            };

            // From here until `permit.send`, no `.await`; dispatch is atomic.
            let task = self
                .proxy_task_state
                .tasks
                .pop_front()
                .expect("queue non-empty");

            match task {
                HttpTask::Header(header, hdr_end) => {
                    if hdr_end {
                        self.proxy_task_state.require_finish();
                    }

                    let upgrade_ok = if header.status == 101 || !header.status.is_informational() {
                        let outcome = self.v1_inner.is_upgrade(&header);
                        let mode = body_mode_for_header(
                            &header,
                            self.v1_inner.get_method(),
                            outcome == Some(true),
                        );
                        (outcome, mode)
                    } else {
                        (None, None)
                    };

                    permit.send(HttpTask::Header(header.clone(), false));

                    if let Some(mode) = upgrade_ok.1 {
                        apply_body_mode(&mut self.body_writer, mode);
                    }
                    self.response_written = Some(*header);
                    if let Some(true) = upgrade_ok.0 {
                        debug!("ok upgrade handshake (subrequest, proxy task)");
                        self.upgraded = true;
                        if self.body_reader.need_init() {
                            self.init_body_reader();
                        } else {
                            self.body_reader.convert_to_close_delimited();
                        }
                    } else if upgrade_ok.0 == Some(false) {
                        debug!("bad upgrade handshake! (subrequest, proxy task)");
                    }
                }

                HttpTask::Body(data, end) => {
                    if end {
                        self.proxy_task_state.require_finish();
                    }
                    dispatch_body_inline(
                        &mut self.body_writer,
                        &mut self.body_bytes_sent,
                        self.upgraded,
                        data,
                        /* upgraded_task = */ false,
                        permit,
                    )?;
                }
                HttpTask::UpgradedBody(data, end) => {
                    if end {
                        self.proxy_task_state.require_finish();
                    }
                    dispatch_body_inline(
                        &mut self.body_writer,
                        &mut self.body_bytes_sent,
                        self.upgraded,
                        data,
                        /* upgraded_task = */ true,
                        permit,
                    )?;
                }

                HttpTask::Trailer(trailers) => {
                    permit.send(HttpTask::Trailer(trailers));
                    self.proxy_task_state.require_finish();
                }

                HttpTask::Done | HttpTask::Failed(_) => {
                    unreachable!("Done/Failed are handled above without reserving a permit")
                }
            }
        }

        // Match `response_duplex_vec`: finish whenever any task signalled EOS.
        if self.proxy_task_state.finish_required() || self.body_writer.finished() {
            self.finish_proxy_task().await?;
            self.proxy_task_state.mark_finished();
            return Ok(true);
        }

        Ok(self.body_writer.finished())
    }

    async fn finish_proxy_task(&mut self) -> Result<()> {
        if matches!(
            &self.body_writer.body_mode,
            BodyMode::Complete(_) | BodyMode::ToSelect
        ) {
            self.maybe_force_close_body_reader();
            self.proxy_task_state.finish_in_progress = false;
            return Ok(());
        }

        if let BodyMode::ContentLength(total, written) = self.body_writer.body_mode {
            if written < total {
                self.body_writer.body_mode = BodyMode::Complete(written);
                self.proxy_task_state.finish_in_progress = false;
                self.proxy_task_state.clear_stream_end();
                return Error::e_explain(
                    PREMATURE_BODY_END,
                    format!(
                        "Content-length: {total} bytes written: {written} (subrequest, proxy task)"
                    ),
                );
            }
        }

        self.proxy_task_state.finish_in_progress = true;
        self.dispatch_finish().await?;
        self.proxy_task_state.finish_in_progress = false;
        Ok(())
    }

    /// Dispatch the final `HttpTask::Done`, mirroring `body_writer::finish`.
    async fn dispatch_finish(&mut self) -> Result<()> {
        // Reserve cancel-safely, then synchronously update body_mode and send.
        let tx_ref = self
            .tx
            .as_ref()
            .ok_or_else(|| Error::explain(InternalError, "subrequest tx already shut down"))?;
        let permit = match self.write_timeout {
            Some(t) => match timeout(t, tx_ref.reserve()).await {
                Ok(res) => res.or_err(WriteError, "subrequest channel closed")?,
                Err(_) => {
                    return Error::e_explain(
                        WriteTimedout,
                        format!("reserving subrequest channel slot for finish, timeout: {t:?}"),
                    );
                }
            },
            None => tx_ref
                .reserve()
                .await
                .or_err(WriteError, "subrequest channel closed")?,
        };

        match self.body_writer.body_mode {
            BodyMode::ContentLength(_total, written) => {
                self.body_writer.body_mode = BodyMode::Complete(written);
                permit.send(HttpTask::Done);
            }
            BodyMode::UntilClose(written) => {
                self.body_writer.body_mode = BodyMode::Complete(written);
                permit.send(HttpTask::Done);
            }
            BodyMode::Complete(_) => {
                unreachable!("no-op body modes are handled before reserve")
            }
            BodyMode::ToSelect => {
                unreachable!("no-op body modes are handled before reserve")
            }
        }
        self.maybe_force_close_body_reader();
        Ok(())
    }
}

fn body_mode_for_header(
    header: &ResponseHeader,
    method: Option<&Method>,
    is_upgrade_ok: bool,
) -> Option<BodyMode> {
    use http::StatusCode;
    if header.status.is_informational() && header.status != StatusCode::SWITCHING_PROTOCOLS {
        return None;
    }
    if matches!(
        header.status,
        StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED
    ) || method == Some(&Method::HEAD)
    {
        return Some(BodyMode::ContentLength(0, 0));
    }
    if is_upgrade_ok || is_chunked_encoding_from_headers(&header.headers) {
        Some(BodyMode::UntilClose(0))
    } else {
        let content_length =
            header_value_content_length(header.headers.get(http::header::CONTENT_LENGTH));
        match content_length {
            Some(length) => Some(BodyMode::ContentLength(length, 0)),
            None => Some(BodyMode::UntilClose(0)),
        }
    }
}

fn apply_body_mode(body_writer: &mut BodyWriter, mode: BodyMode) {
    match mode {
        BodyMode::ContentLength(total, 0) => body_writer.init_content_length(total),
        BodyMode::UntilClose(0) => body_writer.init_close_delimited(),
        _ => body_writer.body_mode = mode,
    }
}

/// Body dispatch variant that avoids borrowing the whole session while a
/// channel permit borrows `self.tx`.
fn dispatch_body_inline(
    body_writer: &mut BodyWriter,
    body_bytes_sent: &mut usize,
    upgraded: bool,
    data: Option<Bytes>,
    upgraded_task: bool,
    permit: mpsc::Permit<'_, HttpTask>,
) -> Result<()> {
    if upgraded_task != upgraded {
        if upgraded_task {
            panic!("Unexpected UpgradedBody task received on un-upgraded downstream session (subrequest, proxy task)");
        } else {
            panic!("Unexpected Body task received on upgraded downstream session (subrequest, proxy task)");
        }
    }

    let Some(d) = data else {
        drop(permit);
        return Ok(());
    };
    if d.is_empty() {
        drop(permit);
        return Ok(());
    }

    let (to_count, next_mode) = match &body_writer.body_mode {
        BodyMode::ContentLength(total, written) => {
            if written >= total {
                drop(permit);
                return Ok(());
            }
            let remaining = *total - *written;
            let to_write = if remaining < d.len() {
                warn!("Trying to write data over content-length (subrequest, proxy task): {total}");
                remaining
            } else {
                d.len()
            };
            (
                to_write,
                BodyMode::ContentLength(*total, *written + to_write),
            )
        }
        BodyMode::UntilClose(written) => (d.len(), BodyMode::UntilClose(*written + d.len())),
        BodyMode::Complete(_) => {
            drop(permit);
            return Ok(());
        }
        BodyMode::ToSelect => {
            drop(permit);
            return Error::e_explain(
                InternalError,
                "subrequest body proxy task before header is sent",
            );
        }
    };

    let to_send = if to_count < d.len() {
        d.slice(..to_count)
    } else {
        d
    };
    permit.send(HttpTask::Body(Some(to_send), false));
    body_writer.body_mode = next_mode;
    *body_bytes_sent += to_count;
    Ok(())
}

#[cfg(test)]
mod tests_stream {
    use super::*;
    use crate::protocols::http::subrequest::body::{BodyMode, ParseState};
    use bytes::BufMut;
    use http::StatusCode;
    use rstest::rstest;

    use std::str;
    use tokio_test::io::Builder;

    fn init_log() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    fn test_header(status: StatusCode) -> ResponseHeader {
        ResponseHeader::build(status, None)
            .expect("test status code should build a response header")
    }

    fn recv_task(rx: &mut mpsc::Receiver<HttpTask>) -> HttpTask {
        rx.try_recv()
            .expect("expected subrequest output task to be queued")
    }

    async fn session_from_input(input: &[u8]) -> (HttpSession, SubrequestHandle) {
        let mock_io = Builder::new().read(input).build();
        let mut http_stream = GenericHttpSession::new_http1(Box::new(mock_io));
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        let (mut http_stream, handle) = HttpSession::new_from_session(&http_stream);
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        (http_stream, handle)
    }

    pub(super) async fn build_upgrade_req(
        upgrade: &str,
        conn: &str,
    ) -> (HttpSession, SubrequestHandle) {
        let input = format!("GET / HTTP/1.1\r\nHost: pingora.org\r\nUpgrade: {upgrade}\r\nConnection: {conn}\r\n\r\n");
        session_from_input(input.as_bytes()).await
    }

    pub(super) async fn build_req() -> (HttpSession, SubrequestHandle) {
        let input = "GET / HTTP/1.1\r\nHost: pingora.org\r\n\r\n".to_string();
        session_from_input(input.as_bytes()).await
    }

    pub(super) async fn build_head_req() -> (HttpSession, SubrequestHandle) {
        let input = "HEAD / HTTP/1.1\r\nHost: pingora.org\r\n\r\n".to_string();
        session_from_input(input.as_bytes()).await
    }

    #[tokio::test]
    async fn read_basic() {
        init_log();
        let input = b"GET / HTTP/1.1\r\n\r\n";
        let (http_stream, _handle) = session_from_input(input).await;
        assert_eq!(0, http_stream.req_header().headers.len());
        assert_eq!(Method::GET, http_stream.req_header().method);
        assert_eq!(b"/", http_stream.req_header().uri.path().as_bytes());
    }

    #[tokio::test]
    async fn read_upgrade_req() {
        // http 1.0
        let input = b"GET / HTTP/1.0\r\nHost: pingora.org\r\nUpgrade: websocket\r\nConnection: upgrade\r\n\r\n";
        let (http_stream, _handle) = session_from_input(input).await;
        assert!(!http_stream.is_upgrade_req());

        // different method
        let input = b"POST / HTTP/1.1\r\nHost: pingora.org\r\nUpgrade: websocket\r\nConnection: upgrade\r\n\r\n";
        let (http_stream, _handle) = session_from_input(input).await;
        assert!(http_stream.is_upgrade_req());

        // missing upgrade header
        let input = b"GET / HTTP/1.1\r\nHost: pingora.org\r\nConnection: upgrade\r\n\r\n";
        let (http_stream, _handle) = session_from_input(input).await;
        assert!(!http_stream.is_upgrade_req());

        // no connection header
        let input = b"GET / HTTP/1.1\r\nHost: pingora.org\r\nUpgrade: WebSocket\r\n\r\n";
        let (http_stream, _handle) = session_from_input(input).await;
        assert!(http_stream.is_upgrade_req());

        let (http_stream, _handle) = build_upgrade_req("websocket", "Upgrade").await;
        assert!(http_stream.is_upgrade_req());

        // mixed case
        let (http_stream, _handle) = build_upgrade_req("WebSocket", "Upgrade").await;
        assert!(http_stream.is_upgrade_req());
    }

    #[tokio::test]
    async fn read_upgrade_req_with_1xx_response() {
        let (mut http_stream, _handle) = build_upgrade_req("websocket", "upgrade").await;
        assert!(http_stream.is_upgrade_req());
        let mut response = test_header(StatusCode::CONTINUE);
        response.set_version(http::Version::HTTP_11);
        http_stream
            .write_response_header(Box::new(response))
            .await
            .expect("test operation should succeed");
        // 100 won't affect body state
        assert!(http_stream.is_body_done());
    }

    #[tokio::test]
    async fn write() {
        let (mut http_stream, mut handle) = build_req().await;
        let mut new_response = test_header(StatusCode::OK);
        new_response
            .append_header("Foo", "Bar")
            .expect("test operation should succeed");
        http_stream
            .write_response_header_ref(&new_response)
            .await
            .expect("test operation should succeed");
        match recv_task(&mut handle.rx) {
            HttpTask::Header(header, end) => {
                assert_eq!(header.status, StatusCode::OK);
                assert_eq!(header.headers["foo"], "Bar");
                assert!(!end);
            }
            t => panic!("unexpected task {t:?}"),
        }
    }

    #[tokio::test]
    async fn write_informational() {
        let (mut http_stream, mut handle) = build_req().await;
        let response_100 = test_header(StatusCode::CONTINUE);
        http_stream
            .write_response_header_ref(&response_100)
            .await
            .expect("test operation should succeed");
        match recv_task(&mut handle.rx) {
            HttpTask::Header(header, end) => {
                assert_eq!(header.status, StatusCode::CONTINUE);
                assert!(!end);
            }
            t => panic!("unexpected task {t:?}"),
        }

        let response_200 = test_header(StatusCode::OK);
        http_stream
            .write_response_header_ref(&response_200)
            .await
            .expect("test operation should succeed");
        match recv_task(&mut handle.rx) {
            HttpTask::Header(header, end) => {
                assert_eq!(header.status, StatusCode::OK);
                assert!(!end);
            }
            t => panic!("unexpected task {t:?}"),
        }
    }

    #[tokio::test]
    async fn write_101_switching_protocol() {
        let (mut http_stream, mut handle) = build_upgrade_req("WebSocket", "Upgrade").await;
        let mut response_101 = test_header(StatusCode::SWITCHING_PROTOCOLS);
        response_101
            .append_header("Foo", "Bar")
            .expect("test operation should succeed");
        http_stream
            .write_response_header_ref(&response_101)
            .await
            .expect("test operation should succeed");

        match recv_task(&mut handle.rx) {
            HttpTask::Header(header, end) => {
                assert_eq!(header.status, StatusCode::SWITCHING_PROTOCOLS);
                assert!(!end);
            }
            t => panic!("unexpected task {t:?}"),
        }
        assert!(http_stream.upgraded);

        let wire_body = Bytes::from(&b"PAYLOAD"[..]);
        let n = http_stream
            .write_body(wire_body.clone())
            .await
            .unwrap()
            .expect("test operation should succeed");
        assert_eq!(wire_body.len(), n);
        // this write should be ignored
        let response_502 = ResponseHeader::build(StatusCode::BAD_GATEWAY, None)
            .expect("test operation should succeed");
        http_stream
            .write_response_header_ref(&response_502)
            .await
            .expect("test operation should succeed");

        match recv_task(&mut handle.rx) {
            HttpTask::Body(body, _end) => {
                assert_eq!(body.unwrap().len(), n);
            }
            t => panic!("unexpected task {t:?}"),
        }
        assert_eq!(
            handle.rx.try_recv().unwrap_err(),
            mpsc::error::TryRecvError::Empty
        );
    }

    #[tokio::test]
    async fn write_body_cl() {
        let (mut http_stream, _handle) = build_req().await;
        let wire_body = Bytes::from(&b"a"[..]);
        let mut new_response = test_header(StatusCode::OK);
        new_response
            .append_header("Content-Length", "1")
            .expect("test operation should succeed");
        http_stream
            .write_response_header_ref(&new_response)
            .await
            .expect("test operation should succeed");
        assert_eq!(
            http_stream.body_writer.body_mode,
            BodyMode::ContentLength(1, 0)
        );
        let n = http_stream
            .write_body(wire_body.clone())
            .await
            .unwrap()
            .expect("test operation should succeed");
        assert_eq!(wire_body.len(), n);
        let n = http_stream
            .finish()
            .await
            .expect("test async operation should succeed")
            .expect("test operation should succeed");
        assert_eq!(wire_body.len(), n);
    }

    #[tokio::test]
    async fn write_body_until_close() {
        let (mut http_stream, _handle) = build_req().await;
        let new_response = test_header(StatusCode::OK);
        http_stream
            .write_response_header_ref(&new_response)
            .await
            .expect("test operation should succeed");
        assert_eq!(http_stream.body_writer.body_mode, BodyMode::UntilClose(0));
        let wire_body = Bytes::from(&b"PAYLOAD"[..]);
        let n = http_stream
            .write_body(wire_body.clone())
            .await
            .unwrap()
            .expect("test operation should succeed");
        assert_eq!(wire_body.len(), n);
        let n = http_stream
            .finish()
            .await
            .expect("test async operation should succeed")
            .expect("test operation should succeed");
        assert_eq!(wire_body.len(), n);
    }

    #[tokio::test]
    async fn read_with_illegal() {
        init_log();
        let input1 = b"GET /a?q=b c HTTP/1.1\r\n";
        let input2 = b"Host: pingora.org\r\nContent-Length: 3\r\n\r\n";
        let input3 = b"abc";
        let mock_io = Builder::new().read(&input1[..]).read(&input2[..]).build();
        let mut http_stream = GenericHttpSession::new_http1(Box::new(mock_io));
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        let (mut http_stream, handle) = HttpSession::new_from_session(&http_stream);
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        handle
            .tx
            .send(HttpTask::Body(Some(Bytes::from(&input3[..])), false))
            .await
            .expect("test operation should succeed");

        assert_eq!(http_stream.get_path(), &b"/a?q=b%20c"[..]);
        let res = http_stream
            .read_body()
            .await
            .expect("test async operation should succeed")
            .expect("test operation should succeed");
        assert_eq!(res, &input3[..]);
        assert_eq!(http_stream.body_reader.body_state, ParseState::Complete(3));
    }

    #[tokio::test]
    async fn test_write_body_write_timeout() {
        let (mut http_stream, _handle) = build_req().await;
        http_stream.write_timeout = Some(Duration::from_millis(100));
        let mut new_response = test_header(StatusCode::OK);
        new_response
            .append_header("Content-Length", "10")
            .expect("test operation should succeed");
        http_stream
            .write_response_header_ref(&new_response)
            .await
            .expect("test operation should succeed");
        let body_write_buf = Bytes::from(&b"abc"[..]);
        http_stream
            .write_body(body_write_buf.clone())
            .await
            .expect("test operation should succeed");
        http_stream
            .write_body(body_write_buf.clone())
            .await
            .expect("test operation should succeed");
        http_stream
            .write_body(body_write_buf)
            .await
            .expect("test async operation should succeed");
        // channel full
        let last_body = Bytes::from(&b"a"[..]);
        let res = http_stream.write_body(last_body).await;
        assert_eq!(res.unwrap_err().etype(), &WriteTimedout);
    }

    #[tokio::test]
    async fn test_write_continue_resp() {
        let (mut http_stream, mut handle) = build_req().await;
        http_stream
            .write_continue_response()
            .await
            .expect("test async operation should succeed");
        match recv_task(&mut handle.rx) {
            HttpTask::Header(header, end) => {
                assert_eq!(header.status, StatusCode::CONTINUE);
                assert!(!end);
            }
            t => panic!("unexpected task {t:?}"),
        }
    }

    async fn session_from_input_no_validate(input: &[u8]) -> (HttpSession, SubrequestHandle) {
        let mock_io = Builder::new().read(input).build();
        let mut http_stream = GenericHttpSession::new_http1(Box::new(mock_io));
        // Read the request in v1 inner session to set up headers properly
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        let (http_stream, handle) = HttpSession::new_from_session(&http_stream);
        (http_stream, handle)
    }

    #[rstest]
    #[case::negative("-1")]
    #[case::not_a_number("abc")]
    #[case::float("1.5")]
    #[case::empty("")]
    #[case::spaces("  ")]
    #[case::mixed("123abc")]
    #[tokio::test]
    async fn validate_request_rejects_invalid_content_length(#[case] invalid_value: &str) {
        init_log();
        let input = format!(
            "POST / HTTP/1.1\r\nHost: pingora.org\r\nContent-Length: {}\r\n\r\n",
            invalid_value
        );
        let mock_io = Builder::new().read(input.as_bytes()).build();
        let mut http_stream = GenericHttpSession::new_http1(Box::new(mock_io));
        // read_request calls validate_request internally on the v1 inner stream, so it should fail here
        let res = http_stream.read_request().await;
        assert!(res.is_err());
        assert_eq!(
            res.unwrap_err().etype(),
            &pingora_error::ErrorType::InvalidHTTPHeader
        );
    }

    #[rstest]
    #[case::valid_zero("0")]
    #[case::valid_small("123")]
    #[case::valid_large("999999")]
    #[tokio::test]
    async fn validate_request_accepts_valid_content_length(#[case] valid_value: &str) {
        init_log();
        let input = format!(
            "POST / HTTP/1.1\r\nHost: pingora.org\r\nContent-Length: {}\r\n\r\n",
            valid_value
        );
        let (mut http_stream, _handle) = session_from_input_no_validate(input.as_bytes()).await;
        let res = http_stream.read_request().await;
        assert!(res.is_ok());
    }

    #[tokio::test]
    async fn validate_request_accepts_no_content_length() {
        init_log();
        let input = b"GET / HTTP/1.1\r\nHost: pingora.org\r\n\r\n";
        let (mut http_stream, _handle) = session_from_input_no_validate(input).await;
        let res = http_stream.read_request().await;
        assert!(res.is_ok());
    }

    const POST_CL_UPGRADE_REQ: &[u8] = b"POST / HTTP/1.1\r\nHost: pingora.org\r\nUpgrade: websocket\r\nConnection: upgrade\r\nContent-Length: 10\r\n\r\n";
    const POST_CHUNKED_UPGRADE_REQ: &[u8] = b"POST / HTTP/1.1\r\nHost: pingora.org\r\nUpgrade: websocket\r\nConnection: upgrade\r\nTransfer-Encoding: chunked\r\n\r\n";
    const POST_BODY_DATA: &[u8] = b"abcdefghij";

    async fn build_upgrade_req_with_body(header: &[u8]) -> (HttpSession, SubrequestHandle) {
        let mock_io = Builder::new().read(header).build();
        let mut http_stream = GenericHttpSession::new_http1(Box::new(mock_io));
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        let (mut http_stream, handle) = HttpSession::new_from_session(&http_stream);
        http_stream
            .read_request()
            .await
            .expect("test async operation should succeed");
        (http_stream, handle)
    }

    #[rstest]
    #[case::content_length(POST_CL_UPGRADE_REQ)]
    #[case::chunked(POST_CHUNKED_UPGRADE_REQ)]
    #[tokio::test]
    async fn read_upgrade_req_with_body(#[case] header: &[u8]) {
        init_log();
        let (mut http_stream, handle) = build_upgrade_req_with_body(header).await;
        assert!(http_stream.is_upgrade_req());
        // request has body
        assert!(!http_stream.is_body_done());

        // Send body via the handle
        handle
            .tx
            .send(HttpTask::Body(Some(Bytes::from(POST_BODY_DATA)), true))
            .await
            .expect("test operation should succeed");

        let mut buf = vec![];
        while let Some(b) = http_stream
            .read_body_bytes()
            .await
            .expect("test async operation should succeed")
        {
            buf.put_slice(&b);
        }
        assert_eq!(buf, POST_BODY_DATA);
        assert_eq!(http_stream.body_reader.body_state, ParseState::Complete(10));
        assert_eq!(http_stream.body_bytes_read(), 10);

        assert!(http_stream.is_body_done());

        let mut response = test_header(StatusCode::SWITCHING_PROTOCOLS);
        response.set_version(http::Version::HTTP_11);
        http_stream
            .write_response_header(Box::new(response))
            .await
            .expect("test operation should succeed");
        // body reader type switches
        assert!(!http_stream.is_body_done());

        // now send ws data
        let ws_data = b"data";
        handle
            .tx
            .send(HttpTask::Body(Some(Bytes::from(&ws_data[..])), false))
            .await
            .expect("test operation should succeed");

        let buf = http_stream
            .read_body_bytes()
            .await
            .expect("test async operation should succeed")
            .expect("test operation should succeed");
        assert_eq!(buf, ws_data.as_slice());
        assert!(!http_stream.is_body_done());

        // EOF ends body
        drop(handle.tx);
        assert!(http_stream
            .read_body_bytes()
            .await
            .expect("test async operation should succeed")
            .is_none());
        assert!(http_stream.is_body_done());
    }
}

#[cfg(test)]
mod test_proxy_tasks {
    //! Cancel-safe proxy task API tests for the subrequest server session.

    use super::tests_stream::{build_head_req, build_req, build_upgrade_req};
    use super::*;
    use http::StatusCode;

    fn init_log() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    fn test_header(status: StatusCode) -> ResponseHeader {
        ResponseHeader::build(status, None)
            .expect("test status code should build a response header")
    }

    fn recv_task(rx: &mut mpsc::Receiver<HttpTask>) -> HttpTask {
        rx.try_recv()
            .expect("expected subrequest output task to be queued")
    }

    fn assert_rx_empty(rx: &mut mpsc::Receiver<HttpTask>) {
        assert!(matches!(
            rx.try_recv(),
            Err(mpsc::error::TryRecvError::Empty)
        ));
    }

    /// Dropping a blocked `send` does not deliver its value.
    #[tokio::test(start_paused = true)]
    async fn test_tokio_mpsc_send_cancel_drops_value() {
        let (tx, mut rx) = mpsc::channel::<u32>(1);
        tx.send(1)
            .await
            .expect("test async operation should succeed");
        let send_fut = tx.send(2);
        tokio::pin!(send_fut);
        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(10)) => {}
            _ = &mut send_fut => panic!("expected the timer to win"),
        };
        assert_eq!(rx.recv().await, Some(1));
        assert_eq!(rx.try_recv(), Err(mpsc::error::TryRecvError::Empty));
    }

    /// Dropping a blocked `reserve` does not consume capacity.
    #[tokio::test(start_paused = true)]
    async fn test_tokio_mpsc_reserve_cancel_releases_slot() {
        let (tx, mut rx) = mpsc::channel::<u32>(1);
        tx.send(1)
            .await
            .expect("test async operation should succeed");
        let reserve_fut = tx.reserve();
        tokio::pin!(reserve_fut);
        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(10)) => {}
            _ = &mut reserve_fut => panic!("expected the timer to win"),
        };
        assert_eq!(rx.recv().await, Some(1));
        assert_eq!(rx.try_recv(), Err(mpsc::error::TryRecvError::Empty));
    }

    #[tokio::test]
    async fn test_send_proxy_task_and_write() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        assert!(session.proxy_tasks_enabled());

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "5")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("hello")), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(end);
        assert!(!session.has_pending_proxy_tasks());
        assert_eq!(session.body_bytes_sent(), 5);

        match recv_task(&mut handle.rx) {
            HttpTask::Header(h, false) => assert_eq!(h.status, StatusCode::OK),
            t => panic!("expected Header, got {t:?}"),
        }
        match recv_task(&mut handle.rx) {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"hello"),
            t => panic!("expected Body, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_informational_head_does_not_init_body_writer() {
        init_log();

        let (mut regular, mut regular_handle) = build_head_req().await;
        regular
            .write_response_header(Box::new(test_header(StatusCode::CONTINUE)))
            .await
            .expect("regular informational header write should succeed");
        assert_eq!(regular.body_writer.body_mode, BodyMode::ToSelect);
        assert!(matches!(
            recv_task(&mut regular_handle.rx),
            HttpTask::Header(..)
        ));

        let (mut proxy, mut proxy_handle) = build_head_req().await;
        proxy.set_proxy_tasks_enabled(true);
        proxy.send_proxy_task(HttpTask::Header(
            Box::new(test_header(StatusCode::CONTINUE)),
            false,
        ));
        assert!(!proxy
            .write_proxy_tasks()
            .await
            .expect("proxy task write should succeed"));
        assert_eq!(proxy.body_writer.body_mode, BodyMode::ToSelect);
        assert!(matches!(
            recv_task(&mut proxy_handle.rx),
            HttpTask::Header(..)
        ));
    }

    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_with_timeout() {
        init_log();
        // Do not drain `handle.rx` before the first write; the 5th response task blocks on capacity.
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        session.set_write_timeout(Some(Duration::from_millis(50)));

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        for i in 0..5 {
            session.send_proxy_task(HttpTask::Body(
                Some(Bytes::from(format!("body-{i}"))),
                i == 4,
            ));
        }

        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("full subrequest output channel should time out");
        assert_eq!(err.etype(), &WriteTimedout);
        assert!(session.has_pending_proxy_tasks());

        let mut delivered = Vec::new();
        while let Ok(task) = handle.rx.try_recv() {
            delivered.push(task);
        }
        assert_eq!(delivered.len(), 4);
        assert!(matches!(delivered[0], HttpTask::Header(..)));

        session.set_write_timeout(None);
        let end = session
            .write_proxy_tasks()
            .await
            .expect("retry after freeing channel capacity should complete");
        assert!(end);
        while let Ok(task) = handle.rx.try_recv() {
            delivered.push(task);
        }

        assert_eq!(delivered.len(), 7);
        assert!(matches!(delivered.last(), Some(HttpTask::Done)));
        let body_count = delivered
            .iter()
            .filter(|t| matches!(t, HttpTask::Body(..)))
            .count();
        assert_eq!(body_count, 5);
    }

    #[tokio::test]
    async fn test_proxy_task_channel_closed_errors() {
        init_log();
        let (mut session, handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        drop(handle.rx);

        session.send_proxy_task(HttpTask::Header(
            Box::new(test_header(StatusCode::OK)),
            false,
        ));
        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("closed subrequest output channel should error");
        assert_eq!(err.etype(), &WriteError);
        assert!(session.has_pending_proxy_tasks());
    }

    /// Repeatedly cancel while blocked on channel capacity, then verify the
    /// receiver sees each queued task exactly once and in order.
    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_cancel_safety() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "5")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        for i in 0..4 {
            session.send_proxy_task(HttpTask::Body(
                Some(Bytes::from(vec![b'A' + i as u8; 1])),
                false,
            ));
        }
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("E")), true));

        let mut cancel_count = 0;
        let mut delivered: Vec<HttpTask> = Vec::new();
        loop {
            if !session.has_pending_proxy_tasks() {
                break;
            }
            tokio::select! {
                biased;
                _ = tokio::time::sleep(Duration::from_millis(5)) => {
                    cancel_count += 1;
                    while let Ok(task) = handle.rx.try_recv() {
                        delivered.push(task);
                    }
                }
                result = session.write_proxy_tasks() => {
                    result.expect("test operation should succeed");
                }
            }
        }

        assert!(
            cancel_count >= 1,
            "expected at least one cancellation during cancel-safe write, got {cancel_count}"
        );

        assert_eq!(session.proxy_task_state.tasks.len(), 0);

        while let Ok(task) = handle.rx.try_recv() {
            delivered.push(task);
        }

        assert!(matches!(delivered[0], HttpTask::Header(_, false)));
        let mut body_bytes = Vec::new();
        let mut saw_done = false;
        for task in &delivered[1..] {
            match task {
                HttpTask::Body(Some(b), false) => body_bytes.extend_from_slice(b),
                HttpTask::Done => {
                    assert!(!saw_done, "Done delivered more than once");
                    saw_done = true;
                }
                t => panic!("unexpected task in delivery: {t:?}"),
            }
        }
        assert!(saw_done, "expected Done to be delivered");
        assert_eq!(
            body_bytes, b"ABCDE",
            "body chunks must arrive exactly once, in order"
        );

        assert_eq!(session.body_bytes_sent(), 5);
    }

    /// `was_upgraded()` must remain false if the 101 send is cancelled before
    /// it reaches the subrequest channel.
    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_upgrade_consistency() {
        init_log();
        let (mut session, mut handle) = build_upgrade_req("websocket", "Upgrade").await;
        assert!(session.is_upgrade_req());
        session.set_proxy_tasks_enabled(true);

        // Four 1xx headers fill the upstream channel; the 101 then blocks.
        for _ in 0..4 {
            session.send_proxy_task(HttpTask::Header(
                Box::new(test_header(StatusCode::CONTINUE)),
                false,
            ));
        }
        let mut h101 = test_header(StatusCode::SWITCHING_PROTOCOLS);
        h101.set_version(http::Version::HTTP_11);
        h101.insert_header("upgrade", "websocket")
            .expect("test operation should succeed");
        h101.insert_header("connection", "Upgrade")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(h101), false));

        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(5)) => {}
            _ = session.write_proxy_tasks() => panic!("expected reserve to be cancelled"),
        };

        assert!(
            !session.was_upgraded(),
            "was_upgraded must remain false until the 101 send actually completes"
        );

        for _ in 0..4 {
            recv_task(&mut handle.rx);
        }
        session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(
            session.was_upgraded(),
            "after the 101 send completes, was_upgraded must be true"
        );
    }

    /// Same upgrade consistency check for the regular `write_response_header`
    /// path, which also awaits on the subrequest output channel.
    #[tokio::test(start_paused = true)]
    async fn test_write_response_header_upgrade_cancel_consistency() {
        init_log();
        let (mut session, mut handle) = build_upgrade_req("websocket", "Upgrade").await;

        for _ in 0..4 {
            session
                .write_response_header(Box::new(test_header(StatusCode::CONTINUE)))
                .await
                .expect("test operation should succeed");
        }

        let mut h101 = test_header(StatusCode::SWITCHING_PROTOCOLS);
        h101.set_version(http::Version::HTTP_11);
        h101.insert_header("upgrade", "websocket")
            .expect("test operation should succeed");
        h101.insert_header("connection", "Upgrade")
            .expect("test operation should succeed");

        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(5)) => {}
            _ = session.write_response_header(Box::new(h101.clone())) => {
                panic!("expected header send to be cancelled")
            }
        };
        assert!(!session.was_upgraded());
        assert_eq!(session.body_writer.body_mode, BodyMode::ToSelect);

        for _ in 0..4 {
            recv_task(&mut handle.rx);
        }
        session
            .write_response_header(Box::new(h101))
            .await
            .expect("test async operation should succeed");
        assert!(session.was_upgraded());
    }

    /// Trailers are dispatched correctly through `write_proxy_tasks`.
    /// Matching regular `response_duplex_vec`, a final `Done` follows Trailer.
    #[tokio::test]
    async fn test_proxy_task_trailers() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("hi")), false));
        let mut trailers = http::HeaderMap::new();
        trailers.insert("x-final", http::HeaderValue::from_static("done"));
        session.send_proxy_task(HttpTask::Trailer(Some(Box::new(trailers))));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(end);

        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Body(..)));
        match recv_task(&mut handle.rx) {
            HttpTask::Trailer(Some(t)) => {
                assert_eq!(
                    t.get("x-final").expect("test trailer should be present"),
                    "done"
                );
            }
            t => panic!("expected Trailer, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_trailer_before_content_length_complete_errors() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "5")
            .expect("test content-length header is valid");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        let mut trailers = HeaderMap::new();
        trailers.insert("x-final", http::HeaderValue::from_static("done"));
        session.send_proxy_task(HttpTask::Trailer(Some(Box::new(trailers))));

        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("trailers before content-length body completion should error");
        assert_eq!(err.etype(), &PREMATURE_BODY_END);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Trailer(..)));
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_trailer_cancel_safety() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        for _ in 0..4 {
            session.send_proxy_task(HttpTask::Header(
                Box::new(test_header(StatusCode::CONTINUE)),
                false,
            ));
        }
        let mut trailers = HeaderMap::new();
        trailers.insert("x-final", http::HeaderValue::from_static("done"));
        session.send_proxy_task(HttpTask::Trailer(Some(Box::new(trailers))));

        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(5)) => {}
            _ = session.write_proxy_tasks() => panic!("expected trailer reserve to be cancelled"),
        };

        let mut prefix = Vec::new();
        while let Ok(task) = handle.rx.try_recv() {
            prefix.push(task);
        }
        assert_eq!(prefix.len(), 4);
        assert!(prefix.iter().all(|t| matches!(t, HttpTask::Header(..))));
        let end = session
            .write_proxy_tasks()
            .await
            .expect("resume after trailer cancellation should complete");
        assert!(end);

        match recv_task(&mut handle.rx) {
            HttpTask::Trailer(Some(t)) => {
                assert_eq!(t.get("x-final").expect("trailer present"), "done")
            }
            t => panic!("expected Trailer after resume, got {t:?}"),
        }
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_trailer_cancel_safety_after_body() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        for _ in 0..3 {
            session.send_proxy_task(HttpTask::Body(Some(Bytes::from("x")), false));
        }
        let mut trailers = HeaderMap::new();
        trailers.insert("x-final", http::HeaderValue::from_static("done"));
        session.send_proxy_task(HttpTask::Trailer(Some(Box::new(trailers))));

        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(5)) => {}
            _ = session.write_proxy_tasks() => panic!("expected trailer reserve to be cancelled"),
        };

        let mut prefix = Vec::new();
        while let Ok(task) = handle.rx.try_recv() {
            prefix.push(task);
        }
        assert_eq!(prefix.len(), 4);
        assert!(matches!(prefix[0], HttpTask::Header(..)));
        assert!(prefix[1..].iter().all(|t| matches!(t, HttpTask::Body(..))));
        let end = session
            .write_proxy_tasks()
            .await
            .expect("resume after body trailer cancellation should complete");
        assert!(end);

        match recv_task(&mut handle.rx) {
            HttpTask::Trailer(Some(t)) => {
                assert_eq!(t.get("x-final").expect("trailer present"), "done")
            }
            t => panic!("expected Trailer after resume, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
    }

    /// `body_bytes_sent` is only incremented after the synchronous
    /// `Permit::send`, not on a cancelled `reserve().await`.
    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_body_counter_no_double_count() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "12")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("AAAA")), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("BBBB")), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("CCCC")), true));

        let mut received_body_bytes = Vec::new();
        loop {
            if !session.has_pending_proxy_tasks() {
                break;
            }
            tokio::select! {
                biased;
                _ = tokio::time::sleep(Duration::from_millis(5)) => {
                    while let Ok(task) = handle.rx.try_recv() {
                        if let HttpTask::Body(Some(b), _) = &task {
                            received_body_bytes.extend_from_slice(b);
                        }
                    }
                    assert!(
                        session.body_bytes_sent() <= received_body_bytes.len(),
                        "body_bytes_sent ({}) must not exceed bytes actually delivered ({})",
                        session.body_bytes_sent(),
                        received_body_bytes.len(),
                    );
                }
                result = session.write_proxy_tasks() => {
                    result.expect("test operation should succeed");
                }
            }
        }

        while let Ok(task) = handle.rx.try_recv() {
            if let HttpTask::Body(Some(b), _) = &task {
                received_body_bytes.extend_from_slice(b);
            }
        }

        assert_eq!(session.body_bytes_sent(), 12);
        assert_eq!(&received_body_bytes[..], b"AAAABBBBCCCC");
    }

    /// Cancelling while reserving capacity for the final Done must leave the
    /// finish operation resumable.
    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_finish_cancel_safety() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("a")), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("b")), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("c")), true));

        // Header + three bodies fill the 4-slot channel. The final Done
        // reserve blocks and is cancelled.
        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(5)) => {}
            _ = session.write_proxy_tasks() => panic!("expected finish reserve to be cancelled"),
        };
        assert!(session.proxy_task_state.finish_in_progress);

        let first = recv_task(&mut handle.rx);
        assert!(matches!(first, HttpTask::Header(..)));

        // Only one slot is available. Resuming must emit exactly one Done and
        // return without trying to reserve a second slot.
        let end = tokio::time::timeout(Duration::from_millis(5), session.write_proxy_tasks())
            .await
            .expect("resume should not need a second channel slot")
            .expect("test operation should succeed");
        assert!(end);

        let mut delivered = Vec::new();
        while let Ok(task) = handle.rx.try_recv() {
            delivered.push(task);
        }
        assert_eq!(delivered.len(), 4);
        assert!(matches!(delivered.last(), Some(HttpTask::Done)));
        assert_eq!(
            delivered
                .iter()
                .filter(|t| matches!(t, HttpTask::Done))
                .count(),
            1
        );
    }

    #[tokio::test]
    async fn test_proxy_task_done_only_noops_without_channel() {
        init_log();
        let (mut session, _handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        session.send_proxy_task(HttpTask::Done);
        session.shutdown();

        let end = session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(end);
    }

    #[tokio::test]
    async fn test_proxy_task_done_only_noops_with_live_channel() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        session.send_proxy_task(HttpTask::Done);

        let end = session
            .write_proxy_tasks()
            .await
            .expect("Done-only proxy task should complete without channel output");
        assert!(end);
        assert!(!session.has_pending_proxy_tasks());
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_header_only_end() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::NO_CONTENT);
        session.send_proxy_task(HttpTask::Header(Box::new(header), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(end);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_head_response_drops_body() {
        init_log();
        let (mut session, mut handle) = build_head_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "10")
            .expect("test content-length header is valid");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("not-sent")), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("HEAD proxy task response should complete");
        assert!(end);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
        assert_eq!(session.body_bytes_sent(), 0);
    }

    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_duplicate_final_header_does_not_reserve() {
        init_log();
        let (mut session, _handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");

        for _ in 0..3 {
            session.send_proxy_task(HttpTask::Body(Some(Bytes::from("x")), false));
        }
        let duplicate = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(duplicate), false));

        tokio::time::timeout(Duration::from_millis(5), session.write_proxy_tasks())
            .await
            .expect("duplicate final header should be dropped without reserving")
            .expect("test operation should succeed");
        assert!(!session.has_pending_proxy_tasks());
    }

    #[tokio::test]
    async fn test_proxy_task_duplicate_final_header_preserves_end() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        recv_task(&mut handle.rx);

        let duplicate = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(duplicate), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(end);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_failed_propagates_without_sending_later_tasks() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        session.send_proxy_task(HttpTask::Failed(Error::explain(InternalError, "boom")));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("late")), true));

        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("Failed proxy task should propagate error");
        assert_eq!(err.etype(), &InternalError);
        assert!(session.has_pending_proxy_tasks());
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_failed_clears_sticky_eos() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(None, true));
        session.send_proxy_task(HttpTask::Failed(Error::explain(InternalError, "boom")));

        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("Failed after EOS should still propagate error");
        assert_eq!(err.etype(), &InternalError);
        assert!(!session.has_pending_proxy_tasks());
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        assert_rx_empty(&mut handle.rx);

        let end = session
            .write_proxy_tasks()
            .await
            .expect("retry after Failed should not emit stale Done");
        assert!(!end);
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_body_before_header_errors() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("body")), true));

        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("body before response header should be rejected");
        assert_eq!(err.etype(), &InternalError);
        assert!(!session.has_pending_proxy_tasks());
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_none_body_does_not_reserve() {
        init_log();
        let (mut session, _handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        for _ in 0..3 {
            session.send_proxy_task(HttpTask::Body(Some(Bytes::from("x")), false));
        }
        session.send_proxy_task(HttpTask::Body(None, false));

        tokio::time::timeout(Duration::from_millis(5), session.write_proxy_tasks())
            .await
            .expect("Body(None) should not reserve channel capacity")
            .expect("test operation should succeed");
        assert!(!session.has_pending_proxy_tasks());
    }

    #[tokio::test(start_paused = true)]
    async fn test_proxy_task_no_data_eos_survives_cancellation() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        for _ in 0..3 {
            session.send_proxy_task(HttpTask::Body(Some(Bytes::from("x")), false));
        }
        session.send_proxy_task(HttpTask::Body(None, true));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::new()), true));
        // This later body blocks on the full channel after the no-data EOS
        // task has been consumed. The sticky EOS flag must survive that cancel.
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("y")), false));

        tokio::select! {
            biased;
            _ = tokio::time::sleep(Duration::from_millis(5)) => {}
            _ = session.write_proxy_tasks() => panic!("expected reserve after no-data EOS to be cancelled"),
        };

        let mut prefix = Vec::new();
        while let Ok(task) = handle.rx.try_recv() {
            prefix.push(task);
        }
        assert_eq!(prefix.len(), 4);
        assert!(matches!(prefix[0], HttpTask::Header(..)));
        assert!(prefix[1..].iter().all(|t| matches!(t, HttpTask::Body(..))));
        let end = session
            .write_proxy_tasks()
            .await
            .expect("resume after no-data EOS cancellation should complete");
        assert!(end);

        let mut delivered = Vec::new();
        while let Ok(task) = handle.rx.try_recv() {
            delivered.push(task);
        }
        assert_eq!(delivered.len(), 2);
        match &delivered[0] {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"y"),
            t => panic!("expected Body(y) after resume, got {t:?}"),
        }
        assert!(matches!(delivered[1], HttpTask::Done));
        assert_eq!(session.body_bytes_sent(), 4);
    }

    #[tokio::test]
    async fn test_proxy_task_content_length_overrun_truncates() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "3")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("abcdef")), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");
        assert!(end);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        match recv_task(&mut handle.rx) {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"abc"),
            t => panic!("expected truncated Body, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
        assert_eq!(session.body_bytes_sent(), 3);
    }

    #[tokio::test]
    async fn test_proxy_task_exact_content_length_without_end_sends_done() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "3")
            .expect("test content-length header is valid");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("abc")), false));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("exact content-length proxy task response should complete");
        assert!(end);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        match recv_task(&mut handle.rx) {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"abc"),
            t => panic!("expected exact content-length Body, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    async fn test_proxy_task_late_tasks_after_finished_are_dropped() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "3")
            .expect("test content-length header is valid");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("abc")), true));
        session
            .write_proxy_tasks()
            .await
            .expect("initial response should complete");

        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Body(..)));
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));

        let mut trailers = HeaderMap::new();
        trailers.insert("x-late", http::HeaderValue::from_static("ignored"));
        session.send_proxy_task(HttpTask::Trailer(Some(Box::new(trailers))));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("late")), true));
        session.send_proxy_task(HttpTask::Failed(Error::explain(InternalError, "late")));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("late tasks after finished stream should be dropped");
        assert!(end);
        assert_rx_empty(&mut handle.rx);
        assert!(!session.has_pending_proxy_tasks());
    }

    #[tokio::test]
    async fn test_proxy_task_chunked_header_uses_until_close() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("transfer-encoding", "chunked")
            .expect("test transfer-encoding header is valid");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("chunk")), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("chunked proxy task response should complete");
        assert!(end);
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        match recv_task(&mut handle.rx) {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"chunk"),
            t => panic!("expected chunked body task, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_eq!(session.body_bytes_sent(), 5);
    }

    #[tokio::test]
    async fn test_proxy_task_premature_content_length_errors_before_done() {
        init_log();
        let (mut session, mut handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        let mut header = test_header(StatusCode::OK);
        header
            .insert_header("content-length", "5")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("hi")), true));

        let err = session
            .write_proxy_tasks()
            .await
            .expect_err("premature content-length should error before Done");
        assert_eq!(err.etype(), &PREMATURE_BODY_END);
        assert_eq!(session.body_writer.body_mode, BodyMode::Complete(2));
        assert!(!session.has_pending_proxy_tasks());
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        match recv_task(&mut handle.rx) {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"hi"),
            t => panic!("expected body before premature end, got {t:?}"),
        }
        assert_rx_empty(&mut handle.rx);
    }

    #[tokio::test]
    #[should_panic(
        expected = "Unexpected UpgradedBody task received on un-upgraded downstream session"
    )]
    async fn test_upgraded_body_on_non_upgraded_session_panics() {
        init_log();
        let (mut session, _handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);
        assert!(!session.was_upgraded());

        let header = test_header(StatusCode::OK);
        session.send_proxy_task(HttpTask::Header(Box::new(header), false));
        session.send_proxy_task(HttpTask::UpgradedBody(Some(Bytes::from("ws")), true));

        let _ = session.write_proxy_tasks().await;
    }

    #[tokio::test]
    #[should_panic(expected = "Unexpected Body task received on upgraded downstream session")]
    async fn test_body_on_upgraded_session_panics() {
        init_log();
        let (mut session, _handle) = build_upgrade_req("websocket", "Upgrade").await;
        session.set_proxy_tasks_enabled(true);

        let mut h101 = test_header(StatusCode::SWITCHING_PROTOCOLS);
        h101.set_version(http::Version::HTTP_11);
        h101.insert_header("upgrade", "websocket")
            .expect("test operation should succeed");
        h101.insert_header("connection", "Upgrade")
            .expect("test operation should succeed");
        session.send_proxy_task(HttpTask::Header(Box::new(h101), false));
        session
            .write_proxy_tasks()
            .await
            .expect("test async operation should succeed");

        session.send_proxy_task(HttpTask::Body(Some(Bytes::from("plain")), true));
        let _ = session.write_proxy_tasks().await;
    }

    #[tokio::test]
    async fn test_proxy_task_upgraded_body_happy_path() {
        init_log();
        let (mut session, mut handle) = build_upgrade_req("websocket", "Upgrade").await;
        session.set_proxy_tasks_enabled(true);

        let mut h101 = test_header(StatusCode::SWITCHING_PROTOCOLS);
        h101.set_version(http::Version::HTTP_11);
        h101.insert_header("upgrade", "websocket")
            .expect("test upgrade header is valid");
        h101.insert_header("connection", "Upgrade")
            .expect("test connection header is valid");
        session.send_proxy_task(HttpTask::Header(Box::new(h101), false));
        session.send_proxy_task(HttpTask::UpgradedBody(Some(Bytes::from("ws")), true));

        let end = session
            .write_proxy_tasks()
            .await
            .expect("upgraded proxy task response should complete");
        assert!(end);
        assert!(session.was_upgraded());
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Header(..)));
        match recv_task(&mut handle.rx) {
            HttpTask::Body(Some(b), false) => assert_eq!(&b[..], b"ws"),
            t => panic!("expected upgraded body task, got {t:?}"),
        }
        assert!(matches!(recv_task(&mut handle.rx), HttpTask::Done));
        assert_eq!(session.body_bytes_sent(), 2);
    }

    #[tokio::test]
    #[should_panic(
        expected = "Unexpected UpgradedBody task received on un-upgraded downstream session"
    )]
    async fn test_upgraded_body_on_non_upgraded_session_panics_while_full() {
        init_log();
        let (mut session, _handle) = build_req().await;
        session.set_proxy_tasks_enabled(true);

        for _ in 0..4 {
            session.send_proxy_task(HttpTask::Header(
                Box::new(test_header(StatusCode::CONTINUE)),
                false,
            ));
        }
        session.send_proxy_task(HttpTask::UpgradedBody(Some(Bytes::from("ws")), true));
        let _ = session.write_proxy_tasks().await;
    }
}