ostrya-fetch 0.19.1

Async HTTP/1.1 and HTTP/2 fetcher for ostrya, with TLS over rustls
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
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]

//! An async HTTP/1.1 and HTTP/2 client over rustls, for the mirrors of a remote.
//!
//! A caller builds a [`Fetcher`] from the mirrors, headers, credentials, proxy,
//! TLS configuration, retry limits, and deadlines of one remote. The fetcher
//! downloads files with `GET` and sends uploads with `POST`, `PUT`, or
//! `DELETE`. A response body streams in bounded chunks, so no object is held
//! whole in memory.
//!
//! The [`Target`] of a request is a path under each mirror, a path with a
//! query string under each mirror, or an absolute URL. The fetcher sends a
//! path and a query as written, so the caller escapes them. A fetcher with a
//! [`RequestSigner`] sends a path in the form that
//! [`RequestSigner::request_path`] gives.
//!
//! A failed fetch gives the status of the response. On request, it also gives
//! the headers and a capped body of that response.
//!
//! # Entry points
//!
//! - [`Fetcher::new`] builds a fetcher from [`FetcherOptions`].
//! - [`Fetcher::fetch`] sends a [`FetchRequest`] and returns a [`Fetched`].
//! - [`ErrorResponse`] holds the headers and the capped body of an
//!   unsuccessful response, if the request asks for them.
//! - [`Body`] streams a response body in bounded chunks.
//! - [`Fetcher::refetching`] returns a [`Refetch`], which fetches a failed body again.
//! - [`Fetcher::upload`] sends an [`UploadRequest`] and returns an [`Uploaded`].
//! - [`UploadBody`] gives an upload body whole or streams it from an [`UploadWriter`].
//! - [`RequestSigner`] signs each attempt through [`FetcherOptions::signer`],
//!   and gives the form of each request path.
//! - [`Error`] is the error of each fallible operation of the crate.
//!
//! # Modules
//!
//! - [`gate`]: the priority admission gate that bounds concurrent work.
//!
//! # Features
//!
//! - `smol` (default): the `smol` backend of `ostrya-rt`.
//! - `tokio`: the `tokio` backend of `ostrya-rt`, with tokio I/O impls on [`Body`] and [`UploadWriter`].
//!
//! # Examples
//!
//! ```no_run
//! use futures_lite::AsyncReadExt;
//! use ostrya_fetch::{FetchRequest, Fetched, Fetcher, FetcherOptions};
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let fetcher = Fetcher::new(FetcherOptions::new("https://example.com/repo")).await?;
//! if let Fetched::Body(mut body) = fetcher.fetch(FetchRequest::path("config")).await? {
//!     let mut config = Vec::new();
//!     body.read_to_end(&mut config).await?;
//! }
//! # Ok(())
//! # }
//! ```

use std::borrow::Cow;
use std::collections::HashMap;
use std::future::Future;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::{Duration, Instant};

use futures_io::{AsyncRead, AsyncWrite};
use hyper::body::{Body as _, Bytes, Frame, Incoming, SizeHint};
use hyper::header::{HeaderName, HeaderValue};
use hyper::http::uri::Scheme;
use hyper::{Method, Request, Response, StatusCode, Uri, Version};
use ostrya_rt as rt;
use std::pin::Pin;
use std::task::{Context, Poll, Waker, ready};

mod error;
pub mod gate;
mod io;
mod socks;
mod tls;

pub use self::error::{Error, Result};

use gate::{Gate, Permit};
#[doc(hidden)]
pub use io::{FuturesIo, RtExecutor, RtTimer, WriteVectored};
use tls::{ClientConfigs, client_config};
pub use tls::{ClientIdentity, TlsOptions, TrustRoots};
#[doc(hidden)]
pub use tls::{client_cert_verifier, server_config};

/// The `User-Agent` value of each request. A `User-Agent` header of the fetcher
/// or of the request replaces it.
const USER_AGENT: &str = concat!("ostrya/", env!("CARGO_PKG_VERSION"));

/// The one content coding that a fetch accepts, which is no coding. Each
/// request asks for it, and a response that declares another coding fails the
/// attempt. An `Accept-Encoding` header of the fetcher or of the request
/// replaces the value that the fetcher sends.
const IDENTITY: &str = "identity";

/// The one transfer coding that a response can declare. It frames a message,
/// and the connection removes the framing, so the caller gets the body as the
/// remote wrote it. The fetcher sends no `TE` header, so another transfer coding
/// is a server fault. A response that declares one fails the attempt.
const CHUNKED: &str = "chunked";

/// The variables that [`Proxy::Environment`] reads, in the order of preference
/// of a lookup.
///
/// The list holds the lower-case name of each variable, and the upper-case name
/// where the lookup reads one. Nothing reads `HTTP_PROXY`. A CGI gateway gives a
/// request header `Proxy` to the program as `HTTP_PROXY`, so a client can set
/// that variable with a request.
const PROXY_VARIABLES: [&str; 7] = [
    "http_proxy",
    "https_proxy",
    "HTTPS_PROXY",
    "all_proxy",
    "ALL_PROXY",
    "no_proxy",
    "NO_PROXY",
];

/// The scheme of an HTTP proxy URL. An HTTP proxy connection opens with this
/// scheme alone.
const PROXY_SCHEME: &str = "http://";

/// The scheme of a SOCKS5 proxy URL whose client resolves the origin host.
const SOCKS5_SCHEME: &str = "socks5://";

/// The scheme of a SOCKS5 proxy URL whose proxy resolves the origin host.
const SOCKS5H_SCHEME: &str = "socks5h://";

/// The statuses at which a fetch follows a redirect. A fetch is a GET, so none
/// of the five changes the method of the next hop. The statuses that differ in
/// the method are one case here. An upload follows [`UPLOAD_REDIRECTS`] alone.
const REDIRECTS: [StatusCode; 5] = [
    StatusCode::MOVED_PERMANENTLY,
    StatusCode::FOUND,
    StatusCode::SEE_OTHER,
    StatusCode::TEMPORARY_REDIRECT,
    StatusCode::PERMANENT_REDIRECT,
];

/// The statuses at which an upload follows a redirect. These two keep the
/// method and the body, so the next hop sends the same request again.
const UPLOAD_REDIRECTS: [StatusCode; 2] = [
    StatusCode::TEMPORARY_REDIRECT,
    StatusCode::PERMANENT_REDIRECT,
];

/// The message for a layer that sets both credentials and an `Authorization`
/// header. The layer does not state which of the two the request sends. A
/// choice of one sends a credential that the caller did not choose.
const AMBIGUOUS_AUTHORIZATION: &str =
    "basic-auth credentials and an authorization header both set Authorization: pass one of them";

/// The message for a layer that sets both a bearer token and an
/// `Authorization` header, for the reason that [`AMBIGUOUS_AUTHORIZATION`]
/// states.
const AMBIGUOUS_BEARER: &str =
    "a bearer token and an authorization header both set Authorization: pass one of them";

/// The message for a layer that sets both basic-auth credentials and a bearer
/// token, for the reason that [`AMBIGUOUS_AUTHORIZATION`] states.
const BASIC_AND_BEARER: &str =
    "basic-auth credentials and a bearer token both set Authorization: pass one of them";

/// The message for a fetcher that has a request signer, and basic-auth
/// credentials on the fetcher or on a request. A signer sets the headers of
/// its signature, which can include `Authorization`, so the reason of
/// [`AMBIGUOUS_AUTHORIZATION`] applies.
const SIGNER_AND_BASIC: &str =
    "a request signer and basic-auth credentials both authorize the request: pass one of them";

/// The message for a fetcher that has a request signer, and a bearer token on
/// the fetcher or on a request, for the reason that [`SIGNER_AND_BASIC`]
/// states.
const SIGNER_AND_BEARER: &str =
    "a request signer and a bearer token both authorize the request: pass one of them";

/// The message for a fetcher that has a request signer, and an
/// `Authorization` header on the fetcher or on a request, for the reason that
/// [`SIGNER_AND_BASIC`] states.
const SIGNER_AND_AUTHORIZATION: &str =
    "a request signer and an authorization header both authorize the request: pass one of them";

/// The message for a layer that sets a `Host` header. The header states the
/// authority of the destination, and the fetcher reads it from the URL.
const HOST_COMES_FROM_THE_URL: &str = "the host header is set from the url the request is sent to";

/// The header names that the connection layer sets.
///
/// These names frame a request, or state what happens to the connection that
/// carries it. A value of the caller puts the wire and the connection pool out
/// of step. For example, a `Content-Length` of the caller ends the HTTP/1.1
/// connection under a pooled sender. The next fetch over that fetcher then
/// fails on a channel that the connection task dropped.
const CONNECTION_HEADERS: [&str; 9] = [
    "connection",
    "content-length",
    "expect",
    "keep-alive",
    "proxy-connection",
    "te",
    "trailer",
    "transfer-encoding",
    "upgrade",
];

/// The size of the frames of an upload body.
///
/// The writer fills one frame while the slot holds the frame that it handed
/// over last, so the writer holds at most two frames. hyper takes a body given
/// whole in frames of this size.
///
/// The connection buffers more of the body. Over HTTP/1.1, hyper takes another
/// frame while it holds fewer than 16 frames and less than 408 KiB. Over HTTP/2,
/// it holds up to two frames.
const UPLOAD_FRAME: usize = 64 * 1024;

/// The size under which a flush hands over a copy of its part-filled frame and
/// keeps the buffer. So a small frame that waits in the connection does not hold
/// a whole frame of memory.
const SMALL_FRAME: usize = 4 * 1024;

/// The longest idle time in the pool of an HTTP/1.1 connection that an upload
/// takes. [`Inner::take_upload`] states why an upload takes only a connection
/// that became idle a short time ago.
const UPLOAD_IDLE: Duration = Duration::from_secs(2);

/// The largest declared response body that a failed attempt reads to the end,
/// so that its HTTP/1.1 connection goes back to the pool. If the declared length
/// is larger, or if the response declares no length, the fetcher closes the
/// connection.
const DRAIN_LIMIT: u64 = 64 * 1024;

/// The largest capacity that a kept error body reserves before its bytes
/// arrive: 64 KiB. The declared length of the body is a claim of the server,
/// so it does not size an allocation.
const CAPTURE_RESERVE: u64 = 64 * 1024;

/// The HTTP/2 flow-control window of each stream. The default of hyper is
/// 64 KiB, which limits the throughput of one stream on a link with a high
/// bandwidth-delay product. An object fetch is one stream, so this window limits
/// it.
const H2_STREAM_WINDOW: u32 = 2 * 1024 * 1024;

/// The largest flow-control window the protocol allows, 2^31 - 1.
const H2_MAX_WINDOW: u32 = i32::MAX as u32;

/// How often an HTTP/2 connection with an open stream pings its peer, and how
/// long it waits for the reply. Both values are less than the default
/// [`progress_timeout`](FetcherOptions::progress_timeout). So the ping reports a
/// peer that went away, and no read waits for ever.
const H2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(15);
const H2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(15);

/// The priority of a request that waits for admission to a [`Fetcher`].
///
/// If [`max_outstanding`](FetcherOptions::max_outstanding) requests are in
/// flight, a new request waits in a queue. The queue serves the highest
/// priority first, and requests of one priority in arrival order. The
/// [`gate`] module holds the queue.
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub enum Priority {
    /// The lowest priority, for bulk content.
    Low,
    /// The middle priority, which is the default.
    #[default]
    Normal,
    /// The highest priority, for metadata that other work waits for.
    High,
}

/// The HTTP version of a response.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Protocol {
    /// HTTP/1.1.
    Http11,
    /// HTTP/2.
    Http2,
}

/// The credentials of HTTP basic authentication.
///
/// The [`Debug`] output shows the user name and a fixed word in place of the
/// password. So the log of a struct that holds credentials does not show the
/// password.
#[derive(Clone, Eq, PartialEq)]
pub struct BasicAuth {
    /// The user name.
    pub user: String,
    /// The password.
    pub password: String,
}

impl std::fmt::Debug for BasicAuth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BasicAuth")
            .field("user", &self.user)
            .field("password", &"<redacted>")
            .finish()
    }
}

/// A token for `Authorization: Bearer`.
///
/// The fetcher sends the token as written, so the token must have the token68
/// syntax of HTTP authentication. This is one or more ASCII letters, digits,
/// `-`, `.`, `_`, `~`, `+`, or `/`, then any number of `=`. If the token has
/// another form, [`Error::Fetch`] occurs. A request fails before admission, and
/// [`FetcherOptions::bearer_token`] fails [`Fetcher::new`]. The message holds
/// no part of the token.
///
/// The [`Debug`] output shows a fixed word in place of the token. So the log of
/// a struct that holds a token does not show it.
#[derive(Clone, Eq, PartialEq)]
pub struct BearerToken {
    /// The token.
    pub token: String,
}

impl std::fmt::Debug for BearerToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BearerToken")
            .field("token", &"<redacted>")
            .finish()
    }
}

/// A signer that adds the headers of a request signature to each attempt.
///
/// [`FetcherOptions::signer`] holds the signer of a fetcher. The fetcher calls
/// [`sign`](RequestSigner::sign) once for each request that it sends to the
/// origin that the route names. This includes each retry, each round, and
/// each redirect hop at that origin. The call occurs after the fetcher takes
/// the connection, so a signature with a date is fresh when the request
/// leaves. An upload over a pooled HTTP/1.1 connection that the server closed
/// continues over a new connection, and the fetcher calls the signer again.
///
/// A redirect hop at another origin gets no signature. No later hop of that
/// attempt gets one, also a hop back to the named origin.
///
/// The fetcher calls the signer on the task of the request. A signer that
/// hashes a body given whole does that work on the task, on each call. A
/// caller with a large body gives the SHA-256 of the body with
/// [`UploadBody::bytes_with_sha256`], so that the signer gets
/// [`SignPayload::Sha256`] and hashes no byte of the body.
///
/// The path of a [`Target::Path`] or a [`Target::PathQuery`] goes through
/// [`request_path`](RequestSigner::request_path) before the fetcher adds it
/// to the base path of a mirror. So a signer that signs a canonical form of
/// the path gets a request that carries that form. A [`Target::Url`] goes on
/// the wire as written.
///
/// The [`Debug`] output of a signer must show no secret, because the
/// [`Debug`] output of [`FetcherOptions`] includes it.
pub trait RequestSigner: Send + Sync + std::fmt::Debug {
    /// Returns `path`, the path of a [`Target::Path`] or a
    /// [`Target::PathQuery`], in the form that the request carries.
    ///
    /// The fetcher calls this method once for each fetch and each upload
    /// with a path target, before admission. It adds the result to the base
    /// path of each mirror, in place of `path`. The fetcher does not call it
    /// for a [`Target::Url`], or for the query of a [`Target::PathQuery`].
    ///
    /// The default returns `path` unchanged. A signer whose signature covers
    /// a canonical form of the path returns that form, so the request
    /// carries the bytes that the signature covers.
    ///
    /// # Errors
    ///
    /// The fetcher passes an error of this method through unchanged, and the
    /// request ends with it before admission. Usually, the error is
    /// [`Error::Fetch`].
    fn request_path<'a>(&self, path: &'a str) -> Result<Cow<'a, str>> {
        Ok(Cow::Borrowed(path))
    }

    /// Returns the headers that sign `request`. Each one replaces a header of
    /// the same name.
    ///
    /// The fetcher refuses a returned header as it refuses a request header:
    /// an invalid name or value, a `Host` header, and a header that the
    /// connection layer sets. Each returned value is marked sensitive, so an
    /// HTTP/2 connection never adds it to its header compression table. Two
    /// returned headers of one name both go on the wire.
    ///
    /// # Errors
    ///
    /// The fetcher passes an error of this method through unchanged, and the
    /// request ends with it. The request sends nothing for the attempt, and
    /// the fetcher tries no other destination and no other round. An error on
    /// a redirect hop of an upload ends the upload with
    /// [`Error::UploadInterrupted`], because the first hop sent the request.
    /// Usually, the error is [`Error::Fetch`].
    fn sign(&self, request: &SignRequest<'_>) -> Result<Vec<(String, String)>>;
}

/// The parts of one attempt that a signer signs, as the request carries them.
#[derive(Clone, Copy, Debug)]
pub struct SignRequest<'a> {
    /// The method: `GET`, `POST`, `PUT`, or `DELETE`.
    pub method: &'a str,
    /// The `host[:port]` of the request, which is the `Host` header of an
    /// HTTP/1.1 request and the `:authority` of an HTTP/2 request.
    ///
    /// It has no default port of the scheme. An IPv6 literal keeps its
    /// brackets.
    pub authority: &'a str,
    /// The path of the request target, as the request carries it. It includes
    /// the base path of a mirror, and the escapes of the caller.
    pub path: &'a str,
    /// The query string of the request target, without the `?`.
    ///
    /// It is `None` if the target has no `?`. A target that ends in `?` has an
    /// empty query.
    pub query: Option<&'a str>,
    /// The body of the request.
    pub payload: SignPayload<'a>,
}

/// The body of a signed request.
///
/// The [`Debug`] output of [`Bytes`](SignPayload::Bytes) and of
/// [`Sha256`](SignPayload::Sha256) shows the length of the body, and not its
/// bytes.
#[derive(Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum SignPayload<'a> {
    /// No body: a `GET` or a `DELETE`.
    Empty,
    /// The whole body of a `POST` or a `PUT` that [`UploadBody::bytes`] gives.
    /// An empty body is also this variant.
    ///
    /// The signer gets the whole body on each attempt, and it runs on the task
    /// that sends the request. So the time to hash the body on that task grows
    /// with the size of the body. For a large body,
    /// [`UploadBody::bytes_with_sha256`] gives the signer the SHA-256 of the
    /// body as [`Sha256`](SignPayload::Sha256).
    Bytes(&'a [u8]),
    /// The whole body of a `POST` or a `PUT` that
    /// [`UploadBody::bytes_with_sha256`] gives, with the SHA-256 of the body
    /// that the caller computed.
    ///
    /// The fetcher does not check the digest. A wrong digest signs other
    /// bytes than the bytes that the request sends, and the server refuses
    /// the request.
    Sha256 {
        /// The whole body.
        bytes: &'a [u8],
        /// The SHA-256 of `bytes`, as the caller gave it.
        sha256: [u8; 32],
    },
    /// The body of a `POST` or a `PUT` that streams from an [`UploadWriter`].
    /// Its bytes are not known when the fetcher calls the signer.
    Streamed,
}

impl std::fmt::Debug for SignPayload<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SignPayload::Empty => f.write_str("Empty"),
            SignPayload::Bytes(bytes) => f.debug_tuple("Bytes").field(&bytes.len()).finish(),
            SignPayload::Sha256 { bytes, .. } => {
                f.debug_tuple("Sha256").field(&bytes.len()).finish()
            }
            SignPayload::Streamed => f.write_str("Streamed"),
        }
    }
}

/// The validators of a response, which make a later fetch conditional.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Validators {
    /// The `ETag` of the response, sent back as `If-None-Match`.
    pub etag: Option<String>,
    /// The `Last-Modified` of the response, sent back as `If-Modified-Since`.
    pub last_modified: Option<String>,
}

impl Validators {
    /// Returns `true` if no validator holds a value.
    pub fn is_empty(&self) -> bool {
        self.etag.is_none() && self.last_modified.is_none()
    }
}

/// The lowest transfer rate that a fetch accepts.
///
/// The fetcher samples the rate of a transfer once a second. Each sample is the
/// bytes of the last five seconds divided by five. In the first five seconds, a
/// sample is the bytes so far divided by the whole seconds so far.
///
/// If the rate stays at `limit` or less for `time` without a break, the
/// transfer fails as a transport failure. A transfer starts under the limit,
/// because it carried no bytes. A sample over the limit starts the count of
/// `time` again. For the response head, the count starts at the start of the
/// attempt, and for the body, at the first read.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LowSpeed {
    /// The rate in bytes per second that a transfer must keep.
    ///
    /// A zero fails [`Fetcher::new`].
    pub limit: u32,
    /// The longest time that the rate can stay at `limit` or less.
    ///
    /// A zero fails [`Fetcher::new`]. The fetcher samples the rate once a
    /// second, so a part of a second counts as a whole second.
    pub time: Duration,
}

impl LowSpeed {
    /// Returns `time` rounded up to whole seconds. The addition saturates at the
    /// maximum and does not wrap.
    fn whole_seconds(&self) -> Duration {
        let partial = u64::from(self.time.subsec_nanos() > 0);
        Duration::from_secs(self.time.as_secs().saturating_add(partial))
    }
}

/// How often the low-speed rule samples the rate of a body.
const SAMPLE_PERIOD: Duration = Duration::from_secs(1);

/// The number of samples over which the low-speed rule measures the rate.
const RATE_SPAN: usize = 5;

/// The proxy through which a [`Fetcher`] reaches each origin.
///
/// [`Fetcher::new`] resolves each form once, and refuses a proxy URL that the
/// fetcher cannot connect through. A request does not read the environment
/// again.
///
/// The [`Debug`] output does not show the userinfo of a proxy URL. So the log
/// of a struct that holds proxy credentials does not show them.
///
/// # Variables
///
/// `http_proxy` serves `http` origins, and `https_proxy` serves `https`
/// origins. If the variable of the scheme is unset, `all_proxy` serves the
/// origin. The fetcher also reads each name in upper case, except `HTTP_PROXY`.
/// A CGI gateway gives a request header `Proxy` to the program as `HTTP_PROXY`,
/// so a client can set that variable with a request.
///
/// The lower-case name has priority over the upper-case name. An empty value
/// counts as unset.
///
/// # White space
///
/// If the value of `http_proxy`, `https_proxy`, or `all_proxy`, or of an
/// upper-case name, has white space at its start or end, [`Fetcher::new`]
/// fails with [`Error::Unsupported`]. A value of white space alone also fails.
/// It is not empty, so it counts as set and hides the upper-case name.
///
/// The fetcher checks the value of each variable that the lookup reads, when it
/// builds the fetcher. It checks a variable also if no request goes through
/// that proxy. The message names the variable and holds no part of the value,
/// which can hold a password. The fetcher trims the URL of [`Proxy::Url`] and
/// each `no_proxy` entry.
///
/// # Proxy URL
///
/// A proxy URL has the form `http://host[:port]`, `socks5://host[:port]`, or
/// `socks5h://host[:port]`. The port is 80 by default for `http://` and 1080
/// for `socks5://` and `socks5h://`. The URL has no path other than `/`, no
/// query, and no fragment. For an `http://` proxy, the fetcher percent-decodes
/// the userinfo and sends it to the proxy as `Proxy-Authorization: Basic`.
/// [SOCKS proxies](Proxy#socks-proxies) states the rules of a SOCKS5 proxy.
///
/// Any other form fails [`Fetcher::new`] with [`Error::Unsupported`], for
/// example an `https://` or a `socks4://` proxy. This applies to a URL from the
/// options and to a URL from the environment. The message names the value
/// without its userinfo.
///
/// # Exemptions
///
/// `no_proxy` is a comma-separated list of the origins that the two
/// environment forms exempt. An entry holds a host text, with one leading `.`
/// if it is written with one, and `:port` if it names a port. The fetcher
/// ignores the space around an entry. An entry that names no host exempts
/// nothing, for example an empty entry, `.`, or `:8080`.
///
/// If the port text of an entry is not a number from 0 to 65535, the whole
/// entry is one host text. No origin holds that text, so the entry exempts
/// nothing.
///
/// `*` as a whole entry exempts all hosts. It is the one wildcard that the list
/// reads. `*.example.com` is a host text, and no origin holds it.
///
/// An entry matches an origin if it equals the host, or if the host ends with a
/// `.` and the entry. The fetcher removes one leading `.` from the entry first,
/// so `.example.com` and `example.com` both exempt `a.example.com`. A second
/// leading `.` is part of the host text of the entry. An entry with `:port`
/// matches that port alone.
///
/// The match compares the host text of the URL, with ASCII case ignored. It
/// never compares the address of the host. So `localhost` exempts no origin
/// written as `127.0.0.1`, and an entry that is an IP literal matches that text
/// alone.
///
/// An entry in CIDR notation names no network. The fetcher reads it as a host
/// text, which no origin holds. This is a divergence from curl 7.86 and later,
/// which read such an entry as a network. An empty `no_proxy` value exempts
/// nothing, and [`Proxy::Url`] reads no exemptions.
///
/// # Cleartext origins
///
/// The fetcher reaches a cleartext origin behind an `http://` proxy over a
/// connection to the proxy. The request carries the absolute-form target, the whole
/// `http://host/path` URL, and the `Host` header of the origin. Such a
/// connection uses HTTP/1.1, because cleartext HTTP/2 needs prior knowledge or
/// an upgrade.
///
/// The pool keeps such a connection under the proxy endpoint, so one proxy
/// connection carries requests for all cleartext origins. The pool key marks a
/// connection as a proxy connection. So the pool never gives a direct
/// connection to the proxy endpoint to a proxied request, or the reverse. The
/// two request forms differ, and a server answers the unexpected form with a
/// 404.
///
/// # TLS origins
///
/// The fetcher reaches a TLS origin behind an `http://` proxy over a `CONNECT`
/// tunnel. The
/// request names the target as `host:port`. The port is always written, and an
/// IPv6 literal keeps its brackets. If the proxy URL holds userinfo, the
/// `CONNECT` carries the same `Proxy-Authorization`.
///
/// On a 2xx answer, the fetcher gets the tunneled socket back. The TLS
/// handshake, the ALPN selection, and the pool entry are then the same as for a
/// direct connection to that origin. Nothing follows a `CONNECT` response, so a
/// byte that arrives before the client sends fails the connect.
///
/// A non-2xx answer is a retryable failure, and a 407 is a definitive failure.
/// Both are [`Error::Fetch`] and name the proxy.
/// [`connect_timeout`](FetcherOptions::connect_timeout) is the limit for all
/// these steps together: the connect to the proxy, the `CONNECT` exchange, the
/// TLS handshake, and the HTTP handshake.
///
/// # SOCKS proxies
///
/// A `socks5://` or a `socks5h://` URL names a SOCKS5 proxy (RFC 1928). With
/// `socks5://`, the fetcher resolves the host name of the origin and sends an
/// address to the proxy. With `socks5h://`, the fetcher sends the host name
/// of the origin to the proxy, and the proxy resolves the name. If the origin
/// host is an IP address, the fetcher sends the address under both schemes.
/// `socks4://` and `socks4a://` fail [`Fetcher::new`] with
/// [`Error::Unsupported`].
///
/// The fetcher opens a connection to the proxy and asks the proxy to connect
/// to the origin. Then the connection carries the bytes of the origin. A
/// cleartext origin and a TLS origin use this one way. The TLS handshake, the
/// ALPN selection, the request, and the pool entry are the same as for a
/// direct connection to the origin. So the request has the origin form, and
/// the proxy reads no HTTP. A `Proxy-Authorization` header of the caller goes
/// to the origin.
///
/// If the URL has no userinfo, the fetcher offers the method "no
/// authentication" alone. If the URL has userinfo, the fetcher also offers the
/// user name and password method of RFC 1929. It sends the user name and the
/// password only if the proxy selects that method. The fetcher does not offer
/// GSSAPI.
///
/// The userinfo is `user:password`. The fetcher percent-decodes each field as
/// for an `http://` proxy, and each field must be UTF-8 after the decode. Each
/// field must be 1 to 255 bytes after the decode. [`Fetcher::new`] refuses
/// other userinfo with [`Error::Unsupported`], for example `user@` or
/// `user:@`. The message names the URL without its userinfo.
///
/// A message names the proxy with its URL without the userinfo. The message
/// leaves out the port if it is 1080.
///
/// SOCKS5 sends a host name of 1 to 255 bytes. A longer origin host fails the
/// hop as a definitive failure. A host name with a `%` also fails as a
/// definitive failure, because a SOCKS5 request has no field for the zone id
/// of an IPv6 address. The fetcher applies these two checks under both
/// schemes, before it resolves the name. In these two cases, the fetcher
/// resolves no name and opens no connection to the proxy.
///
/// Under `socks5://`, the fetcher asks the proxy to connect to each resolved
/// address in turn, in the order that a direct connection tries them. Each
/// address gets a new connection to the proxy, because a refused request ends
/// the connection. The first address that the proxy connects wins. A
/// definitive failure stops the loop, and a failed connect to the proxy also
/// stops it. If the proxy refuses each address, the retryable failure states
/// the last failure and the number of addresses. A failed resolve, or a
/// resolve that gives no address, is a retryable failure and names the origin
/// host.
///
/// Two answers of the proxy are definitive failures: the proxy accepts none of
/// the offered methods, or it refuses the user name and the password. A retry
/// gets the same answer. Each other failure is retryable. This includes each
/// reply code other than success, a reply that breaks the protocol, and an I/O
/// failure. The fetcher ignores the value of the reserved byte of the reply.
/// Each failure is [`Error::Fetch`]. A failure of the SOCKS5 exchange names
/// the proxy and the origin.
///
/// [`connect_timeout`](FetcherOptions::connect_timeout) is the limit for all
/// these steps together: the resolve, the connect to the proxy, the SOCKS5
/// exchange, the TLS handshake, and the HTTP handshake. Under `socks5://`,
/// the limit covers the whole address loop. An attempt that stalls can use
/// the full limit, and then the fetcher tries no later address. A timeout
/// names the proxy.
///
/// # Proxy credential
///
/// The proxy credential belongs to the connection layer. It goes to the proxy
/// on a proxied cleartext request and on a `CONNECT`, and to no origin. It is
/// not in the merged header list, so the cleartext credential check and the
/// redirect scoping do not read it. A tunnel carries no part of it. A SOCKS
/// proxy gets its credential in the RFC 1929 exchange alone.
///
/// A `Proxy-Authorization` header that a caller sets keeps the meaning that it
/// has without a proxy. It is a credential in the merged list. A fetch refuses
/// it for a cleartext destination, as it refuses an `Authorization` or a
/// `Cookie` header.
///
/// On a proxied cleartext request, the header of the caller replaces the proxy
/// credential of the fetcher. Two values for one header give two answers to one
/// question. Over a tunnel, the two never meet. The header of the caller goes
/// to the origin, and the credential of the fetcher goes to the proxy.
///
/// # Redirects
///
/// The fetcher makes the proxy decision for each hop, from the origin of that
/// hop. So a redirect to another origin goes the same way as a route that names
/// that origin. A proxy does not change the cleartext rule: a cleartext origin
/// is cleartext also through a proxy.
///
/// # Examples
///
/// ```no_run
/// use ostrya_fetch::{Fetcher, FetcherOptions, Proxy};
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut options = FetcherOptions::new("https://example.com/repo");
/// options.proxy = Proxy::Variables(vec![
///     ("https_proxy".into(), "http://proxy.example.com:3128".into()),
///     ("no_proxy".into(), ".internal.example.com".into()),
/// ]);
/// let fetcher = Fetcher::new(options).await?;
/// # let _ = fetcher;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Default)]
pub enum Proxy {
    /// A direct connection, whatever the environment holds.
    None,
    /// The proxy variables of the process environment.
    ///
    /// [`Fetcher::new`] reads the variables once. This is the default.
    /// [Variables](Proxy#variables) states the names, and
    /// [White space](Proxy#white-space) states the check of each value.
    #[default]
    Environment,
    /// The proxy variables, given as name and value pairs.
    ///
    /// The fetcher does not read the process environment. It ignores a name
    /// that the environment forms do not read. Of two entries of one name, the
    /// first entry with a value counts. [White space](Proxy#white-space) states
    /// the check of each value.
    Variables(Vec<(String, String)>),
    /// One `http://`, `socks5://`, or `socks5h://` proxy URL for all origins.
    ///
    /// The fetcher sends the userinfo of an `http://` URL as
    /// `Proxy-Authorization: Basic`, and the userinfo of a SOCKS5 URL in the
    /// RFC 1929 exchange. No origin is exempt, because the fetcher
    /// reads the environment for neither the proxy nor the exemptions. The
    /// fetcher trims white space around the URL.
    Url(String),
}

impl std::fmt::Debug for Proxy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Proxy::None => f.write_str("None"),
            Proxy::Environment => f.write_str("Environment"),
            Proxy::Variables(variables) => f
                .debug_tuple("Variables")
                .field(&RedactedVariables { variables })
                .finish(),
            Proxy::Url(url) => f.debug_tuple("Url").field(&RedactedUrl::url(url)).finish(),
        }
    }
}

/// The proxy variables in a [`Debug`] output: a list of name and value pairs.
///
/// Each value shows as [`RedactedUrl`] shows an absolute URL.
struct RedactedVariables<'a> {
    variables: &'a [(String, String)],
}

impl std::fmt::Debug for RedactedVariables<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let RedactedVariables { variables } = self;
        f.debug_list()
            .entries(
                variables
                    .iter()
                    .map(|(name, value)| (name, RedactedUrl::url(value))),
            )
            .finish()
    }
}

/// The headers in a [`Debug`] output: a list of name and `<redacted>` pairs.
///
/// A header value can be a credential, so no value shows. Each call of `names`
/// gives the name of each header value, in order.
struct RedactedHeaders<F> {
    names: F,
}

impl<'a, F, I> std::fmt::Debug for RedactedHeaders<F>
where
    F: Fn() -> I,
    I: Iterator<Item = &'a str>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_list()
            .entries((self.names)().map(|name| (name, "<redacted>")))
            .finish()
    }
}

/// A URL or a request path in a [`Debug`] output, with no userinfo, query
/// value, or fragment value.
///
/// The userinfo of an absolute URL is the text after the `://` of the scheme,
/// up to and including the last `@`. When the text has no valid scheme before
/// the first `://`, the userinfo is the text from the start up to and including
/// the last `@`. A password can hold a `/`, `?`, or `#`, so the output removes
/// the userinfo first and leaves no marker for it. Then a query shows as
/// `?<redacted>`, and a fragment shows as `#<redacted>`. A path has no
/// userinfo. Each other part shows as the [`Debug`] output of a `str` shows it.
struct RedactedUrl<'a> {
    text: &'a str,
    /// `true` if `text` is an absolute URL, which can hold userinfo.
    absolute: bool,
}

impl<'a> RedactedUrl<'a> {
    /// Shows an absolute URL.
    fn url(text: &'a str) -> RedactedUrl<'a> {
        RedactedUrl {
            text,
            absolute: true,
        }
    }

    /// Shows a request path.
    fn path(text: &'a str) -> RedactedUrl<'a> {
        RedactedUrl {
            text,
            absolute: false,
        }
    }
}

impl std::fmt::Debug for RedactedUrl<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use std::fmt::Write as _;

        let RedactedUrl { text, absolute } = *self;
        let (scheme, rest) = if absolute {
            let end = scheme_len(text);
            let rest = &text[end..];
            let rest = rest.rfind('@').map_or(rest, |at| &rest[at + 1..]);
            (&text[..end], rest)
        } else {
            ("", text)
        };
        let (shown, query, fragment) = match rest.find(['?', '#']) {
            None => (rest, false, false),
            Some(at) if rest[at..].starts_with('#') => (&rest[..at], false, true),
            Some(at) => (&rest[..at], true, rest[at..].contains('#')),
        };
        // The `Debug` output of a `str` escapes each `char` as
        // `char::escape_debug` does, except `'`, which it writes as it is.
        f.write_char('"')?;
        for c in scheme.chars().chain(shown.chars()) {
            if c == '\'' {
                f.write_char(c)?;
            } else {
                for escaped in c.escape_debug() {
                    f.write_char(escaped)?;
                }
            }
        }
        if query {
            f.write_str("?<redacted>")?;
        }
        if fragment {
            f.write_str("#<redacted>")?;
        }
        f.write_char('"')
    }
}

/// A list of URLs in a [`Debug`] output, each one shown as [`RedactedUrl`]
/// shows an absolute URL.
struct RedactedUrls<'a> {
    urls: &'a [String],
}

impl std::fmt::Debug for RedactedUrls<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let RedactedUrls { urls } = self;
        f.debug_list()
            .entries(urls.iter().map(|url| RedactedUrl::url(url)))
            .finish()
    }
}

/// The options of a [`Fetcher`]: the remote, the credentials, and the limits.
///
/// [`FetcherOptions::default`] sets each field to the default that the field
/// states. [`FetcherOptions::new`] also sets one mirror.
///
/// The [`Debug`] output shows the name of each header, and `<redacted>` in
/// place of its value. The output also shows no password of `basic_auth`, no
/// token of `bearer_token`, no client key or passphrase, and no userinfo of a
/// proxy URL. Each mirror URL shows with no userinfo. A query shows as
/// `?<redacted>`, and a fragment shows as `#<redacted>`. The signer shows as
/// its own [`Debug`] output shows it.
#[derive(Clone)]
pub struct FetcherOptions {
    /// The base URLs, tried in order for each request that names a
    /// [`Target::Path`] or a [`Target::PathQuery`].
    ///
    /// A remote with a mirror list gives one entry for each mirror. A base URL
    /// is a scheme, an authority, and a path. A query string or userinfo fails
    /// [`Fetcher::new`], so neither part of a base URL reaches the wire. A
    /// request target is the base path, then `/` and the request path, then
    /// `?` and the query of a [`Target::PathQuery`]. With a
    /// [`signer`](FetcherOptions::signer), the request path is the path that
    /// [`RequestSigner::request_path`] gives.
    ///
    /// An empty list serves [`Target::Url`] requests alone. The default is an
    /// empty list.
    pub mirrors: Vec<String>,
    /// The extra headers of each request, to each destination and redirect hop.
    ///
    /// A request header of the same name replaces an entry. The fetcher sets
    /// `User-Agent` and `Accept-Encoding: identity`, and an entry of one of
    /// these names replaces the value of the fetcher. Other headers go as
    /// written. The default is an empty list.
    ///
    /// An `Authorization`, `Proxy-Authorization`, or `Cookie` header is a
    /// credential, because its value is a secret, whatever it holds. A
    /// credential goes to the origin that the route names, and to a redirect hop
    /// at that origin alone.
    ///
    /// [`Fetcher::new`] refuses these entries:
    ///
    /// - a credential header, if a mirror is cleartext `http` and
    ///   [`allow_cleartext_credentials`](FetcherOptions::allow_cleartext_credentials)
    ///   is `false`
    /// - a `Host` header
    /// - a header that the connection layer sets: a framing name or a
    ///   hop-by-hop name
    /// - an `Authorization` header beside
    ///   [`basic_auth`](FetcherOptions::basic_auth)
    /// - an `Authorization` header beside
    ///   [`bearer_token`](FetcherOptions::bearer_token)
    /// - an `Authorization` header beside [`signer`](FetcherOptions::signer)
    /// - an invalid header name or value.
    pub headers: Vec<(String, String)>,
    /// The credentials for `Authorization: Basic`, sent with each request.
    ///
    /// The credentials go to each destination, and to a redirect hop at the
    /// origin that the route names. [`FetchRequest::basic_auth`] replaces them
    /// for one request. The default is `None`.
    ///
    /// Each mirror must be `https`, unless
    /// [`allow_cleartext_credentials`](FetcherOptions::allow_cleartext_credentials)
    /// is `true`. [`Fetcher::new`] refuses a cleartext mirror, so the
    /// credentials go in cleartext only with that permission. An
    /// `Authorization` entry in [`headers`](FetcherOptions::headers), a
    /// [`bearer_token`](FetcherOptions::bearer_token), or a
    /// [`signer`](FetcherOptions::signer) beside these also fails
    /// [`Fetcher::new`], because each of them can set the same header.
    pub basic_auth: Option<BasicAuth>,
    /// The token for `Authorization: Bearer`, sent with each request.
    ///
    /// The token goes to each destination, and to a redirect hop at the origin
    /// that the route names. Each of these replaces it for one request:
    /// [`FetchRequest::basic_auth`], [`UploadRequest::basic_auth`],
    /// [`UploadRequest::bearer_token`], or an `Authorization` entry in the
    /// headers of the request. The default is `None`.
    ///
    /// [`Fetcher::new`] refuses a token that does not have the token68 syntax
    /// that [`BearerToken`] states. It also refuses the token beside
    /// [`basic_auth`](FetcherOptions::basic_auth), beside an
    /// `Authorization` entry in [`headers`](FetcherOptions::headers), or
    /// beside a [`signer`](FetcherOptions::signer), because each of them can
    /// set the same header.
    ///
    /// Each mirror must be `https`, unless
    /// [`allow_cleartext_credentials`](FetcherOptions::allow_cleartext_credentials)
    /// is `true`.
    pub bearer_token: Option<BearerToken>,
    /// The signer that adds a signature to each attempt at the origin that
    /// the route names.
    ///
    /// [`RequestSigner`] states when the fetcher calls the signer. The headers
    /// of the signer replace the headers of the same name in each layer: the
    /// fetcher and the request. A redirect hop at another origin gets no
    /// signature. The default is `None`.
    ///
    /// The signer is a credential. [`Fetcher::new`] refuses it beside
    /// [`basic_auth`](FetcherOptions::basic_auth), beside
    /// [`bearer_token`](FetcherOptions::bearer_token), and beside an
    /// `Authorization` entry in [`headers`](FetcherOptions::headers), because
    /// a signature can set the same header. For the same reason, a fetch or an
    /// upload with a credential of its own fails before admission: basic-auth
    /// credentials, a bearer token, or an `Authorization` request header.
    ///
    /// Each mirror must be `https`, unless
    /// [`allow_cleartext_credentials`](FetcherOptions::allow_cleartext_credentials)
    /// is `true`.
    pub signer: Option<Arc<dyn RequestSigner>>,
    /// The permission for a credential to reach a cleartext origin.
    ///
    /// If this is `true`, [`Fetcher::new`] does not refuse a cleartext mirror
    /// beside a credential: [`basic_auth`](FetcherOptions::basic_auth),
    /// [`bearer_token`](FetcherOptions::bearer_token),
    /// [`signer`](FetcherOptions::signer), or a credential header in
    /// [`headers`](FetcherOptions::headers). A fetch or an upload also does
    /// not refuse a credential for a cleartext destination, whatever the flag
    /// of the request is. [`FetchRequest::allow_cleartext_credentials`] states
    /// that rule.
    ///
    /// The redirect rules apply with this flag. A hop at another origin gets no
    /// credential and no signature, and a hop from `https` to `http` is
    /// refused. The default is `false`.
    ///
    /// A caller sets it for a server on a loopback address, or for a server
    /// behind a proxy that terminates TLS.
    pub allow_cleartext_credentials: bool,
    /// The trust anchors and the client certificate for `https` mirrors.
    ///
    /// The default is [`TlsOptions::default`]: [`TrustRoots::System`] and no
    /// client identity.
    pub tls: TlsOptions,
    /// The proxy through which the fetcher reaches an origin.
    ///
    /// [`Fetcher::new`] resolves the value once, and a request does not read it
    /// again. It refuses a proxy URL that is not `http://host[:port]`,
    /// `socks5://host[:port]`, or `socks5h://host[:port]`, a SOCKS5 URL with a
    /// userinfo that [SOCKS proxies](Proxy#socks-proxies) refuses, and a proxy variable with
    /// [white space](Proxy#white-space) around its value.
    /// The default is [`Proxy::Environment`], the proxy that the process
    /// environment names.
    pub proxy: Proxy,
    /// The switch that offers HTTP/2 in ALPN.
    ///
    /// If this is `false`, the fetcher uses HTTP/1.1, also with a server that
    /// supports HTTP/2. The default is `true`.
    pub http2: bool,
    /// The number of times that a fetch repeats a round after a retryable
    /// failure.
    ///
    /// [`Fetcher::fetch`] states the retry rules. The default is 5.
    pub max_retries: u32,
    /// The number of redirects that one attempt against one destination
    /// follows.
    ///
    /// The attempt follows a 301, 302, 303, 307, or 308 while it followed fewer
    /// redirects than this value. If the next one names a URL, the attempt fails
    /// definitively with [`Error::RedirectLimit`]. If it names no URL, the
    /// attempt reports its own status.
    ///
    /// Zero follows nothing, so each of those statuses is then a definitive
    /// answer. A fetch with [`FetchRequest::max_error_body`] then gets the
    /// headers of the redirect, `Location` included, in its
    /// [`Error::HttpStatus`]. The count belongs to one attempt, so a repeated
    /// round counts again from the destination that the route names.
    /// [`Fetcher::fetch`] states the redirect rules. The default is 10.
    pub max_redirects: u32,
    /// The number of requests in flight at one time.
    ///
    /// The fetcher admits this many requests at a time, and serves the queue in
    /// [`Priority`] order. A request holds its permit until the fetch fails, or
    /// until its response body reaches its end or is dropped. A body in flight
    /// occupies a connection, so it keeps the permit.
    ///
    /// The default is 8. The fetcher admits at least one request, so 0 admits
    /// one request.
    pub max_outstanding: usize,
    /// The time limit to open a connection.
    ///
    /// The limit applies to the TCP connect, the TLS handshake, and the HTTP
    /// handshake together. Through a proxy, it also applies to the connect to
    /// the proxy and to the `CONNECT` exchange.
    ///
    /// The TCP connect resolves the host name, then races the addresses that
    /// the resolver gave. A new attempt starts every 250ms, and an attempt that
    /// fails starts the next one immediately. So the limit includes the
    /// resolution and all attempts.
    ///
    /// The expiry is a transport failure. So it is retryable, and the fetch
    /// tries the next destination. The default is 30 s.
    pub connect_timeout: Duration,
    /// The longest time that a response can go without bytes.
    ///
    /// The window applies to the wait for the response head and to each stall
    /// of the body. It measures the silence since a read asked for bytes. So it
    /// limits silence, and leaves the transfer time unlimited.
    ///
    /// The window runs from the read that finds no bytes until bytes arrive.
    /// After a read found no bytes, the window runs also when no read is
    /// outstanding. A body that no read found empty is not on the clock.
    ///
    /// If the body stalls, the read fails with
    /// [`io::ErrorKind::TimedOut`](std::io::ErrorKind::TimedOut), and each
    /// later read fails the same way. An expired wait for the head is a
    /// retryable transport failure, so the fetch tries the next destination.
    /// The default is 60 s.
    pub progress_timeout: Duration,
    /// The lowest transfer rate that an attempt accepts, as [`LowSpeed`]
    /// states.
    ///
    /// The response head must arrive within `time` of the start of the attempt,
    /// rounded up to whole seconds, redirects included. The rate of the body
    /// counts from its first read. So the fetcher does not measure a body that
    /// waits for its consumer before the consumer asks for bytes.
    ///
    /// The fetcher samples the body only while a read is outstanding. A gap
    /// between reads that is longer than a second counts as one second. The
    /// bytes that arrived in the gap count at the next read.
    ///
    /// A transfer slower than the rate is a transport failure. A slow head is a
    /// retryable failure of the attempt. A slow body fails the read with
    /// [`io::ErrorKind::TimedOut`](std::io::ErrorKind::TimedOut).
    ///
    /// `None` checks no rate. The default is `None`.
    pub low_speed: Option<LowSpeed>,
    /// The longest time that one fetch can take to get a response head.
    ///
    /// The limit includes all rounds, all retries, and the delays between them,
    /// from the admission of the fetch until the response head arrives. Only
    /// [`progress_timeout`](FetcherOptions::progress_timeout) limits the body
    /// that follows. `None` sets no limit. The default is 300 s.
    ///
    /// This limit is the longest time that a fetch with no response holds an
    /// admission permit. So a caller sizes it against
    /// [`max_outstanding`](FetcherOptions::max_outstanding). Without this
    /// limit, an unresponsive peer can stall a fetch for a product of three
    /// values. These are the destination count, the retry count, and the two
    /// deadlines of an attempt.
    ///
    /// On expiry, the fetch fails with [`Error::Fetch`] and tries nothing more.
    /// The attempt that the expiry cancels releases the admission permit. An
    /// upload has a narrower limit, which [`Fetcher::upload`] states.
    pub fetch_timeout: Option<Duration>,
}

impl Default for FetcherOptions {
    fn default() -> Self {
        FetcherOptions {
            mirrors: Vec::new(),
            headers: Vec::new(),
            basic_auth: None,
            bearer_token: None,
            signer: None,
            allow_cleartext_credentials: false,
            tls: TlsOptions::default(),
            proxy: Proxy::default(),
            http2: true,
            max_retries: 5,
            max_redirects: 10,
            max_outstanding: 8,
            connect_timeout: Duration::from_secs(30),
            progress_timeout: Duration::from_secs(60),
            low_speed: None,
            fetch_timeout: Some(Duration::from_secs(300)),
        }
    }
}

impl std::fmt::Debug for FetcherOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let FetcherOptions {
            mirrors,
            headers,
            basic_auth,
            bearer_token,
            signer,
            allow_cleartext_credentials,
            tls,
            proxy,
            http2,
            max_retries,
            max_redirects,
            max_outstanding,
            connect_timeout,
            progress_timeout,
            low_speed,
            fetch_timeout,
        } = self;
        let headers = RedactedHeaders {
            names: || headers.iter().map(|(name, _)| name.as_str()),
        };
        f.debug_struct("FetcherOptions")
            .field("mirrors", &RedactedUrls { urls: mirrors })
            .field("headers", &headers)
            .field("basic_auth", basic_auth)
            .field("bearer_token", bearer_token)
            .field("signer", signer)
            .field("allow_cleartext_credentials", allow_cleartext_credentials)
            .field("tls", tls)
            .field("proxy", proxy)
            .field("http2", http2)
            .field("max_retries", max_retries)
            .field("max_redirects", max_redirects)
            .field("max_outstanding", max_outstanding)
            .field("connect_timeout", connect_timeout)
            .field("progress_timeout", progress_timeout)
            .field("low_speed", low_speed)
            .field("fetch_timeout", fetch_timeout)
            .finish()
    }
}

impl FetcherOptions {
    /// Creates options with one base URL and the default of each other field.
    pub fn new(url: impl Into<String>) -> FetcherOptions {
        FetcherOptions {
            mirrors: vec![url.into()],
            ..FetcherOptions::default()
        }
    }
}

/// The target of a request.
///
/// The [`Debug`] output shows no userinfo of a URL. A query shows as
/// `?<redacted>`, and a fragment shows as `#<redacted>`, in a URL and in a
/// path. The query of [`Target::PathQuery`] shows as `<redacted>`.
#[derive(Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum Target<'a> {
    /// A path under the base URL of each mirror.
    Path(&'a str),
    /// A path under the base URL of each mirror, with a query string.
    ///
    /// The fetcher adds `?` and the query to the URL of each mirror. It sends
    /// the query as written, so the query carries the escapes that the server
    /// expects.
    PathQuery {
        /// The path, read as [`Target::Path`] is.
        path: &'a str,
        /// The query string, without the `?`.
        query: &'a str,
    },
    /// An absolute `http` or `https` URL.
    ///
    /// The fetcher does not use the mirror list for it.
    Url(&'a str),
}

impl std::fmt::Debug for Target<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Target::Path(path) => f
                .debug_tuple("Path")
                .field(&RedactedUrl::path(path))
                .finish(),
            Target::PathQuery { path, query: _ } => f
                .debug_struct("PathQuery")
                .field("path", &RedactedUrl::path(path))
                .field("query", &"<redacted>")
                .finish(),
            Target::Url(url) => f.debug_tuple("Url").field(&RedactedUrl::url(url)).finish(),
        }
    }
}

impl<'a> Target<'a> {
    /// Returns the text that names the target in each message: the path, the
    /// path with `?` and its query, or the URL, as the caller wrote it.
    fn describe(&self) -> Cow<'a, str> {
        match *self {
            Target::Path(path) => Cow::Borrowed(path),
            Target::PathQuery { path, query } => Cow::Owned(format!("{path}?{query}")),
            Target::Url(url) => Cow::Borrowed(url),
        }
    }
}

/// The request of one fetch.
///
/// The [`Debug`] output shows the name of each header, and `<redacted>` in
/// place of its value. The target shows as the [`Debug`] output of [`Target`]
/// shows it.
#[derive(Clone)]
pub struct FetchRequest<'a> {
    /// The target: a path under the base URL of each mirror, the same with a
    /// query string, or an absolute URL.
    ///
    /// The fetcher adds a path to the base path as written, so the path carries
    /// the escapes that the server expects. A path holds no `?` and no `#`,
    /// because neither is part of a path. Either one fails the fetch before
    /// admission. If the fetcher has a
    /// [`signer`](FetcherOptions::signer), the fetcher adds the path that
    /// [`RequestSigner::request_path`] gives in place of the path.
    ///
    /// The fetcher adds `?` and the query of [`Target::PathQuery`] after the
    /// path, as written, so the query carries the escapes that the server
    /// expects. A query holds no `#`, because a `#` starts a fragment. A `#`
    /// fails the fetch before admission.
    ///
    /// The fetcher sends the query string of a URL as written. A fragment in a
    /// URL fails the fetch, because no request sends a fragment. A character
    /// that a request target cannot hold fails the fetch when the fetcher
    /// assembles the URL.
    pub target: Target<'a>,
    /// The place of the request in the queue when the fetcher is at its limit.
    pub priority: Priority,
    /// The validators of a previous fetch.
    ///
    /// If the server reports that the copy is still current, the fetch gives
    /// [`Fetched::NotModified`].
    pub validators: Option<&'a Validators>,
    /// The largest response body, in bytes, that the fetch accepts.
    ///
    /// A larger `Content-Length` fails the fetch with [`Error::FetchTooLarge`].
    /// If a body grows past the cap while it streams, the read fails with
    /// [`io::ErrorKind::FileTooLarge`](std::io::ErrorKind::FileTooLarge). The
    /// fetcher compares the cap with the response that answers, and does not
    /// measure an intermediate redirect response.
    pub max_size: Option<u64>,
    /// Headers merged over the headers of the fetcher.
    ///
    /// An entry replaces a fetcher header of the same name. Names compare as
    /// `HeaderName`, so the comparison ignores case. Two entries of one name
    /// both go on the wire. An invalid name or value fails the fetch before
    /// admission.
    ///
    /// The fetcher sets `User-Agent` and `Accept-Encoding: identity`, and an
    /// entry of one of these names replaces the value. An entry can ask for a
    /// content coding. A response that declares a coding fails with
    /// [`Error::ContentEncoded`], whichever layer asked for it.
    ///
    /// A `Range` entry, in either layer, makes a 206 a successful answer. The
    /// fetch then gives the part of the object as a [`Body`], whose
    /// [`status`](Body::status) is 206. A 206 to a request with no `Range`
    /// entry fails the fetch with [`Error::HttpStatus`]. The size cap and the
    /// content coding rules apply to a 206 as to a 200.
    ///
    /// A `Host` entry fails the fetch, because the header comes from the URL. A
    /// header that the connection layer sets fails at both layers: a framing
    /// name or a hop-by-hop name. The connection owns the framing of a request
    /// and the state of its connection. A value of the caller puts the wire and
    /// the connection pool out of step.
    ///
    /// # Authorization
    ///
    /// The request carries the `Authorization` header of the first of these
    /// that is set:
    ///
    /// 1. [`FetchRequest::basic_auth`]
    /// 2. an `Authorization` entry in these headers
    /// 3. [`FetcherOptions::basic_auth`] or [`FetcherOptions::bearer_token`]
    /// 4. an `Authorization` entry in [`FetcherOptions::headers`].
    ///
    /// Credentials beside an `Authorization` header in one layer give two
    /// answers to one question. [`Fetcher::new`] refuses them on the fetcher,
    /// and the fetch refuses them on a request.
    ///
    /// A fetcher with a [`signer`](FetcherOptions::signer) takes no
    /// credential of a request. [`basic_auth`](FetchRequest::basic_auth) or
    /// an `Authorization` entry in these headers then fails the fetch before
    /// admission with [`Error::Fetch`]. [`Fetcher::new`] refuses layers 3 and
    /// 4 beside a signer. So the signature is the one credential of the
    /// request, and each header that the signer returns replaces the header
    /// of the same name.
    pub headers: &'a [(String, String)],
    /// Credentials that replace the credentials of the fetcher for this
    /// request.
    ///
    /// These set the `Authorization` header, so an `Authorization` entry in
    /// [`headers`](FetchRequest::headers) beside them fails the fetch. A
    /// [`signer`](FetcherOptions::signer) of the fetcher beside them also
    /// fails the fetch before admission with [`Error::Fetch`].
    ///
    /// The fetcher encodes the header value for each request. A caller that
    /// sends one credential over many requests can encode the `Authorization`
    /// value once and set it in [`headers`](FetchRequest::headers). That entry
    /// replaces the credential of the fetcher for the request in the same way.
    pub basic_auth: Option<&'a BasicAuth>,
    /// The permission for a credential to reach a cleartext origin.
    ///
    /// If this is `false` and the merged headers carry a credential, the fetch
    /// fails if a destination that it can reach is `http`. The merged headers
    /// include the credentials of the fetcher. The check of [`Fetcher::new`] on
    /// the mirror list applies whatever this value is. The default is `false`.
    ///
    /// [`FetcherOptions::allow_cleartext_credentials`] lifts this refusal for
    /// each request of the fetcher.
    ///
    /// A redirect hop never carries a credential to a cleartext origin of its
    /// own. The fetcher removes the credential for a hop at another origin, and
    /// refuses a hop from `https` to `http`. [`Fetcher::fetch`] states the
    /// redirect rules.
    ///
    /// # Cleartext credentials
    ///
    /// The fetcher withholds a credential from no destination that a route
    /// names. So it refuses a credential and a cleartext destination together.
    /// The credentials are [`basic_auth`](FetchRequest::basic_auth), and an
    /// `Authorization`, `Proxy-Authorization`, or `Cookie` entry in the merged
    /// headers. [`FetcherOptions::basic_auth`] and
    /// [`FetcherOptions::bearer_token`] are `Authorization` entries in the
    /// merged headers. [`FetcherOptions::signer`] is also a credential.
    ///
    /// If a fetch carries a credential and a destination that it can reach is
    /// `http`, the fetch fails before admission with [`Error::Fetch`]. The
    /// message names that origin. The other choice is to withhold the
    /// credential from that one destination. That choice turns a configuration
    /// mistake into a 401 that names nothing.
    pub allow_cleartext_credentials: bool,
    /// The largest body of an unsuccessful response that the fetch keeps.
    ///
    /// With `Some(cap)`, a fetch that fails with [`Error::HttpStatus`] gives
    /// the headers of the reported response in that error. It also gives the
    /// body, if the whole body is `cap` bytes or less. `Some(0)` keeps the
    /// headers and an empty body. [`ErrorResponse`] states when the body is
    /// absent.
    ///
    /// `None` keeps nothing, and the fetch discards each unsuccessful
    /// response. The default is `None`.
    ///
    /// [`Fetcher::fetch`] states the rules in its section "Error responses".
    pub max_error_body: Option<u64>,
}

impl std::fmt::Debug for FetchRequest<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let FetchRequest {
            target,
            priority,
            validators,
            max_size,
            headers,
            basic_auth,
            allow_cleartext_credentials,
            max_error_body,
        } = self;
        let headers = RedactedHeaders {
            names: || headers.iter().map(|(name, _)| name.as_str()),
        };
        f.debug_struct("FetchRequest")
            .field("target", target)
            .field("priority", priority)
            .field("validators", validators)
            .field("max_size", max_size)
            .field("headers", &headers)
            .field("basic_auth", basic_auth)
            .field("allow_cleartext_credentials", allow_cleartext_credentials)
            .field("max_error_body", max_error_body)
            .finish()
    }
}

impl<'a> FetchRequest<'a> {
    /// Creates a request for `path` under each mirror.
    ///
    /// The request has normal priority, no validators, and no size cap. It
    /// carries the headers and the credentials of the fetcher.
    pub fn path(path: &'a str) -> FetchRequest<'a> {
        FetchRequest::for_target(Target::Path(path))
    }

    /// Creates a request for `path` with the query string `query` under each
    /// mirror.
    ///
    /// The query has no leading `?`. The request has normal priority, no
    /// validators, and no size cap. It carries the headers and the
    /// credentials of the fetcher.
    pub fn path_query(path: &'a str, query: &'a str) -> FetchRequest<'a> {
        FetchRequest::for_target(Target::PathQuery { path, query })
    }

    /// Creates a request for the absolute URL `url`.
    ///
    /// The request has normal priority, no validators, and no size cap. It
    /// carries the headers and the credentials of the fetcher.
    pub fn url(url: &'a str) -> FetchRequest<'a> {
        FetchRequest::for_target(Target::Url(url))
    }

    /// Creates a request for `target` with the default of each other field.
    fn for_target(target: Target<'a>) -> FetchRequest<'a> {
        FetchRequest {
            target,
            priority: Priority::default(),
            validators: None,
            max_size: None,
            headers: &[],
            basic_auth: None,
            allow_cleartext_credentials: false,
            max_error_body: None,
        }
    }
}

/// The result of a fetch.
#[derive(Debug)]
// The body variant is much larger than the other variant. A box adds an
// allocation on the path of each object, so the variant stays inline.
#[allow(clippy::large_enum_variant)]
pub enum Fetched {
    /// The body of the object that the server sent.
    Body(Body),
    /// A 304 answer: the copy of the caller is current.
    NotModified,
}

/// The method of an upload.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum UploadMethod {
    /// `POST`, which sends the body.
    ///
    /// This is the default.
    #[default]
    Post,
    /// `PUT`, which sends the body.
    ///
    /// The retry and the redirect rules of [`Post`](UploadMethod::Post)
    /// apply.
    Put,
    /// `DELETE`, which sends no body.
    ///
    /// The request takes an empty [`UploadBody::bytes`]. Any other body fails
    /// the upload before admission with [`Error::Fetch`].
    Delete,
}

/// The request body of an upload.
///
/// A body has one of two forms:
///
/// - [`bytes`](UploadBody::bytes) gives the whole body. The request declares
///   its `Content-Length`, and hyper takes the bytes in frames of 64 KiB. A
///   followed redirect sends the bytes again.
///   [`bytes_with_sha256`](UploadBody::bytes_with_sha256) also gives the
///   SHA-256 of the body to the signer.
/// - [`channel`](UploadBody::channel) streams the body. The caller writes the
///   body into the [`UploadWriter`] while the upload runs. Over HTTP/1.1, the
///   body travels chunked.
///
/// If a channel body is dropped before an upload sends it, or after its upload
/// ends, each write of its writer fails with
/// [`io::ErrorKind::BrokenPipe`](std::io::ErrorKind::BrokenPipe). If the body
/// of a `POST` or a `PUT` is at its end when the fetcher hands over the
/// request, the request declares a `Content-Length` of zero.
pub struct UploadBody {
    form: UploadForm,
}

/// The two forms of an [`UploadBody`].
enum UploadForm {
    /// The whole body, which each hop of the upload sends, and the SHA-256
    /// of the body if the caller gave it.
    Bytes {
        bytes: Bytes,
        sha256: Option<[u8; 32]>,
    },
    /// The reading end of a body that an [`UploadWriter`] fills.
    Channel(BodyEnd),
}

impl UploadBody {
    /// Creates a body of `bytes`.
    ///
    /// The request sends the vector in frames cut from it with no copy.
    pub fn bytes(bytes: Vec<u8>) -> UploadBody {
        UploadBody {
            form: UploadForm::Bytes {
                bytes: Bytes::from(bytes),
                sha256: None,
            },
        }
    }

    /// Creates a body of `bytes`, whose SHA-256 is `sha256`.
    ///
    /// The body is a body of [`bytes`](UploadBody::bytes). A signer gets it
    /// as [`SignPayload::Sha256`] with `sha256`, so a signer that signs the
    /// SHA-256 of the body hashes no byte of it on the task of the request.
    /// Each retry and each redirect hop of the upload uses the same digest,
    /// and so does a copy from [`try_clone`](UploadBody::try_clone).
    ///
    /// The fetcher does not check the digest. A wrong digest signs other
    /// bytes than the bytes that the request sends, and the server refuses
    /// the request.
    pub fn bytes_with_sha256(bytes: Vec<u8>, sha256: [u8; 32]) -> UploadBody {
        UploadBody {
            form: UploadForm::Bytes {
                bytes: Bytes::from(bytes),
                sha256: Some(sha256),
            },
        }
    }

    /// Creates a streamed body and the writer that fills it.
    pub fn channel() -> (UploadBody, UploadWriter) {
        let exchange = Arc::new(Exchange::default());
        let body = UploadBody {
            form: UploadForm::Channel(BodyEnd {
                exchange: exchange.clone(),
            }),
        };
        let writer = UploadWriter {
            exchange,
            // The first write allocates the frame. So a body that gets no
            // write holds no frame of memory.
            buffer: Vec::new(),
            stall: None,
            failed: None,
        };
        (body, writer)
    }

    /// Returns the bytes of a body of [`bytes`](UploadBody::bytes), or `None`
    /// for a streamed body.
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match &self.form {
            UploadForm::Bytes { bytes, .. } => Some(bytes),
            UploadForm::Channel(_) => None,
        }
    }

    /// Returns the SHA-256 of a body of
    /// [`bytes_with_sha256`](UploadBody::bytes_with_sha256), or `None` for
    /// another body.
    pub fn sha256(&self) -> Option<[u8; 32]> {
        match &self.form {
            UploadForm::Bytes { sha256, .. } => *sha256,
            UploadForm::Channel(_) => None,
        }
    }

    /// Returns a second body with the same bytes, or `None` for a streamed
    /// body.
    ///
    /// The two bodies of [`bytes`](UploadBody::bytes) share the one buffer, so
    /// the call copies no byte. The copy keeps the SHA-256 of a body of
    /// [`bytes_with_sha256`](UploadBody::bytes_with_sha256). A caller sends
    /// the copy in a second request, for example after the first one failed
    /// with [`Error::UploadInterrupted`]. A [`channel`](UploadBody::channel)
    /// body has one writer, so it has no copy.
    pub fn try_clone(&self) -> Option<UploadBody> {
        match &self.form {
            UploadForm::Bytes { bytes, sha256 } => Some(UploadBody {
                form: UploadForm::Bytes {
                    bytes: bytes.clone(),
                    sha256: *sha256,
                },
            }),
            UploadForm::Channel(_) => None,
        }
    }

    /// Returns the bytes of a body of [`bytes`](UploadBody::bytes) as a
    /// vector, or the body unchanged.
    ///
    /// If no other body shares the buffer, the call gives the buffer back
    /// with its capacity, and copies no byte. A caller can then fill the
    /// vector again for a later request. The vector does not keep the
    /// SHA-256 of a body of
    /// [`bytes_with_sha256`](UploadBody::bytes_with_sha256). A copy from
    /// [`try_clone`](UploadBody::try_clone) shares the buffer, and so does an
    /// upload that still holds the body. The call then returns the body as
    /// the error, as it does for a [`channel`](UploadBody::channel) body.
    ///
    /// # Errors
    ///
    /// Returns the body unchanged if it is a streamed body, or if another
    /// body shares its buffer.
    pub fn into_vec(self) -> std::result::Result<Vec<u8>, UploadBody> {
        match self.form {
            UploadForm::Bytes { bytes, sha256 } => match bytes.try_into_mut() {
                Ok(bytes) => Ok(Vec::from(bytes)),
                Err(bytes) => Err(UploadBody {
                    form: UploadForm::Bytes { bytes, sha256 },
                }),
            },
            UploadForm::Channel(end) => Err(UploadBody {
                form: UploadForm::Channel(end),
            }),
        }
    }
}

impl From<Vec<u8>> for UploadBody {
    fn from(bytes: Vec<u8>) -> UploadBody {
        UploadBody::bytes(bytes)
    }
}

impl std::fmt::Debug for UploadBody {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.form {
            UploadForm::Bytes { bytes, .. } => f.debug_tuple("Bytes").field(&bytes.len()).finish(),
            UploadForm::Channel(_) => f.write_str("Channel"),
        }
    }
}

/// The request of one upload, which sends a body and gets a bounded response.
///
/// The [`Debug`] output shows the name of each header, and `<redacted>` in
/// place of its value. The target shows as the [`Debug`] output of [`Target`]
/// shows it.
pub struct UploadRequest<'a> {
    /// The target, read as [`FetchRequest::target`] is. A
    /// [`Target::PathQuery`] goes to the mirrors with its query, as for a
    /// fetch.
    ///
    /// The fetcher tries the mirrors in order until one of them takes the
    /// request. It never asks a mirror after that one.
    pub target: Target<'a>,
    /// The method.
    ///
    /// The default is `POST`.
    pub method: UploadMethod,
    /// The body that the request sends.
    pub body: UploadBody,
    /// The place of the request in the queue when the fetcher is at its limit.
    pub priority: Priority,
    /// The largest response body, in bytes, for each status.
    ///
    /// A larger `Content-Length` of a 2xx answer fails the upload with
    /// [`Error::UploadInterrupted`]. An answer other than 2xx with a larger
    /// `Content-Length` keeps its status, and its body is
    /// [withheld](Uploaded::body_withheld). If a body grows past the cap
    /// while it streams, the read fails with
    /// [`io::ErrorKind::FileTooLarge`](std::io::ErrorKind::FileTooLarge). The
    /// default is [`UploadRequest::DEFAULT_MAX_RESPONSE`].
    pub max_response: u64,
    /// Headers merged over the headers of the fetcher.
    ///
    /// [`FetchRequest::headers`] states the merge. The fetcher sets no
    /// `Content-Type`, so a `Content-Type` that the server needs goes here.
    pub headers: &'a [(String, String)],
    /// Credentials for `Authorization: Basic`, for this request.
    ///
    /// They replace the credentials of the fetcher.
    ///
    /// Beside [`bearer_token`](UploadRequest::bearer_token), beside an
    /// `Authorization` entry in [`headers`](UploadRequest::headers), or beside
    /// a [`signer`](FetcherOptions::signer) of the fetcher, they fail the
    /// upload before admission with [`Error::Fetch`].
    pub basic_auth: Option<&'a BasicAuth>,
    /// A token for `Authorization: Bearer`, for this request.
    ///
    /// It replaces the credentials of the fetcher.
    ///
    /// Beside [`basic_auth`](UploadRequest::basic_auth), beside an
    /// `Authorization` entry in [`headers`](UploadRequest::headers), or beside
    /// a [`signer`](FetcherOptions::signer) of the fetcher, it fails the
    /// upload before admission with [`Error::Fetch`].
    pub bearer_token: Option<&'a BearerToken>,
    /// The permission for a credential to reach a cleartext origin.
    ///
    /// [`FetchRequest::allow_cleartext_credentials`] states the rule.
    pub allow_cleartext_credentials: bool,
    /// The longest wait for the response head after hyper takes the end of the
    /// body.
    ///
    /// `None` takes [`progress_timeout`](FetcherOptions::progress_timeout).
    pub response_timeout: Option<Duration>,
}

impl std::fmt::Debug for UploadRequest<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let UploadRequest {
            target,
            method,
            body,
            priority,
            max_response,
            headers,
            basic_auth,
            bearer_token,
            allow_cleartext_credentials,
            response_timeout,
        } = self;
        let headers = RedactedHeaders {
            names: || headers.iter().map(|(name, _)| name.as_str()),
        };
        f.debug_struct("UploadRequest")
            .field("target", target)
            .field("method", method)
            .field("body", body)
            .field("priority", priority)
            .field("max_response", max_response)
            .field("headers", &headers)
            .field("basic_auth", basic_auth)
            .field("bearer_token", bearer_token)
            .field("allow_cleartext_credentials", allow_cleartext_credentials)
            .field("response_timeout", response_timeout)
            .finish()
    }
}

impl<'a> UploadRequest<'a> {
    /// The default response cap of a request: 2 MiB.
    pub const DEFAULT_MAX_RESPONSE: u64 = 2 * 1024 * 1024;

    /// Creates a `POST` of `body` to `path` under each mirror.
    ///
    /// The request has normal priority. It carries the headers and the
    /// credentials of the fetcher.
    pub fn path(path: &'a str, body: UploadBody) -> UploadRequest<'a> {
        UploadRequest::for_target(Target::Path(path), body)
    }

    /// Creates a `POST` of `body` to the absolute URL `url`.
    ///
    /// The request has normal priority. It carries the headers and the
    /// credentials of the fetcher.
    pub fn url(url: &'a str, body: UploadBody) -> UploadRequest<'a> {
        UploadRequest::for_target(Target::Url(url), body)
    }

    /// Creates a request for `target` with the default of each other field.
    fn for_target(target: Target<'a>, body: UploadBody) -> UploadRequest<'a> {
        UploadRequest {
            target,
            method: UploadMethod::default(),
            body,
            priority: Priority::default(),
            max_response: UploadRequest::DEFAULT_MAX_RESPONSE,
            headers: &[],
            basic_auth: None,
            bearer_token: None,
            allow_cleartext_credentials: false,
            response_timeout: None,
        }
    }
}

/// The answer to an upload: the status, the headers, and the response body.
///
/// Each final status arrives here, also an unsuccessful one. The caller
/// decides what it means.
///
/// The [`Debug`] output shows the name of each response header, and
/// `<redacted>` in place of its value. A header with several values shows its
/// name once for each value. The URL shows with no userinfo. A query shows as
/// `?<redacted>`, and a fragment shows as `#<redacted>`.
pub struct Uploaded {
    status: StatusCode,
    headers: hyper::HeaderMap,
    url: String,
    body: Body,
    /// `true` if the upload withheld the body of an answer other than 2xx.
    withheld: bool,
}

impl std::fmt::Debug for Uploaded {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Uploaded {
            status,
            headers,
            url,
            body,
            withheld: _,
        } = self;
        let headers = RedactedHeaders {
            names: || headers.iter().map(|(name, _)| name.as_str()),
        };
        f.debug_struct("Uploaded")
            .field("status", status)
            .field("headers", &headers)
            .field("url", &RedactedUrl::url(url))
            .field("body", body)
            .finish()
    }
}

impl Uploaded {
    /// Returns the status of the response.
    pub fn status(&self) -> u16 {
        self.status.as_u16()
    }

    /// Returns the headers of the response.
    pub fn headers(&self) -> &hyper::HeaderMap {
        &self.headers
    }

    /// Returns the URL that answered.
    ///
    /// After a redirect, this is the URL of the last hop.
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Returns `true` if the upload withheld the body of the response.
    ///
    /// An answer other than 2xx whose head declares a coding, or a
    /// `Content-Length` over [`max_response`](UploadRequest::max_response),
    /// comes with its status and its headers alone. Its body is not read:
    /// [`into_body`](Uploaded::into_body) gives a body that reads as empty,
    /// and the connection closes. [`Fetcher::upload`] states the rule.
    pub fn body_withheld(&self) -> bool {
        self.withheld
    }

    /// Returns the HTTP version of the response.
    pub fn protocol(&self) -> Protocol {
        self.body.protocol()
    }

    /// Returns the response body, capped at
    /// [`max_response`](UploadRequest::max_response).
    ///
    /// The body holds the admission permit until it ends or is dropped. A
    /// [withheld](Uploaded::body_withheld) body reads as empty and holds no
    /// permit.
    pub fn into_body(self) -> Body {
        self.body
    }
}

/// The headers and the capped body of an unsuccessful response to a fetch.
///
/// [`Error::HttpStatus`] carries it if the request set
/// [`max_error_body`](FetchRequest::max_error_body). A caller outside this
/// crate builds one with [`new`](ErrorResponse::new).
///
/// The [`Debug`] output shows the name of each header, and `<redacted>` in
/// place of its value. A header with several values shows its name once for
/// each value. The body shows as its length.
pub struct ErrorResponse {
    headers: hyper::HeaderMap,
    body: Option<Vec<u8>>,
}

impl std::fmt::Debug for ErrorResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let ErrorResponse { headers, body } = self;
        let headers = RedactedHeaders {
            names: || headers.iter().map(|(name, _)| name.as_str()),
        };
        f.debug_struct("ErrorResponse")
            .field("headers", &headers)
            .field("body", &body.as_ref().map(Vec::len))
            .finish()
    }
}

impl ErrorResponse {
    /// Creates a response of `headers` and `body`.
    pub fn new(headers: hyper::HeaderMap, body: Option<Vec<u8>>) -> ErrorResponse {
        ErrorResponse { headers, body }
    }

    /// Returns the headers of the response.
    pub fn headers(&self) -> &hyper::HeaderMap {
        &self.headers
    }

    /// Returns the whole body of the response.
    ///
    /// The body is `None` in these cases:
    ///
    /// - the response declared a `Content-Length` over the cap
    /// - the body grew past the cap while it arrived
    /// - the response declared a coding in `Content-Encoding` or
    ///   `Transfer-Encoding`
    /// - the body failed in transit
    /// - the body did not arrive in full within one
    ///   [`progress_timeout`](FetcherOptions::progress_timeout) from the
    ///   arrival of the response head.
    ///
    /// The fetcher never keeps a part of a body.
    pub fn body(&self) -> Option<&[u8]> {
        self.body.as_deref()
    }
}

/// A parsed base URL.
#[derive(Clone, Debug)]
struct Mirror {
    /// The scheme, the host, and the port of the origin.
    origin: Origin,
    /// The `host[:port]` of this mirror, which is the `Host` header of an
    /// HTTP/1.1 request. It holds the authority as the base URL wrote it,
    /// without the default port of the scheme. An IPv6 literal keeps its
    /// brackets.
    authority: HeaderValue,
    /// The `scheme://authority` prefix of each absolute URL for this mirror.
    /// A message names the origin of the mirror with this prefix.
    prefix: String,
    /// The base path, without a trailing slash. It is empty for a mirror at the
    /// root.
    base: String,
}

impl Mirror {
    /// Returns the destination of a request for `path` and `query` under this
    /// mirror. The one string of the destination is the prefix and the base
    /// path of the mirror, then the request path, then `?` and the query if
    /// there is one.
    fn destination(&self, path: &str, query: Option<&str>) -> Destination {
        let path = path.trim_start_matches('/');
        let url = match query {
            None => format!("{}{}/{path}", self.prefix, self.base),
            Some(query) => format!("{}{}/{path}?{query}", self.prefix, self.base),
        };
        Destination {
            origin: self.origin.clone(),
            authority: self.authority.clone(),
            url,
            path_at: self.prefix.len(),
        }
    }
}

/// The place to which one attempt sends its request.
///
/// A URL target resolves its one destination once for each fetch, and each
/// attempt and each retry round uses it. A path target resolves the
/// destination of a mirror at the attempt that uses that mirror.
#[derive(Clone, Debug)]
struct Destination {
    /// The scheme, the host, and the port of the origin. The pool key holds it,
    /// together with the client identity that the connection presents.
    origin: Origin,
    /// The `host[:port]` of the request, which is the `Host` header of an
    /// HTTP/1.1 request. It holds the authority as the caller wrote it, without
    /// the default port of the scheme. An IPv6 literal keeps its brackets.
    authority: HeaderValue,
    /// The absolute URL. It holds the two forms that one request needs: the
    /// whole string, and the tail from `path_at`.
    url: String,
    /// The start of the request path in `url`, which is the length of the
    /// `scheme://authority` prefix.
    path_at: usize,
}

impl Destination {
    /// Returns the absolute URL, which an HTTP/2 request carries and each
    /// message names.
    fn url(&self) -> &str {
        &self.url
    }

    /// Returns the origin-form request target, which an HTTP/1.1 request to an
    /// origin server carries. It is the path, and the query string of a URL
    /// target or of a path target with a query.
    fn target(&self) -> &str {
        &self.url[self.path_at..]
    }

    /// Returns the `scheme://authority` of this destination. A message about
    /// the origin names it with this string.
    fn origin_url(&self) -> &str {
        &self.url[..self.path_at]
    }

    /// Returns the `host[:port]` of this destination as text, which is the
    /// value of the `Host` header in `authority`. An HTTP/2 request carries
    /// the same text in its `:authority`, because it carries the absolute URL.
    fn authority_text(&self) -> &str {
        &self.url[scheme_len(&self.url)..self.path_at]
    }
}

/// The destinations of a fetch, in the order in which it asks them.
///
/// A path target goes to each mirror. So a round walks the mirror list, and
/// builds the destination of a mirror when it asks that mirror. If the first
/// mirror answers, the fetch builds one destination, whatever the length of the
/// list. A URL target names one destination, which the fetch parses once.
#[derive(Debug)]
enum Route<'a> {
    /// Each mirror in order, for this request path and query string. The
    /// path is in the form that the signer of the fetcher gives.
    Mirrors {
        path: Cow<'a, str>,
        query: Option<&'a str>,
    },
    /// The one destination that a URL target names.
    One(Destination),
}

/// A connection endpoint.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct Origin {
    tls: bool,
    /// The host that the connect resolves, and the source of the TLS server
    /// name. So an IPv6 literal has no brackets here. An ASCII host is in lower
    /// case, so one origin written in two cases is one pool key and one
    /// connection.
    host: String,
    port: u16,
}

/// The key of the connection pool.
///
/// The key holds an endpoint, the client configuration that opened the
/// connection, and the proxy flag. A connection presents the client
/// certificate for each request that it carries. So the pool never gives a
/// connection with the certificate to a hop that must not present it. It never
/// gives a connection without the certificate to the origin that the route
/// named.
///
/// The identity flag is in this key, and not in [`Origin`]. [`Origin`] states
/// the identity of a network endpoint, and the cleartext and trust-anchor
/// checks read it.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct PoolKey {
    origin: Origin,
    /// `true` if the connection presents the configured client certificate.
    identity: bool,
    /// `true` if the connection goes to a proxy and carries absolute-form
    /// requests.
    ///
    /// The pool keeps such a connection under the proxy endpoint. A cleartext
    /// origin of the same host and port has the same endpoint, and the two
    /// carry different request forms. This flag keeps the pool from giving one
    /// to a request of the other.
    proxied: bool,
}

/// One proxy that the fetcher connects through, resolved from a proxy URL.
#[derive(Debug)]
struct ProxyEndpoint {
    /// The endpoint of the connection. The fetcher reaches a proxy over
    /// cleartext, so this origin is never TLS.
    endpoint: Origin,
    /// The protocol of the proxy and its credential.
    kind: ProxyKind,
    /// The name of this proxy in a message, which is its URL without the
    /// userinfo.
    named: String,
}

/// The protocol that the fetcher speaks to a proxy.
#[derive(Debug)]
enum ProxyKind {
    /// An `http://` proxy.
    Http {
        /// The `Proxy-Authorization` value from the userinfo of the proxy URL,
        /// if the URL holds userinfo.
        credential: Option<HeaderValue>,
    },
    /// A `socks5://` or a `socks5h://` proxy.
    Socks(SocksProxy),
}

/// The settings of a SOCKS5 proxy.
#[derive(Debug)]
struct SocksProxy {
    /// The side that resolves the host name of an origin.
    resolve: ResolveSide,
    /// The user name and the password from the userinfo of the proxy URL, if
    /// the URL holds userinfo.
    credential: Option<socks::Credential>,
}

/// The side of a SOCKS5 connection that resolves the host name of an origin.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ResolveSide {
    /// The fetcher resolves the name and sends each address to the proxy in
    /// turn. A `socks5://` proxy uses this side.
    Client,
    /// The fetcher sends the name, and the proxy resolves it. A `socks5h://`
    /// proxy uses this side.
    Proxy,
}

/// One `no_proxy` entry: a host text, and the port of the entry if it names
/// one.
#[derive(Debug)]
struct Exemption {
    /// The host that the entry names, in ASCII lower case, without one leading
    /// `.`. It is never empty: an entry that names no host exempts nothing and
    /// gets no place in a list.
    host: String,
    /// The port that the entry matches, if it names a port that the fetcher
    /// can read.
    port: Option<u16>,
}

impl Exemption {
    /// Returns `true` if this entry exempts `origin`.
    ///
    /// The comparison is byte by byte, with ASCII case ignored. So a host that
    /// is not ASCII needs no slice at a character boundary, which can panic.
    fn matches(&self, origin: &Origin) -> bool {
        if self.port.is_some_and(|port| port != origin.port) {
            return false;
        }
        let host = origin.host.as_bytes();
        let entry = self.host.as_bytes();
        if host.eq_ignore_ascii_case(entry) {
            return true;
        }
        // The `.` before the entry states that the host is a name under the
        // entry. `notexample.com` is no part of `example.com`.
        host.len() > entry.len()
            && host[host.len() - entry.len() - 1] == b'.'
            && host[host.len() - entry.len()..].eq_ignore_ascii_case(entry)
    }
}

/// The proxy of each scheme, and the exemptions from both.
#[derive(Debug, Default)]
struct Proxies {
    /// The proxy of a cleartext origin.
    http: Option<Arc<ProxyEndpoint>>,
    /// The proxy of a TLS origin. If one proxy URL serves both schemes, this
    /// field and `http` hold one endpoint.
    https: Option<Arc<ProxyEndpoint>>,
    /// `true` if `no_proxy` lists `*`, which exempts all hosts.
    exempt_all: bool,
    /// The hosts that `no_proxy` lists.
    exempt: Vec<Exemption>,
}

impl Proxies {
    /// Returns the way to reach a hop at `origin`. This reads the resolved
    /// state and allocates nothing, so the decision costs a fetch one walk of
    /// the exemption list.
    fn via(&self, origin: &Origin) -> Via<'_> {
        let proxy = if origin.tls { &self.https } else { &self.http };
        let Some(proxy) = proxy.as_deref() else {
            return Via::Direct;
        };
        if self.exempt_all || self.exempt.iter().any(|entry| entry.matches(origin)) {
            return Via::Direct;
        }
        match &proxy.kind {
            ProxyKind::Socks(socks) => Via::Socks(proxy, socks),
            ProxyKind::Http { .. } if origin.tls => Via::Tunnel(proxy),
            ProxyKind::Http { .. } => Via::Absolute(proxy),
        }
    }
}

/// The way that one hop reaches its origin.
#[derive(Clone, Copy, Debug)]
enum Via<'a> {
    /// A direct connection to the origin.
    Direct,
    /// A connection to the proxy that carries absolute-form requests. A
    /// cleartext origin behind an HTTP proxy uses this way.
    Absolute(&'a ProxyEndpoint),
    /// A `CONNECT` tunnel through the proxy. A TLS origin behind an HTTP
    /// proxy uses this way.
    Tunnel(&'a ProxyEndpoint),
    /// A connection to the origin through a SOCKS5 proxy. It carries
    /// origin-form requests, as a direct connection does. Each origin behind
    /// a SOCKS5 proxy uses this way.
    Socks(&'a ProxyEndpoint, &'a SocksProxy),
}

/// The body of a request: no body for a fetch and for a `CONNECT`, or the
/// reading end of an upload body. One type serves both, so a fetch and an
/// upload share a pooled connection.
enum RequestBody {
    /// No body: a fetch, a `CONNECT`.
    Empty,
    /// The body of an upload.
    Upload(BodyEnd),
}

impl hyper::body::Body for RequestBody {
    type Data = Bytes;
    type Error = std::io::Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<std::result::Result<Frame<Bytes>, Self::Error>>> {
        match self.get_mut() {
            RequestBody::Empty => Poll::Ready(None),
            RequestBody::Upload(end) => end.poll_frame(cx),
        }
    }

    fn is_end_stream(&self) -> bool {
        match self {
            RequestBody::Empty => true,
            RequestBody::Upload(end) => end.is_end_stream(),
        }
    }

    fn size_hint(&self) -> SizeHint {
        match self {
            RequestBody::Empty => SizeHint::with_exact(0),
            RequestBody::Upload(end) => match end.exchange.exact {
                Some(length) => SizeHint::with_exact(length),
                None => SizeHint::default(),
            },
        }
    }
}

/// The state that one upload body shares between its writer, the reading end
/// that hyper polls, and the upload that waits for the response.
#[derive(Default)]
struct Exchange {
    slot: Mutex<Slot>,
    /// The length of a body given whole, which the request declares. This
    /// field is here, and not in the reading end, so the body of a fetch stays
    /// one pointer wide.
    exact: Option<u64>,
}

/// The one slot through which an upload body passes its frames, and the
/// progress of each party.
#[derive(Default)]
struct Slot {
    /// A frame that the writer handed over and hyper did not take yet. For a
    /// body given whole, this is the remaining part of the body, which hyper
    /// takes in frames of [`UPLOAD_FRAME`].
    frame: Option<Bytes>,
    /// `true` if the writer closed the body, so no frame follows `frame`.
    closed: bool,
    /// The reason for the failure of the body. The writer was dropped before
    /// it closed the body, a frame waited past the stall window, or the
    /// response ended before the body.
    aborted: Option<String>,
    /// `true` if the reading end is dropped, which ends the upload.
    gone: bool,
    /// `true` if the reading end gave hyper the end of the body.
    ended: bool,
    /// `true` if hyper polled the body for a frame.
    polled: bool,
    /// `true` if the body was at its end at the hand-over of the request, so
    /// the request declares a length of zero.
    empty: bool,
    /// The stall window, which runs from the hand-over of the request.
    stall: Option<Stall>,
    /// The writer, which waits for the slot to empty or for the end.
    writer: Option<Waker>,
    /// hyper, which waits for a frame.
    reader: Option<Waker>,
    /// The upload, which waits for the end of the body or for its failure.
    upload: Option<Waker>,
}

/// The stall window of an upload body: the longest time that a frame can wait
/// in the slot for hyper to take it.
#[derive(Clone, Copy)]
struct Stall {
    window: Duration,
    /// The start of the wait of the frame in the slot. This is the latest of
    /// three instants: the frame got its place, hyper took the frame before it
    /// from a body given whole, or the window started.
    since: Instant,
}

impl Slot {
    /// Records that the body is at its end, and gives back the wakers of the
    /// two parties that wait for it.
    fn end(&mut self) -> [Option<Waker>; 2] {
        self.ended = true;
        [self.writer.take(), self.upload.take()]
    }

    /// Fails the body for `reason`, and gives back the wakers of the parties
    /// that read the failure. If the body failed before, the first reason
    /// stays.
    fn abort(&mut self, reason: impl FnOnce() -> String) -> [Option<Waker>; 3] {
        self.aborted.get_or_insert_with(reason);
        [self.reader.take(), self.upload.take(), self.writer.take()]
    }

    /// Returns `true` if the body is at its end: closed, with no frame left to
    /// take and no failure.
    fn at_end(&self) -> bool {
        self.closed && self.frame.is_none() && self.aborted.is_none()
    }

    /// Returns the instant at which the frame in the slot waited the whole
    /// stall window. It is `None` if no frame waits, no window runs, or the
    /// body failed.
    fn stall_end(&self) -> Option<Instant> {
        let stall = self.stall?;
        if self.frame.is_none() || self.aborted.is_some() {
            return None;
        }
        stall.since.checked_add(stall.window)
    }

    /// Starts the wait of the frame in the slot again, from now.
    fn restart_stall(&mut self) {
        if let Some(stall) = &mut self.stall {
            stall.since = Instant::now();
        }
    }
}

/// The state of the body that the upload sees while it waits for the response.
enum Watch {
    /// The body still streams. A body given whole has no writer, so the upload
    /// runs its stall window, which ends at the given instant.
    Streaming(Option<Instant>),
    /// hyper took the end of the body.
    Ended,
    /// The body failed, for the reason given.
    Aborted(String),
}

impl Exchange {
    /// Creates the exchange of a body given whole. The bytes are the frame that
    /// hyper takes in parts, and the body is closed. An empty body holds no
    /// frame, so it is at its end before hyper polls it.
    fn whole(bytes: &Bytes) -> Exchange {
        Exchange {
            slot: Mutex::new(Slot {
                frame: (!bytes.is_empty()).then(|| bytes.clone()),
                closed: true,
                ..Slot::default()
            }),
            exact: Some(bytes.len() as u64),
        }
    }

    fn lock(&self) -> MutexGuard<'_, Slot> {
        self.slot.lock().expect("upload body mutex")
    }

    /// Returns the reason for the failure of the body, if the body failed.
    fn aborted(&self) -> Option<String> {
        self.lock().aborted.clone()
    }

    /// Hands the body to hyper. This starts the stall window of `stall` from
    /// now, and returns `true` if the body is at its end, which the request then
    /// declares. The function does not hand over a failed body, and returns
    /// the reason for the failure as the error.
    fn hand_over(&self, stall: Duration) -> std::result::Result<bool, String> {
        let mut slot = self.lock();
        if let Some(reason) = &slot.aborted {
            return Err(reason.clone());
        }
        slot.stall = Some(Stall {
            window: stall,
            since: Instant::now(),
        });
        slot.empty = slot.at_end();
        let empty = slot.empty;
        let writer = slot.writer.take();
        drop(slot);
        wake(writer);
        Ok(empty)
    }

    /// Returns `true` if hyper took the end of the body, with no failure.
    fn has_ended(&self) -> bool {
        let slot = self.lock();
        slot.ended && slot.aborted.is_none()
    }

    /// Takes the body back from hyper, which gave the request back unwritten.
    /// The stall window stops until the next hand-over.
    fn withdraw(&self) {
        let mut slot = self.lock();
        slot.stall = None;
        slot.empty = false;
    }

    /// Fails the body if the frame in the slot waited the whole stall window,
    /// and returns the reason. If the frame still has time, or no frame waits,
    /// the error holds the instant at which the wait ends.
    fn check_stall(&self) -> std::result::Result<String, Option<Instant>> {
        let mut slot = self.lock();
        let end = slot.stall_end();
        let (Some(end), Some(stall)) = (end, slot.stall) else {
            return Err(None);
        };
        if Instant::now() < end {
            return Err(Some(end));
        }
        let message = format!("the upload body was not taken for {:?}", stall.window);
        let wakers = slot.abort(|| message.clone());
        drop(slot);
        wakers.into_iter().for_each(wake);
        Ok(message)
    }

    /// Fails a body that did not end, because its response ended or was
    /// dropped. hyper stops the body and closes the connection, and the writer
    /// gets a broken pipe.
    fn cut(&self) {
        let mut slot = self.lock();
        if slot.ended {
            return;
        }
        let wakers = slot.abort(|| "the response to the upload ended before its body".into());
        drop(slot);
        wakers.into_iter().for_each(wake);
    }

    /// Returns the state of the body, and registers the upload for a wake when
    /// the state changes.
    fn watch(&self, cx: &mut Context<'_>) -> Watch {
        let mut slot = self.lock();
        if let Some(reason) = &slot.aborted {
            return Watch::Aborted(reason.clone());
        }
        if slot.ended {
            return Watch::Ended;
        }
        park(&mut slot.upload, cx);
        Watch::Streaming(self.exact.and(slot.stall_end()))
    }
}

/// Keeps the waker of `cx` in `waker`, unless the held waker wakes the same
/// task.
fn park(waker: &mut Option<Waker>, cx: &Context<'_>) {
    if !waker
        .as_ref()
        .is_some_and(|held| held.will_wake(cx.waker()))
    {
        *waker = Some(cx.waker().clone());
    }
}

/// Wakes `waker`, if a party left one.
fn wake(waker: Option<Waker>) {
    if let Some(waker) = waker {
        waker.wake();
    }
}

/// Polls a timer toward `end`, and is ready after `end`.
///
/// The function arms the timer once, for the remaining time. If the start of a
/// window moves later, as the stall window does with each frame, `end` moves
/// later and the timer stays. If the timer fires early, the function arms it
/// again for the remaining time.
fn poll_until(timer: &mut Option<rt::Deadline>, cx: &mut Context<'_>, end: Instant) -> Poll<()> {
    loop {
        let left = end.saturating_duration_since(Instant::now());
        if left.is_zero() {
            *timer = None;
            return Poll::Ready(());
        }
        let deadline = timer.get_or_insert_with(|| rt::Deadline::new(left));
        if deadline.poll_expired(cx).is_pending() {
            return Poll::Pending;
        }
        *timer = None;
    }
}

/// The reading end of an upload body, which hyper polls for frames.
///
/// The reading end takes a frame out of the slot as it is. So the bytes of a
/// frame get at most one copy, from the buffer of the caller into the buffer of
/// the writer. A body given whole goes in frames of [`UPLOAD_FRAME`] cut from
/// its bytes, with no copy. A drop of the end wakes the writer, and each later
/// write fails.
struct BodyEnd {
    exchange: Arc<Exchange>,
}

impl BodyEnd {
    /// Creates the reading end of a body given whole.
    fn whole(bytes: &Bytes) -> BodyEnd {
        BodyEnd {
            exchange: Arc::new(Exchange::whole(bytes)),
        }
    }

    /// Polls for the next frame. A failed body fails the poll. So hyper ends
    /// the request unfinished, and does not send a truncated body as a whole
    /// body.
    fn poll_frame(&mut self, cx: &mut Context<'_>) -> Poll<Option<std::io::Result<Frame<Bytes>>>> {
        let mut slot = self.exchange.lock();
        slot.polled = true;
        if let Some(reason) = &slot.aborted {
            return Poll::Ready(Some(Err(std::io::Error::other(reason.clone()))));
        }
        // hyper takes a body given whole in frames. So the end follows the last
        // frame, and the stall window runs over each frame.
        if let Some(held) = &mut slot.frame
            && held.len() > UPLOAD_FRAME
        {
            let frame = held.split_to(UPLOAD_FRAME);
            slot.restart_stall();
            return Poll::Ready(Some(Ok(Frame::data(frame))));
        }
        if let Some(frame) = slot.frame.take() {
            // The last frame of a closed body is also its end. hyper reads the
            // end from `is_end_stream`, and does not poll for it.
            let wakers = if slot.closed {
                slot.end()
            } else {
                [slot.writer.take(), None]
            };
            drop(slot);
            wakers.into_iter().for_each(wake);
            return Poll::Ready(Some(Ok(Frame::data(frame))));
        }
        if slot.closed {
            let wakers = slot.end();
            drop(slot);
            wakers.into_iter().for_each(wake);
            return Poll::Ready(None);
        }
        park(&mut slot.reader, cx);
        Poll::Pending
    }

    /// Returns `true` if the body has no more frames. hyper asks before it
    /// polls, and never polls a body at its end. So this answer records the
    /// end, as a poll that finds the end does.
    ///
    /// hyper frames the request from the first answer. Over HTTP/1.1, a
    /// request at its end at that time carries no length of its own. So before
    /// the first poll, a body is at its end only if the request declared a
    /// length of zero at the hand-over. If a body reached its end after the
    /// hand-over, hyper frames it as a stream, and the stream ends empty.
    fn is_end_stream(&self) -> bool {
        let mut slot = self.exchange.lock();
        if slot.ended {
            return true;
        }
        if !slot.at_end() || !(slot.polled || slot.empty) {
            return false;
        }
        let wakers = slot.end();
        drop(slot);
        wakers.into_iter().for_each(wake);
        true
    }
}

impl Drop for BodyEnd {
    fn drop(&mut self) {
        let mut slot = self.exchange.lock();
        slot.gone = true;
        let writer = slot.writer.take();
        drop(slot);
        wake(writer);
    }
}

/// The request body of an upload, which its response holds. If the response
/// ends or is dropped before the request body ends, the request body fails. So
/// hyper stops the body and closes the connection.
struct RequestEnd {
    exchange: Arc<Exchange>,
}

impl Drop for RequestEnd {
    fn drop(&mut self) {
        self.exchange.cut();
    }
}

type H1Sender = hyper::client::conn::http1::SendRequest<RequestBody>;
type H2Sender = hyper::client::conn::http2::SendRequest<RequestBody>;

/// A connection ready to carry one request.
enum Sender {
    /// An HTTP/1.1 connection, which carries one request at a time. It goes
    /// back to the pool when its response body ends.
    H1(H1Sender),
    /// A handle on a pooled HTTP/2 connection, which multiplexes requests.
    H2(H2Sender),
}

/// The connections pooled under one [`PoolKey`].
#[derive(Default)]
struct PoolEntry {
    /// The HTTP/2 connection of the origin, if one is open.
    h2: Option<H2Sender>,
    /// The idle HTTP/1.1 connections. The connection that became idle last is
    /// at the end.
    h1: Vec<IdleH1>,
}

/// An idle HTTP/1.1 connection in the pool, and the instant at which it became
/// idle.
struct IdleH1 {
    sender: H1Sender,
    since: Instant,
}

/// The shared state of a fetcher. A [`Fetcher`] is a handle on this state.
struct Inner {
    mirrors: Vec<Mirror>,
    headers: Vec<(HeaderName, HeaderValue)>,
    tls: ClientConfigs,
    /// `true` if a handshake has the anchors to verify the peer.
    ///
    /// A store with no anchor gets here only for a fetcher with all mirrors in
    /// cleartext, which opens no handshake that reads the store. The fetcher
    /// then refuses a TLS destination. A bypass variant of [`TrustRoots`] reads
    /// no store and sets `true`, because its handshake reads no anchor and
    /// completes.
    has_trust_anchors: bool,
    /// `true` if a client certificate is configured, so the two client
    /// configurations differ. If none is configured, each connection opens
    /// over one configuration, and the pool holds one entry for each origin.
    client_identity: bool,
    /// The proxy of each scheme, resolved at construction.
    proxies: Proxies,
    /// `true` if a credential can reach a cleartext destination. A fetch and
    /// an upload then skip the cleartext check.
    allow_cleartext_credentials: bool,
    /// The signer of each attempt at the origin that the route names.
    signer: Option<Arc<dyn RequestSigner>>,
    max_retries: u32,
    max_redirects: u32,
    connect_timeout: Duration,
    progress_timeout: Duration,
    low_speed: Option<LowSpeed>,
    fetch_timeout: Option<Duration>,
    /// The number of requests that the gate admits at one time, at least 1.
    max_outstanding: usize,
    gate: Arc<Gate>,
    h2_connection_window: u32,
    pool: Mutex<HashMap<PoolKey, PoolEntry>>,
}

/// Returns the HTTP/2 flow-control window of the connection for a fetcher that
/// admits `max_outstanding` requests: one stream window for each request.
///
/// A receiver credits a window back when the caller reads the data. So a
/// parked stream, whose body the caller received and did not read yet, holds
/// its own credit. The connection window is the sum of the stream windows, so
/// that credit belongs to the parked stream alone. Each other open stream
/// still has a full window.
///
/// For example, the HTTP pull of `ostrya` parks content bodies while they wait
/// for a write permit. The metadata object that its scan waits for uses the
/// same connection.
///
/// The cost is the received and unread data that one connection can hold,
/// which is this window. At the default limit of 8, it is 16 MiB.
fn h2_connection_window(max_outstanding: usize) -> u32 {
    u32::try_from(max_outstanding)
        .unwrap_or(u32::MAX)
        .saturating_mul(H2_STREAM_WINDOW)
        .min(H2_MAX_WINDOW)
}

/// A failed attempt, and the retry class of the failure.
enum Failure {
    /// A transport failure, or a status that a later attempt can avoid.
    Retry(Error),
    /// A definitive answer, which a retry gets again.
    Fatal(Error),
    /// A failure of the request itself, which no destination and no round
    /// avoids. The request ends with it.
    Final(Error),
}

impl Failure {
    /// Returns the error of the failure, of any kind.
    fn into_error(self) -> Error {
        match self {
            Failure::Retry(e) | Failure::Fatal(e) | Failure::Final(e) => e,
        }
    }

    /// Returns the cap of the error body that `keep` asks for in the class of
    /// this failure, or `None` if the fetch keeps no response of this class.
    fn kept_cap(&self, keep: Option<Keep>) -> Option<u64> {
        let keep = keep?;
        match self {
            Failure::Retry(_) if keep.retryable => Some(keep.cap),
            Failure::Fatal(_) if keep.definitive => Some(keep.cap),
            _ => None,
        }
    }

    /// Puts `kept` into the [`Error::HttpStatus`] of this failure. A failure
    /// of another error stays as it is.
    fn with_response(mut self, kept: ErrorResponse) -> Failure {
        if let Failure::Retry(Error::HttpStatus { response, .. })
        | Failure::Fatal(Error::HttpStatus { response, .. }) = &mut self
        {
            *response = Some(Box::new(kept));
        }
        self
    }
}

/// The unsuccessful responses that one attempt of a fetch keeps.
///
/// A fetch reports one failure. So an attempt reads the body of a failure
/// only if that failure can become the reported one. The flags follow the
/// failure order of [`Fetcher::fetch`].
#[derive(Clone, Copy)]
struct Keep {
    /// The largest body to keep: [`FetchRequest::max_error_body`].
    cap: u64,
    /// `true` if no failure is reported yet, so a retryable failure becomes
    /// the reported one.
    retryable: bool,
    /// `true` if no definitive answer is reported yet, so a definitive answer
    /// becomes the reported one.
    definitive: bool,
}

/// A failed upload attempt, and the send state of the request.
enum UploadFailure {
    /// No part of the request reached hyper, so the round continues as for a
    /// fetch.
    Unsent(Failure),
    /// hyper took the request, so the upload ends with
    /// [`Error::UploadInterrupted`] of this URL and message. A sent request has
    /// no other error, and [`Error::is_unsent`] reads this fact.
    Sent { url: String, message: String },
}

/// The parts that each attempt of one upload sends, set before admission.
struct UploadPlan<'a> {
    method: Method,
    headers: &'a [(HeaderName, HeaderValue)],
    /// The bytes of a body given whole, which a followed redirect sends again.
    /// A streamed body has none, so the upload follows no redirect of it.
    bytes: Option<Bytes>,
    /// The SHA-256 of `bytes` that the caller gave, which the signer of each
    /// attempt and each hop gets.
    sha256: Option<[u8; 32]>,
    max_response: u64,
    response_timeout: Duration,
}

/// The result of a request after hyper took it.
enum Answer<T> {
    /// The send resolved, with the response head or the error of hyper.
    Sent(T),
    /// The writer failed the body before the response head arrived.
    Aborted(String),
    /// The body ended, and no response head followed within the window.
    Silent(Duration),
}

/// The response of one upload hop, its protocol, and the body that the request
/// sent. It also holds the HTTP/1.1 connection, which goes back to the pool
/// when the response body ends.
type UploadResponse = (Response<Incoming>, Protocol, RequestEnd, Option<H1Sender>);

/// An async HTTP client for one remote.
///
/// A fetcher holds the mirrors, the headers, the credentials, and the TLS
/// configuration of one remote. It serves a [`Target::Url`] from the origin of
/// that URL. A clone of a `Fetcher` is another handle on the same connection
/// pool and the same admission limit. The clone has the counters of received
/// body bytes of the handle. [`counted`](Fetcher::counted) gives a handle with
/// more counters.
///
/// # Connections
///
/// The TLS handshake selects the protocol: ALPN offers `h2` and `http/1.1`,
/// and the fetcher uses the choice of the server. Over cleartext, the fetcher
/// uses HTTP/1.1. The pool keeps one HTTP/2 connection for each origin, which
/// carries concurrent requests. The pool also keeps HTTP/1.1 connections, and
/// the fetcher uses one again after the previous body is read to the end.
///
/// The pool key holds three terms:
///
/// - the endpoint of the connection
/// - `true` if the connection presents the configured client certificate
/// - `true` if the connection goes to a proxy and carries absolute-form
///   requests.
///
/// The endpoint is the origin for a direct connection and for a tunnel,
/// because the TLS of a tunnel reaches the origin. For a proxied cleartext
/// connection, the endpoint is the proxy. So a URL target shares a connection
/// with a mirror at the same origin. One proxy connection carries requests for
/// all cleartext origins.
///
/// An unsuccessful status, or a `Content-Length` over the cap of the request,
/// ends an attempt with a response body in flight. If the declared length is
/// 64 KiB or less, the fetcher reads the body to the end. The HTTP/1.1
/// connection then goes back to the pool. If the declared length is larger, or
/// if there is none, the fetcher closes the connection.
///
/// A fetch with [`FetchRequest::max_error_body`] reads the body of the
/// unsuccessful response that it reports, up to that cap, with or without a
/// declared length. If the body ends within the cap, the HTTP/1.1 connection
/// goes back to the pool. If it grows past the cap, the fetcher closes the
/// connection. [`Fetcher::fetch`] states the rule in "Error responses".
///
/// A 404 is the usual answer for an object that the remote does not hold. So
/// this rule saves a connection setup for each absent object.
///
/// All such reads of one attempt share one
/// [`progress_timeout`](FetcherOptions::progress_timeout)
/// window, whatever the number of hops. A peer can declare a short body on
/// each hop and send fewer bytes. Such a peer uses the window once, and the
/// later reads close their connections and do not wait.
#[derive(Clone)]
pub struct Fetcher {
    inner: Arc<Inner>,
    /// The counters that get the bytes of each response body of a request of
    /// this handle.
    received: Arc<[Arc<AtomicU64>]>,
}

impl std::fmt::Debug for Fetcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Fetcher")
            .field("mirrors", &self.inner.mirrors)
            .finish_non_exhaustive()
    }
}

impl Fetcher {
    /// Creates a fetcher from `options`.
    ///
    /// If the mirror list is empty, the fetcher serves [`Target::Url`] requests
    /// alone.
    ///
    /// The fetcher has no counters of received body bytes.
    /// [`counted`](Fetcher::counted) gives a handle that has counters.
    ///
    /// The function is async, because [`TrustRoots::System`], the default,
    /// reads the host trust store on the blocking pool. An encrypted client key
    /// runs a key derivation function, also on the blocking pool. Under
    /// [`TrustRoots::Pem`] with no encrypted client key, all work is in memory,
    /// and the function never yields.
    ///
    /// # Trust store
    ///
    /// The function reads the system store if a fetch can open a handshake.
    /// That is the case if one or more mirrors are `https`. It is also the case
    /// if the mirror list is empty, because a request can then name an `https`
    /// URL. It is also the case if
    /// [`max_redirects`](FetcherOptions::max_redirects) is more than zero,
    /// because a redirect can go to an `https` URL.
    ///
    /// A fetcher with all mirrors `http` that follows no redirect reads no
    /// store and holds no trust anchor. If a request of that fetcher names an
    /// `https` URL of its own, the fetch fails before admission and names the
    /// origin. A host without a CA bundle still reaches a cleartext remote.
    ///
    /// Under either bypass variant of [`TrustRoots`], the function reads no
    /// store, so an empty host store fails no fetcher. With no encrypted client
    /// key, the function then never yields.
    ///
    /// # Errors
    ///
    /// - [`Error::Fetch`] if a [`low_speed`](FetcherOptions::low_speed) rule
    ///   holds a zero.
    /// - [`Error::Fetch`] if a mirror URL is not a valid absolute URL, has no
    ///   usable host, or holds a query string or userinfo.
    /// - [`Error::Fetch`] if a mirror URL names a port that is not a number
    ///   from 0 to 65535.
    /// - [`Error::Unsupported`] if the scheme of a mirror URL is neither `http`
    ///   nor `https`.
    /// - [`Error::Fetch`] if a header name or value is invalid, or if a header
    ///   is `Host` or a header that the connection layer sets.
    /// - [`Error::Fetch`] if a mirror is cleartext `http`, the options hold a
    ///   credential, and
    ///   [`allow_cleartext_credentials`](FetcherOptions::allow_cleartext_credentials)
    ///   is `false`. The credentials are
    ///   [`basic_auth`](FetcherOptions::basic_auth),
    ///   [`bearer_token`](FetcherOptions::bearer_token),
    ///   [`signer`](FetcherOptions::signer), and an `Authorization`,
    ///   `Proxy-Authorization`, or `Cookie` header.
    /// - [`Error::Fetch`] if two of these are set:
    ///   [`basic_auth`](FetcherOptions::basic_auth),
    ///   [`bearer_token`](FetcherOptions::bearer_token),
    ///   [`signer`](FetcherOptions::signer), and an `Authorization` header.
    /// - [`Error::Fetch`] if [`basic_auth`](FetcherOptions::basic_auth) does
    ///   not give a valid header value, or if
    ///   [`bearer_token`](FetcherOptions::bearer_token) does not have the
    ///   token68 syntax.
    /// - [`Error::Unsupported`] if a proxy URL is not `http://host[:port]`,
    ///   `socks5://host[:port]`, or `socks5h://host[:port]`, from the options
    ///   or from the environment.
    /// - [`Error::Unsupported`] if a SOCKS5 URL has a userinfo that
    ///   [SOCKS proxies](Proxy#socks-proxies) refuses.
    /// - [`Error::Unsupported`] if a proxy variable has
    ///   [white space](Proxy#white-space) around its value.
    /// - [`Error::Fetch`] if the TLS material does not parse or the TLS setup
    ///   fails: a trust anchor, the client certificate, or the client key.
    ///   [`ClientIdentity::key_passphrase`] states the refusals of a client
    ///   key.
    /// - [`Error::Fetch`] if the system trust store holds no certificate, and
    ///   one or more mirrors are `https` or the mirror list is empty.
    pub async fn new(options: FetcherOptions) -> Result<Fetcher> {
        let Checked {
            mirrors,
            headers,
            proxies,
        } = checked(&options)?;
        let max_outstanding = options.max_outstanding.max(1);
        // A fetcher with no mirror serves the URLs of its requests, and each
        // of them can be `https`. So it must hold trust anchors, and an empty
        // system store fails it as for an `https` mirror.
        let https = mirrors.is_empty() || mirrors.iter().any(|mirror| mirror.origin.tls);
        // A redirect can go from a cleartext mirror to a TLS origin. So the
        // fetcher also reads the system store if it follows redirects. If not,
        // a fetch reaches a TLS origin only through a URL target of its own. A
        // fetcher with no anchor refuses that target before admission.
        let reaches_tls = https || options.max_redirects > 0;
        let tls = client_config(&options.tls, options.http2, https, reaches_tls).await?;
        Ok(Fetcher {
            inner: Arc::new(Inner {
                mirrors,
                headers,
                has_trust_anchors: tls.has_trust_anchors,
                client_identity: options.tls.client_identity.is_some(),
                proxies,
                tls,
                allow_cleartext_credentials: options.allow_cleartext_credentials,
                signer: options.signer,
                max_retries: options.max_retries,
                max_redirects: options.max_redirects,
                connect_timeout: options.connect_timeout,
                progress_timeout: options.progress_timeout,
                low_speed: options.low_speed,
                fetch_timeout: options.fetch_timeout,
                max_outstanding,
                gate: Arc::new(Gate::new(max_outstanding)),
                h2_connection_window: h2_connection_window(max_outstanding),
                pool: Mutex::new(HashMap::new()),
            }),
            received: Arc::new([]),
        })
    }

    /// Returns a handle on this fetcher that adds counters of received body
    /// bytes.
    ///
    /// The new handle shares the configuration, the connection pool, and the
    /// admission limit of this handle. It has the counters of this handle and
    /// the counters of `received`. A clone of the new handle has the same
    /// counters.
    ///
    /// Each response body of a request of the new handle adds the bytes that it
    /// takes from the connection to each of these counters. This includes the
    /// bytes of a body before it fails, the body of a [`Refetch`], a body that
    /// the caller drops before its end, and the response body of an upload. A
    /// request does not count a response that it does not give to the caller:
    /// a redirect, an unsuccessful status of a fetch, or a refused response.
    ///
    /// The counters of this handle do not change. A request of this handle adds
    /// to the counters of this handle alone. Each data frame costs one relaxed
    /// atomic add for each counter. A counter that the new handle has two times
    /// gets the bytes two times.
    pub fn counted(&self, received: Vec<Arc<AtomicU64>>) -> Fetcher {
        Fetcher {
            inner: Arc::clone(&self.inner),
            received: self.received.iter().cloned().chain(received).collect(),
        }
    }

    /// Returns the number of requests that the fetcher admits at one time.
    ///
    /// The number is [`max_outstanding`](FetcherOptions::max_outstanding) of
    /// the options, and at least 1. All handles on one fetcher share this
    /// limit.
    pub fn max_outstanding(&self) -> usize {
        self.inner.max_outstanding
    }

    /// Sends `request` to each destination in turn and returns the response.
    ///
    /// # Retries
    ///
    /// A path target goes to each mirror, in the mirror order. The fetch
    /// resolves the destination of a mirror at the attempt that uses it. A URL
    /// target has the one destination that the URL names.
    ///
    /// - The fetch tries each destination in order before it retries.
    /// - Transport failures and the statuses 408, 429, and 5xx are retryable.
    ///   If one or more destinations of a round fail that way, the fetch
    ///   repeats the round, up to [`max_retries`](FetcherOptions::max_retries)
    ///   times. The delay before a repeat starts at 250ms, doubles each time,
    ///   and stops at two seconds. A repeated round asks only the destinations
    ///   with a retryable failure.
    /// - Each other unsuccessful status is definitive. The fetch does not ask
    ///   that destination again, because its answer is the same in each round.
    /// - The expiry of [`connect_timeout`](FetcherOptions::connect_timeout),
    ///   [`progress_timeout`](FetcherOptions::progress_timeout), or
    ///   [`low_speed`](FetcherOptions::low_speed) is a transport failure. The
    ///   fetch reports the deadline that expires first.
    ///
    /// The hop count belongs to one attempt. So a retryable status on a hop
    /// makes the whole attempt retryable, and the repeated round starts again
    /// from the destination that the route names.
    ///
    /// The retries end at the response head. So if a body fails in transit,
    /// the caller gets a failed read. [`refetching`](Fetcher::refetching)
    /// fetches such a body again.
    ///
    /// # Failure order
    ///
    /// A fetch ends if it has no more destinations to ask or no more rounds to
    /// repeat. In both cases, it reports a definitive answer if it got one, and
    /// the first retryable failure if not.
    ///
    /// A caller can act on a definitive answer, for example a 404 that states
    /// that the object is absent. So the fetch reports such an answer from any
    /// round, and an earlier retryable failure does not hide it. Of two
    /// definitive answers, the fetch reports the answer that it got first. In
    /// one round, this is the mirror order.
    ///
    /// # Redirects
    ///
    /// A 301, 302, 303, 307, or 308 sends the attempt to the URL of its
    /// `Location` header, up to [`max_redirects`](FetcherOptions::max_redirects)
    /// times. A fetch is a GET, so none of the five statuses changes the method
    /// of the next hop.
    ///
    /// The fetch resolves `Location` against the URL of the response that
    /// carries it. So `Location` can be an absolute URL, a relative URL
    /// (`/other/path`, `sibling`), or a scheme-relative URL (`//host/path`). The
    /// resolution normalizes the result, as the HTTP specification prescribes
    /// for a redirect:
    ///
    /// - it resolves dot segments
    /// - it reads a backslash as a path separator
    /// - it removes tabs and newlines
    /// - it percent-encodes a character that a path or a query cannot carry
    /// - it puts an IPv4 or an IPv6 host in canonical form
    /// - it removes the fragment.
    ///
    /// A [`Target::Url`] goes on the wire as the caller wrote it. So one string
    /// as a URL target and as a `Location` can reach the server as two
    /// different request targets.
    ///
    /// The fetch refuses two hops definitively: a scheme other than `http` or
    /// `https`, and a hop from `https` to `http`. The [`Error::Fetch`] names the
    /// URL that redirected and the URL that it named. On a fetcher with no trust
    /// anchors, the fetch also refuses a hop to a TLS origin in the same way.
    /// The fetch follows a hop from `http` to `https`.
    ///
    /// If a response with one of the five statuses has no readable `Location`,
    /// the fetch reports the status, whatever the hop count. This applies to a
    /// missing header, an empty value, a value that is not text, and a value
    /// that the resolution cannot read.
    ///
    /// `Authorization`, `Proxy-Authorization`, and `Cookie` from either layer
    /// go to the origin that the route names. They also go to a hop at that
    /// origin: the same scheme, host, and port.
    ///
    /// The fetch removes them for a hop at another origin. A removed credential
    /// stays removed for the rest of the attempt, also on a redirect back to the
    /// named origin. At that time, the operator of the other origin has the
    /// request.
    ///
    /// The [`signer`](FetcherOptions::signer) follows the same rule. It signs
    /// each hop at the named origin again, with the path and the query of that
    /// hop. It signs no hop at another origin, and no hop after it.
    ///
    /// All other headers go to each hop, also the `User-Agent` and the
    /// `Accept-Encoding` of the fetcher. The fetcher presents a configured
    /// client certificate to the origin that the route names and to no other
    /// origin. So the pool keeps the connections with the certificate apart
    /// from the connections without it.
    ///
    /// The fetch discards the body of an intermediate response as it discards
    /// the body of an unsuccessful response. So if the declared length is
    /// 64 KiB or less, the HTTP/1.1 connection goes back to the pool. A chain
    /// on one origin then uses one connection. If the intermediate responses
    /// declare more, or declare no length, the chain opens a connection for
    /// each hop.
    ///
    /// The validators go to each hop, because they describe the resource. A 304
    /// from any hop is the answer of the fetch. Each message names the URL of
    /// the response that answered, which after a redirect is the URL of the
    /// last hop.
    ///
    /// A fetch with [`max_error_body`](FetchRequest::max_error_body) and a
    /// redirect limit of zero gets each redirect as an unsuccessful response.
    /// So the caller reads its headers, for example `Location`, and its body.
    /// A redirect with no usable `Location` is also such a response, at any
    /// limit.
    ///
    /// # Content coding
    ///
    /// A fetch gives the bytes that the remote stores. So each request carries
    /// `Accept-Encoding: identity`, and asks for no content coding. Without
    /// that header, a server can compress a response and stay within the HTTP
    /// specification. The body then holds bytes other than the bytes that the
    /// checksum of the object names.
    ///
    /// A 200 response, or a 206 response to a request with a `Range` header,
    /// fails the attempt definitively with [`Error::ContentEncoded`] in two
    /// cases: a `Content-Encoding` other than `identity`, or a
    /// `Transfer-Encoding` other than `chunked` and `identity`. This applies
    /// whichever layer asked for the coding. A caller that wants a coded body
    /// decodes it outside the fetcher.
    ///
    /// `chunked` frames a message, and the connection removes the framing. So a
    /// response with `chunked` alone gives the body as the remote wrote it.
    ///
    /// # Error responses
    ///
    /// A request with [`max_error_body`](FetchRequest::max_error_body) set
    /// keeps the unsuccessful response that the fetch reports. The
    /// [`Error::HttpStatus`] then carries an [`ErrorResponse`] with the
    /// headers of that response, and its body if the whole body is within the
    /// cap.
    ///
    /// - The fetch reads the body of a failure only if the failure order can
    ///   make it the reported failure. So it reads at most two bodies: one
    ///   retryable failure and one definitive answer. It keeps the body of the
    ///   failure that it reports. If each round gets a 503, the error holds
    ///   the body of the first 503. A 503 and then a 404 give the body of the
    ///   404.
    /// - The read takes place within one
    ///   [`progress_timeout`](FetcherOptions::progress_timeout) from the
    ///   arrival of the response head, and within
    ///   [`fetch_timeout`](FetcherOptions::fetch_timeout). The
    ///   [`low_speed`](FetcherOptions::low_speed) rule does not apply to it.
    /// - The fetch does not read a body that declares a coding or a
    ///   `Content-Length` over the cap. It discards such a body, and the error
    ///   holds the headers alone.
    /// - A body that grows past the cap, fails in transit, or does not end
    ///   within the window gives the headers alone. The fetch keeps no part of
    ///   such a body.
    /// - The read never changes the retry class of the status.
    /// - The bytes of a kept body do not count in the received counters of
    ///   [`counted`](Fetcher::counted).
    ///
    /// Before a body arrives, the read reserves at most 64 KiB, whatever
    /// length the response declares. The capacity of a kept body never passes
    /// the cap. So the fetch holds at most two kept bodies of at most the cap
    /// each, one retryable and one definitive, and one frame of the body that
    /// it reads.
    ///
    /// # Errors
    ///
    /// If more than one destination fails, the failure order rules state which
    /// error the fetch reports.
    ///
    /// - [`Error::Fetch`] before admission, if a path holds a `?` or a `#`, if
    ///   the query of a [`Target::PathQuery`] holds a `#`, or if the fetcher
    ///   has no mirror for a path target.
    /// - [`Error::Fetch`] before admission, if a URL target is not a valid
    ///   absolute URL, or holds a fragment or userinfo.
    /// - [`Error::Unsupported`] if the scheme of a URL target is neither `http`
    ///   nor `https`.
    /// - [`Error::Fetch`] before admission, if a request header is invalid, is
    ///   `Host`, or is a header that the connection layer sets.
    /// - [`Error::Fetch`] before admission, if an `Authorization` header is
    ///   beside [`basic_auth`](FetchRequest::basic_auth).
    /// - [`Error::Fetch`] before admission, if the fetcher has a
    ///   [`signer`](FetcherOptions::signer) and the request carries
    ///   [`basic_auth`](FetchRequest::basic_auth) or an `Authorization`
    ///   header.
    /// - [`Error::Fetch`] before admission, if a credential can reach a
    ///   cleartext destination, and neither
    ///   [`allow_cleartext_credentials`](FetchRequest::allow_cleartext_credentials)
    ///   nor [`FetcherOptions::allow_cleartext_credentials`] is `true`.
    /// - [`Error::Fetch`] before admission, if the route names a TLS
    ///   destination and the fetcher holds no trust anchors.
    /// - [`Error::Fetch`] if a transport failure ends the rounds: a failed
    ///   connect, handshake, or proxy `CONNECT`, or an expired deadline.
    /// - [`Error::Fetch`] if the fetch refuses a redirect hop, as the redirect
    ///   rules state.
    /// - [`Error::Fetch`] if hyper cannot build a request, for example from an
    ///   invalid validator value.
    /// - The error of the [`signer`](FetcherOptions::signer), unchanged, if it
    ///   fails. An [`Error::Fetch`] if it returns a header that a request
    ///   cannot carry. The fetch asks no other destination and repeats no
    ///   round.
    /// - [`Error::Fetch`] if [`fetch_timeout`](FetcherOptions::fetch_timeout)
    ///   expires.
    /// - [`Error::HttpStatus`] if a destination answers with a status other
    ///   than 200 and 304, and other than 206 to a request with a `Range`
    ///   header. This includes a redirect that the fetch does not
    ///   follow: no usable `Location`, or a redirect limit of zero. With
    ///   [`max_error_body`](FetchRequest::max_error_body), the error carries
    ///   the response, as the rules of the error responses state.
    /// - [`Error::RedirectLimit`] if an attempt follows
    ///   [`max_redirects`](FetcherOptions::max_redirects) redirects, and the
    ///   next response names a URL to follow.
    /// - [`Error::FetchTooLarge`] if the `Content-Length` of the response is
    ///   more than [`max_size`](FetchRequest::max_size).
    /// - [`Error::ContentEncoded`] if the response declares a coding, as the
    ///   content coding rules state.
    pub async fn fetch(&self, request: FetchRequest<'_>) -> Result<Fetched> {
        let (fetched, _) = self
            .fetch_budgeted(&request, self.inner.max_retries)
            .await?;
        Ok(fetched)
    }

    /// Returns a [`Refetch`] of `request`, which fetches a failed body again.
    ///
    /// [`Refetch`] states the budget that it spends.
    pub fn refetching<'r>(&self, request: FetchRequest<'r>) -> Refetch<'_, 'r> {
        Refetch {
            fetcher: self,
            request,
            left: self.inner.max_retries,
            round: 0,
            interrupted: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Fetches `request`, and repeats a round at most `max_retries` times.
    /// Returns the response and the number of repeated rounds.
    async fn fetch_budgeted(
        &self,
        request: &FetchRequest<'_>,
        max_retries: u32,
    ) -> Result<(Fetched, u32)> {
        // The target, the headers, their credentials, and the anchors of a TLS
        // destination are the same for each destination. So the fetch settles
        // all of them before admission, and reports a failure of them one
        // time. The fetch takes no permit and opens no socket for it.
        let route = self.route(request.target)?;
        if self.inner.signer.is_some() {
            refuse_beside_signer(request.headers, request.basic_auth.is_some(), false)?;
        }
        let headers = merge_headers(
            &self.inner.headers,
            request.headers,
            request.basic_auth,
            None,
        )?;
        if !request.allow_cleartext_credentials && !self.inner.allow_cleartext_credentials {
            self.check_cleartext(&route, &headers)?;
        }
        self.check_trust_anchors(&route)?;
        let permit = self.inner.gate.acquire(request.priority).await;
        let rounds = self.rounds(request, &route, &headers, permit, max_retries);
        let Some(limit) = self.inner.fetch_timeout else {
            return rounds.await;
        };
        // The expiry drops the rounds, the attempt in flight, and the permit.
        // So the slot is free before the fetch reports the failure.
        match within(limit, rounds).await {
            Some(result) => result,
            None => Err(Error::Fetch(format!(
                "fetch of {} timed out after {limit:?}",
                request.target.describe()
            ))),
        }
    }

    /// Returns the destinations of a fetch of `target`, in the order in which
    /// the fetch asks them.
    ///
    /// A URL target names its one destination, which this function parses. A
    /// path target, with or without a query, goes to the mirrors, so a
    /// fetcher with no mirror has no destination for it. The signer of the
    /// fetcher, if there is one, gives the form of the path.
    fn route<'a>(&self, target: Target<'a>) -> Result<Route<'a>> {
        let (path, query) = match target {
            Target::Url(url) => return Ok(Route::One(parse_url(url)?)),
            Target::Path(path) => (path, None),
            Target::PathQuery { path, query } => (path, Some(query)),
        };
        check_path(path)?;
        if let Some(query) = query {
            check_query(path, query)?;
        }
        if self.inner.mirrors.is_empty() {
            return Err(Error::Fetch(format!(
                "fetch of the path {} has nowhere to go: no mirror is configured",
                target.describe()
            )));
        }
        let path = match &self.inner.signer {
            Some(signer) => signer.request_path(path)?,
            None => Cow::Borrowed(path),
        };
        Ok(Route::Mirrors { path, query })
    }

    /// Refuses a fetch if its headers carry a credential that a cleartext
    /// destination gets in the clear.
    ///
    /// The fetch withholds a credential from no destination. So one cleartext
    /// destination that the fetch can reach is enough for a refusal. The other
    /// choice is to send the request without the credential, which gets a 401
    /// that names nothing.
    fn check_cleartext(
        &self,
        route: &Route<'_>,
        headers: &[(HeaderName, HeaderValue)],
    ) -> Result<()> {
        // A signer is a credential, as an `Authorization` header is.
        if self.inner.signer.is_none() && !headers.iter().any(|(name, _)| is_credential(name)) {
            return Ok(());
        }
        let Some(origin) = self.matching_origin(route, |origin| !origin.tls) else {
            return Ok(());
        };
        Err(Error::Fetch(format!(
            "the request carries credentials, which the cleartext origin {origin} would receive \
             in the clear: fetch over https, drop the credentials, or set \
             allow_cleartext_credentials on the request or on the fetcher"
        )))
    }

    /// Refuses a fetch if its route names a TLS destination and the fetcher
    /// holds no trust anchors.
    ///
    /// A handshake verifies the server certificate against an anchor, so a
    /// fetcher with no anchor reaches no TLS origin. Only one case gets here: a
    /// request with an `https` URL of its own, on a fetcher with all mirrors in
    /// cleartext. Each other combination of route and anchors fails
    /// [`Fetcher::new`].
    ///
    /// The handshake reports this case as an unknown issuer. That is a
    /// retryable failure, which uses all rounds and all backoffs before it
    /// names the cause. So the refusal occurs here, before admission.
    ///
    /// This function reads the destinations of the route. A redirect hop names
    /// an origin that the server chooses. If that origin is TLS, the attempt
    /// refuses the hop with the same message, from [`no_trust_anchors`].
    fn check_trust_anchors(&self, route: &Route<'_>) -> Result<()> {
        if self.inner.has_trust_anchors {
            return Ok(());
        }
        let Some(origin) = self.matching_origin(route, |origin| origin.tls) else {
            return Ok(());
        };
        Err(no_trust_anchors(origin))
    }

    /// Returns the `scheme://authority` of the first destination on `route`
    /// whose origin `wanted` accepts.
    fn matching_origin<'a>(
        &'a self,
        route: &'a Route<'_>,
        wanted: fn(&Origin) -> bool,
    ) -> Option<&'a str> {
        match route {
            Route::Mirrors { .. } => self
                .inner
                .mirrors
                .iter()
                .find(|mirror| wanted(&mirror.origin))
                .map(|mirror| mirror.prefix.as_str()),
            Route::One(destination) => {
                wanted(&destination.origin).then(|| destination.origin_url())
            }
        }
    }

    /// Tries each destination in turn, and repeats the round at most
    /// `max_retries` times while a destination has a retryable failure.
    ///
    /// The permit moves into the body of a successful round. If no round
    /// succeeds, the permit drops with this future. A success returns the
    /// number of repeated rounds.
    async fn rounds(
        &self,
        request: &FetchRequest<'_>,
        route: &Route<'_>,
        headers: &[(HeaderName, HeaderValue)],
        permit: Permit,
        max_retries: u32,
    ) -> Result<(Fetched, u32)> {
        let destination_count = match route {
            Route::Mirrors { .. } => self.inner.mirrors.len(),
            Route::One(_) => 1,
        };
        // A destination with a definitive answer gives the same answer in each
        // round, so the fetch asks it once. A repeated round asks only the
        // destinations with a retryable failure.
        let mut settled = vec![false; destination_count];
        // The failure that both end paths report. A caller can act on a
        // definitive answer, so it has priority over a retryable failure from
        // any round. A destination that fails transiently and then answers 404
        // reports the 404. Of two failures of one kind, the fetch keeps the
        // first.
        let mut reported: Option<Error> = None;
        let mut definitive = false;
        let mut round = 0;
        loop {
            let mut retryable = false;
            for (position, settled) in settled.iter_mut().enumerate() {
                if *settled {
                    continue;
                }
                // The fetch builds the destination of a mirror for the attempt
                // that uses it. So if the first mirror answers, the fetch builds
                // one destination, whatever the length of the list.
                let destination = match route {
                    Route::Mirrors { path, query } => {
                        Cow::Owned(self.inner.mirrors[position].destination(path, *query))
                    }
                    Route::One(destination) => Cow::Borrowed(destination),
                };
                // A fetch reads at most one body of each class, and keeps the
                // body of the failure that it reports.
                let keep = request.max_error_body.map(|cap| Keep {
                    cap,
                    retryable: reported.is_none(),
                    definitive: !definitive,
                });
                let failure = match self.attempt(&destination, request, headers, keep).await {
                    Ok(Attempted::Body(mut body)) => {
                        body.permit = Some(permit);
                        return Ok((Fetched::Body(body), round));
                    }
                    Ok(Attempted::NotModified) => return Ok((Fetched::NotModified, round)),
                    Err(failure) => failure,
                };
                match failure {
                    Failure::Retry(e) => {
                        retryable = true;
                        if reported.is_none() {
                            reported = Some(e);
                        }
                    }
                    Failure::Fatal(e) => {
                        *settled = true;
                        if !definitive {
                            reported = Some(e);
                            definitive = true;
                        }
                    }
                    Failure::Final(e) => return Err(e),
                }
            }
            // If `retryable` is `true`, one or more destinations had a
            // retryable failure. If not, each destination gave a definitive
            // answer.
            if !retryable || round >= max_retries {
                return Err(reported.expect("a failed round holds a failure"));
            }
            round += 1;
            rt::Timer::after(backoff(round)).await;
        }
    }

    /// Sends one request to one destination, and follows the redirects of the
    /// responses.
    ///
    /// The hop state of an attempt is three locals:
    ///
    /// - the destination, borrowed from the route until a redirect names
    ///   another destination
    /// - the header list, which is the slice of the caller until a hop goes to
    ///   another origin and a credential must come out
    /// - the count of followed redirects.
    ///
    /// If the first response answers the request, all three stay as they
    /// start. So the attempt builds no URL, copies no header list, and
    /// allocates no bookkeeping.
    ///
    /// `keep` names the unsuccessful responses whose headers and body the
    /// attempt keeps in the failure. The attempt discards each other one.
    async fn attempt(
        &self,
        destination: &Destination,
        request: &FetchRequest<'_>,
        headers: &[(HeaderName, HeaderValue)],
        keep: Option<Keep>,
    ) -> std::result::Result<Attempted, Failure> {
        // The origin that the route names. A credential and the client
        // certificate go to this origin alone.
        let named = &destination.origin;
        let mut hop = Cow::Borrowed(destination);
        let mut headers = Cow::Borrowed(headers);
        // The signer signs each hop at the named origin. It stays out of the
        // attempt after a hop at another origin, as a credential does.
        let mut signer = self.inner.signer.as_deref();
        let mut followed = 0u32;
        let progress_timeout = self.inner.progress_timeout;
        // One progress window applies to all drains of this attempt, whatever
        // the number of hops. A peer can answer each hop with a short declared
        // body and then stop. Such a peer uses the window on the first drain,
        // and each later drain drops its connection and does not wait. An
        // attempt that follows no redirect drains once, with the whole window.
        let drain_until = Instant::now() + progress_timeout;
        // The response head must arrive within the time of the rule, in whole
        // seconds, from the start of the attempt, whatever the number of hops.
        // A head carries no bytes for the rate. So the rate stays under the
        // limit from the start until the head arrives.
        let head_until = self
            .inner
            .low_speed
            .and_then(|low| Some((Instant::now().checked_add(low.whole_seconds())?, low)));
        loop {
            // Each hop makes its own proxy decision. The pool keeps a cleartext
            // hop behind a proxy under the proxy endpoint. So one such
            // connection carries requests for all cleartext origins.
            let via = self.inner.proxies.via(&hop.origin);
            let key = PoolKey {
                origin: match via {
                    Via::Absolute(proxy) => proxy.endpoint.clone(),
                    Via::Direct | Via::Tunnel(_) | Via::Socks(..) => hop.origin.clone(),
                },
                identity: self.presents_identity(&hop.origin, named),
                proxied: matches!(via, Via::Absolute(_)),
            };
            let url = hop.url();
            let (response, protocol, reuse) = match head_until {
                None => {
                    self.send(&hop, &key, via, request, &headers, signer)
                        .await?
                }
                Some((until, low)) => {
                    // The send is in a box under the window. So the attempt
                    // holds one inline copy of its state.
                    let sent = Box::pin(self.send(&hop, &key, via, request, &headers, signer));
                    match within(until.saturating_duration_since(Instant::now()), sent).await {
                        Some(result) => result?,
                        None => return Err(Failure::Retry(too_slow(url, low))),
                    }
                }
            };
            let status = response.status();
            if status == StatusCode::NOT_MODIFIED {
                // A 304 carries no body, so the connection is ready for the
                // next request immediately. The validators describe the
                // resource. So a 304 from a hop is the answer for the caller,
                // as a 304 from the named destination is.
                if let Some(sender) = reuse {
                    self.inner.put_h1(&key, sender);
                }
                return Ok(Attempted::NotModified);
            }
            // A limit of zero follows nothing. Each redirect status is then a
            // definitive answer.
            if is_redirect(status) && self.inner.max_redirects > 0 {
                let location = response
                    .headers()
                    .get(hyper::header::LOCATION)
                    .and_then(|value| resolve_location(url, value));
                let Some(location) = location else {
                    // There is no URL to follow. So the attempt reports the
                    // status of the hop, whatever the hop count is.
                    let failure = classify(status, url);
                    return Err(self
                        .conclude(&key, response, reuse, drain_until, failure, keep)
                        .await);
                };
                // The limit stops the attempt at a URL that it can follow. So
                // the attempt reads the limit after the `Location` names a URL.
                if followed >= self.inner.max_redirects {
                    self.discard(&key, response, reuse, drain_until).await;
                    return Err(Failure::Fatal(Error::RedirectLimit {
                        url: url.to_string(),
                        hops: followed,
                    }));
                }
                let next = redirect_destination(&hop, &location);
                // The attempt discards the intermediate body as it discards an
                // unsuccessful body. So a short body returns its HTTP/1.1
                // connection to the pool, and the next hop at this origin
                // uses it.
                self.discard(&key, response, reuse, drain_until).await;
                let next = next.map_err(Failure::Fatal)?;
                // A handshake verifies the server certificate against an
                // anchor. So the attempt refuses a hop to a TLS origin if the
                // fetcher holds no anchor. Without this check, the handshake
                // fails retryably, and uses all rounds and all backoffs on a
                // message about the peer.
                if next.origin.tls && !self.inner.has_trust_anchors {
                    return Err(Failure::Fatal(no_trust_anchors(next.origin_url())));
                }
                // The operator of the origin that a request reaches gets its
                // credentials. So the attempt removes them for a hop at an
                // origin other than the named origin. They stay removed for the
                // rest of the attempt, also on a redirect back to the named
                // origin. At that time, the other operator controls the request.
                // The same applies to the signer.
                if next.origin != *named {
                    signer = None;
                }
                if next.origin != *named && headers.iter().any(|(name, _)| is_credential(name)) {
                    let scoped = headers
                        .iter()
                        .filter(|(name, _)| !is_credential(name))
                        .cloned()
                        .collect::<Vec<_>>();
                    headers = Cow::Owned(scoped);
                }
                hop = Cow::Owned(next);
                followed += 1;
                continue;
            }
            // A 206 answers a request that names a range. A 206 to any other
            // request holds a part of the object that the caller did not ask
            // for, so it is an unsuccessful status.
            let ranged = status == StatusCode::PARTIAL_CONTENT
                && headers.iter().any(|(name, _)| name == hyper::header::RANGE);
            if status != StatusCode::OK && !ranged {
                let failure = classify(status, url);
                return Err(self
                    .conclude(&key, response, reuse, drain_until, failure, keep)
                    .await);
            }
            // A coded body holds bytes other than the bytes that the remote
            // stores. So the declared length tells nothing about the object,
            // and the attempt refuses the coding before it compares the cap.
            // The refusal names the coding, and a checksum mismatch cannot. The
            // refusal is definitive, because another attempt against the same
            // destination gets the same answer.
            if let Some(encoding) = declared_coding(response.headers()) {
                let url = url.to_string();
                self.discard(&key, response, reuse, drain_until).await;
                return Err(Failure::Fatal(Error::ContentEncoded { url, encoding }));
            }
            let validators = read_validators(response.headers());
            let content_length = content_length(response.headers());
            // The cap is the limit of the caller on the object. So the attempt
            // compares it with the response that carries the object, and with
            // no redirect before it.
            if let (Some(limit), Some(length)) = (request.max_size, content_length)
                && length > limit
            {
                self.discard(&key, response, reuse, drain_until).await;
                return Err(Failure::Fatal(Error::FetchTooLarge { limit }));
            }
            let protocol = match response.version() {
                Version::HTTP_2 => Protocol::Http2,
                _ => protocol,
            };
            return Ok(Attempted::Body(Body {
                incoming: response.into_body(),
                chunk: Bytes::new(),
                received: 0,
                max_size: request.max_size,
                status: status.as_u16(),
                validators,
                content_length,
                protocol,
                inner: self.inner.clone(),
                counters: self.received.clone(),
                key,
                reuse,
                permit: None,
                done: false,
                failed: None,
                deadline: rt::Deadline::new(progress_timeout),
                waiting: false,
                low_speed: self.inner.low_speed.map(Monitor::new),
                interrupted: None,
                request_body: None,
            }));
        }
    }

    /// Takes or opens a connection for one hop, and sends the request over it.
    ///
    /// The function returns the response, its protocol, and the HTTP/1.1
    /// connection of the response. That connection goes back to the pool after
    /// the response is read to its end.
    ///
    /// A `signer` signs the request after the connection is taken. So the
    /// wait for a connect is not part of the age of the signature. If the
    /// signer fails, an HTTP/1.1 connection goes back to the pool unused.
    async fn send(
        &self,
        hop: &Destination,
        key: &PoolKey,
        via: Via<'_>,
        request: &FetchRequest<'_>,
        headers: &[(HeaderName, HeaderValue)],
        signer: Option<&dyn RequestSigner>,
    ) -> std::result::Result<(Response<Incoming>, Protocol, Option<H1Sender>), Failure> {
        let url = hop.url();
        let origin = &key.origin;
        let connect_timeout = self.inner.connect_timeout;
        let progress_timeout = self.inner.progress_timeout;
        let (sender, idle_since) = match self.inner.take_conn(key) {
            Some(taken) => taken,
            None => {
                // The open of a connection is the largest state of a fetch: the
                // TLS handshake and the state of hyper. It is also the rarest,
                // because it occurs only if the pool has no connection for this
                // origin. A box keeps that state out of the fetch future, which
                // each caller nests in its own future.
                //
                // A fetch measures 5064 bytes with the box and 36136 bytes
                // without it. A caller that wraps several helpers around one
                // fetch multiplies the saving.
                let opened = within(connect_timeout, Box::pin(self.connect(key, via))).await;
                match opened {
                    Some(result) => (result?, None),
                    None => {
                        return Err(Failure::Retry(connect_timed_out(
                            origin,
                            via,
                            connect_timeout,
                        )));
                    }
                }
            }
        };
        let headers = match signer {
            None => Cow::Borrowed(headers),
            Some(signer) => match signed(signer, &Method::GET, hop, SignPayload::Empty, headers) {
                Ok(signed) => signed,
                Err(failure) => {
                    // The connection carried no request, so it goes back to
                    // the pool as it came.
                    if let Sender::H1(sender) = sender {
                        let since = idle_since.unwrap_or_else(Instant::now);
                        self.inner.put_idle_h1(key, sender, since);
                    }
                    return Err(failure);
                }
            },
        };
        match sender {
            Sender::H1(mut sender) => {
                let http_request = self
                    .build_request(hop, request, &headers, Protocol::Http11, via)
                    .map_err(Failure::Fatal)?;
                // The request and the wait for the response head share the
                // progress window, because the head is the first bytes of the
                // response.
                let sent = within(progress_timeout, async {
                    sender.ready().await?;
                    sender.send_request(http_request).await
                })
                .await;
                let response = match sent {
                    Some(result) => result.map_err(|e| Failure::Retry(transport(url, e)))?,
                    None => return Err(Failure::Retry(stalled(url, progress_timeout))),
                };
                Ok((response, Protocol::Http11, Some(sender)))
            }
            Sender::H2(mut sender) => {
                let http_request = self
                    .build_request(hop, request, &headers, Protocol::Http2, via)
                    .map_err(Failure::Fatal)?;
                let sent = within(progress_timeout, async {
                    sender.ready().await?;
                    sender.send_request(http_request).await
                })
                .await;
                let response = match sent {
                    Some(result) => result.map_err(|e| Failure::Retry(transport(url, e)))?,
                    None => return Err(Failure::Retry(stalled(url, progress_timeout))),
                };
                Ok((response, Protocol::Http2, None))
            }
        }
    }

    /// Returns `true` if a connection to `hop` presents the configured client
    /// certificate.
    ///
    /// This is the case if a certificate is configured, the hop is the origin
    /// that the route names, and that origin is TLS. A cleartext connection
    /// presents no certificate. If no certificate is configured, the result is
    /// always `false`, so the pool holds one entry for each origin.
    fn presents_identity(&self, hop: &Origin, named: &Origin) -> bool {
        self.inner.client_identity && hop.tls && hop == named
    }

    /// Ends an attempt with a response that the caller did not ask for.
    ///
    /// If the response declares a body of at most [`DRAIN_LIMIT`] bytes, the
    /// function reads it to the end. This frees its HTTP/1.1 connection for the
    /// next request. The function drops a larger declared body, or a body with
    /// no declared length. The connection then closes, because the rest of the
    /// response is still in flight.
    ///
    /// An HTTP/2 stream has no such cost, because its connection stays in the
    /// pool, whatever the stream did. So it needs no drain.
    ///
    /// `drain_until` is the limit for all reads of one attempt here. It is one
    /// progress window from the start of the attempt, and the reads share it. A
    /// read that reaches the end of the window drops the remaining response. A
    /// read that finds no time left drops the connection and reads nothing.
    async fn discard(
        &self,
        key: &PoolKey,
        response: Response<Incoming>,
        reuse: Option<H1Sender>,
        drain_until: Instant,
    ) {
        let length = content_length(response.headers());
        self.drain(key, length, response.into_body(), reuse, drain_until)
            .await;
    }

    /// Ends an attempt with the unsuccessful response that `failure`
    /// classifies, and returns the failure.
    ///
    /// If `keep` asks for a response of the class of the failure, the
    /// function reads the response with [`capture`](Fetcher::capture) and
    /// puts it into the failure. Else it discards the response.
    async fn conclude(
        &self,
        key: &PoolKey,
        response: Response<Incoming>,
        reuse: Option<H1Sender>,
        drain_until: Instant,
        failure: Failure,
        keep: Option<Keep>,
    ) -> Failure {
        match failure.kept_cap(keep) {
            None => {
                self.discard(key, response, reuse, drain_until).await;
                failure
            }
            Some(cap) => {
                let kept = self.capture(key, response, reuse, cap).await;
                failure.with_response(kept)
            }
        }
    }

    /// Reads the headers and the body of an unsuccessful response, for the
    /// error of the fetch.
    ///
    /// The function keeps the whole body if it is `cap` bytes or less, and
    /// keeps no part of a larger body. It reads no body that declares a
    /// coding or a length over the cap, and discards it as
    /// [`discard`](Fetcher::discard) does. A body that grows past the cap, or
    /// fails, closes its connection.
    ///
    /// Before the first byte, the function reserves at most
    /// [`CAPTURE_RESERVE`] bytes, whatever length the response declares. The
    /// capacity of the kept body never passes `cap`.
    ///
    /// All reads take place within one progress window from the call. The
    /// read stops at the end of the window, and the body is then absent. The
    /// bytes do not count in the received counters, because they are not part
    /// of an object. A clean end of the body returns an HTTP/1.1 connection to
    /// the pool.
    async fn capture(
        &self,
        key: &PoolKey,
        response: Response<Incoming>,
        reuse: Option<H1Sender>,
        cap: u64,
    ) -> ErrorResponse {
        let until = Instant::now() + self.inner.progress_timeout;
        let (parts, mut body) = response.into_parts();
        let headers = parts.headers;
        let length = content_length(&headers);
        if declared_coding(&headers).is_some() || length.is_some_and(|length| length > cap) {
            self.drain(key, length, body, reuse, until).await;
            return ErrorResponse::new(headers, None);
        }
        // The declared length is a claim of the server, so the reserve before
        // the first byte stays small. The vector grows as the bytes arrive.
        let reserve = length.unwrap_or(0).min(cap).min(CAPTURE_RESERVE);
        let mut bytes = Vec::with_capacity(usize::try_from(reserve).unwrap_or(0));
        let budget = until.saturating_duration_since(Instant::now());
        let read = within(budget, async {
            loop {
                let frame = std::future::poll_fn(|cx| Pin::new(&mut body).poll_frame(cx)).await;
                match frame {
                    Some(Ok(frame)) => {
                        // A trailer carries no bytes of the body.
                        let Ok(data) = frame.into_data() else {
                            continue;
                        };
                        if !extend_capped(&mut bytes, &data, cap) {
                            return false;
                        }
                    }
                    Some(Err(_)) => return false,
                    None => return true,
                }
            }
        })
        .await;
        if read != Some(true) {
            // The rest of the response is still in flight, so the connection
            // closes when `reuse` drops.
            return ErrorResponse::new(headers, None);
        }
        if let Some(sender) = reuse {
            self.inner.put_h1(key, sender);
        }
        ErrorResponse::new(headers, Some(bytes))
    }

    /// Reads a response body of the declared `length` to its end within
    /// `drain_until`, and returns its HTTP/1.1 connection to the pool.
    ///
    /// [`discard`](Fetcher::discard) states the rule.
    async fn drain(
        &self,
        key: &PoolKey,
        length: Option<u64>,
        mut body: Incoming,
        reuse: Option<H1Sender>,
        drain_until: Instant,
    ) {
        let Some(sender) = reuse else { return };
        if length.is_none_or(|length| length > DRAIN_LIMIT) {
            return;
        }
        let budget = drain_until.saturating_duration_since(Instant::now());
        if budget.is_zero() {
            return;
        }
        // The drain runs in the remaining window of the attempt. So a peer that
        // declares a short body and then stops costs the attempt no more than a
        // stalled body, whatever the hop count.
        let drained = within(budget, async {
            loop {
                let frame = std::future::poll_fn(|cx| Pin::new(&mut body).poll_frame(cx)).await;
                match frame {
                    Some(Ok(_)) => continue,
                    Some(Err(_)) => return false,
                    None => return true,
                }
            }
        })
        .await;
        if drained == Some(true) {
            self.inner.put_h1(key, sender);
        }
    }

    /// Sends `request` and tries each destination until one takes it.
    ///
    /// Each final status returns an [`Uploaded`], also 408, 429, and 5xx. The
    /// mirrors, the proxy, TLS, the client certificate, the headers, the
    /// credentials, the signer, and the cleartext rule apply to an upload as
    /// they apply to [`fetch`](Fetcher::fetch). An upload can also carry a
    /// [`BearerToken`] of its own as its credential. The fetcher sets no
    /// `Content-Type` header and no `Expect` header.
    ///
    /// The hand-over gives the request to hyper. A failure before the
    /// hand-over returns the error that a fetch returns. A failure after the
    /// hand-over returns [`Error::UploadInterrupted`].
    ///
    /// # Retries
    ///
    /// The fetcher tries an upload again only while no part of the request is
    /// sent. The request is unsent if the attempt fails before hyper accepts
    /// it, in one of these steps:
    ///
    /// - the connect
    /// - the `CONNECT` tunnel
    /// - the TLS handshake
    /// - the HTTP handshake
    /// - the wait for a ready connection.
    ///
    /// The request is also unsent if hyper gives it back unwritten. The bytes
    /// of a `CONNECT` go to the proxy alone and are no part of the request.
    ///
    /// An unsent attempt spends a round as a retryable failure of a fetch
    /// does. The upload asks the next mirror, then repeats the round after the
    /// backoff, up to [`max_retries`](FetcherOptions::max_retries) times. A 407
    /// to the `CONNECT` is definitive. If the rounds run out before the request
    /// is sent, the upload returns the [`Error::Fetch`] that a fetch returns.
    ///
    /// If the writer of a streamed body is dropped before it closes the body,
    /// the upload fails before the hand-over. It fails with [`Error::Fetch`]
    /// and sends nothing.
    ///
    /// All other outcomes count as sent. The upload then ends on the
    /// destination that took it, and asks no other mirror and no other round.
    /// A failure after the hand-over fails the upload with
    /// [`Error::UploadInterrupted`], because the server can have received the
    /// whole request and acted on it. An HTTP/2 stream that the server refuses
    /// or resets after the hand-over is such a failure.
    ///
    /// # Redirects
    ///
    /// For a body given whole, the upload follows a 307 or a 308. It keeps the
    /// method and sends the bytes again. The redirect rules of
    /// [`fetch`](Fetcher::fetch) apply: the redirect limit, the refusal of a
    /// hop from `https` to `http`, and the scope of the credentials and of the
    /// signer.
    ///
    /// The first hop sends the request. So a failure on a later hop, a refused
    /// hop, and a redirect past the limit each fail the upload with
    /// [`Error::UploadInterrupted`].
    ///
    /// The upload returns these redirects as their status in an [`Uploaded`]:
    ///
    /// - a redirect status other than 307 and 308
    /// - each redirect of a streamed body
    /// - a 307 or a 308 with no `Location` that the upload can read
    /// - each redirect if [`max_redirects`](FetcherOptions::max_redirects) is
    ///   0.
    ///
    /// # Unsuccessful answers
    ///
    /// An answer other than 2xx returns an [`Uploaded`] with its status and
    /// its headers, also when the upload cannot deliver its body. The upload
    /// does not deliver a body that declares a coding in `Content-Encoding` or
    /// `Transfer-Encoding`, or a `Content-Length` over
    /// [`max_response`](UploadRequest::max_response). It withholds such a
    /// body: [`Uploaded::body_withheld`] returns `true`, the body reads as
    /// empty, and the connection closes. So a definitive refusal, for example
    /// a 403 behind a proxy that compresses it, keeps its status, and the
    /// caller does not take it for an interrupted upload.
    ///
    /// A 2xx answer with such a body fails the upload with
    /// [`Error::UploadInterrupted`].
    ///
    /// # Connections
    ///
    /// An upload uses the pooled HTTP/2 connection of its origin. It takes an
    /// idle HTTP/1.1 connection from the pool only if the connection became
    /// idle less than 2 seconds ago. Otherwise, it opens a connection of its
    /// own.
    ///
    /// A server can close an idle connection at any time. If hyper began to
    /// write an upload over a connection that the server closed, the upload
    /// counts as sent. A server closes an idle connection at the end of its
    /// idle timeout, so it keeps a connection that became idle a short time
    /// ago.
    ///
    /// If a pooled HTTP/1.1 connection fails before hyper writes the request,
    /// the fetcher drops the connection. The upload then continues over a new
    /// connection and spends no round.
    ///
    /// The HTTP/1.1 connection of an upload goes back to the pool if both
    /// bodies end cleanly. That is, hyper takes the end of the request body
    /// before the response head arrives. Then the caller reads the response
    /// body to its end with no failure. Each of these closes the connection:
    ///
    /// - a response that closes the connection
    /// - an HTTP/1.0 response
    /// - an answer that arrives before the end of the request body
    /// - a response body that fails
    /// - a response that is dropped before its end.
    ///
    /// # The response ends the request body
    ///
    /// A server can answer before it reads the whole request body. If the
    /// response body ends or is dropped before the request body ends, the
    /// fetcher fails the request body. The writer then gets
    /// [`io::ErrorKind::BrokenPipe`](std::io::ErrorKind::BrokenPipe), and hyper
    /// stops the body.
    ///
    /// The stop of the body closes an HTTP/1.1 connection and resets an HTTP/2
    /// stream. The same applies to a response that the upload does not
    /// deliver: a followed redirect, a refused coding, a declared length over
    /// the cap, and a withheld body.
    ///
    /// # Deadlines
    ///
    /// - [`connect_timeout`](FetcherOptions::connect_timeout) bounds the open
    ///   of a connection.
    /// - [`fetch_timeout`](FetcherOptions::fetch_timeout) bounds the upload
    ///   from admission to the hand-over alone.
    /// - While a streamed body streams, the one bound is the stall window of
    ///   [`UploadWriter`], which starts at the hand-over.
    /// - For a body given whole, a frame that the connection does not take for
    ///   [`progress_timeout`](FetcherOptions::progress_timeout) ends the upload
    ///   with [`Error::UploadInterrupted`].
    /// - The wait for the response head lasts
    ///   [`response_timeout`](UploadRequest::response_timeout).
    /// - The response body has the progress window of a fetch.
    ///
    /// Before the hand-over, a wait of the writer has no bound of its own. This
    /// applies at the gate, during the connect, and during the backoff between
    /// rounds.
    /// The low-speed rule does not apply to an upload.
    ///
    /// # Errors
    ///
    /// If more than one destination fails before the hand-over, the failure
    /// order of [`fetch`](Fetcher::fetch) states which error the upload
    /// returns.
    ///
    /// - [`Error::Fetch`] before admission, if a path holds a `?` or a `#`, if
    ///   the query of a [`Target::PathQuery`] holds a `#`, or if the fetcher
    ///   has no mirror for a path target.
    /// - [`Error::Fetch`] before admission, if a URL target is not a valid
    ///   absolute URL, or holds a fragment or userinfo.
    /// - [`Error::Unsupported`] if the scheme of a URL target is neither `http`
    ///   nor `https`.
    /// - [`Error::Fetch`] before admission, if a request header is invalid, is
    ///   `Host`, or is a header that the connection layer sets.
    /// - [`Error::Fetch`] before admission, if two credentials of the request
    ///   meet: [`basic_auth`](UploadRequest::basic_auth),
    ///   [`bearer_token`](UploadRequest::bearer_token), and an `Authorization`
    ///   header. A bearer token that is not token68 also fails.
    /// - [`Error::Fetch`] before admission, if the fetcher has a
    ///   [`signer`](FetcherOptions::signer) and the request carries one of
    ///   these credentials: [`basic_auth`](UploadRequest::basic_auth),
    ///   [`bearer_token`](UploadRequest::bearer_token), or an `Authorization`
    ///   header.
    /// - [`Error::Fetch`] before admission, if a credential can reach a
    ///   cleartext destination, and neither
    ///   [`allow_cleartext_credentials`](UploadRequest::allow_cleartext_credentials)
    ///   nor [`FetcherOptions::allow_cleartext_credentials`] is `true`.
    /// - [`Error::Fetch`] before admission, if the route names a TLS
    ///   destination and the fetcher holds no trust anchors.
    /// - [`Error::Fetch`] before admission, if the method is `DELETE` and the
    ///   body is not an empty [`UploadBody::bytes`].
    /// - [`Error::Fetch`] if a transport failure ends the rounds before the
    ///   hand-over: a failed connect, handshake, or proxy `CONNECT`, or an
    ///   expired deadline.
    /// - [`Error::Fetch`] if hyper cannot build the request.
    /// - The error of the [`signer`](FetcherOptions::signer), unchanged, if it
    ///   fails before the hand-over. An [`Error::Fetch`] if it returns a
    ///   header that a request cannot carry. The upload asks no other
    ///   destination and repeats no round. A failure of the signer on a
    ///   redirect hop is a failure after the hand-over.
    /// - [`Error::Fetch`] if the writer of a streamed body is dropped before
    ///   the hand-over.
    /// - [`Error::Fetch`] if [`fetch_timeout`](FetcherOptions::fetch_timeout)
    ///   expires before the hand-over.
    /// - [`Error::UploadInterrupted`] if a failure occurs after the hand-over.
    ///   This includes a refused redirect hop, a redirect past the limit, and
    ///   a 2xx response that declares a coding or a `Content-Length` over
    ///   [`max_response`](UploadRequest::max_response). The message names the
    ///   cause.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ostrya_fetch::{Fetcher, FetcherOptions, UploadBody, UploadRequest};
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// let fetcher = Fetcher::new(FetcherOptions::new("https://example.com/repo")).await?;
    /// let body = UploadBody::bytes(b"report".to_vec());
    /// let uploaded = fetcher.upload(UploadRequest::path("inbox/report", body)).await?;
    /// println!("the server answered {}", uploaded.status());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn upload(&self, request: UploadRequest<'_>) -> Result<Uploaded> {
        let UploadRequest {
            target,
            method,
            body,
            priority,
            max_response,
            headers,
            basic_auth,
            bearer_token,
            allow_cleartext_credentials,
            response_timeout,
        } = request;
        // As for a fetch, the checks of the request run before admission.
        // The check of the body that a DELETE does not send runs there too.
        let route = self.route(target)?;
        if self.inner.signer.is_some() {
            refuse_beside_signer(headers, basic_auth.is_some(), bearer_token.is_some())?;
        }
        let headers = merge_headers(&self.inner.headers, headers, basic_auth, bearer_token)?;
        if !allow_cleartext_credentials && !self.inner.allow_cleartext_credentials {
            self.check_cleartext(&route, &headers)?;
        }
        self.check_trust_anchors(&route)?;
        let method = match method {
            UploadMethod::Post => Method::POST,
            UploadMethod::Put => Method::PUT,
            UploadMethod::Delete => Method::DELETE,
        };
        let (end, bytes, sha256) = match body.form {
            UploadForm::Bytes { bytes, sha256 } => (BodyEnd::whole(&bytes), Some(bytes), sha256),
            UploadForm::Channel(end) => (end, None, None),
        };
        if method == Method::DELETE && !bytes.as_ref().is_some_and(Bytes::is_empty) {
            return Err(Error::Fetch(format!(
                "the delete of {} carries a body, which a delete does not send: pass an empty \
                 UploadBody::bytes",
                target.describe()
            )));
        }
        let plan = UploadPlan {
            method,
            headers: &headers,
            bytes,
            sha256,
            max_response,
            response_timeout: response_timeout.unwrap_or(self.inner.progress_timeout),
        };
        let permit = self.inner.gate.acquire(priority).await;
        // This flag is set while hyper holds the request. The admission
        // deadline stops then, because the steps after the hand-over have
        // deadlines of their own.
        let handed = AtomicBool::new(false);
        let rounds = self.upload_rounds(&plan, &route, end, &handed);
        let mut uploaded = match self.inner.fetch_timeout {
            None => rounds.await?,
            Some(limit) => match before_hand_over(limit, &handed, rounds).await {
                Some(result) => result?,
                None => {
                    return Err(Error::Fetch(format!(
                        "upload of {} was not sent within {limit:?}",
                        target.describe()
                    )));
                }
            },
        };
        if !uploaded.withheld {
            uploaded.body.permit = Some(permit);
        }
        Ok(uploaded)
    }

    /// Tries each destination in turn, as [`rounds`](Fetcher::rounds) does for
    /// a fetch.
    ///
    /// The function repeats the round while the request is unsent and a
    /// destination failed retryably. A request that is sent ends the rounds,
    /// whatever its outcome.
    async fn upload_rounds(
        &self,
        plan: &UploadPlan<'_>,
        route: &Route<'_>,
        end: BodyEnd,
        handed: &AtomicBool,
    ) -> Result<Uploaded> {
        let destination_count = match route {
            Route::Mirrors { .. } => self.inner.mirrors.len(),
            Route::One(_) => 1,
        };
        let mut settled = vec![false; destination_count];
        let mut reported: Option<Error> = None;
        let mut definitive = false;
        let mut round = 0;
        // An attempt takes the body when it hands the request over. If the
        // request comes back unsent, the attempt puts the body back here.
        let mut body = Some(end);
        loop {
            let mut retryable = false;
            for (position, settled) in settled.iter_mut().enumerate() {
                if *settled {
                    continue;
                }
                let destination = match route {
                    Route::Mirrors { path, query } => {
                        Cow::Owned(self.inner.mirrors[position].destination(path, *query))
                    }
                    Route::One(destination) => Cow::Borrowed(destination),
                };
                match self
                    .upload_attempt(&destination, plan, &mut body, handed)
                    .await
                {
                    Ok(uploaded) => return Ok(uploaded),
                    Err(UploadFailure::Sent { url, message }) => {
                        return Err(Error::UploadInterrupted { url, message });
                    }
                    Err(UploadFailure::Unsent(Failure::Retry(e))) => {
                        retryable = true;
                        if reported.is_none() {
                            reported = Some(e);
                        }
                    }
                    Err(UploadFailure::Unsent(Failure::Fatal(e))) => {
                        *settled = true;
                        if !definitive {
                            reported = Some(e);
                            definitive = true;
                        }
                    }
                    Err(UploadFailure::Unsent(Failure::Final(e))) => return Err(e),
                }
            }
            if !retryable || round >= self.inner.max_retries {
                return Err(reported.expect("a failed round holds a failure"));
            }
            round += 1;
            rt::Timer::after(backoff(round)).await;
        }
    }

    /// Runs one upload against one destination, and follows the redirects of a
    /// body given whole.
    ///
    /// The first hop that hyper takes sends the request. So a failure on a
    /// later hop is final: the function reports an interrupted upload, and the
    /// rounds stop. The function drops a response that the upload does not
    /// deliver. This also drops a request body that did not end, and closes
    /// the connection of an HTTP/1.1 hop.
    async fn upload_attempt(
        &self,
        destination: &Destination,
        plan: &UploadPlan<'_>,
        body: &mut Option<BodyEnd>,
        handed: &AtomicBool,
    ) -> std::result::Result<Uploaded, UploadFailure> {
        let named = &destination.origin;
        let mut hop = Cow::Borrowed(destination);
        let mut headers = Cow::Borrowed(plan.headers);
        let mut signer = self.inner.signer.as_deref();
        let mut followed = 0u32;
        loop {
            let via = self.inner.proxies.via(&hop.origin);
            let key = PoolKey {
                origin: match via {
                    Via::Absolute(proxy) => proxy.endpoint.clone(),
                    Via::Direct | Via::Tunnel(_) | Via::Socks(..) => hop.origin.clone(),
                },
                identity: self.presents_identity(&hop.origin, named),
                proxied: matches!(via, Via::Absolute(_)),
            };
            let url = hop.url();
            let sent = self
                .upload_send(&hop, &key, via, plan, &headers, signer, body, handed)
                .await;
            let (response, protocol, request_end, reuse) = match sent {
                Ok(sent) => sent,
                Err(UploadFailure::Unsent(failure)) if followed > 0 => {
                    return Err(interrupted(url, failure.into_error().to_string()));
                }
                Err(failure) => return Err(failure),
            };
            let status = response.status();
            if let Some(bytes) = &plan.bytes
                && UPLOAD_REDIRECTS.contains(&status)
                && self.inner.max_redirects > 0
                && let Some(location) = response
                    .headers()
                    .get(hyper::header::LOCATION)
                    .and_then(|value| resolve_location(url, value))
            {
                if followed >= self.inner.max_redirects {
                    return Err(undelivered(
                        url,
                        Error::RedirectLimit {
                            url: url.to_string(),
                            hops: followed,
                        },
                    ));
                }
                let next =
                    redirect_destination(&hop, &location).map_err(|e| undelivered(url, e))?;
                if next.origin.tls && !self.inner.has_trust_anchors {
                    return Err(undelivered(url, no_trust_anchors(next.origin_url())));
                }
                // As for a fetch, a credential and the signer stay with the
                // origin that the route names.
                if next.origin != *named {
                    signer = None;
                }
                if next.origin != *named && headers.iter().any(|(name, _)| is_credential(name)) {
                    let scoped = headers
                        .iter()
                        .filter(|(name, _)| !is_credential(name))
                        .cloned()
                        .collect::<Vec<_>>();
                    headers = Cow::Owned(scoped);
                }
                hop = Cow::Owned(next);
                followed += 1;
                *body = Some(BodyEnd::whole(bytes));
                continue;
            }
            let coding = declared_coding(response.headers());
            let content_length = content_length(response.headers());
            let over = content_length.is_some_and(|length| length > plan.max_response);
            // An answer other than 2xx keeps its status and its headers, so a
            // refusal of the server reaches the caller as a refusal. Its body
            // is withheld, and the connection closes.
            let withheld = !status.is_success() && (coding.is_some() || over);
            if !withheld {
                if let Some(encoding) = coding {
                    return Err(undelivered(
                        url,
                        Error::ContentEncoded {
                            url: url.to_string(),
                            encoding,
                        },
                    ));
                }
                if over {
                    return Err(undelivered(
                        url,
                        Error::FetchTooLarge {
                            limit: plan.max_response,
                        },
                    ));
                }
            }
            let protocol = match response.version() {
                Version::HTTP_2 => Protocol::Http2,
                _ => protocol,
            };
            let (parts, incoming) = response.into_parts();
            // A withheld body ends at once. Its request body stops, and its
            // HTTP/1.1 connection does not go back to the pool, because the
            // rest of the response is still in flight.
            let (reuse, request_body) = if withheld {
                (None, None)
            } else {
                (reuse, Some(request_end))
            };
            return Ok(Uploaded {
                status: parts.status,
                headers: parts.headers,
                url: url.to_string(),
                withheld,
                body: Body {
                    incoming,
                    chunk: Bytes::new(),
                    received: 0,
                    max_size: Some(plan.max_response),
                    status: parts.status.as_u16(),
                    validators: Validators::default(),
                    content_length: if withheld { None } else { content_length },
                    protocol,
                    inner: self.inner.clone(),
                    counters: self.received.clone(),
                    key,
                    reuse,
                    permit: None,
                    done: withheld,
                    failed: None,
                    deadline: rt::Deadline::new(self.inner.progress_timeout),
                    waiting: false,
                    low_speed: None,
                    interrupted: None,
                    request_body,
                },
            });
        }
    }

    /// Takes or opens a connection for one upload hop, and hands the request
    /// to hyper.
    ///
    /// An upload takes the pooled HTTP/2 connection, or an idle HTTP/1.1
    /// connection from [`Inner::take_upload`]. If a pooled HTTP/1.1 connection
    /// fails before hyper writes the request, the function drops it. The hop
    /// then continues over a new connection and spends no round.
    ///
    /// A failure before the hand-over leaves the body in `body`. A request
    /// that hyper gives back unwritten also leaves it there. A body that failed
    /// before the hand-over is never sent. The stall window of the body starts
    /// at the hand-over, and [`answer`] then bounds the wait for the response
    /// head.
    ///
    /// The HTTP/1.1 connection comes back with the response if it can carry
    /// the next request after the response body ends. That is, hyper took the
    /// end of the request body before the response head arrived, and the
    /// response does not close the connection. In each other case, the
    /// connection closes at the end of the exchange.
    ///
    /// A `signer` signs the request over each connection that the function
    /// takes or opens, when the connection is ready and before the function
    /// starts the request. So a request that goes again over a new connection
    /// carries a new signature. If the signer fails, an HTTP/1.1 connection
    /// goes back to the pool unused.
    #[allow(clippy::too_many_arguments)]
    async fn upload_send(
        &self,
        hop: &Destination,
        key: &PoolKey,
        via: Via<'_>,
        plan: &UploadPlan<'_>,
        headers: &[(HeaderName, HeaderValue)],
        signer: Option<&dyn RequestSigner>,
        body: &mut Option<BodyEnd>,
        handed: &AtomicBool,
    ) -> std::result::Result<UploadResponse, UploadFailure> {
        let url = hop.url();
        let unsent = |failure: Failure| -> std::result::Result<UploadResponse, UploadFailure> {
            Err(UploadFailure::Unsent(failure))
        };
        let exchange = body
            .as_ref()
            .expect("an upload attempt holds its body until it sends it")
            .exchange
            .clone();
        if let Some(reason) = exchange.aborted() {
            return unsent(Failure::Fatal(not_sent(url, reason)));
        }
        let progress_timeout = self.inner.progress_timeout;
        let mut pooled = self.inner.take_upload(key);
        loop {
            // If a pooled HTTP/1.1 connection fails before hyper writes the
            // request, the server closed it while it was idle.
            let stale_h1 = matches!(pooled, Some((Sender::H1(_), _)));
            let (mut sender, idle_since) = match pooled.take() {
                Some(taken) => taken,
                None => {
                    let connect_timeout = self.inner.connect_timeout;
                    match within(connect_timeout, Box::pin(self.connect(key, via))).await {
                        Some(Ok(sender)) => (sender, None),
                        Some(Err(failure)) => return unsent(failure),
                        None => {
                            return unsent(Failure::Retry(connect_timed_out(
                                &key.origin,
                                via,
                                connect_timeout,
                            )));
                        }
                    }
                }
            };
            let protocol = match sender {
                Sender::H1(_) => Protocol::Http11,
                Sender::H2(_) => Protocol::Http2,
            };
            let ready = match &mut sender {
                Sender::H1(sender) => within(progress_timeout, sender.ready()).await,
                Sender::H2(sender) => within(progress_timeout, sender.ready()).await,
            };
            match ready {
                Some(Ok(())) => {}
                Some(Err(_)) if stale_h1 => continue,
                Some(Err(e)) => return unsent(Failure::Retry(transport(url, e))),
                None => return unsent(Failure::Retry(stalled(url, progress_timeout))),
            }
            // The signature is made when the connection is ready to carry the
            // request. So a connection that fails here costs no signature, and
            // the wait for a stream slot is not part of the age of the
            // signature.
            let signed_headers = match signer {
                None => Cow::Borrowed(headers),
                Some(signer) => {
                    // The signer reads a body given whole as the slice that
                    // the request sends, with no copy, and with the digest
                    // of the caller if it gave one. A streamed body is not
                    // known yet.
                    let payload = match (&plan.bytes, plan.sha256) {
                        _ if plan.method == Method::DELETE => SignPayload::Empty,
                        (Some(bytes), Some(sha256)) => SignPayload::Sha256 { bytes, sha256 },
                        (Some(bytes), None) => SignPayload::Bytes(bytes),
                        (None, _) => SignPayload::Streamed,
                    };
                    match signed(signer, &plan.method, hop, payload, headers) {
                        Ok(signed) => signed,
                        Err(failure) => {
                            // The connection carried no request, so it goes
                            // back to the pool as it came.
                            if let Sender::H1(sender) = sender {
                                let since = idle_since.unwrap_or_else(Instant::now);
                                self.inner.put_idle_h1(key, sender, since);
                            }
                            return unsent(failure);
                        }
                    }
                }
            };
            let mut head = match self.upload_head(hop, plan, &signed_headers, protocol, via) {
                Ok(head) => head,
                Err(e) => return unsent(Failure::Fatal(e)),
            };
            let empty = match exchange.hand_over(progress_timeout) {
                Ok(empty) => empty,
                Err(reason) => return unsent(Failure::Fatal(not_sent(url, reason))),
            };
            // A `POST` or a `PUT` whose body is at its end states its length.
            // hyper writes no length for an HTTP/1.1 request at the end of its
            // body. Each of the two methods defines content, so a server that
            // waits for the framing reads `0`. A `DELETE` sends no body.
            if empty && plan.method != Method::DELETE {
                head.headers_mut()
                    .insert(hyper::header::CONTENT_LENGTH, HeaderValue::from_static("0"));
            }
            let end = body
                .take()
                .expect("an upload attempt holds its body until it sends it");
            let request = head.map(|()| RequestBody::Upload(end));
            handed.store(true, Ordering::Relaxed);
            let window = plan.response_timeout;
            let (answered, h1) = match sender {
                Sender::H1(mut sender) => {
                    let answered =
                        answer(sender.try_send_request(request), &exchange, window).await;
                    (answered, Some(sender))
                }
                Sender::H2(mut sender) => {
                    let answered =
                        answer(sender.try_send_request(request), &exchange, window).await;
                    (answered, None)
                }
            };
            let response = match answered {
                Answer::Sent(Ok(response)) => response,
                Answer::Sent(Err(mut e)) => {
                    let Some(request) = e.take_message() else {
                        exchange.cut();
                        return Err(interrupted(url, with_cause(&e.into_error())));
                    };
                    // hyper gives the request back if the connection failed
                    // before hyper wrote a part of it. So the body is whole,
                    // and the request is unsent.
                    exchange.withdraw();
                    if let RequestBody::Upload(end) = request.into_body() {
                        *body = Some(end);
                    }
                    handed.store(false, Ordering::Relaxed);
                    if stale_h1 {
                        continue;
                    }
                    return unsent(Failure::Retry(transport(url, e.into_error())));
                }
                Answer::Aborted(reason) => return Err(interrupted(url, reason)),
                Answer::Silent(window) => {
                    return Err(interrupted(
                        url,
                        format!("no response within {window:?} of the end of the request body"),
                    ));
                }
            };
            // A head that arrived before hyper took the end of the body is an
            // early answer. The body is cut when the response ends, and the
            // cut closes the connection.
            let reuse = h1.filter(|_| exchange.has_ended() && !closes(&response));
            return Ok((response, protocol, RequestEnd { exchange }, reuse));
        }
    }

    /// Starts the upload request against `destination`, in the form that
    /// `protocol` and `via` give it.
    ///
    /// [`request_head`](Fetcher::request_head) does the same for a fetch.
    fn upload_head(
        &self,
        destination: &Destination,
        plan: &UploadPlan<'_>,
        headers: &[(HeaderName, HeaderValue)],
        protocol: Protocol,
        via: Via<'_>,
    ) -> Result<Request<()>> {
        self.request_head(plan.method.clone(), destination, headers, protocol, via)?
            .body(())
            .map_err(|e| Error::Fetch(format!("invalid request for {}: {e}", destination.url())))
    }

    /// Builds the GET for `request` against `destination`, with `headers` and
    /// the validators of the request.
    fn build_request(
        &self,
        destination: &Destination,
        request: &FetchRequest<'_>,
        headers: &[(HeaderName, HeaderValue)],
        protocol: Protocol,
        via: Via<'_>,
    ) -> Result<Request<RequestBody>> {
        let mut builder = self.request_head(Method::GET, destination, headers, protocol, via)?;
        if let Some(validators) = request.validators {
            if let Some(etag) = &validators.etag {
                builder = builder.header(hyper::header::IF_NONE_MATCH, etag);
            }
            if let Some(last_modified) = &validators.last_modified {
                builder = builder.header(hyper::header::IF_MODIFIED_SINCE, last_modified);
            }
        }
        builder
            .body(RequestBody::Empty)
            .map_err(|e| Error::Fetch(format!("invalid request for {}: {e}", destination.url())))
    }

    /// Starts the request of `method` against `destination`, with `headers`.
    ///
    /// An HTTP/1.1 request carries the origin-form target and a `Host` header,
    /// as an origin server expects. The absolute form is for proxy requests,
    /// and a plain static-file server answers 404 to it. An HTTP/2 request
    /// carries the absolute URL, and hyper fills the `:scheme` and
    /// `:authority` pseudo-headers from it.
    ///
    /// A request to a cleartext origin over a proxy connection carries the
    /// absolute form, which tells the proxy where to send it. Its `Host`
    /// header names the origin.
    ///
    /// The request also carries the credential of the proxy, if the merged
    /// headers hold no `Proxy-Authorization`. A `Proxy-Authorization` header
    /// that the caller set states the value for the proxy. Two values of one
    /// name give the proxy two answers to one question.
    ///
    /// A `CONNECT` tunnel carries the proxy credential of the fetcher on the
    /// `CONNECT` alone. So the request over the tunnel is the request that a
    /// direct connection sends.
    fn request_head(
        &self,
        method: Method,
        destination: &Destination,
        headers: &[(HeaderName, HeaderValue)],
        protocol: Protocol,
        via: Via<'_>,
    ) -> Result<hyper::http::request::Builder> {
        let absolute = protocol == Protocol::Http2 || matches!(via, Via::Absolute(_));
        let url = if absolute {
            destination.url()
        } else {
            destination.target()
        };
        let uri =
            Uri::try_from(url).map_err(|e| Error::Fetch(format!("invalid url {url}: {e}")))?;
        let mut builder = Request::builder().method(method).uri(uri);
        if protocol == Protocol::Http11 {
            builder = builder.header(hyper::header::HOST, &destination.authority);
        }
        for (name, value) in headers {
            builder = builder.header(name, value);
        }
        if let Via::Absolute(proxy) = via
            && let ProxyKind::Http {
                credential: Some(credential),
            } = &proxy.kind
            && !headers
                .iter()
                .any(|(name, _)| *name == hyper::header::PROXY_AUTHORIZATION)
        {
            builder = builder.header(hyper::header::PROXY_AUTHORIZATION, credential);
        }
        Ok(builder)
    }

    /// Opens a connection for `key`, and selects the protocol over ALPN if the
    /// origin is TLS.
    ///
    /// The key states the client configuration of the handshake. So a
    /// connection that presents the client certificate and a connection that
    /// presents none are two pool entries. The pool gives neither to the hop
    /// of the other.
    ///
    /// `via` states what the connection reaches. The key of a proxied
    /// cleartext origin is the proxy endpoint. So the socket goes where the key
    /// names, and the requests over it carry the absolute form.
    ///
    /// The key of a tunneled TLS origin is the origin. So the socket goes to
    /// the proxy, and the handshake runs over the `CONNECT` exchange.
    ///
    /// The key of an origin behind a SOCKS5 proxy is the origin. The socket
    /// goes to the proxy, and the SOCKS5 exchange runs first. Under
    /// `socks5://`, the fetcher resolves a host name first and tries each
    /// address over a connection of its own, as [`socks_each`] states. Then
    /// the connection goes on as a direct connection does, for a cleartext
    /// origin and for a TLS origin.
    ///
    /// A failure states if another attempt can get a different result. A proxy
    /// that refuses the tunnel with 407 refuses the credential of the fetcher,
    /// and no round of retries changes it. A SOCKS5 proxy that accepts no
    /// offered method or refuses the user name and the password does the
    /// same. A host name that SOCKS5 cannot send is definitive too. Each other
    /// failure is retryable.
    async fn connect(&self, key: &PoolKey, via: Via<'_>) -> std::result::Result<Sender, Failure> {
        let origin = &key.origin;
        let tcp = match via {
            Via::Direct => rt::TcpStream::connect(&origin.host, origin.port)
                .await
                .map_err(|e| {
                    Failure::Retry(Error::Fetch(format!(
                        "connect to {}:{} failed: {e}",
                        origin.host, origin.port
                    )))
                })?,
            // The key of a proxied cleartext origin is the proxy endpoint.
            Via::Absolute(proxy) | Via::Tunnel(proxy) => connect_proxy(proxy).await?,
            Via::Socks(proxy, socks) => socks_connect(proxy, socks, origin).await?,
        };
        if !origin.tls {
            // Cleartext HTTP/2 needs prior knowledge or an upgrade. The
            // fetcher uses neither. So a cleartext origin speaks HTTP/1.1,
            // over a proxy and over a direct connection.
            return self
                .handshake_h1(key, FuturesIo::new(tcp))
                .await
                .map_err(Failure::Retry);
        }
        let tcp = match via {
            Via::Tunnel(proxy) => self.tunnel(proxy, origin, tcp).await?,
            Via::Direct | Via::Absolute(_) | Via::Socks(..) => tcp,
        };
        let server_name =
            rustls::pki_types::ServerName::try_from(origin.host.clone()).map_err(|e| {
                Failure::Retry(Error::Fetch(format!(
                    "invalid server name {}: {e}",
                    origin.host
                )))
            })?;
        let config = if key.identity {
            &self.inner.tls.with_identity
        } else {
            &self.inner.tls.without_identity
        };
        let stream = futures_rustls::TlsConnector::from(config.clone())
            .connect(server_name, tcp)
            .await
            .map_err(|e| {
                Failure::Retry(Error::Fetch(format!(
                    "tls handshake with {} failed: {e}",
                    origin.host
                )))
            })?;
        let h2 = stream.get_ref().1.alpn_protocol() == Some(b"h2");
        let io = FuturesIo::new(stream);
        if h2 {
            self.handshake_h2(key, io).await.map_err(Failure::Retry)
        } else {
            self.handshake_h1(key, io).await.map_err(Failure::Retry)
        }
    }

    /// Asks `proxy` for a tunnel to `origin`, and returns the socket of the
    /// tunnel.
    ///
    /// The `CONNECT` goes over the HTTP/1.1 client of hyper, which reads the
    /// answer and gives the socket back. A 2xx to a `CONNECT` is an upgrade,
    /// and the upgraded I/O is the stream of the handshake. The connection
    /// future delivers it, so the future runs in its own task and ends with the
    /// upgrade.
    ///
    /// Bytes in the buffer after the response fail the connect. Nothing comes
    /// after a `CONNECT` response before the client speaks. So a proxy that
    /// sent bytes does not speak the protocol of the tunnel. A TLS handshake
    /// over that stream starts to read after those bytes.
    async fn tunnel(
        &self,
        proxy: &ProxyEndpoint,
        origin: &Origin,
        tcp: rt::TcpStream,
    ) -> std::result::Result<rt::TcpStream, Failure> {
        let refused = |what: String| Failure::Retry(Error::Fetch(what));
        let authority = connect_authority(origin);
        let host = HeaderValue::try_from(&authority).map_err(|_| {
            Failure::Fatal(Error::Fetch(format!(
                "the proxy {} cannot be asked to reach {authority}, which is not a usable host",
                proxy.named
            )))
        })?;
        let mut builder = Request::builder()
            .method(Method::CONNECT)
            .uri(&authority)
            .header(hyper::header::HOST, host);
        if let ProxyKind::Http {
            credential: Some(credential),
        } = &proxy.kind
        {
            builder = builder.header(hyper::header::PROXY_AUTHORIZATION, credential);
        }
        let request = builder.body(RequestBody::Empty).map_err(|e| {
            refused(format!(
                "the connect request to the proxy {} for {authority} is invalid: {e}",
                proxy.named
            ))
        })?;
        let (mut sender, connection) = hyper::client::conn::http1::handshake(FuturesIo::new(tcp))
            .await
            .map_err(|e| {
                refused(format!(
                    "http/1.1 handshake with the proxy {} failed: {e}",
                    proxy.named
                ))
            })?;
        // The connection future delivers the upgrade, so it runs beside the
        // request. The future and its task end with the upgrade.
        drop(rt::spawn(async move {
            let _ = connection.with_upgrades().await;
        }));
        let response = async {
            sender.ready().await?;
            sender.send_request(request).await
        }
        .await
        .map_err(|e| {
            refused(format!(
                "connect to {authority} through the proxy {} failed: {e}",
                proxy.named
            ))
        })?;
        let status = response.status();
        if !status.is_success() {
            let message = format!(
                "the proxy {} answered the connect to {authority} with {}",
                proxy.named,
                status.as_u16()
            );
            // A 407 refuses the credential of the fetcher, or the absence of
            // one. Each round of retries offers the same again.
            return Err(match status {
                StatusCode::PROXY_AUTHENTICATION_REQUIRED => Failure::Fatal(Error::Fetch(message)),
                _ => Failure::Retry(Error::Fetch(message)),
            });
        }
        let upgraded = hyper::upgrade::on(response).await.map_err(|e| {
            refused(format!(
                "the proxy {} accepted the connect to {authority} without tunneling it: {e}",
                proxy.named
            ))
        })?;
        let parts = upgraded
            .downcast::<FuturesIo<rt::TcpStream>>()
            .map_err(|_| {
                refused(format!(
                    "the tunnel through the proxy {} to {authority} is not the socket it was \
                     opened over",
                    proxy.named
                ))
            })?;
        if !parts.read_buf.is_empty() {
            return Err(refused(format!(
                "the proxy {} sent {} bytes after the connect to {authority}, which nothing may \
                 follow",
                proxy.named,
                parts.read_buf.len()
            )));
        }
        Ok(parts.io.into_inner())
    }

    /// Completes an HTTP/1.1 handshake, and runs the connection in its own
    /// task.
    async fn handshake_h1<S>(&self, key: &PoolKey, io: FuturesIo<S>) -> Result<Sender>
    where
        S: AsyncRead + AsyncWrite + WriteVectored + Send + Unpin + 'static,
    {
        let (sender, connection) =
            hyper::client::conn::http1::handshake(io)
                .await
                .map_err(|e| {
                    Error::Fetch(format!(
                        "http/1.1 handshake with {} failed: {e}",
                        key.origin.host
                    ))
                })?;
        drop(rt::spawn(async move {
            // The connection ends when the last sender drops or the peer closes
            // it. An error here shows on the next request over it.
            let _ = connection.await;
        }));
        Ok(Sender::H1(sender))
    }

    /// Completes an HTTP/2 handshake, runs the connection in its own task, and
    /// puts it in the pool.
    ///
    /// The next requests to this origin multiplex over the connection. The
    /// function opens the connection through the builder of hyper. Only the
    /// builder sets the flow-control windows and the keep-alive ping.
    async fn handshake_h2<S>(&self, key: &PoolKey, io: FuturesIo<S>) -> Result<Sender>
    where
        S: AsyncRead + AsyncWrite + WriteVectored + Send + Unpin + 'static,
    {
        let (sender, connection) = hyper::client::conn::http2::Builder::new(RtExecutor)
            .timer(RtTimer)
            .initial_stream_window_size(H2_STREAM_WINDOW)
            .initial_connection_window_size(self.inner.h2_connection_window)
            .keep_alive_interval(H2_KEEP_ALIVE_INTERVAL)
            .keep_alive_timeout(H2_KEEP_ALIVE_TIMEOUT)
            .handshake(io)
            .await
            .map_err(|e| {
                Error::Fetch(format!(
                    "http/2 handshake with {} failed: {e}",
                    key.origin.host
                ))
            })?;
        drop(rt::spawn(async move {
            let _ = connection.await;
        }));
        self.inner.put_h2(key, sender.clone());
        Ok(Sender::H2(sender))
    }
}

impl Inner {
    /// Returns a pooled connection for `key`, if one is still usable.
    ///
    /// An HTTP/1.1 connection comes with the instant at which it became idle.
    /// [`Inner::put_idle_h1`] takes that instant back if the connection
    /// carried no request.
    fn take_conn(&self, key: &PoolKey) -> Option<(Sender, Option<Instant>)> {
        let mut pool = self.pool.lock().expect("fetcher pool mutex");
        let entry = pool.get_mut(key)?;
        if let Some(h2) = &entry.h2 {
            if h2.is_closed() {
                entry.h2 = None;
            } else {
                return Some((Sender::H2(h2.clone()), None));
            }
        }
        while let Some(h1) = entry.h1.pop() {
            if !h1.sender.is_closed() {
                return Some((Sender::H1(h1.sender), Some(h1.since)));
            }
        }
        None
    }

    /// Returns a pooled connection that an upload can take for `key`.
    ///
    /// This is the HTTP/2 connection. If there is none, it is the HTTP/1.1
    /// connection that became idle last, if it became idle less than
    /// [`UPLOAD_IDLE`] ago.
    ///
    /// A server can close an idle connection at any time. If hyper began to
    /// write a request over a connection that the server closed, the request
    /// counts as sent. A server closes an idle connection at the end of its
    /// idle timeout, which is seconds or more. So the server keeps a connection
    /// that became idle a short time ago.
    ///
    /// An older idle connection stays in the pool for a fetch. If a fetch over
    /// it fails, the fetcher sends the fetch again.
    ///
    /// An HTTP/1.1 connection comes with the instant at which it became idle,
    /// as [`Inner::take_conn`] gives it.
    fn take_upload(&self, key: &PoolKey) -> Option<(Sender, Option<Instant>)> {
        let mut pool = self.pool.lock().expect("fetcher pool mutex");
        let entry = pool.get_mut(key)?;
        match &entry.h2 {
            Some(h2) if h2.is_closed() => entry.h2 = None,
            Some(h2) => return Some((Sender::H2(h2.clone()), None)),
            None => {}
        }
        while let Some(h1) = entry.h1.last() {
            if h1.sender.is_closed() {
                entry.h1.pop();
                continue;
            }
            if h1.since.elapsed() >= UPLOAD_IDLE {
                return None;
            }
            return entry
                .h1
                .pop()
                .map(|h1| (Sender::H1(h1.sender), Some(h1.since)));
        }
        None
    }

    /// Returns an idle HTTP/1.1 connection to the pool.
    ///
    /// The function finds the existing entry of an origin by reference. A new
    /// entry needs a key of its own, and the host string of that key is an
    /// allocation. The lookup by reference keeps that allocation off the warm
    /// path of each object fetch.
    fn put_h1(&self, key: &PoolKey, sender: H1Sender) {
        self.put_idle_h1(key, sender, Instant::now());
    }

    /// Returns an HTTP/1.1 connection to the pool that has been idle since
    /// `since`.
    ///
    /// A connection that the fetcher took and did not use goes back with the
    /// instant at which it became idle. So [`Inner::take_upload`] reads its
    /// true idle time.
    fn put_idle_h1(&self, key: &PoolKey, sender: H1Sender, since: Instant) {
        if sender.is_closed() {
            return;
        }
        let idle = IdleH1 { sender, since };
        let mut pool = self.pool.lock().expect("fetcher pool mutex");
        if let Some(entry) = pool.get_mut(key) {
            entry.h1.push(idle);
            return;
        }
        pool.entry(key.clone()).or_default().h1.push(idle);
    }

    /// Records the HTTP/2 connection of the origin, and keeps a usable
    /// connection that is already in the pool.
    ///
    /// Two concurrent connects to one origin each complete a handshake. The
    /// connection that loses the race serves only the request that opened it,
    /// and closes with that request. So it does not replace a pooled entry
    /// that other requests multiplex over. A replaced entry stays open, with
    /// no pool reference, until its own senders drop.
    ///
    /// The function finds the existing entry of an origin by reference, as
    /// [`Inner::put_h1`] does.
    fn put_h2(&self, key: &PoolKey, sender: H2Sender) {
        let mut pool = self.pool.lock().expect("fetcher pool mutex");
        if let Some(entry) = pool.get_mut(key) {
            match &entry.h2 {
                Some(pooled) if !pooled.is_closed() => {}
                _ => entry.h2 = Some(sender),
            }
            return;
        }
        pool.entry(key.clone()).or_default().h2 = Some(sender);
    }
}

/// A fetch that starts again if its body fails in transit.
///
/// A body fails in transit in these cases:
///
/// - the connection reports an error
/// - the peer stays silent past the
///   [`progress_timeout`](FetcherOptions::progress_timeout) window
/// - the transfer is slower than the
///   [`low_speed`](FetcherOptions::low_speed) rule allows.
///
/// Each refetch spends one repeat of
/// [`max_retries`](FetcherOptions::max_retries), the count that also limits
/// the rounds of the fetch. So the rounds of each fetch and the refetches
/// between them share one budget. A refetch sends the request again, from
/// the first destination. It sends the `Range` header of the request again,
/// if the request has one, and adds no `Range` header of its own. So a
/// refetch asks for the whole body again, or for the same part of it.
///
/// A body that fails in another way is not fetched again, because another
/// fetch fails in the same way. Examples are the size cap and a failure of
/// the consumer, such as a checksum mismatch.
pub struct Refetch<'f, 'r> {
    fetcher: &'f Fetcher,
    request: FetchRequest<'r>,
    /// The repeats left in the budget.
    left: u32,
    /// The repeats spent. The next delay doubles from this count.
    round: u32,
    /// The flag that the body of the latest fetch sets if it fails in transit.
    interrupted: Arc<AtomicBool>,
}

impl Refetch<'_, '_> {
    /// Fetches the request, with the rest of the budget for its rounds.
    ///
    /// # Errors
    ///
    /// This method returns the errors that [`Fetcher::fetch`] returns.
    pub async fn fetch(&mut self) -> Result<Fetched> {
        self.interrupted.store(false, Ordering::Relaxed);
        let (mut fetched, used) = self
            .fetcher
            .fetch_budgeted(&self.request, self.left)
            .await?;
        self.left -= used;
        self.round += used;
        if let Fetched::Body(body) = &mut fetched {
            body.interrupted = Some(self.interrupted.clone());
        }
        Ok(fetched)
    }

    /// Prepares the next refetch, or returns `error` if no refetch applies.
    ///
    /// `error` is the error that ended the consumer of the latest body, in the
    /// error type of the caller. If that body failed in transit and a repeat
    /// is left, the method spends the repeat. It then waits for the backoff
    /// delay before the next [`fetch`](Refetch::fetch).
    ///
    /// The consumer drops the body before it calls this method. So the delay
    /// holds no connection and no admission permit.
    ///
    /// # Errors
    ///
    /// Returns `error` as it is if the latest body did not fail in transit, or
    /// if no repeat is left.
    pub async fn retry<E>(&mut self, error: E) -> std::result::Result<(), E> {
        if !self.interrupted.load(Ordering::Relaxed) || self.left == 0 {
            return Err(error);
        }
        self.left -= 1;
        self.round += 1;
        rt::Timer::after(backoff(self.round)).await;
        Ok(())
    }
}

/// The result of one attempt.
#[allow(clippy::large_enum_variant)]
enum Attempted {
    Body(Body),
    NotModified,
}

/// A failure that ends a body. Each later read gives it again.
struct Failed {
    kind: std::io::ErrorKind,
    message: String,
}

impl Failed {
    fn error(&self) -> std::io::Error {
        std::io::Error::new(self.kind, self.message.clone())
    }
}

/// The response body of a fetch or of an upload.
///
/// A read gives the bytes of the response in bounded chunks. The body releases
/// its connection and the admission permit when it reaches its end or is
/// dropped. A body dropped before its end closes an HTTP/1.1 connection and
/// resets an HTTP/2 stream, because the rest of the response is still in
/// flight.
///
/// # Failures
///
/// A failure ends the body. The failed read and each later read fail with the
/// same error. So a consumer that reads past a failure never gets a clean end
/// of stream, and cannot take a truncated object for a complete one. The
/// failures and their error kinds are:
///
/// - the size cap: [`FileTooLarge`](std::io::ErrorKind::FileTooLarge)
/// - the progress window and the low-speed rule:
///   [`TimedOut`](std::io::ErrorKind::TimedOut)
/// - a transport failure: [`Other`](std::io::ErrorKind::Other).
///
/// A failure releases neither the connection nor the permit. The drop of the
/// body releases both.
pub struct Body {
    incoming: Incoming,
    /// Bytes received from the connection and not yet copied to the caller.
    chunk: Bytes,
    /// The bytes taken from the connection. The caller is behind this count by
    /// the bytes still in `chunk`. The size cap applies to this count.
    received: u64,
    max_size: Option<u64>,
    status: u16,
    validators: Validators,
    content_length: Option<u64>,
    protocol: Protocol,
    inner: Arc<Inner>,
    /// The counters of the fetcher handle that made the request. Each data
    /// frame adds its bytes to each of them.
    counters: Arc<[Arc<AtomicU64>]>,
    /// The pool entry of the connection of this body. The connection goes back
    /// to this entry.
    key: PoolKey,
    /// The HTTP/1.1 connection to return to the pool at the end of the body.
    reuse: Option<H1Sender>,
    /// The admission permit, which the body holds while it is in flight.
    permit: Option<Permit>,
    done: bool,
    /// The failure that ended the body, if one did.
    failed: Option<Failed>,
    /// The time that the peer can stay silent while a read waits for it.
    deadline: rt::Deadline,
    /// `true` while the window runs, from the read that finds nothing until
    /// the next frame arrives.
    ///
    /// The window measures the silence since a read wanted bytes. It keeps
    /// running also when no read is outstanding. A body that no read found
    /// empty is not on the clock.
    waiting: bool,
    /// The low-speed rule of the body, if the fetcher has one.
    low_speed: Option<Monitor>,
    /// The flag that reports a failure in transit to a [`Refetch`], which
    /// fetches the body again. The size cap is not a failure in transit,
    /// because another fetch of the body grows past the cap in the same way.
    interrupted: Option<Arc<AtomicBool>>,
    /// The request body of an upload. It fails when this body ends, or when
    /// this body is dropped before the request body ends.
    request_body: Option<RequestEnd>,
}

/// The low-speed state of one body: the delivered bytes, the last samples of
/// that count, and the time below the limit.
struct Monitor {
    rule: LowSpeed,
    /// The bytes that the body delivered since its first read.
    counted: u64,
    /// `counted` at the last [`RATE_SPAN`] samples. The slot of the next
    /// sample holds the oldest sample. A slot that no sample filled holds the
    /// count at the first read, which is zero.
    samples: [u64; RATE_SPAN],
    /// The samples taken since the first read.
    taken: u64,
    /// The whole seconds for which the rate stayed at or below the limit, or
    /// `None` while the rate is above it. A body starts below the limit, at
    /// zero.
    below: Option<u64>,
    /// The time to the next sample.
    deadline: rt::Deadline,
    /// `true` after the measurement starts, which is at the first read.
    started: bool,
}

impl Monitor {
    /// Creates the monitor for `rule`. The measurement starts at the first
    /// read.
    fn new(rule: LowSpeed) -> Monitor {
        Monitor {
            rule,
            counted: 0,
            samples: [0; RATE_SPAN],
            taken: 0,
            below: Some(0),
            deadline: rt::Deadline::new(SAMPLE_PERIOD),
            started: false,
        }
    }

    /// Returns `true` if the rate stayed below the limit for the time of the
    /// rule.
    ///
    /// Each expiry of the timer takes one sample. If the next sample is not
    /// due, the poll leaves the timer to wake the task at the due time.
    fn below_rate(&mut self, cx: &mut Context<'_>) -> bool {
        while self.deadline.poll_expired(cx).is_ready() {
            if self.sample() {
                return true;
            }
            self.deadline.restart();
        }
        false
    }

    /// Takes one sample, and returns `true` if the rate now stayed below the
    /// limit for the time of the rule.
    fn sample(&mut self) -> bool {
        self.taken += 1;
        let slot = (self.taken % RATE_SPAN as u64) as usize;
        let span = self.taken.min(RATE_SPAN as u64);
        let carried = self.counted - self.samples[slot];
        self.samples[slot] = self.counted;
        // A sample of exactly the limit counts as below it, as in the `ostree`
        // command. The samples of that command span a little more than their
        // whole seconds.
        if carried > u64::from(self.rule.limit) * span {
            self.below = None;
            return false;
        }
        let below = self.below.map_or(0, |seconds| seconds + 1);
        self.below = Some(below);
        Duration::from_secs(below) >= self.rule.time
    }
}

impl std::fmt::Debug for Body {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Body")
            .field("protocol", &self.protocol)
            .field("content_length", &self.content_length)
            .field("received", &self.received)
            .finish_non_exhaustive()
    }
}

impl Body {
    /// Returns the status of the response that carries the body.
    ///
    /// The status of a fetch is 200, or 206 for a request with a `Range`
    /// header. A caller that sends `Range` reads the status to tell a part of
    /// the object from the whole object. The status of the body of an upload
    /// is [`Uploaded::status`].
    pub fn status(&self) -> u16 {
        self.status
    }

    /// Returns the validators to send on the next fetch of this target.
    pub fn validators(&self) -> &Validators {
        &self.validators
    }

    /// Returns the `Content-Length` that the response declared, if it declared
    /// one.
    pub fn content_length(&self) -> Option<u64> {
        self.content_length
    }

    /// Returns the HTTP version that carried the response.
    pub fn protocol(&self) -> Protocol {
        self.protocol
    }

    /// Returns the number of bytes that the body took from the connection.
    ///
    /// The body takes a frame whole and gives it out in as many reads as the
    /// buffers of the caller need. So this count can be up to one frame ahead
    /// of the bytes that the caller read. The size cap applies to this count.
    /// A caller that needs the count of delivered bytes counts them in its own
    /// read loop.
    pub fn received(&self) -> u64 {
        self.received
    }

    /// Latches a failure that ends the body, and returns it.
    ///
    /// The body keeps the connection and the permit, so the drop releases
    /// them. The drop closes the connection, because a response still in
    /// flight does not go back to the pool.
    fn fail(&mut self, kind: std::io::ErrorKind, message: String) -> std::io::Error {
        let failed = Failed { kind, message };
        let error = failed.error();
        self.failed = Some(failed);
        error
    }

    /// Latches a failure in transit, and returns it. A [`Refetch`] can repeat
    /// the fetch for such a failure.
    fn interrupt(&mut self, kind: std::io::ErrorKind, message: String) -> std::io::Error {
        if let Some(interrupted) = &self.interrupted {
            interrupted.store(true, Ordering::Relaxed);
        }
        self.fail(kind, message)
    }

    /// Returns the failure of the low-speed rule, if the rate stayed at or
    /// below the limit for the time of the rule.
    ///
    /// Returns `None` while the rate holds, or if no rule applies.
    fn poll_low_speed(&mut self, cx: &mut Context<'_>) -> Option<std::io::Error> {
        let monitor = self.low_speed.as_mut()?;
        if !monitor.below_rate(cx) {
            return None;
        }
        let LowSpeed { limit, time } = monitor.rule;
        Some(self.interrupt(
            std::io::ErrorKind::TimedOut,
            format!("fetched body averaged below {limit} bytes per second for {time:?}"),
        ))
    }
}

/// The read trait of `futures-io`.
impl AsyncRead for Body {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        if buf.is_empty() {
            return Poll::Ready(Ok(0));
        }
        let me = self.get_mut();
        if let Some(failed) = &me.failed {
            return Poll::Ready(Err(failed.error()));
        }
        // The low-speed measurement starts at the first read. So the time for
        // which a body waits for its consumer before that read is not counted.
        if let Some(monitor) = &mut me.low_speed
            && !monitor.started
        {
            monitor.deadline.restart();
            monitor.started = true;
        }
        loop {
            if !me.chunk.is_empty() {
                let n = me.chunk.len().min(buf.len());
                buf[..n].copy_from_slice(&me.chunk[..n]);
                me.chunk = me.chunk.slice(n..);
                return Poll::Ready(Ok(n));
            }
            if me.done {
                return Poll::Ready(Ok(0));
            }
            let frame = match Pin::new(&mut me.incoming).poll_frame(cx) {
                Poll::Ready(frame) => frame,
                Poll::Pending => {
                    // Nothing arrived since the last frame. So the read fails
                    // when the progress window runs out, or when the rate
                    // stayed below the low-speed limit for the time of the rule.
                    if let Some(error) = me.poll_low_speed(cx) {
                        return Poll::Ready(Err(error));
                    }
                    if !me.waiting {
                        me.deadline.restart();
                        me.waiting = true;
                    }
                    return match me.deadline.poll_expired(cx) {
                        Poll::Ready(()) => {
                            let window = me.deadline.window();
                            Poll::Ready(Err(me.interrupt(
                                std::io::ErrorKind::TimedOut,
                                format!("fetched body delivered nothing for {window:?}"),
                            )))
                        }
                        Poll::Pending => Poll::Pending,
                    };
                }
            };
            match frame {
                Some(Ok(frame)) => {
                    // The peer delivered. The window stops until the next read
                    // finds nothing.
                    me.waiting = false;
                    // Trailers carry no payload, so the loop polls again for
                    // data.
                    if let Ok(data) = frame.into_data() {
                        me.received += data.len() as u64;
                        for counter in me.counters.iter() {
                            counter.fetch_add(data.len() as u64, Ordering::Relaxed);
                        }
                        if let Some(limit) = me.max_size
                            && me.received > limit
                        {
                            return Poll::Ready(Err(me.fail(
                                std::io::ErrorKind::FileTooLarge,
                                format!("fetched body exceeds the {limit}-byte cap"),
                            )));
                        }
                        if let Some(monitor) = &mut me.low_speed {
                            monitor.counted += data.len() as u64;
                        }
                        me.chunk = data;
                        if let Some(error) = me.poll_low_speed(cx) {
                            return Poll::Ready(Err(error));
                        }
                    }
                }
                Some(Err(e)) => {
                    // hyper reports a body error once, and then reports the end
                    // of the body. Without the latch, the next read returns a
                    // clean end of stream for a truncated object.
                    let message = with_cause(&e);
                    return Poll::Ready(Err(me.interrupt(std::io::ErrorKind::Other, message)));
                }
                None => {
                    me.done = true;
                    // The whole response arrived, so the connection can serve
                    // the next request. The response to an upload ends its
                    // request body, and hyper then stops the send of that body.
                    if let Some(sender) = me.reuse.take() {
                        me.inner.put_h1(&me.key, sender);
                    }
                    me.request_body = None;
                    me.permit = None;
                    return Poll::Ready(Ok(0));
                }
            }
        }
    }
}

/// The read trait of tokio.
#[cfg(feature = "tokio")]
impl rt::tokio_io::AsyncRead for Body {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut rt::tokio_io::ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        let unfilled = buf.initialize_unfilled();
        let n = ready!(AsyncRead::poll_read(self, cx, unfilled))?;
        buf.advance(n);
        Poll::Ready(Ok(()))
    }
}

/// The writing end of a streamed [`UploadBody`], from
/// [`UploadBody::channel`].
///
/// The writer and the upload run together. So a caller writes from a task
/// other than the task that awaits [`Fetcher::upload`], or joins the two
/// futures.
///
/// # Frames
///
/// Writes fill a frame of 64 KiB. The writer gives a full frame to the
/// connection through a slot of one frame. If the slot is still full, a write
/// waits until hyper takes the frame in it.
///
/// - [`flush`](AsyncWrite::poll_flush) hands over a part-filled frame, and
///   waits until hyper takes it.
/// - [`close`](AsyncWrite::poll_close) hands over the rest, ends the body,
///   and waits until hyper takes the end.
///
/// A flush of less than 4 KiB hands over a copy of the bytes and keeps the
/// frame buffer. A larger frame moves to the connection as it is, and the next
/// write allocates the next buffer.
///
/// # Memory
///
/// The writer holds at most two frames: the frame that it fills and the frame
/// in the slot. The connection holds more for each upload in flight.
///
/// Over HTTP/1.1, hyper takes another frame while it holds fewer than 16
/// frames and less than 408 KiB. So it holds up to 472 KiB. Over HTTP/2, it
/// holds up to two frames.
///
/// # Stalls
///
/// After the upload hands its request to the connection, a frame can wait in
/// the slot for the [`progress_timeout`](FetcherOptions::progress_timeout) of
/// its fetcher. If it does, the wait fails with
/// [`io::ErrorKind::TimedOut`](std::io::ErrorKind::TimedOut), and the body
/// fails. Before the hand-over, a wait has no bound of its own.
///
/// # Failures
///
/// If the writer is dropped before close, the body fails. So hyper ends the
/// request unfinished, and the server never receives a truncated body as a
/// whole body. A writer dropped before the hand-over leaves the request
/// unsent.
///
/// A write fails with
/// [`io::ErrorKind::BrokenPipe`](std::io::ErrorKind::BrokenPipe) in these
/// cases:
///
/// - after close
/// - after the upload is done with the body
/// - after the response ended before the body.
///
/// A failure is latched: each later call fails in the same way.
pub struct UploadWriter {
    exchange: Arc<Exchange>,
    /// The frame that the writer fills. If a frame is handed over whole, the
    /// buffer has no capacity, and the next write allocates the next frame.
    buffer: Vec<u8>,
    /// The timer of the stall window. The first wait on a full slot after the
    /// hand-over arms it. If the timer fires before the frame in the slot
    /// waited the whole window, the writer arms it again for the rest.
    stall: Option<rt::Deadline>,
    /// The failure that ended the writer, if one did.
    failed: Option<Failed>,
}

impl std::fmt::Debug for UploadWriter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UploadWriter")
            .field("buffered", &self.buffer.len())
            .finish_non_exhaustive()
    }
}

/// The message of a write after the upload is done with the body.
const UPLOAD_ENDED: &str = "the upload of this body has ended";

impl UploadWriter {
    /// Returns `true` if the request of this body is handed over to a
    /// connection.
    ///
    /// The value becomes `true` at the hand-over. It becomes `false` again if
    /// the connection gives the request back unwritten, until the next round
    /// hands it over. A caller that counts the bytes that a request sent counts
    /// the bytes that it wrote after the hand-over.
    pub fn is_handed_over(&self) -> bool {
        self.exchange.lock().stall.is_some()
    }

    /// Latches a failure that ends the writer, and returns it.
    fn fail(&mut self, kind: std::io::ErrorKind, message: String) -> std::io::Error {
        let failed = Failed { kind, message };
        let error = failed.error();
        self.failed = Some(failed);
        error
    }

    /// Moves the buffer into the slot as the next frame, when the slot is
    /// free.
    ///
    /// The function copies a frame of less than [`SMALL_FRAME`] bytes, and
    /// keeps the buffer for the next frames. So a small frame that waits in the
    /// connection holds no more memory than its bytes. A larger frame moves as
    /// it is, and the next write allocates the next buffer.
    fn poll_hand_over(&mut self, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let mut slot = self.exchange.lock();
        if slot.gone || slot.aborted.is_some() {
            drop(slot);
            return Poll::Ready(Err(
                self.fail(std::io::ErrorKind::BrokenPipe, UPLOAD_ENDED.into())
            ));
        }
        if slot.frame.is_some() {
            park(&mut slot.writer, cx);
            let end = slot.stall_end();
            drop(slot);
            return self.poll_stall(cx, end);
        }
        let frame = if self.buffer.len() < SMALL_FRAME {
            let frame = Bytes::copy_from_slice(&self.buffer);
            self.buffer.clear();
            frame
        } else {
            Bytes::from(std::mem::take(&mut self.buffer))
        };
        slot.frame = Some(frame);
        slot.restart_stall();
        let reader = slot.reader.take();
        drop(slot);
        wake(reader);
        Poll::Ready(Ok(()))
    }

    /// Waits until hyper takes the frame in the slot.
    fn poll_taken(&mut self, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let mut slot = self.exchange.lock();
        if slot.gone || slot.aborted.is_some() {
            drop(slot);
            return Poll::Ready(Err(
                self.fail(std::io::ErrorKind::BrokenPipe, UPLOAD_ENDED.into())
            ));
        }
        if slot.frame.is_none() {
            return Poll::Ready(Ok(()));
        }
        park(&mut slot.writer, cx);
        let end = slot.stall_end();
        drop(slot);
        self.poll_stall(cx, end)
    }

    /// Polls the stall window of the frame in the slot. At `end`, the frame
    /// waited the whole window.
    ///
    /// The poll is pending while the window runs. It gives the failure of the
    /// body when the window runs out. Before the hand-over, `end` is `None`.
    /// The poll is then pending alone, and the take of the frame wakes the
    /// writer.
    fn poll_stall(
        &mut self,
        cx: &mut Context<'_>,
        mut end: Option<Instant>,
    ) -> Poll<std::io::Result<()>> {
        while let Some(at) = end {
            ready!(poll_until(&mut self.stall, cx, at));
            match self.exchange.check_stall() {
                Ok(message) => {
                    return Poll::Ready(Err(self.fail(std::io::ErrorKind::TimedOut, message)));
                }
                Err(later) => end = later,
            }
        }
        Poll::Pending
    }
}

/// The write trait of `futures-io`.
impl AsyncWrite for UploadWriter {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        let me = self.get_mut();
        if let Some(failed) = &me.failed {
            return Poll::Ready(Err(failed.error()));
        }
        let slot = me.exchange.lock();
        let refused = if slot.gone || slot.aborted.is_some() {
            Some(UPLOAD_ENDED)
        } else if slot.closed {
            Some("the upload body is closed")
        } else {
            None
        };
        drop(slot);
        if let Some(message) = refused {
            return Poll::Ready(Err(me.fail(std::io::ErrorKind::BrokenPipe, message.into())));
        }
        if buf.is_empty() {
            return Poll::Ready(Ok(0));
        }
        if me.buffer.len() == UPLOAD_FRAME {
            ready!(me.poll_hand_over(cx))?;
        }
        if me.buffer.capacity() == 0 {
            me.buffer.reserve_exact(UPLOAD_FRAME);
        }
        let n = (UPLOAD_FRAME - me.buffer.len()).min(buf.len());
        me.buffer.extend_from_slice(&buf[..n]);
        Poll::Ready(Ok(n))
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let me = self.get_mut();
        if let Some(failed) = &me.failed {
            return Poll::Ready(Err(failed.error()));
        }
        if !me.buffer.is_empty() {
            ready!(me.poll_hand_over(cx))?;
        }
        me.poll_taken(cx)
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let me = self.get_mut();
        if let Some(failed) = &me.failed {
            return Poll::Ready(Err(failed.error()));
        }
        if !me.buffer.is_empty() {
            ready!(me.poll_hand_over(cx))?;
        }
        let mut slot = me.exchange.lock();
        let reader = if slot.closed {
            None
        } else {
            slot.closed = true;
            slot.reader.take()
        };
        if slot.ended {
            drop(slot);
            wake(reader);
            return Poll::Ready(Ok(()));
        }
        if slot.gone || slot.aborted.is_some() {
            drop(slot);
            return Poll::Ready(Err(
                me.fail(std::io::ErrorKind::BrokenPipe, UPLOAD_ENDED.into())
            ));
        }
        park(&mut slot.writer, cx);
        // The stall window runs over a frame that waits in the slot. An empty
        // slot waits for hyper to poll the end, and that wait is no stall of
        // the body.
        let end = slot.stall_end();
        drop(slot);
        wake(reader);
        me.poll_stall(cx, end)
    }
}

/// The write trait of tokio.
#[cfg(feature = "tokio")]
impl rt::tokio_io::AsyncWrite for UploadWriter {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        AsyncWrite::poll_write(self, cx, buf)
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        AsyncWrite::poll_flush(self, cx)
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        AsyncWrite::poll_close(self, cx)
    }
}

impl Drop for UploadWriter {
    fn drop(&mut self) {
        let mut slot = self.exchange.lock();
        if slot.closed {
            return;
        }
        let wakers =
            slot.abort(|| "the upload writer was dropped before it closed the body".into());
        drop(slot);
        wakers.into_iter().for_each(wake);
    }
}

/// Returns `true` if a header name carries credentials, which no cleartext
/// destination gets.
///
/// The value of these three header names is a secret, whatever it holds. The
/// fetcher sends each other header as the caller wrote it, except `Host` and
/// the connection-layer names, which both layers refuse. A caller can put a
/// secret into such a header, and the fetcher cannot tell.
fn is_credential(name: &HeaderName) -> bool {
    *name == hyper::header::AUTHORIZATION
        || *name == hyper::header::PROXY_AUTHORIZATION
        || *name == hyper::header::COOKIE
}

/// Returns `true` if the connection layer sets a header of this name, so that
/// no caller can set it.
///
/// A `HeaderName` holds its name in lower case, and [`CONNECTION_HEADERS`] is
/// in lower case too.
fn is_connection_header(name: &HeaderName) -> bool {
    CONNECTION_HEADERS.contains(&name.as_str())
}

/// Returns the error for a layer that sets a header of the connection layer.
fn connection_header_refused(name: &HeaderName) -> Error {
    Error::Fetch(format!(
        "the {name} header is set by the connection layer, which frames the request and \
         holds its connection: drop the header"
    ))
}

/// Checks that a request path can go after the base path of a mirror.
///
/// A target is the base path with the request path added as written. So a `?`
/// or a `#` in that path is a delimiter. A `?` sends its tail as a query string
/// that the server matches on. A `#` and its tail drop out when the URL is
/// built, and the request asks for a different resource.
fn check_path(path: &str) -> Result<()> {
    if let Some(at) = path.find(['?', '#']) {
        let found = &path[at..=at];
        return Err(Error::Fetch(format!(
            "fetch path {path} carries {found}: a path holds no query and no fragment"
        )));
    }
    Ok(())
}

/// Checks that the query string `query` of a request for `path` can go after
/// the path.
///
/// The target is the path with `?` and the query added as written. So a `#`
/// in the query is a delimiter. The `#` and its tail drop out when the URL is
/// built, and the server gets a query other than the query of the caller.
fn check_query(path: &str, query: &str) -> Result<()> {
    if query.contains('#') {
        return Err(Error::Fetch(format!(
            "fetch query of {path}?{query} carries #: a query holds no fragment"
        )));
    }
    Ok(())
}

/// Returns the headers of one request: the headers of the fetcher, with the
/// headers of the request merged over them.
///
/// A request header replaces the fetcher header of the same name. The
/// credentials of the request replace the `Authorization` of the fetcher,
/// whichever of the two layers set it. These credentials are basic-auth
/// credentials or a bearer token, never both.
///
/// Two request headers of one name both reach the wire, as two fetcher headers
/// do. A request that adds no header and no credentials sends the list of the
/// fetcher as it is. Each object fetch takes this path.
///
/// A credential value is marked sensitive. So an HTTP/2 connection never adds
/// it to its header compression table.
fn merge_headers<'a>(
    fetcher: &'a [(HeaderName, HeaderValue)],
    extra: &[(String, String)],
    basic_auth: Option<&BasicAuth>,
    bearer_token: Option<&BearerToken>,
) -> Result<Cow<'a, [(HeaderName, HeaderValue)]>> {
    if extra.is_empty() && basic_auth.is_none() && bearer_token.is_none() {
        return Ok(Cow::Borrowed(fetcher));
    }
    if basic_auth.is_some() && bearer_token.is_some() {
        return Err(Error::Fetch(BASIC_AND_BEARER.into()));
    }
    let credential = usize::from(basic_auth.is_some() || bearer_token.is_some());
    let mut added = Vec::with_capacity(extra.len() + credential);
    for (name, value) in extra {
        let name = HeaderName::try_from(name.as_str())
            .map_err(|_| Error::Fetch(format!("invalid header name: {name}")))?;
        if name == hyper::header::HOST {
            return Err(Error::Fetch(HOST_COMES_FROM_THE_URL.into()));
        }
        if is_connection_header(&name) {
            return Err(connection_header_refused(&name));
        }
        if name == hyper::header::AUTHORIZATION && basic_auth.is_some() {
            return Err(Error::Fetch(AMBIGUOUS_AUTHORIZATION.into()));
        }
        if name == hyper::header::AUTHORIZATION && bearer_token.is_some() {
            return Err(Error::Fetch(AMBIGUOUS_BEARER.into()));
        }
        let mut value = HeaderValue::try_from(value.as_str())
            .map_err(|_| Error::Fetch(format!("invalid value for header {name}")))?;
        value.set_sensitive(is_credential(&name));
        added.push((name, value));
    }
    if let Some(auth) = basic_auth {
        added.push((hyper::header::AUTHORIZATION, basic_auth_value(auth)?));
    }
    if let Some(token) = bearer_token {
        added.push((hyper::header::AUTHORIZATION, bearer_value(token)?));
    }
    let mut headers = Vec::with_capacity(fetcher.len() + added.len());
    headers.extend(
        fetcher
            .iter()
            .filter(|(name, _)| !added.iter().any(|(replaced, _)| replaced == name))
            .cloned(),
    );
    headers.extend(added);
    Ok(Cow::Owned(headers))
}

/// Refuses a credential beside a request signer: basic-auth credentials if
/// `basic_auth` is `true`, a bearer token if `bearer_token` is `true`, or an
/// `Authorization` entry in `headers`.
///
/// A signature can set `Authorization`, as each of these can. So the signer
/// goes alone, and a choice of one sends a credential that the caller did not
/// choose. [`Fetcher::new`] refuses these on the fetcher, and a fetch and an
/// upload refuse them on a request.
fn refuse_beside_signer(
    headers: &[(String, String)],
    basic_auth: bool,
    bearer_token: bool,
) -> Result<()> {
    if basic_auth {
        return Err(Error::Fetch(SIGNER_AND_BASIC.into()));
    }
    if bearer_token {
        return Err(Error::Fetch(SIGNER_AND_BEARER.into()));
    }
    if headers.iter().any(|(name, _)| {
        HeaderName::try_from(name.as_str()).is_ok_and(|name| name == hyper::header::AUTHORIZATION)
    }) {
        return Err(Error::Fetch(SIGNER_AND_AUTHORIZATION.into()));
    }
    Ok(())
}

/// Returns the [`SignRequest`] of one request of `method` to `hop`, with
/// `payload`.
///
/// The path and the query come from the origin-form target of the hop, which
/// splits at its first `?`. The authority is the text of the `Host` header.
fn sign_request<'a>(
    method: &'a Method,
    hop: &'a Destination,
    payload: SignPayload<'a>,
) -> SignRequest<'a> {
    let target = hop.target();
    let (path, query) = match target.split_once('?') {
        Some((path, query)) => (path, Some(query)),
        None => (target, None),
    };
    SignRequest {
        method: method.as_str(),
        authority: hop.authority_text(),
        path,
        query,
        payload,
    }
}

/// Returns `headers` with the headers that `signer` gives for one request of
/// `method` to `hop`, with `payload`.
///
/// [`merge_headers`] merges the headers of the signer as headers of a
/// request. So each one replaces a header of the same name, and a `Host`
/// header or a header of the connection layer is refused. Each value of the
/// signer is marked sensitive, because a signature header such as a session
/// token is a credential whatever its name is. An error of the signer, or a
/// refused header, is a [`Failure::Final`].
fn signed<'a>(
    signer: &dyn RequestSigner,
    method: &Method,
    hop: &Destination,
    payload: SignPayload<'_>,
    headers: &'a [(HeaderName, HeaderValue)],
) -> std::result::Result<Cow<'a, [(HeaderName, HeaderValue)]>, Failure> {
    let added = signer
        .sign(&sign_request(method, hop, payload))
        .map_err(Failure::Final)?;
    let merged = merge_headers(headers, &added, None, None).map_err(Failure::Final)?;
    if added.is_empty() {
        return Ok(merged);
    }
    // A name of the signer replaces each header of that name, so each entry
    // of such a name holds a value of the signer.
    let mut merged = merged.into_owned();
    for (name, value) in &mut merged {
        if added
            .iter()
            .any(|(added, _)| name.as_str().eq_ignore_ascii_case(added))
        {
            value.set_sensitive(true);
        }
    }
    Ok(Cow::Owned(merged))
}

/// The state that the refusals of [`Fetcher::new`] that read no file give.
struct Checked {
    mirrors: Vec<Mirror>,
    headers: Vec<(HeaderName, HeaderValue)>,
    proxies: Proxies,
}

/// Makes each refusal of [`Fetcher::new`] that reads no file, in its order,
/// and gives the mirrors, the headers, and the proxies of `options`.
fn checked(options: &FetcherOptions) -> Result<Checked> {
    // A limit of zero or a time of zero measures nothing. No rate is under
    // a zero limit, and each transfer fails a zero time immediately.
    if let Some(low) = options.low_speed
        && (low.limit == 0 || low.time.is_zero())
    {
        return Err(Error::Fetch(format!(
            "low-speed limit {} bytes per second over {:?}: neither may be zero",
            low.limit, low.time
        )));
    }
    let mirrors = options
        .mirrors
        .iter()
        .map(|url| parse_mirror(url))
        .collect::<Result<Vec<_>>>()?;
    // The fetcher sends a credential with each request to each mirror. So
    // one cleartext entry in the list puts the credential on the wire in
    // the clear, and the fetcher refuses such a configuration, unless the
    // caller permits it. A fetch that withholds the credential gets a 401
    // that does not state why.
    let cleartext = if options.allow_cleartext_credentials {
        None
    } else {
        options
            .mirrors
            .iter()
            .zip(&mirrors)
            .find(|(_, mirror)| !mirror.origin.tls)
            .map(|(url, _)| url.as_str())
    };
    let credential = match (&options.basic_auth, &options.bearer_token) {
        (Some(_), Some(_)) => return Err(Error::Fetch(BASIC_AND_BEARER.into())),
        (Some(_), None) => Some("basic-auth credentials"),
        (None, Some(_)) => Some("the bearer token"),
        (None, None) if options.signer.is_some() => Some("the signature of the request signer"),
        (None, None) => None,
    };
    if options.signer.is_some() {
        refuse_beside_signer(
            &options.headers,
            options.basic_auth.is_some(),
            options.bearer_token.is_some(),
        )?;
    }
    if let Some(credential) = credential
        && let Some(url) = cleartext
    {
        return Err(Error::Fetch(format!(
            "{credential} would reach the http mirror {url} in the clear: \
             use https mirrors, drop the credentials, or set \
             FetcherOptions::allow_cleartext_credentials"
        )));
    }
    let mut headers = vec![
        (
            hyper::header::USER_AGENT,
            HeaderValue::from_static(USER_AGENT),
        ),
        (
            hyper::header::ACCEPT_ENCODING,
            HeaderValue::from_static(IDENTITY),
        ),
    ];
    // The length of the part of the list that the fetcher sets. A
    // configured header of one of those names replaces that entry, as a
    // request header replaces a fetcher header of the same name. Without
    // this rule, both go on the wire and give one question two answers.
    // After this part, the list holds configured headers, and two entries
    // of one name both go on the wire.
    let mut base = headers.len();
    for (name, value) in &options.headers {
        let name = HeaderName::try_from(name.as_str())
            .map_err(|_| Error::Fetch(format!("invalid header name: {name}")))?;
        if name == hyper::header::HOST {
            return Err(Error::Fetch(HOST_COMES_FROM_THE_URL.into()));
        }
        if is_connection_header(&name) {
            return Err(connection_header_refused(&name));
        }
        if name == hyper::header::AUTHORIZATION && options.basic_auth.is_some() {
            return Err(Error::Fetch(AMBIGUOUS_AUTHORIZATION.into()));
        }
        if name == hyper::header::AUTHORIZATION && options.bearer_token.is_some() {
            return Err(Error::Fetch(AMBIGUOUS_BEARER.into()));
        }
        if is_credential(&name)
            && let Some(url) = cleartext
        {
            return Err(Error::Fetch(format!(
                "the {name} header carries credentials, which the http mirror {url} \
                 would receive in the clear: use https mirrors, drop the header, or set \
                 FetcherOptions::allow_cleartext_credentials"
            )));
        }
        let mut value = HeaderValue::try_from(value.as_str())
            .map_err(|_| Error::Fetch(format!("invalid value for header {name}")))?;
        value.set_sensitive(is_credential(&name));
        if let Some(at) = headers[..base].iter().position(|(held, _)| *held == name) {
            headers.remove(at);
            base -= 1;
        }
        headers.push((name, value));
    }
    if let Some(auth) = &options.basic_auth {
        headers.push((hyper::header::AUTHORIZATION, basic_auth_value(auth)?));
    }
    if let Some(token) = &options.bearer_token {
        headers.push((hyper::header::AUTHORIZATION, bearer_value(token)?));
    }
    let proxies = resolve_proxy(&options.proxy)?;
    Ok(Checked {
        mirrors,
        headers,
        proxies,
    })
}

/// Makes each refusal of [`Fetcher::new`] that reads no file, in the order of
/// [`Fetcher::new`], with the same errors.
///
/// The check reads the low-speed rule, the mirrors, the credentials, the
/// headers, and the proxy of `options`. For [`Proxy::Environment`], it reads
/// the proxy variables of the process environment. It reads no file and no
/// trust store, parses no TLS material, builds no client, resolves no name,
/// and opens no connection. A caller can check the options of a fetcher with
/// it before it reads the files that complete the options, for example a
/// token file or a PEM file.
///
/// [`Fetcher::new`] makes these refusals first, and then the refusals of the
/// TLS setup. So a success of this check does not show that [`Fetcher::new`]
/// succeeds.
///
/// # Errors
///
/// - [`Error::Fetch`] if a [`low_speed`](FetcherOptions::low_speed) rule
///   holds a zero.
/// - [`Error::Fetch`] if a mirror URL is not a valid absolute URL, has no
///   usable host, or holds a query string or userinfo.
/// - [`Error::Fetch`] if a mirror URL names a port that is not a number
///   from 0 to 65535.
/// - [`Error::Unsupported`] if the scheme of a mirror URL is neither `http`
///   nor `https`.
/// - [`Error::Fetch`] if a header name or value is invalid, or if a header
///   is `Host` or a header that the connection layer sets.
/// - [`Error::Fetch`] if a mirror is cleartext `http`, the options hold a
///   credential, and
///   [`allow_cleartext_credentials`](FetcherOptions::allow_cleartext_credentials)
///   is `false`. [`Fetcher::new`] lists the credentials.
/// - [`Error::Fetch`] if two of these are set:
///   [`basic_auth`](FetcherOptions::basic_auth),
///   [`bearer_token`](FetcherOptions::bearer_token),
///   [`signer`](FetcherOptions::signer), and an `Authorization` header.
/// - [`Error::Fetch`] if [`basic_auth`](FetcherOptions::basic_auth) does
///   not give a valid header value, or if
///   [`bearer_token`](FetcherOptions::bearer_token) does not have the
///   token68 syntax.
/// - [`Error::Unsupported`] if a proxy URL is not `http://host[:port]`,
///   `socks5://host[:port]`, or `socks5h://host[:port]`, from the options
///   or from the environment.
/// - [`Error::Unsupported`] if a SOCKS5 URL has a userinfo that
///   [SOCKS proxies](Proxy#socks-proxies) refuses, or if an `http://` proxy
///   URL has a userinfo that the fetcher cannot send.
/// - [`Error::Unsupported`] if a proxy variable has
///   [white space](Proxy#white-space) around its value.
///
/// # Examples
///
/// ```
/// use ostrya_fetch::{Error, FetcherOptions, Proxy, check_options};
///
/// let mut options = FetcherOptions::new("https://example.com/repo");
/// options.proxy = Proxy::Url("http://proxy.example.com:3128".into());
/// check_options(&options).unwrap();
/// options.proxy = Proxy::Url("ftp://proxy.example.com".into());
/// assert!(matches!(check_options(&options), Err(Error::Unsupported(_))));
/// options.headers = vec![("Host".into(), "other.example.com".into())];
/// assert!(matches!(check_options(&options), Err(Error::Fetch(_))));
/// ```
pub fn check_options(options: &FetcherOptions) -> Result<()> {
    checked(options).map(drop)
}

/// Resolves the proxy configuration into the state that a fetch reads.
///
/// The function resolves each form once. So a fetch reads no environment and
/// parses no URL. A proxy URL that the fetcher cannot connect through fails
/// the constructor, from the options and from the environment. A value read
/// under a meaning of this crate sends the request to a place that the caller
/// did not name.
fn resolve_proxy(proxy: &Proxy) -> Result<Proxies> {
    match proxy {
        Proxy::None => Ok(Proxies::default()),
        Proxy::Environment => resolve_variables(&environment()),
        Proxy::Variables(variables) => resolve_variables(variables),
        Proxy::Url(url) => {
            let endpoint = Arc::new(parse_proxy(url)?);
            Ok(Proxies {
                http: Some(endpoint.clone()),
                https: Some(endpoint),
                exempt_all: false,
                exempt: Vec::new(),
            })
        }
    }
}

/// Returns the proxy variables of the process environment.
///
/// An empty value counts as unset. So the function leaves it out, and
/// [`variable`] has one rule to apply.
fn environment() -> Vec<(String, String)> {
    PROXY_VARIABLES
        .iter()
        .filter_map(|name| {
            let value = std::env::var(name).ok()?;
            (!value.is_empty()).then(|| ((*name).to_owned(), value))
        })
        .collect()
}

/// Resolves the variable forms of [`Proxy`] into the state that a fetch reads.
fn resolve_variables(variables: &[(String, String)]) -> Result<Proxies> {
    let all = variable(variables, "all_proxy", Some("ALL_PROXY"));
    let http = variable(variables, "http_proxy", None);
    let https = variable(variables, "https_proxy", Some("HTTPS_PROXY"));
    // The loop parses each variable that holds a value. The choice of the
    // proxy for each scheme comes after the loop, from the parsed values. So
    // the fetcher refuses a proxy URL that it cannot connect through, also if
    // no fetch reads it. An example is `all_proxy` beside both
    // scheme-specific variables.
    //
    // The loop parses one URL for both schemes once, and holds it as one
    // endpoint. So the pool key of a proxy connection is the same, whichever
    // variable named it.
    //
    // The loop refuses a value with white space at its start or end before
    // the parse, because the parse trims it. The rule is the same: the loop
    // checks each variable that it reads, also if no fetch uses it.
    // The message names the variable, and leaves out the value, which can hold
    // a password.
    let mut endpoints: Vec<(&str, Arc<ProxyEndpoint>)> = Vec::new();
    for (name, url) in [all, http, https].into_iter().flatten() {
        if url.trim() != url {
            return Err(Error::Unsupported(format!(
                "the proxy variable {name} has white space at the start or the end of \
                 its value"
            )));
        }
        if !endpoints.iter().any(|(named, _)| *named == url) {
            endpoints.push((url, Arc::new(parse_proxy(url)?)));
        }
    }
    let parsed = |held: Option<(&str, &str)>| {
        held.and_then(|(_, url)| endpoints.iter().find(|(named, _)| *named == url))
            .map(|(_, endpoint)| endpoint.clone())
    };
    let (http, https) = (parsed(http.or(all)), parsed(https.or(all)));
    let mut exempt_all = false;
    let mut exempt = Vec::new();
    for entry in variable(variables, "no_proxy", Some("NO_PROXY"))
        .map(|(_, value)| value)
        .unwrap_or_default()
        .split(',')
        .map(str::trim)
        .filter(|entry| !entry.is_empty())
    {
        if entry == "*" {
            exempt_all = true;
            continue;
        }
        exempt.extend(parse_exemption(entry));
    }
    Ok(Proxies {
        http,
        https,
        exempt_all,
        exempt,
    })
}

/// Returns the name and the value of one proxy variable.
///
/// The function reads the lower-case name first, then the upper-case name if
/// there is one. An empty value counts as unset. Of two entries of one name,
/// the function reads the first entry with a value.
fn variable<'a>(
    variables: &'a [(String, String)],
    lower: &str,
    upper: Option<&str>,
) -> Option<(&'a str, &'a str)> {
    let held = |name: &str| {
        variables
            .iter()
            .find(|(held, value)| held == name && !value.is_empty())
            .map(|(held, value)| (held.as_str(), value.as_str()))
    };
    held(lower).or_else(|| upper.and_then(held))
}

/// Reads one `no_proxy` entry, or returns `None` if it names no host.
///
/// An IPv6 literal holds colons of its own. So the function reads a port from
/// the bracketed form and from an entry with one colon. An unbracketed entry
/// with more colons is an address, with no port.
///
/// A port text that is not a number from 0 to 65535 leaves the whole entry as
/// one host text. No origin host holds that text, so the entry exempts
/// nothing.
///
/// The function drops an entry with no host after it removes the leading `.`
/// and the port. `.` and `:8080` are the two forms of such an entry. An empty
/// host text is the suffix of each host, so it exempts every origin if it
/// stays.
fn parse_exemption(entry: &str) -> Option<Exemption> {
    let (host, port) = match entry
        .strip_prefix('[')
        .and_then(|rest| rest.split_once(']'))
    {
        Some((inside, rest)) => (inside, rest.strip_prefix(':')),
        None => match entry.split_once(':') {
            Some((host, port)) if !port.contains(':') => (host, Some(port)),
            _ => (entry, None),
        },
    };
    // One leading `.` states the suffix match that the comparison makes in
    // all cases. So the code removes it, and the two forms of one entry are
    // one entry. A second `.` is part of the host text of the entry, which no
    // origin host holds.
    let named = |host: &str| host.strip_prefix('.').unwrap_or(host).to_ascii_lowercase();
    let (host, port) = match port.map(str::parse::<u16>) {
        Some(Ok(port)) => (named(host), Some(port)),
        Some(Err(_)) => (named(entry), None),
        None => (named(host), None),
    };
    (!host.is_empty()).then_some(Exemption { host, port })
}

/// Reads a proxy URL into the endpoint of a connection.
///
/// The request to a proxy names the origin. So the URL names an endpoint and
/// nothing else. A path other than `/`, a query, or a fragment states a value
/// that the request has no place for, and the function refuses it. The
/// scheme is `http`, with the default port 80, or `socks5` or `socks5h`, with
/// the default port 1080. Under `socks5` the client resolves the origin host,
/// and under `socks5h` the proxy resolves it.
///
/// Userinfo is the credential of the proxy. For an `http` proxy, the function
/// percent-decodes it and sends it as `Proxy-Authorization: Basic`, as a proxy
/// that works with other clients expects. For a SOCKS5 proxy, it becomes the
/// user name and the password of RFC 1929. A userinfo holds a password, so
/// each refusal names the URL with the userinfo left out.
fn parse_proxy(url: &str) -> Result<ProxyEndpoint> {
    let refused = |what: &str| {
        Error::Unsupported(format!(
            "proxy url {}: {what}",
            without_userinfo(url.trim())
        ))
    };
    let url = url.trim();
    let has_scheme = |scheme: &str| {
        url.get(..scheme.len())
            .is_some_and(|text| text.eq_ignore_ascii_case(scheme))
    };
    let (scheme, default_port, socks) = if has_scheme(PROXY_SCHEME) {
        (PROXY_SCHEME, 80, None)
    } else if has_scheme(SOCKS5_SCHEME) {
        (SOCKS5_SCHEME, 1080, Some(ResolveSide::Client))
    } else if has_scheme(SOCKS5H_SCHEME) {
        (SOCKS5H_SCHEME, 1080, Some(ResolveSide::Proxy))
    } else {
        return Err(refused(
            "a proxy is reached over http://host[:port], socks5://host[:port], or \
             socks5h://host[:port] alone",
        ));
    };
    if url.contains('#') {
        return Err(refused("a proxy url carries no fragment"));
    }
    let uri = Uri::try_from(url).map_err(|e| refused(&format!("invalid url: {e}")))?;
    // A `/` or a `?` in the userinfo moves a part of the password into the
    // path or the query. So these refusals do not show the path or the query.
    if uri.query().is_some() {
        return Err(refused("a proxy url carries no query string"));
    }
    if !matches!(uri.path(), "" | "/") {
        return Err(refused("a proxy url carries no path"));
    }
    // `Uri::host` holds an IPv6 literal in brackets. The brackets belong to
    // the authority. A connect resolves the bracketed form to nothing.
    let literal = uri.host().ok_or_else(|| refused("the url has no host"))?;
    let authority = uri
        .authority()
        .expect("a url holding a host holds an authority")
        .as_str();
    let userinfo = authority.rsplit_once('@').map(|(userinfo, _)| userinfo);
    let kind = if let Some(resolve) = socks {
        let credential = userinfo
            .map(socks::Credential::from_userinfo)
            .transpose()
            .map_err(|e| refused(&e.to_string()))?;
        ProxyKind::Socks(SocksProxy {
            resolve,
            credential,
        })
    } else {
        let credential = userinfo
            .map(|userinfo| {
                proxy_credential(userinfo).ok_or_else(|| {
                    refused(
                        "the userinfo is not a percent-encoded user name and password the proxy \
                         can be sent",
                    )
                })
            })
            .transpose()?;
        ProxyKind::Http { credential }
    };
    let host = literal
        .strip_prefix('[')
        .and_then(|inner| inner.strip_suffix(']'))
        .unwrap_or(literal)
        .to_ascii_lowercase();
    // `Uri` accepts an authority whose port it cannot read, and reports no
    // port for it. So the code reads the port text from the authority and
    // refuses it. The default port does not replace it.
    let port_text = authority
        .rsplit_once('@')
        .map_or(authority, |(_, host)| host)
        .strip_prefix(literal)
        .and_then(|rest| rest.strip_prefix(':'));
    let port = match (uri.port_u16(), port_text) {
        (Some(port), _) => port,
        (None, None) => default_port,
        (None, Some(text)) => {
            return Err(refused(&format!(
                "the port {text:?} is not a number from 0 to 65535"
            )));
        }
    };
    let named = if port == default_port {
        format!("{scheme}{literal}")
    } else {
        format!("{scheme}{literal}:{port}")
    };
    Ok(ProxyEndpoint {
        endpoint: Origin {
            tls: false,
            host,
            port,
        },
        kind,
        named,
    })
}

/// Returns the `Proxy-Authorization` value of the userinfo of one proxy URL.
///
/// The function percent-decodes the two fields, because a URL states a `:` or
/// an `@` of a credential in that encoding. `None` states that the userinfo
/// holds something else: an escape that is not two hexadecimal digits, or a
/// field whose bytes are not text.
fn proxy_credential(userinfo: &str) -> Option<HeaderValue> {
    let (user, password) = userinfo.split_once(':').unwrap_or((userinfo, ""));
    let field = |text: &str| String::from_utf8(percent_decode(text)?).ok();
    basic_auth_value(&BasicAuth {
        user: field(user)?,
        password: field(password)?,
    })
    .ok()
}

/// Percent-decodes one field of the userinfo of a proxy URL.
///
/// `%` starts an escape of exactly two hexadecimal digits. `None` states that
/// the field holds a different escape, which the fetcher refuses. The bytes as
/// written can give the proxy a credential other than the one that the caller
/// meant. The 407 answer to that credential names nothing.
fn percent_decode(field: &str) -> Option<Vec<u8>> {
    let bytes = field.as_bytes();
    let mut decoded = Vec::with_capacity(bytes.len());
    let mut at = 0;
    while at < bytes.len() {
        if bytes[at] != b'%' {
            decoded.push(bytes[at]);
            at += 1;
            continue;
        }
        let escape = bytes.get(at + 1..at + 3)?;
        if !escape.iter().all(u8::is_ascii_hexdigit) {
            return None;
        }
        let text = std::str::from_utf8(escape).ok()?;
        decoded.push(u8::from_str_radix(text, 16).ok()?);
        at += 3;
    }
    Some(decoded)
}

/// Returns the `host:port` that names the target of a `CONNECT`, and the
/// origin in a message of a SOCKS5 hop.
///
/// The port is always written, whatever the default of the scheme. The proxy
/// opens a TCP connection, which names a port and reads no scheme. An IPv6
/// literal keeps the brackets that separate the address from the port.
fn connect_authority(origin: &Origin) -> String {
    if origin.host.contains(':') {
        format!("[{}]:{}", origin.host, origin.port)
    } else {
        format!("{}:{}", origin.host, origin.port)
    }
}

/// Returns the failure of a SOCKS5 exchange that asks `proxy` to connect to
/// `origin`.
///
/// The error names the proxy and the origin, and keeps the class of the
/// SOCKS5 failure.
fn socks_failure(proxy: &ProxyEndpoint, origin: &Origin, e: socks::SocksError) -> Failure {
    let error = Error::Fetch(format!(
        "connect to {} through the proxy {} failed: {}",
        connect_authority(origin),
        proxy.named,
        e.reason
    ));
    match e.class {
        socks::Class::Definitive => Failure::Fatal(error),
        socks::Class::Retryable => Failure::Retry(error),
    }
}

/// Opens a TCP connection to `proxy`.
///
/// A failure is retryable and names the proxy.
async fn connect_proxy(proxy: &ProxyEndpoint) -> std::result::Result<rt::TcpStream, Failure> {
    rt::TcpStream::connect(&proxy.endpoint.host, proxy.endpoint.port)
        .await
        .map_err(|e| {
            Failure::Retry(Error::Fetch(format!(
                "connect to the proxy {} failed: {e}",
                proxy.named
            )))
        })
}

/// Opens a connection through the SOCKS5 `proxy` to `origin`, and returns
/// the stream after the handshake.
///
/// The function checks the host name before the resolve and before the
/// connect to the proxy, under each resolve side. So a name that SOCKS5
/// cannot send fails as definitive with no resolver and no reachable proxy.
///
/// If the client resolves the name, the function gives the addresses of
/// [`rt::resolve`] to [`socks_each`]. A failed resolve is retryable and names
/// the origin host. Each other case uses one connection to the proxy and one
/// handshake: the proxy resolves the name, or the origin host is an IP
/// address.
async fn socks_connect(
    proxy: &ProxyEndpoint,
    socks: &SocksProxy,
    origin: &Origin,
) -> std::result::Result<rt::TcpStream, Failure> {
    let target = socks::Target::new(&origin.host, origin.port);
    target
        .check()
        .map_err(|e| socks_failure(proxy, origin, e))?;
    if let (socks::Host::Name(name), ResolveSide::Client) = (&target.host, socks.resolve) {
        let addrs = rt::resolve(name, origin.port).await.map_err(|e| {
            Failure::Retry(Error::Fetch(format!(
                "connect to {} through the proxy {} failed: cannot resolve {name}: {e}",
                connect_authority(origin),
                proxy.named
            )))
        })?;
        return socks_each(proxy, socks, origin, &addrs).await;
    }
    let mut tcp = connect_proxy(proxy).await?;
    socks::handshake(&mut tcp, &target, socks.credential.as_ref())
        .await
        .map_err(|e| socks_failure(proxy, origin, e))?;
    Ok(tcp)
}

/// Asks the SOCKS5 `proxy` to connect to each address of `addrs` in turn, and
/// returns the stream of the first address that the proxy connects.
///
/// A refused `CONNECT` ends the connection to the proxy, so each address gets
/// a new connection. A definitive failure ends the loop, because each later
/// address gets the same answer. A failed connect to the proxy also ends the
/// loop. If the proxy refuses each address, the retryable failure names the
/// last address, the number of addresses, and the last reason.
///
/// `addrs` is not empty. The one caller gives the output of [`rt::resolve`],
/// which gives an error in place of an empty list.
async fn socks_each(
    proxy: &ProxyEndpoint,
    socks: &SocksProxy,
    origin: &Origin,
    addrs: &[SocketAddr],
) -> std::result::Result<rt::TcpStream, Failure> {
    let mut last = None;
    for addr in addrs {
        let mut tcp = connect_proxy(proxy).await?;
        let target = socks::Target {
            host: socks::Host::Ip(addr.ip()),
            port: addr.port(),
        };
        match socks::handshake(&mut tcp, &target, socks.credential.as_ref()).await {
            Ok(()) => return Ok(tcp),
            Err(e) if e.class == socks::Class::Definitive => {
                return Err(socks_failure(proxy, origin, e));
            }
            Err(e) => last = Some((addr, e)),
        }
    }
    let authority = connect_authority(origin);
    let (addr, e) = last.expect("the address list is never empty");
    let count = match addrs.len() {
        1 => "1 address".to_owned(),
        n => format!("{n} addresses"),
    };
    Err(Failure::Retry(Error::Fetch(format!(
        "connect to {authority} through the proxy {} failed at {addr}, the last of {count}: {}",
        proxy.named, e.reason
    ))))
}

/// Returns the `Authorization` value of basic credentials, marked sensitive.
fn basic_auth_value(auth: &BasicAuth) -> Result<HeaderValue> {
    let encoded =
        ostrya_core::base64::encode(format!("{}:{}", auth.user, auth.password).as_bytes());
    let mut value = HeaderValue::try_from(format!("Basic {encoded}"))
        .map_err(|_| Error::Fetch("invalid basic-auth credentials".into()))?;
    value.set_sensitive(true);
    Ok(value)
}

/// Returns the `Authorization` value of a bearer token, marked sensitive.
///
/// The fetcher sends the token as written, so it must hold the token68 syntax
/// of the header. The token is a secret, so a refusal names no part of it.
fn bearer_value(token: &BearerToken) -> Result<HeaderValue> {
    let refused = || {
        Error::Fetch(
            "the bearer token is not token68: it holds letters, digits, -, ., _, ~, +, or /, \
             then any number of ="
                .into(),
        )
    };
    if !is_token68(&token.token) {
        return Err(refused());
    }
    let mut value =
        HeaderValue::try_from(format!("Bearer {}", token.token)).map_err(|_| refused())?;
    value.set_sensitive(true);
    Ok(value)
}

/// Returns `true` if `token` holds the token68 syntax.
///
/// The syntax is one or more ASCII letters, digits, `-`, `.`, `_`, `~`, `+`,
/// or `/`, then any number of `=`.
fn is_token68(token: &str) -> bool {
    let body = token.trim_end_matches('=');
    !body.is_empty()
        && body
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"-._~+/".contains(&byte))
}

/// Returns the origin of one absolute URL, and the authority of that origin.
///
/// The function checks the scheme before it parses the URL. So a URL that the
/// fetcher cannot serve gets a message that names the scheme. An example is a
/// `file://` URL, which is not a well-formed HTTP URL.
///
/// The function refuses userinfo. Credentials that never reach the wire get a
/// 401 answer, which points at nothing. `credentials` names the field that
/// carries them.
///
/// A userinfo holds a password. So the refusal names the scheme and the host.
/// A message about a URL before the parse reaches its authority names the URL
/// with the userinfo left out.
fn parse_authority(url: &str, credentials: &str) -> Result<(Uri, Origin, String)> {
    let scheme = url
        .split_once("://")
        .map(|(scheme, _)| scheme)
        .ok_or_else(|| {
            Error::Fetch(format!(
                "url {} is not an absolute http or https url",
                without_userinfo(url)
            ))
        })?;
    let tls = match scheme {
        _ if scheme.eq_ignore_ascii_case(Scheme::HTTPS.as_str()) => true,
        _ if scheme.eq_ignore_ascii_case(Scheme::HTTP.as_str()) => false,
        other => {
            return Err(Error::Unsupported(format!(
                "fetch url scheme {other}: only http and https are fetched"
            )));
        }
    };
    let scheme = if tls { "https" } else { "http" };
    let uri = Uri::try_from(url)
        .map_err(|e| Error::Fetch(format!("invalid url {}: {e}", without_userinfo(url))))?;
    // `Uri::host` puts an IPv6 literal in brackets. The brackets belong to the
    // authority, and the `Host` header and the absolute URL carry them. A
    // connect resolves the bracketed form to nothing, and a TLS server name
    // cannot come from it.
    let literal = uri
        .host()
        .ok_or_else(|| Error::Fetch(format!("url {} has no host", without_userinfo(url))))?;
    if uri
        .authority()
        .is_some_and(|authority| authority.as_str().contains('@'))
    {
        return Err(Error::Fetch(format!(
            "the {scheme} url for {literal} carries userinfo, which the fetcher does not send: \
             pass credentials as {credentials}"
        )));
    }
    // A host is one origin in each case of its letters. So the pool key holds
    // it in lower case. A `Location` header repeats the case that the origin
    // server wrote. With two cases of one host in the pool, the fetcher opens
    // two connections and two HTTP/2 sessions to it.
    let host = literal
        .strip_prefix('[')
        .and_then(|inner| inner.strip_suffix(']'))
        .unwrap_or(literal)
        .to_ascii_lowercase();
    // `Uri` accepts an authority whose port it cannot read, and reports no
    // port for it. Examples are `h:99999`, `h:`, and `h:abc`. A fallback to the
    // default of the scheme serves the request from a port that the caller did
    // not name. The rebuilt authority also drops the port. So the code reads
    // the port text from the authority and refuses it.
    //
    // `Uri` also reads the port with the integer parse of `std`, which takes a
    // leading `+`. A port is ASCII digits alone, so the code refuses a port
    // text with any other byte. Leading zeros are digits, and `080` names the
    // port 80.
    let port_text = uri
        .authority()
        .and_then(|authority| authority.as_str().strip_prefix(literal))
        .and_then(|rest| rest.strip_prefix(':'));
    let default_port = if tls { 443 } else { 80 };
    let digits = port_text.is_none_or(|text| text.bytes().all(|b| b.is_ascii_digit()));
    let port = match (uri.port_u16().filter(|_| digits), port_text) {
        (Some(port), _) => port,
        (None, None) => default_port,
        (None, Some(text)) => {
            // A password with a `?` or a `#` ends the authority before the
            // `@`. So the userinfo check passes, and a URL with userinfo can
            // get this message.
            return Err(Error::Fetch(format!(
                "url {} names the port {text:?}, which is not a number from 0 to 65535",
                without_userinfo(url)
            )));
        }
    };
    let authority = if port == default_port {
        literal.to_string()
    } else {
        format!("{literal}:{port}")
    };
    Ok((uri, Origin { tls, host, port }, authority))
}

/// Returns the form of a URL for a message, before the parse reaches the
/// authority and refuses the userinfo.
///
/// Userinfo holds a password, so the function leaves out the part of the
/// authority before the `@`. The password arrives as the caller wrote it. It
/// can hold a `/`, a `?`, or a `#`, which are the characters that end an
/// authority. So the end of the authority is known only after the userinfo is
/// found.
///
/// The function reads an `@` at any position after the scheme as the end of a
/// userinfo. It leaves out all text up to the last `@`, so no password of any
/// form reaches the message. When the text has no valid scheme before the
/// first `://`, the userinfo starts at the start of the text.
///
/// If a URL has an `@` in its path and no userinfo, the message loses its
/// host. A redaction that reads no well-formed authority has this cost.
fn without_userinfo(url: &str) -> Cow<'_, str> {
    let start = scheme_len(url);
    match url[start..].rfind('@') {
        Some(at) => Cow::Owned(format!("{}{}", &url[..start], &url[start + at + 1..])),
        None => Cow::Borrowed(url),
    }
}

/// Returns the length of the `<scheme>://` prefix of `url`, or 0.
///
/// The prefix counts only when the text before the first `://` is a valid
/// scheme: an ASCII letter, then ASCII letters, digits, `+`, `-`, or `.`.
fn scheme_len(url: &str) -> usize {
    let Some(end) = url.find("://") else {
        return 0;
    };
    let mut chars = url[..end].chars();
    let valid = chars.next().is_some_and(|c| c.is_ascii_alphabetic())
        && chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'));
    if valid { end + 3 } else { 0 }
}

/// Returns the `Host` header value of one authority.
fn host_header(url: &str, authority: &str) -> Result<HeaderValue> {
    HeaderValue::try_from(authority).map_err(|_| {
        Error::Fetch(format!(
            "url {} has an unusable host",
            without_userinfo(url)
        ))
    })
}

/// Checks that `url` is a base URL that a fetcher takes as a mirror.
///
/// The check opens no connection and reads no file. It refuses these parts:
///
/// - a scheme other than `http` and `https`
/// - userinfo
/// - a query string
/// - a fragment
/// - an empty host
/// - a port that is not a number from 0 to 65535 in ASCII digits.
///
/// A port with leading zeros is the number that its digits give.
///
/// The check refuses an `@` at any position, also in the path and in the
/// query. The authority of a URL ends at the first `/`, `?`, or `#`. So a
/// password with one of those ends the authority before the `@`. The rest of
/// the userinfo then reads as a path or a query.
///
/// No refusal names the part of the URL before the last `@`.
///
/// # Errors
///
/// - [`Error::Fetch`] if `url` holds a `#` or an `@`.
/// - [`Error::Unsupported`] if the scheme is neither `http` nor `https`.
/// - [`Error::Fetch`] if `url` is not a valid absolute URL, or has no host.
/// - [`Error::Fetch`] if `url` holds a query string, or names a port that is
///   not valid.
/// - [`Error::Fetch`] if a `Host` header cannot hold the authority.
pub fn check_base_url(url: &str) -> Result<()> {
    if url.contains('#') {
        return Err(Error::Fetch(format!(
            "url {} carries a fragment, which no request sends",
            without_userinfo(url)
        )));
    }
    if url.contains('@') {
        return Err(Error::Fetch(format!(
            "url {} holds an '@', which can mark userinfo: a base url carries no userinfo, so \
             pass the credential in the options and not in the url",
            without_userinfo(url)
        )));
    }
    let mirror = parse_mirror(url)?;
    if mirror.origin.host.is_empty() {
        return Err(Error::Fetch(format!(
            "url {} has no host",
            without_userinfo(url)
        )));
    }
    Ok(())
}

/// Parses one base URL into a [`Mirror`].
fn parse_mirror(url: &str) -> Result<Mirror> {
    let (uri, origin, authority) = parse_authority(url, "FetcherOptions::basic_auth")?;
    // A request target is the base path of the mirror with the object path
    // added. So a query string of the base URL drops out with no message. The
    // code refuses it. A presigned URL that lost its signature gets a 403
    // answer, which does not point at the URL that caused it.
    //
    // A password with a `?` ends the authority there, and the rest of the
    // userinfo reads as the query. So the message names the URL with the
    // userinfo left out, and does not quote the query.
    if uri.query().is_some() {
        return Err(Error::Fetch(format!(
            "mirror url {} carries a query string, which the fetcher does not send",
            without_userinfo(url)
        )));
    }
    let scheme = if origin.tls { "https" } else { "http" };
    Ok(Mirror {
        authority: host_header(url, &authority)?,
        prefix: format!("{scheme}://{authority}"),
        base: uri.path().trim_end_matches('/').to_string(),
        origin,
    })
}

/// Parses the absolute URL of a request into its [`Destination`].
///
/// The query string is part of the name of the request, so it reaches the
/// wire as written. The function refuses a fragment. A fragment is never sent,
/// so a URL with one asks for a resource other than the resource that the
/// caller named.
fn parse_url(url: &str) -> Result<Destination> {
    if url.contains('#') {
        return Err(Error::Fetch(format!(
            "fetch url {} carries a fragment, which no request sends",
            without_userinfo(url)
        )));
    }
    let (uri, origin, authority) = parse_authority(url, "FetchRequest::basic_auth")?;
    let scheme = if origin.tls { "https" } else { "http" };
    let path_and_query = uri.path_and_query().map_or("/", |pq| pq.as_str());
    // The code builds the one string of the destination from the parts of the
    // parse. The whole string is the absolute URL. Its tail from the prefix
    // length is the origin-form target.
    let mut text =
        String::with_capacity(scheme.len() + 3 + authority.len() + 1 + path_and_query.len());
    text.push_str(scheme);
    text.push_str("://");
    text.push_str(&authority);
    let path_at = text.len();
    if !path_and_query.starts_with('/') {
        text.push('/');
    }
    text.push_str(path_and_query);
    Ok(Destination {
        authority: host_header(url, &authority)?,
        url: text,
        path_at,
        origin,
    })
}

/// Returns `true` if `response` closes its HTTP/1.1 connection when it ends.
///
/// This is an HTTP/1.0 response, or a response whose `Connection` header holds
/// `close`.
fn closes(response: &Response<Incoming>) -> bool {
    response.version() != Version::HTTP_11
        || response
            .headers()
            .get_all(hyper::header::CONNECTION)
            .iter()
            .filter_map(|value| value.to_str().ok())
            .flat_map(|value| value.split(','))
            .any(|token| token.trim().eq_ignore_ascii_case("close"))
}

/// Returns the `Content-Length` that a response declared, if it is usable.
fn content_length(headers: &hyper::HeaderMap) -> Option<u64> {
    headers
        .get(hyper::header::CONTENT_LENGTH)?
        .to_str()
        .ok()?
        .parse()
        .ok()
}

/// Returns the coding that a response declared, if the body needs a decode.
///
/// `Content-Encoding` codes the content, and `Transfer-Encoding` codes the
/// message that carries the content. A body under either holds bytes other
/// than the bytes that the remote stores, so the function reads both headers.
/// If a response declares both, the function returns its content coding.
fn declared_coding(headers: &hyper::HeaderMap) -> Option<String> {
    coding(headers, hyper::header::CONTENT_ENCODING, &[IDENTITY]).or_else(|| {
        coding(
            headers,
            hyper::header::TRANSFER_ENCODING,
            &[IDENTITY, CHUNKED],
        )
    })
}

/// Returns the coding that a response declared under `name`, if it is not in
/// `undone`.
///
/// `undone` holds the codings that leave the body as the remote wrote it. A
/// value is a comma-separated list of codings, and several headers of one name
/// state one list together. So the function reads each token of each value.
///
/// A value whose tokens are all empty names no coding. The function adds it
/// to no result, so a stray separator reaches no message. The result is the
/// text of the response, in its order, so the refusal names the value of the
/// server. A response with nothing to refuse builds no string.
fn coding(headers: &hyper::HeaderMap, name: HeaderName, undone: &[&str]) -> Option<String> {
    let declared = || {
        headers
            .get_all(&name)
            .iter()
            .map(|value| String::from_utf8_lossy(value.as_bytes()))
            .filter(|value| value.split(',').any(|token| !token.trim().is_empty()))
    };
    if !declared().any(|value| value.split(',').any(|token| names_a_coding(token, undone))) {
        return None;
    }
    Some(declared().collect::<Vec<_>>().join(", "))
}

/// Returns `true` if one token of a coding list names a coding that is not in
/// `undone`.
///
/// The comparison ignores case and the space around the token. An empty token
/// names nothing.
fn names_a_coding(token: &str, undone: &[&str]) -> bool {
    let token = token.trim();
    !token.is_empty() && !undone.iter().any(|name| token.eq_ignore_ascii_case(name))
}

/// Reads the cache validators of a response.
fn read_validators(headers: &hyper::HeaderMap) -> Validators {
    let text = |name: HeaderName| {
        headers
            .get(name)
            .and_then(|v| v.to_str().ok())
            .map(str::to_string)
    };
    Validators {
        etag: text(hyper::header::ETAG),
        last_modified: text(hyper::header::LAST_MODIFIED),
    }
}

/// Returns `true` if a response with this status sends the attempt to another
/// URL.
fn is_redirect(status: StatusCode) -> bool {
    REDIRECTS.contains(&status)
}

/// Returns the URL of a `Location` header, resolved against `from`, the URL of
/// the response that carried it.
///
/// A `Location` can be an absolute URL, a URL relative to the URL of the
/// response, or a scheme-relative URL. The resolution is the one that the URL
/// specification states. The function drops a fragment of the result. A
/// fragment names a part of a representation and reaches no request, and the
/// HTTP specification tells a redirect to drop it.
///
/// The resolution normalizes its result. A [`Target::Url`] reaches the wire as
/// the caller wrote it. The changes are:
///
/// - it resolves dot segments: `/deep/../root` becomes `/root`, and `/./p`
///   becomes `/p`
/// - it reads a backslash as a path separator: `/a\b` becomes `/a/b`
/// - it removes tabs and newlines
/// - it percent-encodes a character that a path or a query cannot carry:
///   `?q='x'` becomes `?q=%27x%27`
/// - it puts an IPv4 or an IPv6 host in canonical form: `2130706433` and
///   `0177.0.0.1` both become `127.0.0.1`.
///
/// If the signature of a signed request target covers one of those
/// characters, the target reaches the server in a new encoding. The server
/// reads it as a different target, and answers 403.
///
/// `None` states that the header holds no URL for a request. This covers a
/// value that is not text, an empty value, and a value that the resolution
/// cannot read. A value that resolves to the URL of the response itself, a
/// fragment alone such as `#frag`, is a hop of its own. It spends one of the
/// hops of the attempt.
fn resolve_location(from: &str, location: &HeaderValue) -> Option<String> {
    let location = location.to_str().ok()?;
    // An empty value resolves to the URL of the response that carried it. The
    // attempt then follows the hop that it just made, so the code returns
    // `None` for it.
    if location.is_empty() {
        return None;
    }
    let mut resolved = url::Url::parse(from).ok()?.join(location).ok()?;
    resolved.set_fragment(None);
    Some(resolved.into())
}

/// Reads the URL that a redirect named into the destination of the next hop.
///
/// The function refuses two hops. Both refusals name the URL that redirected
/// and the URL that it named:
///
/// - a scheme other than `http` or `https`, under which the fetcher serves
///   nothing
/// - a hop from `https` to `http`, which puts a request that the caller made
///   over TLS on the wire in the clear.
///
/// The function follows a hop from `http` to `https`.
///
/// The input is the resolved URL, normalized by [`resolve_location`]. The
/// parse reads it as it reads a [`Target::Url`]. So that parse refuses
/// userinfo and a port that the URL parser cannot read, with its own message.
/// A userinfo holds a password, so the two refusals of this function name the
/// URL with the userinfo left out.
fn redirect_destination(from: &Destination, to: &str) -> Result<Destination> {
    let refused = |what: &str| {
        Error::Fetch(format!(
            "{} redirects to {}, {what}",
            from.url(),
            without_userinfo(to)
        ))
    };
    let tls = match to.split_once("://").map(|(scheme, _)| scheme) {
        Some(scheme) if scheme.eq_ignore_ascii_case(Scheme::HTTPS.as_str()) => true,
        Some(scheme) if scheme.eq_ignore_ascii_case(Scheme::HTTP.as_str()) => false,
        _ => return Err(refused("which is not an absolute http or https url")),
    };
    if from.origin.tls && !tls {
        return Err(refused(
            "which is cleartext: a request made over tls is not followed onto http",
        ));
    }
    parse_url(to)
}

/// Returns the error of a fetch of a TLS origin on a fetcher that holds no
/// trust anchors.
///
/// The route or a redirect hop can name the origin.
fn no_trust_anchors(origin: &str) -> Error {
    Error::Fetch(format!(
        "the certificate of the tls origin {origin} has nothing to verify against: the \
         fetcher holds no trust anchors, so set TlsOptions::roots"
    ))
}

/// Classifies an unsuccessful status as retryable or definitive.
fn classify(status: StatusCode, url: &str) -> Failure {
    let error = Error::HttpStatus {
        status: status.as_u16(),
        url: url.to_string(),
        response: None,
    };
    if status.is_server_error()
        || status == StatusCode::REQUEST_TIMEOUT
        || status == StatusCode::TOO_MANY_REQUESTS
    {
        Failure::Retry(error)
    } else {
        Failure::Fatal(error)
    }
}

/// Returns the error of a transport failure against one URL.
fn transport(url: &str, error: hyper::Error) -> Error {
    Error::Fetch(format!("{url}: {error}"))
}

/// Returns the error of a response that did not deliver its head within the
/// progress window.
fn stalled(url: &str, limit: Duration) -> Error {
    Error::Fetch(format!("{url}: no response after {limit:?}"))
}

/// Returns the error of a connect that did not complete within `limit`.
///
/// The window covers the connect to the endpoint of the hop. So the error of
/// a proxied hop names the proxy. The fetcher reaches the origin over that
/// connection, and nothing contacts the origin before the connection is open.
/// Under `socks5://`, the window also covers the resolve of the origin host
/// name, and the error still names the proxy.
fn connect_timed_out(origin: &Origin, via: Via<'_>, limit: Duration) -> Error {
    Error::Fetch(match via {
        Via::Direct => format!(
            "connect to {}:{} timed out after {limit:?}",
            origin.host, origin.port
        ),
        Via::Absolute(proxy) | Via::Tunnel(proxy) | Via::Socks(proxy, _) => format!(
            "connect to the proxy {} timed out after {limit:?}",
            proxy.named
        ),
    })
}

/// Returns the error of an upload whose body failed before the hand-over. The
/// request was never sent.
fn not_sent(url: &str, reason: String) -> Error {
    Error::Fetch(format!("upload to {url} not sent: {reason}"))
}

/// Returns the failure of an upload that failed after hyper took its request.
fn interrupted(url: &str, message: String) -> UploadFailure {
    UploadFailure::Sent {
        url: url.to_string(),
        message,
    }
}

/// Returns the failure of an upload that does not deliver its response, for
/// `cause`.
///
/// hyper took the request, so the upload is interrupted. The message names
/// the cause.
fn undelivered(url: &str, cause: Error) -> UploadFailure {
    interrupted(url, cause.to_string())
}

/// Appends `data` to `bytes` if the result holds at most `cap` bytes, and
/// returns `false` otherwise.
///
/// The capacity of `bytes` doubles as it grows, and never passes `cap`.
fn extend_capped(bytes: &mut Vec<u8>, data: &[u8], cap: u64) -> bool {
    let needed = bytes.len().saturating_add(data.len());
    if needed as u64 > cap {
        return false;
    }
    if needed > bytes.capacity() {
        let doubled = bytes.capacity().saturating_mul(2).max(needed);
        let grown = usize::try_from(cap).map_or(doubled, |cap| doubled.min(cap));
        bytes.reserve_exact(grown - bytes.len());
    }
    bytes.extend_from_slice(data);
    true
}

/// Returns the message of an error and of its cause.
///
/// The message of a hyper error is generic, and its cause names what the
/// connection did.
fn with_cause(error: &dyn std::error::Error) -> String {
    match error.source() {
        Some(cause) => format!("{error}: {cause}"),
        None => error.to_string(),
    }
}

/// Returns the error of a response that did not deliver its head within the
/// time of the low-speed rule.
fn too_slow(url: &str, rule: LowSpeed) -> Error {
    let LowSpeed { limit, time } = rule;
    Error::Fetch(format!(
        "{url}: transfer below {limit} bytes per second for {time:?}"
    ))
}

/// Runs `future` under a deadline, and returns `None` if `limit` expires
/// first.
///
/// The function drops the future on expiry, which cancels its work.
async fn within<F: Future>(limit: Duration, future: F) -> Option<F::Output> {
    // The future is pinned before the race. So the block that awaits it holds
    // a pointer, and no second copy of it. A caller that fetches many objects
    // holds one such future for each object in flight, and its size is a cost
    // on the stack.
    let mut future = core::pin::pin!(future);
    futures_lite::future::or(async { Some(future.as_mut().await) }, async {
        rt::Timer::after(limit).await;
        None
    })
    .await
}

/// Runs `future` under a deadline of `limit` that applies while `handed` is
/// `false`, and returns `None` if the deadline expires first.
///
/// The deadline runs from the call. An upload sets `handed` when hyper takes
/// the request. The request then has deadlines of its own, and the function
/// drops the timer. A request that comes back unsent clears `handed`, and the
/// function arms the timer again for the rest of the deadline.
async fn before_hand_over<F: Future>(
    limit: Duration,
    handed: &AtomicBool,
    future: F,
) -> Option<F::Output> {
    let mut future = core::pin::pin!(future);
    let end = Instant::now().checked_add(limit);
    let mut deadline: Option<rt::Deadline> = None;
    std::future::poll_fn(|cx| {
        if let Poll::Ready(output) = future.as_mut().poll(cx) {
            return Poll::Ready(Some(output));
        }
        if handed.load(Ordering::Relaxed) {
            deadline = None;
            return Poll::Pending;
        }
        // An end past the range of the clock is never reached. A timer of the
        // whole limit stands for it.
        let deadline = deadline.get_or_insert_with(|| {
            rt::Deadline::new(
                end.map_or(limit, |end| end.saturating_duration_since(Instant::now())),
            )
        });
        deadline.poll_expired(cx).map(|()| None)
    })
    .await
}

/// Waits for the answer to a request that hyper took.
///
/// While a streamed body streams, nothing bounds the wait here, because the
/// stall window of the writer bounds the body. A body given whole has no
/// writer, so its stall window runs here. A frame that hyper does not take
/// within the window fails the body and ends the wait.
///
/// A body that fails ends the wait at once, also if hyper does not poll it
/// again. The window for the response head starts when hyper takes the end of
/// the body.
async fn answer<F: Future>(send: F, exchange: &Exchange, window: Duration) -> Answer<F::Output> {
    let mut send = core::pin::pin!(send);
    let mut head: Option<rt::Deadline> = None;
    let mut stall: Option<rt::Deadline> = None;
    std::future::poll_fn(|cx| {
        if let Poll::Ready(output) = send.as_mut().poll(cx) {
            return Poll::Ready(Answer::Sent(output));
        }
        while head.is_none() {
            match exchange.watch(cx) {
                Watch::Streaming(None) => return Poll::Pending,
                Watch::Streaming(Some(end)) => {
                    if poll_until(&mut stall, cx, end).is_pending() {
                        return Poll::Pending;
                    }
                    if let Ok(reason) = exchange.check_stall() {
                        return Poll::Ready(Answer::Aborted(reason));
                    }
                }
                Watch::Aborted(reason) => return Poll::Ready(Answer::Aborted(reason)),
                Watch::Ended => {
                    stall = None;
                    head = Some(rt::Deadline::new(window));
                }
            }
        }
        head.as_mut()
            .expect("the head window runs once the body has ended")
            .poll_expired(cx)
            .map(|()| Answer::Silent(window))
    })
    .await
}

/// Returns the delay before retry round `round`: 250 ms, doubled for each
/// round, up to 2 seconds.
fn backoff(round: u32) -> Duration {
    let ms = 250u64 << (round - 1).min(3);
    Duration::from_millis(ms)
}

// The fetcher, its bodies, and the parts of an upload move freely across tasks
// and threads.
const _: fn() = || {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<Fetcher>();
    assert_send_sync::<Body>();
    assert_send_sync::<Fetched>();
    assert_send_sync::<BearerToken>();
    assert_send_sync::<UploadBody>();
    assert_send_sync::<UploadWriter>();
    assert_send_sync::<UploadRequest<'static>>();
    assert_send_sync::<Uploaded>();
    assert_send_sync::<ErrorResponse>();
};

#[cfg(test)]
mod tests {
    use super::*;

    /// The connection window holds one stream window for each request that
    /// the fetcher admits, and stops at the ceiling of the protocol.
    #[test]
    fn the_connection_window_covers_every_admitted_stream() {
        assert_eq!(h2_connection_window(1), H2_STREAM_WINDOW);
        assert_eq!(h2_connection_window(8), 8 * H2_STREAM_WINDOW);
        assert_eq!(h2_connection_window(usize::MAX), H2_MAX_WINDOW);
    }

    #[test]
    fn mirror_urls_join_base_and_path() {
        let mirror = parse_mirror("https://example.com/repo/").unwrap();
        assert_eq!(
            mirror.destination("objects/ab/cd.filez", None).url(),
            "https://example.com/repo/objects/ab/cd.filez"
        );
        assert_eq!(mirror.origin.port, 443);
        assert!(mirror.origin.tls);

        let root = parse_mirror("http://example.com").unwrap();
        assert_eq!(
            root.destination("summary", None).url(),
            "http://example.com/summary"
        );
        assert_eq!(root.origin.port, 80);
        assert!(!root.origin.tls);

        // A non-default port stays in the URL and in the host header. A leading
        // slash on the path is not doubled.
        let ported = parse_mirror("http://127.0.0.1:8080/r").unwrap();
        let config = ported.destination("/config", None);
        assert_eq!(config.url(), "http://127.0.0.1:8080/r/config");
        assert_eq!(config.target(), "/r/config");
        assert_eq!(ported.authority, "127.0.0.1:8080");
        assert_eq!(root.authority, "example.com");
    }

    /// A query string goes after the request path, as written, in the URL
    /// and in the origin-form target.
    #[test]
    fn mirror_urls_join_base_path_and_query() {
        let mirror = parse_mirror("http://127.0.0.1:8080/base/").unwrap();
        let destination = mirror.destination("_ostrya/refs/v1", Some("pattern=a%2Fb&pattern=c"));
        assert_eq!(
            destination.url(),
            "http://127.0.0.1:8080/base/_ostrya/refs/v1?pattern=a%2Fb&pattern=c"
        );
        assert_eq!(
            destination.target(),
            "/base/_ostrya/refs/v1?pattern=a%2Fb&pattern=c"
        );
        // An empty query keeps its `?`.
        let root = parse_mirror("https://example.com").unwrap();
        assert_eq!(
            root.destination("/p", Some("")).url(),
            "https://example.com/p?"
        );
    }

    /// An IPv6 literal has brackets in the authority, and has none where the
    /// address itself is used. The brackets reach neither the connect nor the
    /// TLS server name, and both fail on them.
    #[test]
    fn an_ipv6_literal_keeps_its_brackets_only_in_the_authority() {
        let ported = parse_mirror("http://[::1]:8080/r").unwrap();
        assert_eq!(ported.origin.host, "::1");
        assert_eq!(ported.origin.port, 8080);
        assert_eq!(ported.authority, "[::1]:8080");
        assert_eq!(
            ported.destination("summary", None).url(),
            "http://[::1]:8080/r/summary"
        );

        let tls = parse_mirror("https://[2001:db8::1]/repo").unwrap();
        assert_eq!(tls.origin.host, "2001:db8::1");
        assert_eq!(tls.origin.port, 443);
        assert_eq!(tls.authority, "[2001:db8::1]");
        assert_eq!(
            tls.destination("summary", None).url(),
            "https://[2001:db8::1]/repo/summary"
        );
        // The server name the TLS handshake is opened with comes from the same
        // field, and rejects the bracketed form.
        rustls::pki_types::ServerName::try_from(tls.origin.host.clone()).unwrap();
    }

    #[test]
    fn mirror_urls_are_validated() {
        let err = parse_mirror("file:///srv/repo").unwrap_err();
        assert!(matches!(err, Error::Unsupported(_)), "{err}");
        let err = parse_mirror("/srv/repo").unwrap_err();
        assert!(err.to_string().contains("not an absolute"), "{err}");
        let err = parse_mirror("http://").unwrap_err();
        assert!(err.to_string().contains("invalid url"), "{err}");

        // Neither a query nor userinfo reaches the wire. So the parse refuses
        // a URL with one, and does not serve it with the part missing.
        let err = parse_mirror("https://host/repo?X-Amz-Signature=deadbeef").unwrap_err();
        assert!(err.to_string().contains("query string"), "{err}");
        let err = parse_mirror("https://user:pass@host/repo").unwrap_err();
        assert!(err.to_string().contains("userinfo"), "{err}");
        // A bare user with no password is userinfo too.
        let err = parse_mirror("http://user@host/repo").unwrap_err();
        assert!(err.to_string().contains("userinfo"), "{err}");
    }

    /// A base URL check takes each URL a mirror takes, and refuses a
    /// fragment and an empty host as well. A refusal holds no password.
    #[test]
    fn a_base_url_check_refuses_what_a_mirror_cannot_carry() {
        for url in [
            "http://host",
            "https://host/repo/",
            "http://127.0.0.1:8080/r",
            "https://[::1]:8443/",
            "HTTPS://host/repo",
        ] {
            check_base_url(url).unwrap();
        }
        let refused = |url: &str, part: &str| {
            let err = check_base_url(url).unwrap_err();
            let text = err.to_string();
            assert!(text.contains(part), "{url}: {text}");
            assert!(!text.contains("secret"), "{url}: {text}");
        };
        refused("https://user:secret@host/repo", "userinfo");
        refused("https://host/repo?q=1", "query string");
        refused("https://host/repo#part", "fragment");
        refused("https://u:secret@host/repo#part", "fragment");
        refused("http://", "invalid url");
        refused("http:///repo", "invalid url");
        refused("http://:80/repo", "has no host");
        refused("http://host:99999/repo", "not a number");
        refused("http://host:x/repo", "not a number");
        refused("ftp://host/repo", "scheme ftp");
        refused("host/repo", "not an absolute");
        // A password that holds a `/` or a `?` ends the authority before the
        // `@`. So the rest of the userinfo reads as the path or the query.
        refused("https://user:443/pw-secret@host/repo", "userinfo");
        refused("https://user:1234?pw-secret@host/", "userinfo");
        refused("https://host/repo@v1", "userinfo");
        // A port is ASCII digits alone. Leading zeros are digits.
        refused("http://h:+80/", "not a number");
        refused("http://h:-80/", "not a number");
        check_base_url("http://h:080/").unwrap();
        assert_eq!(parse_mirror("http://h:080/r").unwrap().prefix, "http://h");
        assert_eq!(
            parse_mirror("http://h:0808/r").unwrap().prefix,
            "http://h:808"
        );
    }

    /// No refusal of a mirror URL names the userinfo that a password holding
    /// a `?` hides in the query.
    #[test]
    fn a_mirror_refusal_names_no_userinfo() {
        let err = parse_mirror("https://user:1234?pw-secret@host/").unwrap_err();
        let text = err.to_string();
        assert!(text.contains("query string"), "{text}");
        assert!(!text.contains("secret"), "{text}");
        let err = parse_mirror("http://h:+80/x").unwrap_err();
        assert!(err.to_string().contains("not a number"), "{err}");
    }

    #[test]
    fn options_are_validated_at_construction() {
        rt::block_on(async {
            let mut options = direct_options("http://example.com");
            options.headers = vec![("not a header".into(), "v".into())];
            let err = Fetcher::new(options).await.unwrap_err();
            assert!(err.to_string().contains("invalid header name"), "{err}");

            // The host header comes from the url of the request, so a host
            // header from the caller collides with it.
            let mut options = direct_options("http://example.com");
            options.headers = vec![("host".into(), "elsewhere".into())];
            let err = Fetcher::new(options).await.unwrap_err();
            assert!(err.to_string().contains("host header"), "{err}");

            // Credentials and an authorization header both set the same
            // header, and which of the two to send is not stated.
            let mut options = tls_options("https://example.com");
            options.headers = vec![("authorization".into(), "Basic aaa".into())];
            options.basic_auth = Some(BasicAuth {
                user: "u".into(),
                password: "p".into(),
            });
            let err = Fetcher::new(options).await.unwrap_err();
            assert!(err.to_string().contains("pass one of them"), "{err}");

            // A low-speed rule with a zero measures nothing. The constructor
            // refuses it, and does not read it as no rule.
            for low_speed in [
                LowSpeed {
                    limit: 0,
                    time: Duration::from_secs(30),
                },
                LowSpeed {
                    limit: 1000,
                    time: Duration::ZERO,
                },
            ] {
                let mut options = direct_options("http://example.com");
                options.low_speed = Some(low_speed);
                let err = Fetcher::new(options).await.unwrap_err();
                assert!(err.to_string().contains("neither may be zero"), "{err}");
            }
        });
    }

    /// A low-speed time is rounded up to whole seconds, and saturates at the
    /// maximum with no wrap.
    #[test]
    fn a_low_speed_time_rounds_up_to_whole_seconds() {
        let rule = |time| LowSpeed { limit: 1000, time };
        let secs = Duration::from_secs;
        assert_eq!(rule(secs(30)).whole_seconds(), secs(30));
        assert_eq!(rule(Duration::from_millis(200)).whole_seconds(), secs(1));
        assert_eq!(rule(Duration::from_millis(1500)).whole_seconds(), secs(2));
        assert_eq!(rule(Duration::MAX).whole_seconds(), secs(u64::MAX));
    }

    /// The second at which the low-speed rule fails a body that delivers
    /// `bursts`. Each burst is a byte count at a whole second after the first
    /// read. `None` states that the rule does not fail the body within
    /// `seconds`.
    fn low_speed_failure(time: u64, bursts: &[(u64, u64)], seconds: u64) -> Option<u64> {
        let mut monitor = Monitor::new(LowSpeed {
            limit: 1000,
            time: Duration::from_secs(time),
        });
        (1..=seconds).find(|&second| {
            let arrived: u64 = bursts
                .iter()
                .filter(|(at, _)| *at == second - 1)
                .map(|(_, bytes)| bytes)
                .sum();
            monitor.counted += arrived;
            monitor.sample()
        })
    }

    /// The low-speed rule fails a body at the second at which the `ostree`
    /// command fails the same transfer. The limit is 1000 bytes per second,
    /// and the time is 2 seconds.
    #[test]
    fn the_low_speed_rule_fails_where_the_tool_fails() {
        rt::block_on(async {
            // Nothing arrives: the rate is below the limit from the start.
            assert_eq!(low_speed_failure(2, &[], 20), Some(2));
            assert_eq!(low_speed_failure(3, &[], 20), Some(3));
            // One burst and then nothing. The rate falls below the limit when
            // the burst ages out of the five-second span. It also falls below
            // when the burst over the seconds so far is below the limit. A
            // sample of exactly the limit counts as below it.
            assert_eq!(low_speed_failure(2, &[(0, 1_000)], 20), Some(2));
            assert_eq!(low_speed_failure(2, &[(0, 3_000)], 20), Some(5));
            assert_eq!(low_speed_failure(2, &[(0, 5_000)], 20), Some(7));
            assert_eq!(low_speed_failure(2, &[(0, 20_000)], 20), Some(8));
            assert_eq!(low_speed_failure(2, &[(0, 8_000)], 20), Some(8));
            assert_eq!(low_speed_failure(2, &[(0, 3_500)], 20), Some(6));
            // A second burst that arrives before the rate stays below the
            // limit for 2 seconds starts the count again. The body ends with
            // the second burst.
            let second = |at| [(0, 6_000), (at, 6_000)];
            assert_eq!(low_speed_failure(2, &second(6), 7), None);
            assert_eq!(low_speed_failure(2, &second(7), 8), None);
            assert_eq!(low_speed_failure(2, &second(8), 9), Some(8));
            // Bursts whose average is above the limit pass, though some whole
            // seconds carry nothing.
            let bursts: Vec<(u64, u64)> = (0..20).step_by(2).map(|at| (at, 2_100)).collect();
            assert_eq!(low_speed_failure(1, &bursts, 20), None);
        });
    }

    /// A fetcher with no mirror serves the URLs its requests name. A path
    /// target has nowhere to go there, and is refused before admission.
    #[test]
    fn a_path_target_needs_a_mirror() {
        rt::block_on(async {
            let fetcher = Fetcher::new(mirrorless_options()).await.unwrap();
            let err = fetcher.route(Target::Path("summary")).unwrap_err();
            assert!(err.to_string().contains("no mirror is configured"), "{err}");
            let route = fetcher
                .route(Target::Url("https://example.com/summary"))
                .unwrap();
            let Route::One(destination) = route else {
                panic!("a url target names one destination");
            };
            assert_eq!(destination.url(), "https://example.com/summary");
        });
    }

    /// A request path is added to the base path as written. So a `?` or a `#`
    /// in it is a delimiter, and the target is not the target that the caller
    /// asked for.
    #[test]
    fn a_query_or_a_fragment_in_a_path_is_rejected() {
        for path in ["refs/heads/a?b=c", "refs/heads/a#frag"] {
            let Err(err) = check_path(path) else {
                panic!("{path} was accepted as a request path");
            };
            assert!(
                err.to_string().contains("no query and no fragment"),
                "{path}: {err}"
            );
        }
        check_path("refs/heads/a").unwrap();
    }

    /// A path with a query goes to the mirrors. The path has the rules of a
    /// path target, and a `#` in the query is refused before admission.
    #[test]
    fn a_path_query_target_is_checked_and_goes_to_the_mirrors() {
        rt::block_on(async {
            let fetcher = Fetcher::new(direct_options("http://example.com/r"))
                .await
                .unwrap();
            let route = fetcher
                .route(Target::PathQuery {
                    path: "_ostrya/refs/v1",
                    query: "pattern=a",
                })
                .unwrap();
            let Route::Mirrors { path, query } = route else {
                panic!("a path query target goes to the mirrors");
            };
            assert_eq!((&*path, query), ("_ostrya/refs/v1", Some("pattern=a")));

            let refused = [
                ("p?x", "q", "no query and no fragment"),
                ("p#x", "q", "no query and no fragment"),
                ("p", "q#x", "a query holds no fragment"),
            ];
            for (path, query, message) in refused {
                let err = fetcher
                    .route(Target::PathQuery { path, query })
                    .unwrap_err();
                assert!(matches!(err, Error::Fetch(_)), "{err:?}");
                assert!(err.to_string().contains(message), "{path}?{query}: {err}");
            }

            let fetcher = Fetcher::new(mirrorless_options()).await.unwrap();
            let err = fetcher
                .route(Target::PathQuery {
                    path: "p",
                    query: "q",
                })
                .unwrap_err();
            assert!(
                err.to_string().contains("path p?q has nowhere to go"),
                "{err}"
            );
        });
    }

    /// The request path is added to the base path of the mirror, and the
    /// target carries the origin form for HTTP/1.1.
    #[test]
    fn a_request_target_appends_the_path_to_the_base() {
        rt::block_on(async {
            let fetcher = Fetcher::new(direct_options("http://example.com/r"))
                .await
                .unwrap();
            let mirror = &fetcher.inner.mirrors[0];
            let destination = mirror.destination("refs/heads/a", None);
            let request = fetcher
                .build_request(
                    &destination,
                    &FetchRequest::path("refs/heads/a"),
                    &fetcher.inner.headers,
                    Protocol::Http11,
                    Via::Direct,
                )
                .unwrap();
            assert_eq!(request.uri(), "/r/refs/heads/a");
        });
    }

    /// A `Location` is resolved against the URL of the response that carried
    /// it. So it reads as an absolute URL, a relative URL, or a
    /// scheme-relative URL. A fragment names a part of a representation and
    /// reaches no request, so a redirect drops it.
    #[test]
    fn a_location_resolves_against_the_response_that_carried_it() {
        let resolved = |value: &str| {
            resolve_location(
                "https://example.com/repo/objects/summary?x=1",
                &HeaderValue::try_from(value).unwrap(),
            )
        };
        for (value, hop) in [
            // Absolute, at another origin and at another port.
            ("http://other.example/p", "http://other.example/p"),
            (
                "https://other.example:8443/p?q=2",
                "https://other.example:8443/p?q=2",
            ),
            // Relative: rooted at the origin, and a sibling of the path the
            // response was answered at.
            ("/other/path", "https://example.com/other/path"),
            ("sibling", "https://example.com/repo/objects/sibling"),
            // Scheme-relative, which takes the scheme of the response that
            // redirected.
            ("//host.example/path", "https://host.example/path"),
            // A fragment is dropped, and the resolution does not refuse it.
            ("/other/path#frag", "https://example.com/other/path"),
            ("http://other.example/p#frag", "http://other.example/p"),
            // A scheme under which the fetcher serves nothing resolves here.
            // The hop check refuses it.
            ("file:///srv/repo", "file:///srv/repo"),
        ] {
            assert_eq!(resolved(value).as_deref(), Some(hop), "{value}");
        }

        // A value that the resolution cannot read holds no URL for a request.
        // An empty value holds none either, because it resolves to the URL of
        // the response itself.
        assert_eq!(resolved("http://"), None);
        assert_eq!(resolved(""), None);

        // A fragment alone resolves to the URL of the response itself, which is
        // a hop of its own.
        assert_eq!(
            resolved("#frag").as_deref(),
            Some("https://example.com/repo/objects/summary?x=1")
        );

        // The resolution normalizes its result. A dot segment is resolved
        // away, a backslash reads as a path separator, and a tab is removed. A
        // character that a query cannot carry is percent-encoded, and an IPv4
        // host is canonicalized.
        for (value, hop) in [
            ("/deep/../root", "https://example.com/root"),
            ("/./p", "https://example.com/p"),
            ("/a\\b", "https://example.com/a/b"),
            ("/pa\tth", "https://example.com/path"),
            ("/p?q='x'", "https://example.com/p?q=%27x%27"),
            ("http://2130706433/p", "http://127.0.0.1/p"),
        ] {
            assert_eq!(resolved(value).as_deref(), Some(hop), "{value}");
        }
    }

    /// A hop is read as a URL target is. Two hops are refused with both URLs
    /// named: a scheme under which the fetcher serves nothing, and a hop from
    /// `https` to `http`. A hop from `http` to `https` is followed.
    #[test]
    fn a_redirect_hop_is_validated_against_the_url_that_redirected() {
        let secure = parse_url("https://example.com/repo/summary").unwrap();
        let cleartext = parse_url("http://example.com/repo/summary").unwrap();

        // A hop at another origin, and one at the origin that redirected.
        let hop = redirect_destination(&secure, "https://other.example/p?q=1").unwrap();
        assert_eq!(hop.url(), "https://other.example/p?q=1");
        assert_eq!(hop.origin.host, "other.example");
        let hop = redirect_destination(&secure, "https://example.com/elsewhere").unwrap();
        assert_eq!(hop.origin, secure.origin);
        // A hop from cleartext to tls is followed, and so is one that stays
        // cleartext.
        let hop = redirect_destination(&cleartext, "https://example.com/elsewhere").unwrap();
        assert!(hop.origin.tls);
        redirect_destination(&cleartext, "http://other.example/p").unwrap();

        // A hop from tls to cleartext is refused, and both URLs are named.
        let err = redirect_destination(&secure, "http://other.example/p").unwrap_err();
        let message = err.to_string();
        assert!(
            message.contains("https://example.com/repo/summary"),
            "{message}"
        );
        assert!(message.contains("http://other.example/p"), "{message}");
        assert!(message.contains("cleartext"), "{message}");

        // A scheme under which the fetcher serves nothing, named in the same
        // way.
        let err = redirect_destination(&secure, "file:///srv/repo").unwrap_err();
        let message = err.to_string();
        assert!(
            message.contains("https://example.com/repo/summary"),
            "{message}"
        );
        assert!(message.contains("file:///srv/repo"), "{message}");
        assert!(
            message.contains("not an absolute http or https url"),
            "{message}"
        );

        // Userinfo is refused, and the password reaches no message.
        let err =
            redirect_destination(&secure, "https://alice:sup3rs3cret@other.example/p").unwrap_err();
        let message = err.to_string();
        assert!(message.contains("userinfo"), "{message}");
        assert!(!message.contains("sup3rs3cret"), "{message}");
    }

    /// The statuses that send a fetch to another URL. A fetch is a GET, so the
    /// statuses that differ on the method are one case.
    #[test]
    fn the_followed_statuses_are_the_five_redirects() {
        for status in [301u16, 302, 303, 307, 308] {
            assert!(
                is_redirect(StatusCode::from_u16(status).unwrap()),
                "{status}"
            );
        }
        for status in [200u16, 300, 304, 305, 306, 400, 404, 500] {
            assert!(
                !is_redirect(StatusCode::from_u16(status).unwrap()),
                "{status}"
            );
        }
    }

    #[test]
    fn retryable_statuses_are_classified() {
        for status in [500u16, 502, 503, 408, 429] {
            let failure = classify(StatusCode::from_u16(status).unwrap(), "http://h/p");
            assert!(matches!(failure, Failure::Retry(_)), "{status}");
        }
        for status in [400u16, 401, 403, 404, 410] {
            let failure = classify(StatusCode::from_u16(status).unwrap(), "http://h/p");
            assert!(matches!(failure, Failure::Fatal(_)), "{status}");
        }
    }

    #[test]
    fn backoff_doubles_and_stops_at_two_seconds() {
        assert_eq!(backoff(1), Duration::from_millis(250));
        assert_eq!(backoff(2), Duration::from_millis(500));
        assert_eq!(backoff(3), Duration::from_secs(1));
        assert_eq!(backoff(4), Duration::from_secs(2));
        assert_eq!(backoff(9), Duration::from_secs(2));
    }

    #[test]
    fn basic_auth_and_extra_headers_reach_the_header_list() {
        let mut options = tls_options("https://example.com");
        options.basic_auth = Some(BasicAuth {
            user: "u".into(),
            password: "p".into(),
        });
        options.headers = vec![("x-trace".into(), "abc".into())];
        let fetcher = rt::block_on(Fetcher::new(options)).unwrap();
        let headers = &fetcher.inner.headers;
        let value = |name: HeaderName| {
            headers
                .iter()
                .find(|(n, _)| *n == name)
                .map(|(_, v)| v.to_str().unwrap().to_string())
        };
        assert_eq!(
            value(hyper::header::USER_AGENT).as_deref(),
            Some(USER_AGENT)
        );
        assert_eq!(
            value(HeaderName::from_static("x-trace")).as_deref(),
            Some("abc")
        );
        // base64("u:p")
        assert_eq!(
            value(hyper::header::AUTHORIZATION).as_deref(),
            Some("Basic dTpw")
        );
    }

    /// Options for a fetcher with no mirror, which serves the URLs its
    /// requests name. The anchors come from the fixture authority, so the
    /// constructor reads no host trust store, which an empty mirror list
    /// otherwise demands.
    fn mirrorless_options() -> FetcherOptions {
        FetcherOptions {
            mirrors: Vec::new(),
            ..tls_options("unused")
        }
    }

    /// Options for a fetcher at `url` that reaches every origin directly.
    ///
    /// A test that is not about the proxy states this. So the proxy variables
    /// of the host that runs the suite have no effect. The default form reads
    /// them, and a fetcher built under one goes to a proxy that no test
    /// started.
    fn direct_options(url: impl Into<String>) -> FetcherOptions {
        FetcherOptions {
            proxy: Proxy::None,
            ..FetcherOptions::new(url)
        }
    }

    /// Options for an `https` mirror whose anchors come from the fixture
    /// authority, so the constructor reads no host trust store.
    fn tls_options(url: &str) -> FetcherOptions {
        FetcherOptions {
            tls: TlsOptions {
                roots: TrustRoots::Pem(
                    include_bytes!("../../../tests/fixtures/tls/ca.pem").to_vec(),
                ),
                ..TlsOptions::default()
            },
            ..direct_options(url)
        }
    }

    /// A credential reaches every mirror, so one cleartext mirror is enough to
    /// refuse the configuration. Credentials named in `headers` are refused in
    /// the same way. Any other header is sent, whatever the scheme.
    #[test]
    fn credentials_alongside_a_cleartext_mirror_are_refused() {
        let auth = || {
            Some(BasicAuth {
                user: "u".into(),
                password: "p".into(),
            })
        };

        // The message names the cleartext mirror, also if it is one entry among
        // https entries.
        for mirrors in [
            vec!["http://cleartext.example/repo".to_owned()],
            vec![
                "https://secure.example/repo".to_owned(),
                "http://cleartext.example/repo".to_owned(),
            ],
        ] {
            let options = FetcherOptions {
                mirrors,
                basic_auth: auth(),
                ..tls_options("unused")
            };
            let err = rt::block_on(Fetcher::new(options)).unwrap_err();
            let message = err.to_string();
            assert!(message.contains("basic-auth credentials"), "{message}");
            assert!(
                message.contains("http://cleartext.example/repo"),
                "{message}"
            );
        }

        // Each header name that carries a credential, refused in the same way.
        for name in ["authorization", "proxy-authorization", "cookie"] {
            let options = FetcherOptions {
                headers: vec![(name.to_owned(), "secret".to_owned())],
                ..direct_options("http://cleartext.example/repo")
            };
            let err = rt::block_on(Fetcher::new(options)).unwrap_err();
            let message = err.to_string();
            assert!(message.contains(name), "{message}");
            assert!(message.contains("carries credentials"), "{message}");
        }

        // A header that is not a credential reaches a cleartext mirror.
        let options = FetcherOptions {
            headers: vec![("x-trace".to_owned(), "abc".to_owned())],
            ..direct_options("http://cleartext.example/repo")
        };
        rt::block_on(Fetcher::new(options)).unwrap();

        // With every mirror on https, both kinds are accepted.
        let options = FetcherOptions {
            mirrors: vec![
                "https://one.example/repo".to_owned(),
                "https://two.example/repo".to_owned(),
            ],
            basic_auth: auth(),
            headers: vec![("cookie".to_owned(), "session=1".to_owned())],
            ..tls_options("unused")
        };
        rt::block_on(Fetcher::new(options)).unwrap();
    }

    /// A request URL is served as it is written. The query string is part of
    /// the name of the request, and reaches the wire. Userinfo and a fragment
    /// are never sent, so the parse refuses them. The absolute URL, the
    /// origin-form target, the authority, and the origin all come from the one
    /// string of the destination.
    #[test]
    fn url_targets_are_parsed_and_validated() {
        // Every row states the URL, then the absolute URL, the origin-form
        // target, the authority, and the origin the parse produces.
        let rows = [
            (
                "https://example.com/repo/summary?sig=a%2Fb&x=1",
                "https://example.com/repo/summary?sig=a%2Fb&x=1",
                "/repo/summary?sig=a%2Fb&x=1",
                "example.com",
                "https://example.com",
            ),
            // A URL with no path of its own asks for the root, with or without
            // the trailing slash.
            ("http://h", "http://h/", "/", "h", "http://h"),
            ("http://h/", "http://h/", "/", "h", "http://h"),
            // An empty query is part of what the request names.
            ("http://h/p?", "http://h/p?", "/p?", "h", "http://h"),
            (
                "http://h/p?a=%2F&b=1",
                "http://h/p?a=%2F&b=1",
                "/p?a=%2F&b=1",
                "h",
                "http://h",
            ),
            // The default port of the scheme is left out of the authority, also
            // if the URL wrote it. The scheme is held in lower case.
            ("https://h:443/p", "https://h/p", "/p", "h", "https://h"),
            ("HTTP://h/p", "http://h/p", "/p", "h", "http://h"),
            // An IPv6 literal keeps its brackets in the authority, and a
            // non-default port stays with it.
            (
                "http://[2001:db8::1]:8080/r/config",
                "http://[2001:db8::1]:8080/r/config",
                "/r/config",
                "[2001:db8::1]:8080",
                "http://[2001:db8::1]:8080",
            ),
        ];
        for (url, absolute, target, authority, origin) in rows {
            let dest = parse_url(url).unwrap();
            assert_eq!(dest.url(), absolute, "{url}");
            assert_eq!(dest.target(), target, "{url}");
            assert_eq!(dest.authority, authority, "{url}");
            assert_eq!(dest.origin_url(), origin, "{url}");
        }

        let dest = parse_url("https://example.com/repo/summary?sig=a%2Fb&x=1").unwrap();
        assert_eq!(dest.origin.port, 443);
        assert!(dest.origin.tls);
        let root = parse_url("http://example.com").unwrap();
        assert_eq!(root.origin.port, 80);
        assert!(!root.origin.tls);
        // The address is held without the brackets of the authority.
        let ported = parse_url("http://[::1]:8080/r/config").unwrap();
        assert_eq!(ported.origin.host, "::1");
        assert_eq!(ported.origin.port, 8080);

        // The refusal names the scheme and the host, and leaves the password
        // out of a message a caller logs.
        let err = parse_url("https://alice:sup3rs3cret@example.com/p").unwrap_err();
        let message = err.to_string();
        assert!(message.contains("userinfo"), "{message}");
        assert!(message.contains("https url for example.com"), "{message}");
        assert!(message.contains("FetchRequest::basic_auth"), "{message}");
        assert!(!message.contains("sup3rs3cret"), "{message}");

        let err = parse_url("https://example.com/p#frag").unwrap_err();
        assert!(err.to_string().contains("fragment"), "{err}");
        let err = parse_url("file:///srv/repo").unwrap_err();
        assert!(matches!(err, Error::Unsupported(_)), "{err}");
        let err = parse_url("/srv/repo").unwrap_err();
        assert!(err.to_string().contains("not an absolute"), "{err}");
    }

    /// Each message about a URL whose authority the parse did not read yet
    /// leaves out the userinfo.
    #[test]
    fn a_password_reaches_no_message() {
        for url in [
            "https://alice:sup3rs3cret@example.com/p",
            "https://alice:sup3rs3cret@example.com/p#frag",
            "http://alice:sup3rs3cret@example.com/ p",
            "http://alice:sup3rs3cret@",
            "alice:sup3rs3cret@example.com/p",
        ] {
            let err = parse_url(url).unwrap_err();
            let message = err.to_string();
            assert!(!message.contains("sup3rs3cret"), "{url}: {message}");
        }

        // A password with a `/`, a `?`, or a `#` ends the authority before the
        // `@`. The userinfo is left out also in this case.
        for password in ["pa#ss", "pa?ss", "p/ss"] {
            let url = format!("https://alice:{password}@example.com/p");
            let message = parse_url(&url).unwrap_err().to_string();
            assert!(!message.contains(password), "{password}: {message}");
        }
    }

    /// `Uri` accepts an authority whose port it cannot read, and reports no
    /// port for it. So the parse refuses a URL that names such a port, and does
    /// not serve it from the default of the scheme.
    #[test]
    fn a_port_the_url_parser_cannot_read_is_refused() {
        for (url, text) in [
            ("http://h:99999/x", "99999"),
            ("http://h:65536/x", "65536"),
            ("http://h:abc/x", "abc"),
            ("http://h:/x", ""),
            // The integer parse of `std` takes a leading `+`, and a port is
            // ASCII digits alone.
            ("http://h:+80/x", "+80"),
        ] {
            let err = parse_url(url).unwrap_err();
            let message = err.to_string();
            assert!(message.contains(url), "{url}: {message}");
            assert!(message.contains(text), "{url}: {message}");
            assert!(message.contains("not a number"), "{url}: {message}");
            // A mirror URL is read by the same parse.
            assert!(parse_mirror(url).is_err(), "{url}");
        }

        // A URL with no port at all takes the scheme default, and a port the
        // parser reads is kept.
        let default = parse_url("http://h/x").unwrap();
        assert_eq!(default.origin.port, 80);
        assert_eq!(default.url(), "http://h/x");
        let ported = parse_url("http://h:8080/x").unwrap();
        assert_eq!(ported.origin.port, 8080);
        assert_eq!(ported.url(), "http://h:8080/x");
        // Leading zeros are digits: the port is the number they give.
        let zeros = parse_url("http://h:0080/x").unwrap();
        assert_eq!(zeros.origin.port, 80);
        assert_eq!(zeros.url(), "http://h/x");
    }

    /// A host is one origin in each case of its letters, so two spellings of
    /// one origin are one pool key.
    #[test]
    fn a_host_in_another_case_is_one_origin() {
        let upper = parse_url("http://LOCALHOST:8080/x").unwrap();
        let lower = parse_url("http://localhost:8080/x").unwrap();
        assert_eq!(upper.origin, lower.origin);
        assert_eq!(upper.origin.host, "localhost");
        // The authority reaches the wire as the caller wrote it.
        assert_eq!(upper.authority, "LOCALHOST:8080");
        assert_eq!(upper.url(), "http://LOCALHOST:8080/x");
    }

    /// The rendering of a struct with credentials leaves out the password.
    #[test]
    fn basic_auth_debug_holds_no_password() {
        let auth = BasicAuth {
            user: "alice".into(),
            password: "sup3rs3cret".into(),
        };
        let rendered = format!("{auth:?}");
        assert!(rendered.contains("alice"), "{rendered}");
        assert!(!rendered.contains("sup3rs3cret"), "{rendered}");

        let request = FetchRequest {
            basic_auth: Some(&auth),
            ..FetchRequest::path("summary")
        };
        let rendered = format!("{request:?}");
        assert!(!rendered.contains("sup3rs3cret"), "{rendered}");

        let options = FetcherOptions {
            basic_auth: Some(auth),
            ..direct_options("https://example.com")
        };
        let rendered = format!("{options:?}");
        assert!(!rendered.contains("sup3rs3cret"), "{rendered}");
    }

    /// The origin of one absolute URL, which is what a proxy decision reads.
    fn origin_of(url: &str) -> Origin {
        parse_url(url).unwrap().origin
    }

    /// How a hop at `url` is reached: the proxy that it names, and the way,
    /// which is the absolute form, a tunnel, or a SOCKS5 connection.
    fn via_of(proxies: &Proxies, url: &str) -> Option<(String, &'static str)> {
        match proxies.via(&origin_of(url)) {
            Via::Direct => None,
            Via::Absolute(proxy) => Some((proxy.named.clone(), "absolute")),
            Via::Tunnel(proxy) => Some((proxy.named.clone(), "tunnel")),
            Via::Socks(proxy, _) => Some((proxy.named.clone(), "socks")),
        }
    }

    /// The variable list one test states, as the options carry it.
    fn variables(pairs: &[(&str, &str)]) -> Vec<(String, String)> {
        pairs
            .iter()
            .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
            .collect()
    }

    /// A proxy URL names an endpoint and nothing else. A path, a query, a
    /// fragment, or a scheme other than `http` states a value that a proxy
    /// connection has no place for. The constructor refuses each of them.
    #[test]
    fn a_proxy_url_names_an_http_endpoint() {
        // Every row states the URL, then the endpoint host, the port, and how a
        // diagnostic names the proxy.
        for (url, host, port, named) in [
            (
                "http://proxy.example",
                "proxy.example",
                80,
                "http://proxy.example",
            ),
            (
                "http://proxy.example:3128",
                "proxy.example",
                3128,
                "http://proxy.example:3128",
            ),
            // A path of `/` alone is the root, which is no path at all.
            (
                "http://proxy.example/",
                "proxy.example",
                80,
                "http://proxy.example",
            ),
            (
                "HTTP://Proxy.Example:3128",
                "proxy.example",
                3128,
                "http://Proxy.Example:3128",
            ),
            // An environment variable can carry space around its value. The
            // endpoint is read without it.
            (
                " http://proxy.example:3128 ",
                "proxy.example",
                3128,
                "http://proxy.example:3128",
            ),
            // An IPv6 literal keeps its brackets in the authority, and is held
            // without them. The connect resolves the bracketed form to nothing.
            ("http://[::1]:3128", "::1", 3128, "http://[::1]:3128"),
            (
                "http://alice:s3cret@proxy.example:3128",
                "proxy.example",
                3128,
                "http://proxy.example:3128",
            ),
        ] {
            let proxy = parse_proxy(url).unwrap_or_else(|e| panic!("{url}: {e}"));
            assert_eq!(proxy.endpoint.host, host, "{url}");
            assert_eq!(proxy.endpoint.port, port, "{url}");
            assert!(!proxy.endpoint.tls, "{url}");
            assert_eq!(proxy.named, named, "{url}");
        }

        // Userinfo is the credential of the proxy, after the percent-decode:
        // base64("alice:s3cret") and base64("al:ce:p@ss").
        let credential = |url: &str| {
            let ProxyKind::Http { credential } = parse_proxy(url).unwrap().kind else {
                panic!("{url} names an http proxy");
            };
            credential.map(|value| value.to_str().unwrap().to_owned())
        };
        assert_eq!(credential("http://proxy.example"), None);
        assert_eq!(
            credential("http://alice:s3cret@proxy.example").as_deref(),
            Some("Basic YWxpY2U6czNjcmV0")
        );
        assert_eq!(
            credential("http://al%3Ace:p%40ss@proxy.example").as_deref(),
            Some("Basic YWw6Y2U6cEBzcw==")
        );
        // A user with no password is userinfo too, and it is sent with the
        // empty password an `Authorization` value holds: base64("alice:").
        assert_eq!(
            credential("http://alice@proxy.example").as_deref(),
            Some("Basic YWxpY2U6")
        );

        // Every row states a URL the fetcher cannot connect through and a word
        // its refusal carries.
        for (url, says) in [
            ("socks4://proxy.example:1080", "socks5h://host[:port] alone"),
            (
                "socks4a://proxy.example:1080",
                "socks5h://host[:port] alone",
            ),
            ("https://proxy.example:3128", "socks5h://host[:port] alone"),
            ("proxy.example:3128", "socks5h://host[:port] alone"),
            ("http://proxy.example/squid", "carries no path"),
            ("http://proxy.example/?upstream=1", "carries no query"),
            ("http://proxy.example/#frag", "carries no fragment"),
            ("http://", "invalid url"),
            ("http://proxy.example:99999", "not a number"),
            ("http://proxy.example:abc", "not a number"),
            ("http://alice:p%zz@proxy.example", "percent-encoded"),
            ("http://alice:p%4@proxy.example", "percent-encoded"),
        ] {
            let err = parse_proxy(url).unwrap_err();
            assert!(matches!(err, Error::Unsupported(_)), "{url}: {err}");
            let message = err.to_string();
            assert!(message.contains(says), "{url}: {message}");
        }
    }

    /// A `socks5h` URL names a SOCKS5 endpoint, with the port 1080 by default.
    /// It takes the checks of an `http` URL, and its userinfo is the user name
    /// and the password of RFC 1929, each 1 to 255 bytes after the decode.
    #[test]
    fn a_socks5h_proxy_url_names_its_endpoint() {
        for (url, host, port, named) in [
            (
                "socks5h://proxy.example",
                "proxy.example",
                1080,
                "socks5h://proxy.example",
            ),
            (
                "socks5h://proxy.example:1080",
                "proxy.example",
                1080,
                "socks5h://proxy.example",
            ),
            (
                "SOCKS5H://Proxy.Example:9050",
                "proxy.example",
                9050,
                "socks5h://Proxy.Example:9050",
            ),
            ("socks5h://[::1]", "::1", 1080, "socks5h://[::1]"),
            (
                "socks5h://proxy.example/",
                "proxy.example",
                1080,
                "socks5h://proxy.example",
            ),
            (
                "socks5h://alice:s3cret@proxy.example",
                "proxy.example",
                1080,
                "socks5h://proxy.example",
            ),
        ] {
            let proxy = parse_proxy(url).unwrap_or_else(|e| panic!("{url}: {e}"));
            assert_eq!(proxy.endpoint.host, host, "{url}");
            assert_eq!(proxy.endpoint.port, port, "{url}");
            assert!(!proxy.endpoint.tls, "{url}");
            assert_eq!(proxy.named, named, "{url}");
            assert!(matches!(proxy.kind, ProxyKind::Socks(_)), "{url}");
        }
        let credential = |url: &str| match parse_proxy(url).unwrap().kind {
            ProxyKind::Socks(socks) => socks.credential.is_some(),
            ProxyKind::Http { .. } => panic!("{url} names a SOCKS5 proxy"),
        };
        assert!(!credential("socks5h://proxy.example"));
        assert!(credential("socks5h://alice:s3cret@proxy.example"));

        // Every row states a URL that the fetcher refuses and a word of the
        // refusal. The message names the URL without its userinfo.
        let long = "a".repeat(256);
        for (url, says) in [
            ("socks5h://proxy.example/x".to_owned(), "carries no path"),
            (
                "socks5h://proxy.example/?a=1".to_owned(),
                "carries no query",
            ),
            (
                "socks5h://proxy.example/#f".to_owned(),
                "carries no fragment",
            ),
            ("socks5h://proxy.example:99999".to_owned(), "not a number"),
            (
                "socks5h://s3cret@proxy.example".to_owned(),
                "password is empty",
            ),
            (
                "socks5h://s3cret:@proxy.example".to_owned(),
                "password is empty",
            ),
            (
                "socks5h://:s3cret@proxy.example".to_owned(),
                "user name is empty",
            ),
            ("socks5h://@proxy.example".to_owned(), "user name is empty"),
            (
                format!("socks5h://{long}:s3cret@proxy.example"),
                "user name is longer",
            ),
            (
                format!("socks5h://alice:{long}s3cret@proxy.example"),
                "password is longer",
            ),
            (
                "socks5h://alice:s3cret%zz@proxy.example".to_owned(),
                "percent-encoded",
            ),
            (
                "socks5h://alice:s3cret%ff@proxy.example".to_owned(),
                "percent-encoded",
            ),
        ] {
            let err = parse_proxy(&url).unwrap_err();
            assert!(matches!(err, Error::Unsupported(_)), "{url}: {err}");
            let message = err.to_string();
            assert!(message.contains(says), "{url}: {message}");
            assert!(
                message.contains("socks5h://proxy.example"),
                "{url}: {message}"
            );
            assert!(!message.contains("s3cret"), "{url}: {message}");
        }
    }

    /// A `socks5` URL names a SOCKS5 endpoint whose client resolves the origin
    /// host, with the port 1080 by default. A `socks5h` URL names one whose
    /// proxy resolves it. The URL rules and the userinfo rules are the rules
    /// of a `socks5h` URL.
    #[test]
    fn a_socks5_proxy_url_names_its_endpoint() {
        let resolve = |url: &str| match parse_proxy(url).unwrap().kind {
            ProxyKind::Socks(socks) => (socks.resolve, socks.credential.is_some()),
            ProxyKind::Http { .. } => panic!("{url} names a SOCKS5 proxy"),
        };
        for (url, host, port, named) in [
            (
                "socks5://proxy.example",
                "proxy.example",
                1080,
                "socks5://proxy.example",
            ),
            (
                "socks5://proxy.example:1080",
                "proxy.example",
                1080,
                "socks5://proxy.example",
            ),
            (
                "SOCKS5://Proxy.Example:9050",
                "proxy.example",
                9050,
                "socks5://Proxy.Example:9050",
            ),
            ("socks5://[::1]", "::1", 1080, "socks5://[::1]"),
            (
                "socks5://alice:s3cret@proxy.example",
                "proxy.example",
                1080,
                "socks5://proxy.example",
            ),
        ] {
            let proxy = parse_proxy(url).unwrap_or_else(|e| panic!("{url}: {e}"));
            assert_eq!(proxy.endpoint.host, host, "{url}");
            assert_eq!(proxy.endpoint.port, port, "{url}");
            assert!(!proxy.endpoint.tls, "{url}");
            assert_eq!(proxy.named, named, "{url}");
            assert_eq!(resolve(url).0, ResolveSide::Client, "{url}");
        }
        assert_eq!(
            resolve("socks5h://proxy.example"),
            (ResolveSide::Proxy, false)
        );
        assert_eq!(
            resolve("socks5://alice:s3cret@proxy.example"),
            (ResolveSide::Client, true)
        );
        assert_eq!(
            resolve("socks5://proxy.example"),
            (ResolveSide::Client, false)
        );

        let err = parse_proxy("socks5://s3cret@proxy.example").unwrap_err();
        assert!(matches!(err, Error::Unsupported(_)), "{err}");
        let message = err.to_string();
        assert!(message.contains("password is empty"), "{message}");
        assert!(message.contains("socks5://proxy.example"), "{message}");
        assert!(!message.contains("s3cret"), "{message}");
    }

    /// The marker that [`socks_peer`] writes after a reply of success. It
    /// stands for the first bytes of the origin.
    const SOCKS_MARKER: &[u8] = b"origin bytes";

    /// The answers of a [`socks_peer`].
    struct SocksScript {
        /// The method that the peer selects.
        method: u8,
        /// The status of the RFC 1929 reply, if the peer selects `02`.
        auth_status: u8,
        /// The `REP` of each connection in turn. A connection after the end of
        /// the list gets `00`.
        replies: Vec<u8>,
    }

    /// What a [`socks_peer`] saw.
    #[derive(Debug, Default)]
    struct SocksSeen {
        /// The number of connections that the peer accepted.
        connections: usize,
        /// The `ATYP`, the address bytes, and the port of each request.
        requests: Vec<(u8, Vec<u8>, u16)>,
    }

    /// Starts a scripted SOCKS5 peer on a loopback port, and gives the port
    /// and what the peer sees.
    ///
    /// The peer serves one connection at a time. It answers the greeting with
    /// the method of `script`, the RFC 1929 exchange with its status, and the
    /// request of each connection with the next `REP`. After a `REP` of `00`,
    /// it writes [`SOCKS_MARKER`].
    async fn socks_peer(script: SocksScript) -> (u16, Arc<Mutex<SocksSeen>>) {
        use futures_lite::io::{AsyncReadExt, AsyncWriteExt};

        let peer = rt::TcpListener::bind("127.0.0.1:0".parse().unwrap())
            .await
            .unwrap();
        let port = peer.local_addr().unwrap().port();
        let seen = Arc::new(Mutex::new(SocksSeen::default()));
        let record = seen.clone();
        drop(rt::spawn(async move {
            while let Ok((mut stream, _peer)) = peer.accept().await {
                let index = {
                    let mut seen = record.lock().unwrap();
                    seen.connections += 1;
                    seen.connections - 1
                };
                let reply = script.replies.get(index).copied().unwrap_or(0);
                let record = record.clone();
                let script = &script;
                let served = async move {
                    let mut head = [0u8; 2];
                    stream.read_exact(&mut head).await?;
                    let mut methods = vec![0u8; usize::from(head[1])];
                    stream.read_exact(&mut methods).await?;
                    stream.write_all(&[0x05, script.method]).await?;
                    stream.flush().await?;
                    if script.method == 0x02 {
                        let mut len = [0u8; 2];
                        stream.read_exact(&mut len).await?;
                        let mut user = vec![0u8; usize::from(len[1])];
                        stream.read_exact(&mut user).await?;
                        stream.read_exact(&mut len[..1]).await?;
                        let mut password = vec![0u8; usize::from(len[0])];
                        stream.read_exact(&mut password).await?;
                        stream.write_all(&[0x01, script.auth_status]).await?;
                        stream.flush().await?;
                        if script.auth_status != 0x00 {
                            return std::io::Result::Ok(());
                        }
                    } else if script.method != 0x00 {
                        return Ok(());
                    }
                    let mut request = [0u8; 4];
                    stream.read_exact(&mut request).await?;
                    let mut addr = match request[3] {
                        0x01 => vec![0u8; 4],
                        0x04 => vec![0u8; 16],
                        _ => {
                            let mut len = [0u8; 1];
                            stream.read_exact(&mut len).await?;
                            vec![0u8; usize::from(len[0])]
                        }
                    };
                    stream.read_exact(&mut addr).await?;
                    let mut port = [0u8; 2];
                    stream.read_exact(&mut port).await?;
                    record.lock().unwrap().requests.push((
                        request[3],
                        addr,
                        u16::from_be_bytes(port),
                    ));
                    stream
                        .write_all(&[0x05, reply, 0x00, 0x01, 0, 0, 0, 0, 0, 0])
                        .await?;
                    if reply == 0x00 {
                        stream.write_all(SOCKS_MARKER).await?;
                    }
                    stream.flush().await
                };
                let _ = served.await;
            }
        }));
        (port, seen)
    }

    /// The endpoint of a SOCKS5 proxy URL.
    fn socks_endpoint(url: &str) -> ProxyEndpoint {
        let proxy = parse_proxy(url).unwrap();
        assert!(matches!(proxy.kind, ProxyKind::Socks(_)), "{url}");
        proxy
    }

    /// The SOCKS5 settings of an endpoint that [`socks_endpoint`] gives.
    fn socks_of(proxy: &ProxyEndpoint) -> &SocksProxy {
        let ProxyKind::Socks(socks) = &proxy.kind else {
            panic!("{} names a SOCKS5 proxy", proxy.named);
        };
        socks
    }

    /// The TLS origin of the address loop tests.
    fn loop_origin() -> Origin {
        Origin {
            tls: true,
            host: "origin.example".to_owned(),
            port: 443,
        }
    }

    /// The address list of the address loop tests: an IPv4 address, then an
    /// IPv6 address.
    fn two_addresses() -> [SocketAddr; 2] {
        [
            "192.0.2.1:443".parse().unwrap(),
            "[2001:db8::1]:443".parse().unwrap(),
        ]
    }

    /// A refusal of the first address moves the loop to the second address
    /// over a new connection to the proxy. Each request names the address,
    /// and the stream of the second connection carries the bytes after the
    /// reply.
    #[test]
    fn the_address_loop_moves_past_a_refused_address() {
        use futures_lite::io::AsyncReadExt;

        rt::block_on(async {
            let (port, seen) = socks_peer(SocksScript {
                method: 0x00,
                auth_status: 0x00,
                replies: vec![0x05, 0x00],
            })
            .await;
            let proxy = socks_endpoint(&format!("socks5://127.0.0.1:{port}"));
            let addrs = two_addresses();
            let connected = within(
                Duration::from_secs(5),
                socks_each(&proxy, socks_of(&proxy), &loop_origin(), &addrs),
            )
            .await
            .expect("the loop ends within the limit");
            let Ok(mut stream) = connected else {
                panic!("the second address connects");
            };
            let mut marker = vec![0u8; SOCKS_MARKER.len()];
            stream.read_exact(&mut marker).await.unwrap();
            assert_eq!(marker, SOCKS_MARKER);

            let seen = seen.lock().unwrap();
            assert_eq!(seen.connections, 2);
            let v6 = "2001:db8::1".parse::<std::net::Ipv6Addr>().unwrap();
            assert_eq!(
                seen.requests,
                vec![
                    (0x01, vec![192, 0, 2, 1], 443),
                    (0x04, v6.octets().to_vec(), 443),
                ]
            );
        });
    }

    /// A refused user name and password is definitive, so the loop stops at
    /// the first address. The message names the proxy and the origin, and
    /// holds no part of the credential.
    #[test]
    fn an_auth_refusal_stops_the_address_loop() {
        rt::block_on(async {
            let (port, seen) = socks_peer(SocksScript {
                method: 0x02,
                auth_status: 0x01,
                replies: Vec::new(),
            })
            .await;
            let proxy = socks_endpoint(&format!("socks5://alice:s3cret@127.0.0.1:{port}"));
            let addrs = two_addresses();
            let failure = within(
                Duration::from_secs(5),
                socks_each(&proxy, socks_of(&proxy), &loop_origin(), &addrs),
            )
            .await
            .expect("the loop ends within the limit");
            let Err(Failure::Fatal(err)) = failure else {
                panic!("an auth refusal is definitive");
            };
            let message = err.to_string();
            assert!(
                message.contains(&format!("socks5://127.0.0.1:{port}")),
                "{message}"
            );
            assert!(message.contains("origin.example:443"), "{message}");
            assert!(!message.contains("s3cret"), "{message}");
            assert!(!message.contains("alice"), "{message}");
            assert_eq!(seen.lock().unwrap().connections, 1);
        });
    }

    /// With each address refused, the retryable failure names the last
    /// address, the number of addresses, and the last reason.
    #[test]
    fn every_refused_address_gives_the_last_failure() {
        rt::block_on(async {
            let (port, seen) = socks_peer(SocksScript {
                method: 0x00,
                auth_status: 0x00,
                replies: vec![0x05, 0x04],
            })
            .await;
            let proxy = socks_endpoint(&format!("socks5://127.0.0.1:{port}"));
            let addrs = two_addresses();
            let failure = within(
                Duration::from_secs(5),
                socks_each(&proxy, socks_of(&proxy), &loop_origin(), &addrs),
            )
            .await
            .expect("the loop ends within the limit");
            let Err(Failure::Retry(err)) = failure else {
                panic!("a refused connect is retryable");
            };
            assert_eq!(
                err.to_string(),
                format!(
                    "fetch: connect to origin.example:443 through the proxy \
                     socks5://127.0.0.1:{port} failed at [2001:db8::1]:443, the last of 2 addresses: the proxy refused \
                     the connect with the code 04 (host unreachable)"
                )
            );
            assert_eq!(seen.lock().unwrap().connections, 2);
        });
    }

    /// Under `socks5://`, a host name that the resolver refuses is retryable.
    /// The message names the origin host, and the fetcher opens no connection
    /// to the proxy. A name with a NUL byte passes the SOCKS5 checks of the
    /// name, and `std` refuses it before it calls the resolver of the system.
    /// So the test sends no DNS query.
    #[test]
    fn a_failed_resolve_names_the_origin_host() {
        rt::block_on(async {
            let (port, seen) = socks_peer(SocksScript {
                method: 0x00,
                auth_status: 0x00,
                replies: Vec::new(),
            })
            .await;
            let proxy = socks_endpoint(&format!("socks5://127.0.0.1:{port}"));
            let origin = Origin {
                tls: true,
                host: "origin\0example".to_owned(),
                port: 443,
            };
            let failure = within(
                Duration::from_secs(5),
                socks_connect(&proxy, socks_of(&proxy), &origin),
            )
            .await
            .expect("the resolve ends within the limit");
            let Err(Failure::Retry(err)) = failure else {
                panic!("a failed resolve is retryable");
            };
            let message = err.to_string();
            assert!(
                message.starts_with(&format!(
                    "fetch: connect to origin\0example:443 through the proxy \
                     socks5://127.0.0.1:{port} failed: cannot resolve origin\0example: "
                )),
                "{message}"
            );
            assert_eq!(seen.lock().unwrap().connections, 0);
        });
    }

    /// A failed connect to the proxy ends the loop at the first address. The
    /// failure is the retryable failure of a proxy connect, with no count of
    /// addresses.
    #[test]
    fn an_unreachable_proxy_ends_the_address_loop() {
        rt::block_on(async {
            // An open connection holds its local port, and nothing listens on
            // that port. So a connect to the port is refused, and no other
            // test can bind the port before the end of this test.
            let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
            let held = std::net::TcpStream::connect(listener.local_addr().unwrap()).unwrap();
            let port = held.local_addr().unwrap().port();
            let proxy = socks_endpoint(&format!("socks5://127.0.0.1:{port}"));
            let addrs = two_addresses();
            let failure = within(
                Duration::from_secs(5),
                socks_each(&proxy, socks_of(&proxy), &loop_origin(), &addrs),
            )
            .await
            .expect("the loop ends within the limit");
            let Err(Failure::Retry(err)) = failure else {
                panic!("a failed proxy connect is retryable");
            };
            let message = err.to_string();
            assert!(
                message.starts_with(&format!(
                    "fetch: connect to the proxy socks5://127.0.0.1:{port} failed: "
                )),
                "{message}"
            );
            assert!(!message.contains("the last of"), "{message}");
            drop((held, listener));
        });
    }

    /// `http_proxy` serves cleartext origins, and `https_proxy` serves TLS
    /// origins. `all_proxy` serves both schemes if the scheme-specific variable
    /// is unset. Lower case wins over upper case, and an empty value counts as
    /// unset. Nothing reads `HTTP_PROXY`.
    #[test]
    fn the_proxy_variables_are_read_per_scheme() {
        let cleartext = "http://origin.example/summary";
        let tls = "https://origin.example/summary";
        let absolute = |named: &str| Some((named.to_owned(), "absolute"));
        let tunnel = |named: &str| Some((named.to_owned(), "tunnel"));
        let socks = |named: &str| Some((named.to_owned(), "socks"));

        // Each scheme is served by its own variable, and the request form
        // follows the origin. Cleartext uses the absolute form over a proxy
        // connection, and TLS uses a tunnel.
        let proxies = resolve_variables(&variables(&[
            ("http_proxy", "http://plain.example:3128"),
            ("https_proxy", "http://secure.example:3128"),
        ]))
        .unwrap();
        assert_eq!(
            via_of(&proxies, cleartext),
            absolute("http://plain.example:3128")
        );
        assert_eq!(via_of(&proxies, tls), tunnel("http://secure.example:3128"));

        // One variable serves its own scheme alone.
        let proxies =
            resolve_variables(&variables(&[("http_proxy", "http://plain.example:3128")])).unwrap();
        assert_eq!(
            via_of(&proxies, cleartext),
            absolute("http://plain.example:3128")
        );
        assert_eq!(via_of(&proxies, tls), None);
        let proxies =
            resolve_variables(&variables(&[("https_proxy", "http://secure.example:3128")]))
                .unwrap();
        assert_eq!(via_of(&proxies, cleartext), None);
        assert_eq!(via_of(&proxies, tls), tunnel("http://secure.example:3128"));

        // `all_proxy` serves either scheme, and the scheme-specific variable
        // takes the scheme it names.
        let proxies =
            resolve_variables(&variables(&[("all_proxy", "http://any.example:3128")])).unwrap();
        assert_eq!(
            via_of(&proxies, cleartext),
            absolute("http://any.example:3128")
        );
        assert_eq!(via_of(&proxies, tls), tunnel("http://any.example:3128"));
        let proxies = resolve_variables(&variables(&[
            ("all_proxy", "http://any.example:3128"),
            ("https_proxy", "http://secure.example:3128"),
        ]))
        .unwrap();
        assert_eq!(
            via_of(&proxies, cleartext),
            absolute("http://any.example:3128")
        );
        assert_eq!(via_of(&proxies, tls), tunnel("http://secure.example:3128"));

        // The upper-case name is read for each variable except `http_proxy`. A
        // CGI gateway gives a request header called `Proxy` to its program
        // under that name.
        let proxies = resolve_variables(&variables(&[
            ("HTTP_PROXY", "http://cgi.example:3128"),
            ("HTTPS_PROXY", "http://secure.example:3128"),
        ]))
        .unwrap();
        assert_eq!(via_of(&proxies, cleartext), None);
        assert_eq!(via_of(&proxies, tls), tunnel("http://secure.example:3128"));
        let proxies =
            resolve_variables(&variables(&[("ALL_PROXY", "http://any.example:3128")])).unwrap();
        assert_eq!(
            via_of(&proxies, cleartext),
            absolute("http://any.example:3128")
        );

        // Lower case wins over upper case, and an empty value counts as unset.
        // So the upper-case name is the one that is read.
        let proxies = resolve_variables(&variables(&[
            ("https_proxy", "http://lower.example:3128"),
            ("HTTPS_PROXY", "http://upper.example:3128"),
        ]))
        .unwrap();
        assert_eq!(via_of(&proxies, tls), tunnel("http://lower.example:3128"));
        let proxies = resolve_variables(&variables(&[
            ("https_proxy", ""),
            ("HTTPS_PROXY", "http://upper.example:3128"),
        ]))
        .unwrap();
        assert_eq!(via_of(&proxies, tls), tunnel("http://upper.example:3128"));
        let proxies = resolve_variables(&variables(&[("http_proxy", "")])).unwrap();
        assert_eq!(via_of(&proxies, cleartext), None);

        // A value the fetcher cannot connect through fails the resolution
        // whichever variable held it.
        for name in ["http_proxy", "https_proxy", "all_proxy"] {
            let err = resolve_variables(&variables(&[(name, "socks4://proxy.example:1080")]))
                .unwrap_err();
            assert!(matches!(err, Error::Unsupported(_)), "{name}: {err}");
        }
        // A variable that the selection passes over is parsed too. An
        // `all_proxy` beside both scheme-specific variables is read by no
        // fetch. If it holds a value that the fetcher cannot connect through,
        // the resolution fails.
        let err = resolve_variables(&variables(&[
            ("all_proxy", "socks4://proxy.example:1080"),
            ("http_proxy", "http://plain.example:3128"),
            ("https_proxy", "http://secure.example:3128"),
        ]))
        .unwrap_err();
        assert!(matches!(err, Error::Unsupported(_)), "{err}");

        // No variable at all, and the explicit forms of the option.
        let proxies = resolve_variables(&[]).unwrap();
        assert_eq!(via_of(&proxies, cleartext), None);
        assert_eq!(via_of(&proxies, tls), None);
        let proxies = resolve_proxy(&Proxy::None).unwrap();
        assert_eq!(via_of(&proxies, cleartext), None);
        // One URL serves every origin, and it reads no exemption.
        let proxies = resolve_proxy(&Proxy::Url("http://any.example:3128".into())).unwrap();
        assert_eq!(
            via_of(&proxies, cleartext),
            absolute("http://any.example:3128")
        );
        assert_eq!(via_of(&proxies, tls), tunnel("http://any.example:3128"));

        // A SOCKS5 proxy in `https_proxy` serves TLS origins alone, and the
        // way is the same for each scheme that a SOCKS5 proxy serves.
        let proxies = resolve_variables(&variables(&[(
            "https_proxy",
            "socks5h://socks.example:9050",
        )]))
        .unwrap();
        assert_eq!(via_of(&proxies, tls), socks("socks5h://socks.example:9050"));
        assert_eq!(via_of(&proxies, cleartext), None);
        let proxies = resolve_proxy(&Proxy::Url("socks5h://socks.example".into())).unwrap();
        assert_eq!(via_of(&proxies, tls), socks("socks5h://socks.example"));
        assert_eq!(
            via_of(&proxies, cleartext),
            socks("socks5h://socks.example")
        );
    }

    /// A proxy variable with white space at the start or the end of its value
    /// fails the resolution, also a value of white space alone. The message
    /// names the variable, and holds nothing of the value. Each variable that
    /// the selection reads is checked, also if no fetch uses it. A variable
    /// that the selection does not read is not checked.
    #[test]
    fn a_proxy_variable_with_white_space_around_it_is_refused() {
        let url = "http://alice:s3cret@proxy.example:3128";
        let refused = |pairs: &[(&str, &str)], name: &str| {
            let err = resolve_variables(&variables(pairs)).unwrap_err();
            assert!(matches!(err, Error::Unsupported(_)), "{pairs:?}: {err}");
            let message = err.to_string();
            assert!(message.contains(name), "{pairs:?}: {message}");
            assert!(!message.contains("s3cret"), "{pairs:?}: {message}");
            assert!(!message.contains("proxy.example"), "{pairs:?}: {message}");
        };

        for name in [
            "http_proxy",
            "https_proxy",
            "HTTPS_PROXY",
            "all_proxy",
            "ALL_PROXY",
        ] {
            for value in [
                format!(" {url}"),
                format!("{url} "),
                format!("{url}\t"),
                format!("{url}\n"),
                format!("{url}\u{a0}"),
                "   ".to_owned(),
            ] {
                refused(&[(name, &value)], name);
            }
        }
        // If the lower-case value is empty, the upper-case name is read, and
        // the refusal names it.
        refused(
            &[("all_proxy", ""), ("ALL_PROXY", "http://p:1 ")],
            "ALL_PROXY",
        );
        // The check comes before the parse, so a value the parse refuses as
        // well gets the white-space refusal.
        refused(
            &[("all_proxy", "socks4://alice:s3cret@proxy.example:1080 ")],
            "all_proxy",
        );
        // The refusal occurs when the fetcher is built. So an exemption of
        // every origin does not prevent it.
        refused(
            &[("http_proxy", &format!("{url} ")), ("no_proxy", "*")],
            "http_proxy",
        );
        // `all_proxy` beside both scheme-specific variables is read by no
        // fetch, and the check still reads its value.
        refused(
            &[
                ("all_proxy", &format!("{url} ")),
                ("http_proxy", "http://plain.example:3128"),
                ("https_proxy", "http://secure.example:3128"),
            ],
            "all_proxy",
        );

        // An upper-case name shadowed by its lower-case one is not read, and
        // `HTTP_PROXY` is read by nothing.
        resolve_variables(&variables(&[
            ("https_proxy", "http://secure.example:3128"),
            ("HTTPS_PROXY", "http://upper.example:3128 "),
        ]))
        .unwrap();
        resolve_variables(&variables(&[("HTTP_PROXY", "http://cgi.example:3128 ")])).unwrap();
        // A URL the caller states in the options keeps its trim.
        resolve_proxy(&Proxy::Url("http://p:3128 ".into())).unwrap();
    }

    /// `check_options` takes the options that the constructor takes up to
    /// its TLS setup, and refuses each other value with the error of the
    /// constructor, in the order of the constructor.
    #[test]
    fn check_options_gives_the_refusals_of_the_constructor_that_read_no_file() {
        let base = || direct_options("http://origin.example/repo");
        let proxy = |proxy: Proxy| FetcherOptions { proxy, ..base() };
        let headers = |pairs: &[(&str, &str)]| FetcherOptions {
            headers: pairs
                .iter()
                .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
                .collect(),
            ..base()
        };
        let bearer = |token: &str| {
            Some(BearerToken {
                token: token.to_owned(),
            })
        };
        for options in [
            base(),
            proxy(Proxy::Url("http://proxy.example:3128".into())),
            proxy(Proxy::Url("socks5h://socks.example".into())),
            proxy(Proxy::Variables(vec![
                ("https_proxy".into(), "http://proxy.example:3128".into()),
                ("no_proxy".into(), ".internal.example".into()),
            ])),
            headers(&[("X-Trace", "1")]),
        ] {
            check_options(&options).unwrap();
        }
        let refused = [
            FetcherOptions {
                low_speed: Some(LowSpeed {
                    limit: 0,
                    time: Duration::from_secs(30),
                }),
                ..base()
            },
            FetcherOptions {
                mirrors: vec!["http://origin.example/repo?x=1".into()],
                ..base()
            },
            FetcherOptions {
                mirrors: vec!["ftp://origin.example/repo".into()],
                ..base()
            },
            headers(&[("Host", "other.example")]),
            headers(&[("bad name", "1")]),
            headers(&[("Connection", "close")]),
            headers(&[("X-Trace", "a\nb")]),
            headers(&[("Cookie", "id=1")]),
            FetcherOptions {
                bearer_token: bearer("tok"),
                ..base()
            },
            FetcherOptions {
                bearer_token: bearer("tok"),
                allow_cleartext_credentials: true,
                ..headers(&[("Authorization", "Bearer other")])
            },
            FetcherOptions {
                bearer_token: bearer("not token68"),
                allow_cleartext_credentials: true,
                ..base()
            },
            proxy(Proxy::Url("ftp://proxy.example:3128".into())),
            proxy(Proxy::Url("http://proxy.example:3128/squid".into())),
            proxy(Proxy::Variables(vec![(
                "http_proxy".into(),
                "ftp://x".into(),
            )])),
            proxy(Proxy::Variables(vec![(
                "all_proxy".into(),
                "socks4://x".into(),
            )])),
            proxy(Proxy::Variables(vec![(
                "https_proxy".into(),
                "http://p:1 ".into(),
            )])),
            // A bad header comes before a bad proxy.
            FetcherOptions {
                proxy: Proxy::Url("ftp://proxy.example:3128".into()),
                ..headers(&[("Host", "other.example")])
            },
        ];
        for options in refused {
            let checked = check_options(&options).unwrap_err();
            let built = rt::block_on(Fetcher::new(options.clone())).err().unwrap();
            assert_eq!(checked.to_string(), built.to_string(), "{options:?}");
        }
        let both = FetcherOptions {
            proxy: Proxy::Url("ftp://proxy.example:3128".into()),
            ..headers(&[("Host", "other.example")])
        };
        assert!(matches!(check_options(&both), Err(Error::Fetch(_))));
    }

    /// What `no_proxy` exempts: an exact host, a host under a listed domain, a
    /// port-qualified entry, and every host under `*`. The match is made
    /// against the host text and never against the address it resolves to.
    #[test]
    fn no_proxy_exempts_by_host_text_and_port() {
        let proxied = |no_proxy: &str, url: &str| {
            let proxies = resolve_variables(&variables(&[
                ("all_proxy", "http://any.example:3128"),
                ("no_proxy", no_proxy),
            ]))
            .unwrap();
            via_of(&proxies, url).is_some()
        };

        // An exact host, and a host of another name at the same domain.
        assert!(!proxied("origin.example", "http://origin.example/p"));
        assert!(proxied("origin.example", "http://other.example/p"));
        // A host under a listed domain, in each of the two spellings of the
        // entry. A name that only ends with the same letters is not exempted.
        for entry in ["example.com", ".example.com"] {
            assert!(!proxied(entry, "http://a.example.com/p"));
            assert!(!proxied(entry, "http://deep.a.example.com/p"));
            assert!(!proxied(entry, "http://example.com/p"));
            assert!(proxied(entry, "http://notexample.com/p"));
        }
        // The list is comma-separated, space around an entry is ignored, and an
        // empty entry names nothing.
        assert!(!proxied(" a.example , b.example ", "http://b.example/p"));
        assert!(!proxied("a.example,,b.example", "http://a.example/p"));
        assert!(proxied(",", "http://a.example/p"));
        assert!(proxied("", "http://a.example/p"));
        // ASCII case is ignored on both sides.
        assert!(!proxied("ORIGIN.example", "http://origin.EXAMPLE/p"));
        // A port-qualified entry matches that port alone.
        assert!(!proxied(
            "origin.example:8080",
            "http://origin.example:8080/p"
        ));
        assert!(proxied(
            "origin.example:8080",
            "http://origin.example:8081/p"
        ));
        assert!(proxied("origin.example:8080", "http://origin.example/p"));
        assert!(!proxied("origin.example:443", "https://origin.example/p"));
        // An entry with no port matches whatever port the origin names.
        assert!(!proxied("origin.example", "http://origin.example:8080/p"));
        // `*` exempts every host, whatever else the list holds.
        for url in ["http://a.example/p", "https://b.example:8443/p"] {
            assert!(!proxied("*", url));
            assert!(!proxied("a.example,*", url));
        }
        // An IP literal matches that text and nothing else. So the address of a
        // name does not exempt the name, and a name does not exempt an address.
        assert!(!proxied("127.0.0.1", "http://127.0.0.1:8080/p"));
        assert!(proxied("localhost", "http://127.0.0.1:8080/p"));
        assert!(proxied("127.0.0.1", "http://localhost:8080/p"));
        // An IPv6 literal is exempted by the address alone, written with or
        // without the brackets the authority carries.
        for entry in ["::1", "[::1]", "[::1]:8080"] {
            assert!(!proxied(entry, "http://[::1]:8080/p"), "{entry}");
        }
        assert!(proxied("[::1]:8081", "http://[::1]:8080/p"));
        // A network in CIDR notation names no network here: it is read as a
        // host text, which no origin holds.
        assert!(proxied("127.0.0.0/8", "http://127.0.0.1:8080/p"));
        // A port text that is no port leaves the entry as one host text, which
        // no origin host holds.
        assert!(proxied("origin.example:abc", "http://origin.example/p"));
        // An entry that names no host exempts nothing, also for a host written
        // with a trailing `.`.
        for entry in [".", ":8080", ".:8080"] {
            assert!(proxied(entry, "http://a.example./p"), "{entry}");
            assert!(proxied(entry, "http://a.example:8080/p"), "{entry}");
        }
        // One leading `.` is stripped, so a second belongs to the host text the
        // entry names.
        assert!(proxied("..example.com", "http://a.example.com/p"));
        // `*` as a whole entry is the one wildcard of the list. Each other form
        // with a `*` is a host text.
        assert!(proxied("*.example.com", "http://a.example.com/p"));
        assert!(proxied("*.example.com", "http://example.com/p"));
    }

    /// The userinfo of a proxy URL holds a password. So it reaches neither a
    /// rendering of the options nor a refusal of the constructor.
    #[test]
    fn a_proxy_password_reaches_no_message() {
        let url = |tail: &str| format!("http://alice:sup3rs3cret@proxy.example:3128{tail}");

        // The rendering of every form that carries a URL.
        for proxy in [
            Proxy::Url(url("")),
            Proxy::Variables(vec![("https_proxy".to_owned(), url(""))]),
        ] {
            let rendered = format!("{proxy:?}");
            assert!(!rendered.contains("sup3rs3cret"), "{rendered}");
            assert!(rendered.contains("proxy.example:3128"), "{rendered}");

            let options = FetcherOptions {
                proxy,
                ..direct_options("https://example.com")
            };
            let rendered = format!("{options:?}");
            assert!(!rendered.contains("sup3rs3cret"), "{rendered}");
        }

        // Each refusal, whichever part of the URL the parse read.
        for tail in ["/squid", "/?upstream=1", "/#frag", ":99999"] {
            let err = parse_proxy(&url(tail)).unwrap_err();
            let message = err.to_string();
            assert!(!message.contains("sup3rs3cret"), "{tail}: {message}");
        }
        let err = parse_proxy("socks4://alice:sup3rs3cret@proxy.example:1080").unwrap_err();
        assert!(!err.to_string().contains("sup3rs3cret"), "{err}");
        let err = parse_proxy("socks5h://alice:sup3rs3cret@proxy.example/x").unwrap_err();
        assert!(!err.to_string().contains("sup3rs3cret"), "{err}");

        // A password with a `/`, a `?`, or a `#` ends the authority before the
        // `@`. The userinfo is left out also in this case.
        for password in ["pa#ss", "pa?ss", "p/ss"] {
            let url = format!("http://alice:{password}@proxy.example:3128/squid");
            let rendered = format!("{:?}", Proxy::Url(url.clone()));
            assert!(!rendered.contains(password), "{password}: {rendered}");
            assert!(
                rendered.contains("proxy.example:3128"),
                "{password}: {rendered}"
            );
            let message = parse_proxy(&url).unwrap_err().to_string();
            assert!(!message.contains(password), "{password}: {message}");
            assert!(
                message.contains("proxy.example:3128"),
                "{password}: {message}"
            );
        }

        // A `/` or a `?` in the userinfo moves the text after it into the path
        // or the query. The refusal of that path or query shows no part of it.
        for userinfo in ["u/pw-QXZ", "tok?pw-QXZ", "u:x/pw-QXZ", "u:x?pw-QXZ"] {
            let url = format!("http://{userinfo}@proxy.invalid:3128");
            let message = parse_proxy(&url).unwrap_err().to_string();
            assert!(!message.contains("pw-QXZ"), "{userinfo}: {message}");
        }

        // The refusal the constructor writes is the one a caller logs.
        rt::block_on(async {
            let err = Fetcher::new(FetcherOptions {
                proxy: Proxy::Url(url("/squid")),
                ..direct_options("http://origin.example/repo")
            })
            .await
            .unwrap_err();
            assert!(matches!(err, Error::Unsupported(_)), "{err}");
            let message = err.to_string();
            assert!(!message.contains("sup3rs3cret"), "{message}");
            assert!(message.contains("proxy.example:3128"), "{message}");
        });
    }

    /// The `Debug` of a proxy writes each URL as `RedactedUrl` writes it. The
    /// form of each variant is the form of `derive(Debug)`.
    #[test]
    fn proxy_debug_redacts_each_url() {
        let url = "http://alice:pw-QXZ@proxy.example:3128/?t=QXZ#QXZ";
        assert_eq!(
            format!("{:?}", Proxy::Url(url.to_owned())),
            r#"Url("http://proxy.example:3128/?<redacted>#<redacted>")"#
        );
        let variables = Proxy::Variables(vec![
            ("https_proxy".to_owned(), url.to_owned()),
            ("no_proxy".to_owned(), ".internal.example.com".to_owned()),
        ]);
        assert_eq!(
            format!("{variables:?}"),
            concat!(
                r#"Variables([("https_proxy", "http://proxy.example:3128/?<redacted>#<redacted>"), "#,
                r#"("no_proxy", ".internal.example.com")])"#
            )
        );
        assert_eq!(format!("{:?}", Proxy::None), "None");
        assert_eq!(format!("{:?}", Proxy::Environment), "Environment");
    }

    /// The text before `://` is a scheme only when it is a valid scheme. Else
    /// the userinfo starts at the start of the text.
    #[test]
    fn without_userinfo_reads_only_a_valid_scheme() {
        assert_eq!(without_userinfo("u:a://b@h/p"), "h/p");
        assert_eq!(without_userinfo("svn+ssh://u@h/p"), "svn+ssh://h/p");
        assert_eq!(without_userinfo("https://h/p"), "https://h/p");
        assert_eq!(scheme_len("1http://h"), 0);
        assert_eq!(scheme_len("://h"), 0);
        assert_eq!(scheme_len("h/p"), 0);
    }

    /// A `CONNECT` names its target by host and port, and always writes the
    /// port. The proxy opens a TCP connection, which reads no scheme.
    #[test]
    fn a_connect_names_the_target_with_its_port() {
        for (url, authority) in [
            ("https://origin.example/p", "origin.example:443"),
            ("https://origin.example:8443/p", "origin.example:8443"),
            ("http://origin.example/p", "origin.example:80"),
            ("https://[2001:db8::1]/p", "[2001:db8::1]:443"),
            ("https://[::1]:8443/p", "[::1]:8443"),
        ] {
            assert_eq!(connect_authority(&origin_of(url)), authority, "{url}");
        }
    }

    /// A proxied cleartext request carries the absolute-form target and the
    /// `Host` header of the origin. It also carries the credential of the
    /// proxy, if the merged headers hold no header of that name. A direct
    /// request and the request over a tunnel carry the origin form and nothing
    /// of the proxy.
    #[test]
    fn a_proxied_cleartext_request_carries_the_absolute_form() {
        rt::block_on(async {
            let fetcher = Fetcher::new(FetcherOptions {
                proxy: Proxy::Url("http://alice:s3cret@proxy.example:3128".into()),
                ..tls_options("http://origin.example/r")
            })
            .await
            .unwrap();
            let destination = fetcher.inner.mirrors[0].destination("refs/heads/a", None);
            let request = FetchRequest::path("refs/heads/a");
            let built = |via: Via<'_>, headers: &[(HeaderName, HeaderValue)]| {
                fetcher
                    .build_request(&destination, &request, headers, Protocol::Http11, via)
                    .unwrap()
            };
            let proxy_credential = |request: &Request<RequestBody>| {
                request
                    .headers()
                    .get(hyper::header::PROXY_AUTHORIZATION)
                    .map(|value| value.to_str().unwrap().to_owned())
            };
            let Via::Absolute(proxy) = fetcher.inner.proxies.via(&destination.origin) else {
                panic!("a cleartext origin behind a proxy travels in absolute form");
            };
            let via = Via::Absolute(proxy);

            let absolute = built(via, &fetcher.inner.headers);
            assert_eq!(absolute.uri(), "http://origin.example/r/refs/heads/a");
            assert_eq!(
                absolute.headers().get(hyper::header::HOST).unwrap(),
                "origin.example"
            );
            // base64("alice:s3cret")
            assert_eq!(
                proxy_credential(&absolute).as_deref(),
                Some("Basic YWxpY2U6czNjcmV0")
            );

            // A `Proxy-Authorization` header that the caller set states the
            // value for the proxy. So the credential of the fetcher is left
            // out, and is not added to it.
            let mut headers = fetcher.inner.headers.clone();
            headers.push((
                hyper::header::PROXY_AUTHORIZATION,
                HeaderValue::from_static("Basic Y2FsbGVy"),
            ));
            let caller = built(via, &headers);
            assert_eq!(
                caller
                    .headers()
                    .get_all(hyper::header::PROXY_AUTHORIZATION)
                    .iter()
                    .count(),
                1
            );
            assert_eq!(proxy_credential(&caller).as_deref(), Some("Basic Y2FsbGVy"));

            // A direct hop, the request over a tunnel, and the request over a
            // SOCKS5 connection carry the origin form and nothing of the proxy.
            let socks_proxy = parse_proxy("socks5h://bob:pw@socks.example").unwrap();
            let ProxyKind::Socks(socks) = &socks_proxy.kind else {
                panic!("a socks5h url names a SOCKS5 proxy");
            };
            for via in [
                Via::Direct,
                Via::Tunnel(proxy),
                Via::Socks(&socks_proxy, socks),
            ] {
                let direct = built(via, &fetcher.inner.headers);
                assert_eq!(direct.uri(), "/r/refs/heads/a");
                assert_eq!(proxy_credential(&direct), None);
            }
        });
    }

    /// The connection layer frames a request and holds its connection. So a
    /// header of one of those names is refused. The constructor refuses a
    /// fetcher header, and a request header fails before admission.
    #[test]
    fn a_connection_header_is_refused_at_both_layers() {
        let fetcher_headers = vec![(
            HeaderName::from_static("x-trace"),
            HeaderValue::from_static("abc"),
        )];
        for name in CONNECTION_HEADERS {
            let options = FetcherOptions {
                headers: vec![(name.to_owned(), "10".to_owned())],
                ..direct_options("http://example.com")
            };
            let err = rt::block_on(Fetcher::new(options)).unwrap_err();
            let message = err.to_string();
            assert!(message.contains(name), "{name}: {message}");
            assert!(message.contains("connection layer"), "{name}: {message}");

            let request = vec![(name.to_owned(), "10".to_owned())];
            let err = merge_headers(&fetcher_headers, &request, None, None).unwrap_err();
            let message = err.to_string();
            assert!(message.contains(name), "{name}: {message}");
            assert!(message.contains("connection layer"), "{name}: {message}");
        }

        // A name written in another case is the same header name.
        let request = vec![("Content-Length".to_owned(), "10".to_owned())];
        let err = merge_headers(&fetcher_headers, &request, None, None).unwrap_err();
        assert!(err.to_string().contains("content-length"), "{err}");
    }

    /// A request that adds no header and no credentials sends the list of the
    /// fetcher by reference. A request header replaces the fetcher header of
    /// the same name. The credentials of the request replace the
    /// `Authorization` of the fetcher.
    #[test]
    fn request_headers_merge_over_the_fetchers() {
        let header = |name: &'static str, value: &'static str| {
            (
                HeaderName::from_static(name),
                HeaderValue::from_static(value),
            )
        };
        let fetcher = vec![
            header("x-trace", "fetcher"),
            header("x-only", "yes"),
            header("authorization", "Basic ZmV0Y2hlcg=="),
        ];
        let sent = |headers: &[(HeaderName, HeaderValue)]| {
            headers
                .iter()
                .map(|(name, value)| (name.as_str().to_owned(), value.to_str().unwrap().to_owned()))
                .collect::<Vec<_>>()
        };

        let borrowed = merge_headers(&fetcher, &[], None, None).unwrap();
        assert!(matches!(borrowed, Cow::Borrowed(_)));
        assert_eq!(sent(&borrowed), sent(&fetcher));

        // The replacement compares names as header names, so the case that the
        // request wrote has no effect. It leaves each other fetcher header in
        // place. Two request headers of one name both reach the wire.
        let extra = vec![
            ("X-Trace".to_owned(), "request".to_owned()),
            ("x-trace".to_owned(), "second".to_owned()),
        ];
        let merged = merge_headers(&fetcher, &extra, None, None).unwrap();
        assert_eq!(
            sent(&merged),
            [
                ("x-only".to_owned(), "yes".to_owned()),
                ("authorization".to_owned(), "Basic ZmV0Y2hlcg==".to_owned()),
                ("x-trace".to_owned(), "request".to_owned()),
                ("x-trace".to_owned(), "second".to_owned()),
            ]
        );

        // base64("u:p")
        let auth = BasicAuth {
            user: "u".into(),
            password: "p".into(),
        };
        let merged = merge_headers(&fetcher, &[], Some(&auth), None).unwrap();
        assert_eq!(
            sent(&merged),
            [
                ("x-trace".to_owned(), "fetcher".to_owned()),
                ("x-only".to_owned(), "yes".to_owned()),
                ("authorization".to_owned(), "Basic dTpw".to_owned()),
            ]
        );

        let both = vec![("authorization".to_owned(), "Basic aaa".to_owned())];
        let err = merge_headers(&fetcher, &both, Some(&auth), None).unwrap_err();
        assert!(err.to_string().contains("pass one of them"), "{err}");
        let host = vec![("host".to_owned(), "elsewhere".to_owned())];
        let err = merge_headers(&fetcher, &host, None, None).unwrap_err();
        assert!(err.to_string().contains("host header"), "{err}");
        let invalid = vec![("not a header".to_owned(), "v".to_owned())];
        let err = merge_headers(&fetcher, &invalid, None, None).unwrap_err();
        assert!(err.to_string().contains("invalid header name"), "{err}");
    }

    /// A fetch delivers the bytes that the remote stores. So a response is
    /// served only if it declares no coding, or declares `identity`. A value is
    /// a comma-separated list, and several headers state one list together. A
    /// token compares with no case and with no space around it.
    #[test]
    fn a_declared_content_coding_is_read_off_the_response() {
        let map = |values: &[&str]| {
            let mut headers = hyper::HeaderMap::new();
            for value in values {
                headers.append(
                    hyper::header::CONTENT_ENCODING,
                    HeaderValue::try_from(*value).unwrap(),
                );
            }
            headers
        };

        // Nothing to undo: no header at all, an empty value, a value of
        // separators alone, and every spelling of the one coding that is served.
        for values in [
            vec![],
            vec![""],
            vec![","],
            vec!["identity"],
            vec!["IDENTITY"],
            vec![" identity "],
            vec!["identity", "identity"],
        ] {
            assert_eq!(declared_coding(&map(&values)), None, "{values:?}");
        }

        // A coding that the body needs a decode from, named as the response
        // wrote it. A list with one coding refuses the whole response, at each
        // position of the coding. Several headers are read together, in the
        // order of arrival. A value whose tokens are all empty names nothing,
        // so it adds no separator to the message.
        for (values, named) in [
            (vec!["gzip"], "gzip"),
            (vec!["GZip"], "GZip"),
            (vec!["identity, gzip"], "identity, gzip"),
            (vec!["gzip, identity"], "gzip, identity"),
            (vec!["identity", "gzip"], "identity, gzip"),
            (vec!["gzip", "br"], "gzip, br"),
            (vec!["", "gzip"], "gzip"),
            (vec!["gzip", ""], "gzip"),
            (vec!["gzip", "", "br"], "gzip, br"),
            (vec!["gzip", ",", "br"], "gzip, br"),
        ] {
            assert_eq!(
                declared_coding(&map(&values)).as_deref(),
                Some(named),
                "{values:?}"
            );
        }
    }

    /// Each request asks for no content coding. A configured header with a
    /// name that the fetcher sets replaces that entry, and does not join it on
    /// the wire.
    #[test]
    fn a_fetcher_header_replaces_the_entry_the_fetcher_sets() {
        let sent = |fetcher: &Fetcher, name: HeaderName| {
            fetcher
                .inner
                .headers
                .iter()
                .filter(|(held, _)| *held == name)
                .map(|(_, value)| value.to_str().unwrap().to_owned())
                .collect::<Vec<_>>()
        };

        let fetcher = rt::block_on(Fetcher::new(direct_options("http://example.com"))).unwrap();
        assert_eq!(
            sent(&fetcher, hyper::header::ACCEPT_ENCODING),
            [IDENTITY.to_owned()]
        );

        for name in ["accept-encoding", "user-agent"] {
            let options = FetcherOptions {
                headers: vec![(name.to_owned(), "caller".to_owned())],
                ..direct_options("http://example.com")
            };
            let fetcher = rt::block_on(Fetcher::new(options)).unwrap();
            let header = HeaderName::from_static(name);
            assert_eq!(sent(&fetcher, header), ["caller".to_owned()], "{name}");
        }

        // Two entries of one name both reach the wire. The first of them
        // replaces the entry that the fetcher sets.
        let options = FetcherOptions {
            headers: vec![
                ("accept-encoding".to_owned(), "one".to_owned()),
                ("Accept-Encoding".to_owned(), "two".to_owned()),
            ],
            ..direct_options("http://example.com")
        };
        let fetcher = rt::block_on(Fetcher::new(options)).unwrap();
        assert_eq!(
            sent(&fetcher, hyper::header::ACCEPT_ENCODING),
            ["one".to_owned(), "two".to_owned()]
        );
        // A name under which the fetcher sets nothing also keeps both entries.
        let options = FetcherOptions {
            headers: vec![
                ("x-trace".to_owned(), "one".to_owned()),
                ("x-trace".to_owned(), "two".to_owned()),
            ],
            ..direct_options("http://example.com")
        };
        let fetcher = rt::block_on(Fetcher::new(options)).unwrap();
        assert_eq!(
            sent(&fetcher, HeaderName::from_static("x-trace")),
            ["one".to_owned(), "two".to_owned()]
        );
    }

    /// A credential is withheld from no destination. So one cleartext
    /// destination that a fetch can reach refuses it, and the message names
    /// the origin. A header that is not a credential reaches a cleartext
    /// destination.
    #[test]
    fn a_credential_refuses_a_cleartext_destination() {
        let credential = vec![(hyper::header::COOKIE, HeaderValue::from_static("session=1"))];
        let plain = vec![(
            HeaderName::from_static("x-trace"),
            HeaderValue::from_static("abc"),
        )];
        // A fetcher whose mirror list holds one cleartext entry among https
        // entries. The credential belongs to the request, so the construction
        // check of the list does not refuse it.
        let options = FetcherOptions {
            mirrors: vec![
                "https://secure.example/repo".to_owned(),
                "http://cleartext.example/repo".to_owned(),
            ],
            ..tls_options("unused")
        };
        let fetcher = rt::block_on(Fetcher::new(options)).unwrap();

        let mirrors = fetcher.route(Target::Path("summary")).unwrap();
        let err = fetcher.check_cleartext(&mirrors, &credential).unwrap_err();
        assert!(
            err.to_string().contains("http://cleartext.example"),
            "{err}"
        );
        fetcher.check_cleartext(&mirrors, &plain).unwrap();

        let cleartext = fetcher
            .route(Target::Url("http://cleartext.example/p"))
            .unwrap();
        let err = fetcher
            .check_cleartext(&cleartext, &credential)
            .unwrap_err();
        assert!(
            err.to_string().contains("http://cleartext.example"),
            "{err}"
        );
        fetcher.check_cleartext(&cleartext, &plain).unwrap();

        let secure = fetcher
            .route(Target::Url("https://secure.example/p"))
            .unwrap();
        fetcher.check_cleartext(&secure, &credential).unwrap();
    }

    /// A handshake verifies the server certificate against a trust anchor. So
    /// if the fetcher holds none, a fetch of a TLS origin is refused before
    /// admission, and the message names the origin.
    ///
    /// A clear flag stands for a fetcher built on a host whose trust store
    /// holds nothing. The test sets the flag by hand, because this host has a
    /// CA bundle. `TrustRoots::Pem` fails the constructor over a blob that
    /// holds no certificate.
    #[test]
    fn a_tls_destination_needs_a_trust_anchor() {
        rt::block_on(async {
            let mut fetcher = Fetcher::new(direct_options("http://cleartext.example/repo"))
                .await
                .unwrap();
            Arc::get_mut(&mut fetcher.inner)
                .expect("the one handle on this fetcher")
                .has_trust_anchors = false;

            // Nothing listens on port 1 of the loopback. So an attempt fails
            // its connect, and the fetcher tries again through each round and
            // each backoff. The refusal is definitive and takes no attempt, so
            // it resolves at once.
            let refused = within(
                Duration::from_secs(1),
                fetcher.fetch(FetchRequest::url("https://127.0.0.1:1/summary")),
            )
            .await;
            let err = refused
                .expect("the refusal takes no attempt")
                .expect_err("a tls url is refused without anchors");
            let message = err.to_string();
            assert!(message.contains("https://127.0.0.1:1"), "{message}");
            assert!(message.contains("no trust anchors"), "{message}");

            // A cleartext destination opens no handshake, so it is served by
            // the same fetcher.
            let cleartext = fetcher
                .route(Target::Url("http://cleartext.example/summary"))
                .unwrap();
            fetcher.check_trust_anchors(&cleartext).unwrap();
            let mirrors = fetcher.route(Target::Path("summary")).unwrap();
            fetcher.check_trust_anchors(&mirrors).unwrap();
        });
    }

    /// The constructor reads the host trust store only if a fetch can open a
    /// handshake. A fetcher whose mirrors are all `http`, and that follows no
    /// redirect, reads no store and holds no anchor. It refuses a URL target
    /// that names an `https` origin. An `https` mirror, an empty mirror list, and
    /// a redirect limit above zero each read the store.
    #[test]
    fn a_cleartext_fetcher_with_no_redirect_reads_no_trust_store() {
        let reads = || tls::SYSTEM_STORE_READS.with(std::cell::Cell::get);
        rt::block_on(async {
            let before = reads();
            let fetcher = Fetcher::new(FetcherOptions {
                max_redirects: 0,
                ..direct_options("http://cleartext.example/repo")
            })
            .await
            .unwrap();
            assert_eq!(reads(), before, "the store was read");
            assert!(!fetcher.inner.has_trust_anchors);
            let refused = within(
                Duration::from_secs(1),
                fetcher.fetch(FetchRequest::url("https://127.0.0.1:1/summary")),
            )
            .await
            .expect("the refusal takes no attempt")
            .expect_err("a tls url is refused without anchors");
            assert!(
                refused.to_string().contains("no trust anchors"),
                "{refused}"
            );

            let cases = [
                direct_options("http://cleartext.example/repo"),
                FetcherOptions {
                    max_redirects: 0,
                    ..direct_options("https://tls.example/repo")
                },
                FetcherOptions {
                    max_redirects: 0,
                    proxy: Proxy::None,
                    ..FetcherOptions::default()
                },
            ];
            for options in cases {
                let before = reads();
                let what = format!("{:?} {}", options.mirrors, options.max_redirects);
                // A host with no CA bundle fails the `https` cases, after the
                // read.
                let _ = Fetcher::new(options).await;
                assert_eq!(reads(), before + 1, "{what}");
            }
        });
    }

    /// The server chooses the origin of a redirect. So a hop to a TLS origin
    /// gets the refusal that a route to a TLS origin gets. The fetcher holds
    /// nothing to verify the certificate against. With no refusal, the
    /// handshake fails retryably with a message about the peer, and spends
    /// each round and each backoff.
    ///
    /// The test sets the flag by hand, for the reason that
    /// `a_tls_destination_needs_a_trust_anchor` states.
    ///
    /// The two peers are raw sockets: one answers a redirect to the other, and
    /// the other reports how many connections reached it.
    #[test]
    fn a_redirect_to_a_tls_origin_needs_a_trust_anchor() {
        use futures_lite::io::{AsyncReadExt, AsyncWriteExt};
        use std::sync::atomic::{AtomicUsize, Ordering};

        rt::block_on(async {
            // The origin that the redirect names. Nothing connects to it, and
            // the count reports this.
            let tls_peer = rt::TcpListener::bind("127.0.0.1:0".parse().unwrap())
                .await
                .unwrap();
            let tls_port = tls_peer.local_addr().unwrap().port();
            let connections = Arc::new(AtomicUsize::new(0));
            let counted = connections.clone();
            drop(rt::spawn(async move {
                while let Ok((stream, _peer)) = tls_peer.accept().await {
                    counted.fetch_add(1, Ordering::SeqCst);
                    drop(stream);
                }
            }));

            let cleartext_peer = rt::TcpListener::bind("127.0.0.1:0".parse().unwrap())
                .await
                .unwrap();
            let cleartext_port = cleartext_peer.local_addr().unwrap().port();
            let requests = Arc::new(AtomicUsize::new(0));
            let counted = requests.clone();
            drop(rt::spawn(async move {
                while let Ok((mut stream, _peer)) = cleartext_peer.accept().await {
                    let mut request = [0u8; 1024];
                    let _ = stream.read(&mut request).await;
                    counted.fetch_add(1, Ordering::SeqCst);
                    let answer = format!(
                        "HTTP/1.1 302 Found\r\nLocation: https://127.0.0.1:{tls_port}/hopped\r\n\
                         Content-Length: 0\r\n\r\n"
                    );
                    let _ = stream.write_all(answer.as_bytes()).await;
                    let _ = stream.flush().await;
                }
            }));

            let mut fetcher =
                Fetcher::new(direct_options(format!("http://127.0.0.1:{cleartext_port}")))
                    .await
                    .unwrap();
            Arc::get_mut(&mut fetcher.inner)
                .expect("the one handle on this fetcher")
                .has_trust_anchors = false;

            // Six rounds of a retryable handshake failure and their backoffs
            // run for more than five seconds. So only a refusal that ends the
            // attempt resolves inside this window.
            let refused = within(
                Duration::from_secs(2),
                fetcher.fetch(FetchRequest::path("summary")),
            )
            .await;
            let err = refused
                .expect("the refusal ends the attempt")
                .expect_err("a tls hop is refused without anchors");
            let message = err.to_string();
            assert!(
                message.contains(&format!("https://127.0.0.1:{tls_port}")),
                "{message}"
            );
            assert!(message.contains("no trust anchors"), "{message}");
            assert_eq!(requests.load(Ordering::SeqCst), 1);
            assert_eq!(connections.load(Ordering::SeqCst), 0);
        });
    }

    /// The reading end of a channel body, taken out of the body as an upload
    /// takes it.
    fn channel() -> (BodyEnd, UploadWriter) {
        let (body, writer) = UploadBody::channel();
        let UploadForm::Channel(end) = body.form else {
            panic!("a channel body holds its reading end");
        };
        (end, writer)
    }

    /// The next frame of `end`, as the bytes it carries.
    async fn next_frame(end: &mut BodyEnd) -> Option<std::io::Result<Bytes>> {
        let frame = std::future::poll_fn(|cx| end.poll_frame(cx)).await?;
        Some(frame.map(|frame| frame.into_data().expect("an upload body yields data")))
    }

    /// The writer hands the body over in frames of 64 KiB, a close hands over
    /// what is left, and the end follows the last frame. The close completes
    /// when the reading end gives the end.
    #[test]
    fn a_writer_hands_over_frames_of_64_kib() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            let data = (0..150_000u32).map(|i| i as u8).collect::<Vec<_>>();
            let written = async {
                futures_lite::io::AsyncWriteExt::write_all(&mut writer, &data).await?;
                futures_lite::io::AsyncWriteExt::close(&mut writer).await
            };
            let read = async {
                let mut frames = Vec::new();
                while let Some(frame) = next_frame(&mut end).await {
                    frames.push(frame.unwrap());
                }
                frames
            };
            let (written, frames) = futures_lite::future::zip(written, read).await;
            written.unwrap();
            assert_eq!(
                frames.iter().map(Bytes::len).collect::<Vec<_>>(),
                [UPLOAD_FRAME, UPLOAD_FRAME, 150_000 - 2 * UPLOAD_FRAME]
            );
            assert_eq!(frames.concat(), data);
            assert!(end.is_end_stream());
            assert!(end.exchange.lock().ended);
            assert_eq!(end.exchange.exact, None);
        });
    }

    /// A flush hands over a part-filled frame, and completes when hyper takes
    /// it.
    #[test]
    fn a_flush_hands_over_a_part_filled_frame() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            let flushed = async {
                futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"hello").await?;
                futures_lite::io::AsyncWriteExt::flush(&mut writer).await
            };
            let (flushed, frame) = futures_lite::future::zip(flushed, next_frame(&mut end)).await;
            flushed.unwrap();
            assert_eq!(frame.unwrap().unwrap(), &b"hello"[..]);
            assert!(!end.is_end_stream());
        });
    }

    /// A writer reports the hand-over of its request, and a request given
    /// back unwritten is no longer handed over.
    #[test]
    fn a_writer_reports_the_hand_over() {
        let (end, writer) = channel();
        assert!(!writer.is_handed_over());
        end.exchange.hand_over(Duration::from_secs(1)).unwrap();
        assert!(writer.is_handed_over());
        end.exchange.withdraw();
        assert!(!writer.is_handed_over());
    }

    /// A writer holds no buffer until its first write. A flush of less than
    /// 4 KiB hands over a copy and keeps the frame buffer. A larger flush
    /// hands the buffer over, and the next one is allocated at the next
    /// write.
    #[test]
    fn a_small_flush_hands_over_a_copy_and_keeps_the_buffer() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            assert_eq!(writer.buffer.capacity(), 0);
            futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"small")
                .await
                .unwrap();
            assert_eq!(writer.buffer.capacity(), UPLOAD_FRAME);
            let kept = writer.buffer.as_ptr();
            let flushed = futures_lite::io::AsyncWriteExt::flush(&mut writer);
            let (flushed, frame) = futures_lite::future::zip(flushed, next_frame(&mut end)).await;
            flushed.unwrap();
            assert_eq!(frame.unwrap().unwrap(), &b"small"[..]);
            assert!(writer.buffer.is_empty());
            assert_eq!(writer.buffer.as_ptr(), kept);
            assert_eq!(writer.buffer.capacity(), UPLOAD_FRAME);

            let large = vec![7u8; SMALL_FRAME];
            let flushed = async {
                futures_lite::io::AsyncWriteExt::write_all(&mut writer, &large).await?;
                futures_lite::io::AsyncWriteExt::flush(&mut writer).await
            };
            let (flushed, frame) = futures_lite::future::zip(flushed, next_frame(&mut end)).await;
            flushed.unwrap();
            assert_eq!(frame.unwrap().unwrap(), large);
            assert_eq!(writer.buffer.capacity(), 0);
            futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"x")
                .await
                .unwrap();
            assert_eq!(writer.buffer.capacity(), UPLOAD_FRAME);
        });
    }

    /// A writer dropped before close fails the body, so hyper never reads a
    /// clean end for a body cut short.
    #[test]
    fn a_writer_dropped_unclosed_fails_the_body() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"part")
                .await
                .unwrap();
            drop(writer);
            let err = next_frame(&mut end).await.unwrap().unwrap_err();
            assert!(err.to_string().contains("dropped"), "{err}");
            assert!(!end.is_end_stream());
            assert!(!end.exchange.lock().ended);
            // The failure stands for every later poll.
            assert!(next_frame(&mut end).await.unwrap().is_err());
        });
    }

    /// Once the reading end is gone, a write, a flush, and a close fail with
    /// a broken pipe.
    #[test]
    fn a_write_after_the_reading_end_is_gone_is_a_broken_pipe() {
        rt::block_on(async {
            let (end, mut writer) = channel();
            drop(end);
            let err = futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"late")
                .await
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::BrokenPipe);
            let err = futures_lite::io::AsyncWriteExt::flush(&mut writer)
                .await
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::BrokenPipe);
        });
    }

    /// A write after close is a broken pipe.
    #[test]
    fn a_write_after_close_is_a_broken_pipe() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            let closed = futures_lite::io::AsyncWriteExt::close(&mut writer);
            let (closed, frame) = futures_lite::future::zip(closed, next_frame(&mut end)).await;
            closed.unwrap();
            assert!(frame.is_none());
            let err = futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"x")
                .await
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::BrokenPipe);
        });
    }

    /// A frame that waits in the slot past the stall window fails the write
    /// with a timeout, and fails the body. The failure is latched. Before
    /// an upload starts the window, a full slot is no stall.
    #[test]
    fn a_frame_not_taken_within_the_window_fails_the_write() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            let frame = vec![0u8; UPLOAD_FRAME];
            // The first frame fills the buffer, the second hands it to the
            // slot, and the third waits for the slot.
            for _ in 0..2 {
                futures_lite::io::AsyncWriteExt::write_all(&mut writer, &frame)
                    .await
                    .unwrap();
            }
            let unbounded = within(
                Duration::from_millis(100),
                futures_lite::io::AsyncWriteExt::write_all(&mut writer, &frame),
            )
            .await;
            assert!(unbounded.is_none(), "a wait before the upload has no bound");

            end.exchange.hand_over(Duration::from_millis(50)).unwrap();
            let started = Instant::now();
            let err = futures_lite::io::AsyncWriteExt::write_all(&mut writer, &frame)
                .await
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::TimedOut);
            assert!(started.elapsed() >= Duration::from_millis(50));
            let again = futures_lite::io::AsyncWriteExt::write_all(&mut writer, b"x")
                .await
                .unwrap_err();
            assert_eq!(again.kind(), std::io::ErrorKind::TimedOut);
            let failed = next_frame(&mut end).await.unwrap().unwrap_err();
            assert!(failed.to_string().contains("not taken"), "{failed}");
        });
    }

    /// A body given whole declares its length and travels in frames of 64 KiB
    /// cut from its bytes with no copy. It reaches its end with the last
    /// frame. An empty body is at its end at the hand-over. The hand-over
    /// reports this, so the request can declare a length of zero.
    #[test]
    fn a_whole_body_travels_in_frames_of_a_declared_length() {
        rt::block_on(async {
            let bytes = Bytes::from((0..150_000u32).map(|i| i as u8).collect::<Vec<_>>());
            let mut end = BodyEnd::whole(&bytes);
            assert_eq!(end.exchange.exact, Some(150_000));
            assert_eq!(end.exchange.hand_over(Duration::from_secs(60)), Ok(false));
            assert!(!end.is_end_stream());
            let mut at = 0;
            for len in [UPLOAD_FRAME, UPLOAD_FRAME, 150_000 - 2 * UPLOAD_FRAME] {
                assert!(!end.exchange.lock().ended);
                let frame = next_frame(&mut end).await.unwrap().unwrap();
                assert_eq!(frame.len(), len);
                assert_eq!(frame.as_ptr(), bytes[at..].as_ptr());
                at += len;
            }
            assert!(end.exchange.lock().ended);
            assert!(end.is_end_stream());
            assert!(next_frame(&mut end).await.is_none());

            let empty = BodyEnd::whole(&Bytes::new());
            assert_eq!(empty.exchange.exact, Some(0));
            assert!(!empty.is_end_stream());
            assert_eq!(empty.exchange.hand_over(Duration::from_secs(60)), Ok(true));
            assert!(empty.is_end_stream());
            assert!(empty.exchange.lock().ended);
        });
    }

    /// A channel body that reaches its end after the hand-over is framed as a
    /// stream: hyper learns of the end from its first poll.
    #[test]
    fn a_body_closed_after_the_hand_over_ends_at_the_first_poll() {
        rt::block_on(async {
            let (mut end, mut writer) = channel();
            assert_eq!(end.exchange.hand_over(Duration::from_secs(60)), Ok(false));
            end.exchange.lock().closed = true;
            assert!(!end.is_end_stream());
            assert!(next_frame(&mut end).await.is_none());
            assert!(end.is_end_stream());
            futures_lite::io::AsyncWriteExt::close(&mut writer)
                .await
                .unwrap();
        });
    }

    /// The timer of a window whose end moves later is armed once, fires at
    /// the first end, and is armed again for the rest alone.
    #[test]
    fn a_lazy_timer_fires_at_the_end_it_was_moved_to() {
        rt::block_on(async {
            let mut timer = None;
            let started = Instant::now();
            let mut end = started + Duration::from_millis(50);
            std::future::poll_fn(|cx| {
                if poll_until(&mut timer, cx, end).is_pending() {
                    end = started + Duration::from_millis(120);
                    return Poll::Pending;
                }
                Poll::Ready(())
            })
            .await;
            assert!(started.elapsed() >= Duration::from_millis(120));
            assert!(timer.is_none());
        });
    }

    /// A bearer token holds the token68 syntax, and a refusal names no part
    /// of it. The bearer and the Basic values are marked sensitive, and so is
    /// a credential header the caller sets.
    #[test]
    fn a_bearer_token_is_token68_and_sensitive() {
        let token = |token: &str| BearerToken {
            token: token.to_owned(),
        };
        for good in ["abc", "a-b.c_d~e+f/g", "dGVzdA==", "x="] {
            assert!(is_token68(good), "{good}");
        }
        for bad in ["", "=", "a b", "a=b", "tök", "a\n", "a,b"] {
            assert!(!is_token68(bad), "{bad:?}");
            let err = merge_headers(&[], &[], None, Some(&token(bad))).unwrap_err();
            let message = err.to_string();
            assert!(message.contains("token68"), "{message}");
            assert!(!message.contains("  "), "{message}");
            if !bad.is_empty() && bad != "=" {
                assert!(!message.contains(bad), "{message}");
            }
        }

        let authorization = |headers: &[(HeaderName, HeaderValue)]| {
            headers
                .iter()
                .find(|(name, _)| *name == hyper::header::AUTHORIZATION)
                .map(|(_, value)| value.clone())
                .unwrap()
        };
        let merged = merge_headers(&[], &[], None, Some(&token("t0k3n"))).unwrap();
        let value = authorization(&merged);
        assert_eq!(value, "Bearer t0k3n");
        assert!(value.is_sensitive());

        let auth = BasicAuth {
            user: "u".into(),
            password: "p".into(),
        };
        let merged = merge_headers(&[], &[], Some(&auth), None).unwrap();
        assert!(authorization(&merged).is_sensitive());
        let header = vec![("authorization".to_owned(), "Custom x".to_owned())];
        let merged = merge_headers(&[], &header, None, None).unwrap();
        assert!(authorization(&merged).is_sensitive());

        let err = merge_headers(&[], &[], Some(&auth), Some(&token("t"))).unwrap_err();
        assert!(err.to_string().contains("pass one of them"), "{err}");
        assert!(err.to_string().contains("bearer token"), "{err}");
        let err = merge_headers(&[], &header, None, Some(&token("t"))).unwrap_err();
        assert!(err.to_string().contains("pass one of them"), "{err}");
        assert!(err.to_string().contains("bearer token"), "{err}");
    }

    #[test]
    fn bearer_token_debug_holds_no_token() {
        let token = BearerToken {
            token: "s3cret".into(),
        };
        let rendered = format!("{token:?}");
        assert!(!rendered.contains("s3cret"), "{rendered}");
        assert!(rendered.contains("redacted"), "{rendered}");
    }

    /// The bearer token of the fetcher is one sensitive `Authorization` entry
    /// in the header list. A request without a credential sends it. Each of
    /// these replaces it for one request: the `basic_auth` of the request, the
    /// `bearer_token` of the request, or an `Authorization` header of the
    /// request.
    #[test]
    fn a_fetcher_bearer_token_joins_the_header_list() {
        let options = FetcherOptions {
            bearer_token: Some(BearerToken {
                token: "t0k3n".into(),
            }),
            ..tls_options("https://example.com")
        };
        let fetcher = rt::block_on(Fetcher::new(options)).unwrap();
        let authorization = |headers: &[(HeaderName, HeaderValue)]| {
            headers
                .iter()
                .filter(|(name, _)| *name == hyper::header::AUTHORIZATION)
                .map(|(_, value)| value.clone())
                .collect::<Vec<_>>()
        };
        let held = authorization(&fetcher.inner.headers);
        assert_eq!(held, ["Bearer t0k3n"]);
        assert!(held[0].is_sensitive());

        let extra = vec![("x-trace".to_owned(), "abc".to_owned())];
        let merged = merge_headers(&fetcher.inner.headers, &extra, None, None).unwrap();
        assert_eq!(authorization(&merged), ["Bearer t0k3n"]);

        // base64("u:p")
        let auth = BasicAuth {
            user: "u".into(),
            password: "p".into(),
        };
        let merged = merge_headers(&fetcher.inner.headers, &[], Some(&auth), None).unwrap();
        assert_eq!(authorization(&merged), ["Basic dTpw"]);
        let token = BearerToken {
            token: "other".into(),
        };
        let merged = merge_headers(&fetcher.inner.headers, &[], None, Some(&token)).unwrap();
        assert_eq!(authorization(&merged), ["Bearer other"]);
        let header = vec![("authorization".to_owned(), "Custom x".to_owned())];
        let merged = merge_headers(&fetcher.inner.headers, &header, None, None).unwrap();
        assert_eq!(authorization(&merged), ["Custom x"]);
    }

    /// The constructor refuses the bearer token of the fetcher beside another
    /// value for `Authorization`, and a token outside the token68 syntax. A
    /// refusal names no part of the token.
    #[test]
    fn a_fetcher_bearer_token_is_refused_beside_another_credential_or_out_of_syntax() {
        let token = |token: &str| {
            Some(BearerToken {
                token: token.to_owned(),
            })
        };
        let beside_basic = FetcherOptions {
            bearer_token: token("t0k3n"),
            basic_auth: Some(BasicAuth {
                user: "u".into(),
                password: "p".into(),
            }),
            ..tls_options("https://example.com")
        };
        let beside_header = FetcherOptions {
            bearer_token: token("t0k3n"),
            headers: vec![("authorization".into(), "Basic aaa".into())],
            ..tls_options("https://example.com")
        };
        for options in [beside_basic, beside_header] {
            let err = rt::block_on(Fetcher::new(options)).unwrap_err();
            let message = err.to_string();
            assert!(message.contains("bearer token"), "{message}");
            assert!(message.contains("pass one of them"), "{message}");
        }

        let options = FetcherOptions {
            bearer_token: token("not a token"),
            ..tls_options("https://example.com")
        };
        let err = rt::block_on(Fetcher::new(options)).unwrap_err();
        assert!(matches!(err, Error::Fetch(_)), "{err}");
        let message = err.to_string();
        assert!(message.contains("token68"), "{message}");
        assert!(!message.contains("not a token"), "{message}");
    }

    /// A credential of the fetcher fails the constructor beside a cleartext
    /// mirror. The cleartext permission of the fetcher admits each kind of
    /// credential.
    #[test]
    fn the_fetcher_cleartext_permission_admits_a_cleartext_mirror() {
        let cleartext = "http://cleartext.example/repo";
        let token = || {
            Some(BearerToken {
                token: "t0k3n".into(),
            })
        };
        let options = FetcherOptions {
            bearer_token: token(),
            ..tls_options(cleartext)
        };
        let err = rt::block_on(Fetcher::new(options)).unwrap_err();
        let message = err.to_string();
        assert!(message.contains("bearer token"), "{message}");
        assert!(message.contains(cleartext), "{message}");
        assert!(
            message.contains("FetcherOptions::allow_cleartext_credentials"),
            "{message}"
        );
        assert!(!message.contains("t0k3n"), "{message}");

        let options = FetcherOptions {
            bearer_token: token(),
            allow_cleartext_credentials: true,
            ..tls_options(cleartext)
        };
        let fetcher = rt::block_on(Fetcher::new(options)).unwrap();
        assert!(
            fetcher.inner.headers.iter().any(
                |(name, value)| name == hyper::header::AUTHORIZATION && value == "Bearer t0k3n"
            )
        );

        let options = FetcherOptions {
            basic_auth: Some(BasicAuth {
                user: "u".into(),
                password: "p".into(),
            }),
            allow_cleartext_credentials: true,
            ..tls_options(cleartext)
        };
        rt::block_on(Fetcher::new(options)).unwrap();
        let options = FetcherOptions {
            headers: vec![("cookie".into(), "session=1".into())],
            allow_cleartext_credentials: true,
            ..tls_options(cleartext)
        };
        rt::block_on(Fetcher::new(options)).unwrap();
    }

    /// A signer that returns one fixed header, or fails if it holds no header.
    #[derive(Debug)]
    struct FixedSigner {
        header: Option<(String, String)>,
    }

    impl RequestSigner for FixedSigner {
        fn sign(&self, _request: &SignRequest<'_>) -> Result<Vec<(String, String)>> {
            match &self.header {
                Some(header) => Ok(vec![header.clone()]),
                None => Err(Error::Fetch("the signer has no key".into())),
            }
        }
    }

    fn fixed_signer() -> Option<Arc<dyn RequestSigner>> {
        Some(Arc::new(FixedSigner {
            header: Some(("authorization".into(), "Fixed x".into())),
        }))
    }

    /// The constructor refuses a signer beside another value for
    /// `Authorization`. A signer is a credential, so a cleartext mirror fails
    /// the constructor unless the fetcher permits it.
    #[test]
    fn a_signer_is_refused_beside_basic_auth_a_bearer_token_or_an_authorization_header() {
        let beside_basic = FetcherOptions {
            signer: fixed_signer(),
            basic_auth: Some(BasicAuth {
                user: "u".into(),
                password: "p".into(),
            }),
            ..tls_options("https://example.com")
        };
        let beside_bearer = FetcherOptions {
            signer: fixed_signer(),
            bearer_token: Some(BearerToken {
                token: "t0k3n".into(),
            }),
            ..tls_options("https://example.com")
        };
        let beside_header = FetcherOptions {
            signer: fixed_signer(),
            headers: vec![("Authorization".into(), "Basic aaa".into())],
            ..tls_options("https://example.com")
        };
        for (options, other) in [
            (beside_basic, "basic-auth credentials"),
            (beside_bearer, "a bearer token"),
            (beside_header, "an authorization header"),
        ] {
            let err = rt::block_on(Fetcher::new(options)).unwrap_err();
            assert!(matches!(err, Error::Fetch(_)), "{err}");
            let message = err.to_string();
            assert!(message.contains("request signer"), "{message}");
            assert!(message.contains(other), "{message}");
            assert!(message.contains("pass one of them"), "{message}");
        }

        // Another header goes beside the signer.
        let options = FetcherOptions {
            signer: fixed_signer(),
            headers: vec![("x-trace".into(), "abc".into())],
            ..tls_options("https://example.com")
        };
        rt::block_on(Fetcher::new(options)).unwrap();

        let cleartext = "http://cleartext.example/repo";
        let options = FetcherOptions {
            signer: fixed_signer(),
            ..tls_options(cleartext)
        };
        let err = rt::block_on(Fetcher::new(options)).unwrap_err();
        let message = err.to_string();
        assert!(message.contains("request signer"), "{message}");
        assert!(message.contains(cleartext), "{message}");
        assert!(
            message.contains("FetcherOptions::allow_cleartext_credentials"),
            "{message}"
        );
        let options = FetcherOptions {
            signer: fixed_signer(),
            allow_cleartext_credentials: true,
            ..tls_options(cleartext)
        };
        rt::block_on(Fetcher::new(options)).unwrap();
    }

    /// The request that a signer gets names the method, the authority of the
    /// `Host` header, and the path and the query of the request target.
    #[test]
    fn a_sign_request_carries_the_wire_authority_path_and_query() {
        let get = Method::GET;
        let put = Method::PUT;

        // A mirror with a base path and the default port of its scheme.
        let mirror = parse_mirror("https://Example.com:443/base/repo/").unwrap();
        let hop = mirror.destination("objects/ab/cd.file", None);
        let request = sign_request(&get, &hop, SignPayload::Empty);
        assert_eq!(request.method, "GET");
        assert_eq!(request.authority, "Example.com");
        assert_eq!(request.authority, hop.authority.to_str().unwrap());
        assert_eq!(request.path, "/base/repo/objects/ab/cd.file");
        assert_eq!(request.query, None);
        assert_eq!(request.payload, SignPayload::Empty);

        // A port other than the default, and a query.
        let mirror = parse_mirror("http://example.com:8080").unwrap();
        let hop = mirror.destination("summary", Some("x-id=1&b=%2F"));
        let request = sign_request(&get, &hop, SignPayload::Empty);
        assert_eq!(request.authority, "example.com:8080");
        assert_eq!(request.path, "/summary");
        assert_eq!(request.query, Some("x-id=1&b=%2F"));

        // An IPv6 literal keeps its brackets. An empty query is not `None`.
        let body = b"abc";
        let hop = parse_url("https://[::1]:8443/a/b?").unwrap();
        let request = sign_request(&put, &hop, SignPayload::Bytes(body));
        assert_eq!(request.method, "PUT");
        assert_eq!(request.authority, "[::1]:8443");
        assert_eq!(request.path, "/a/b");
        assert_eq!(request.query, Some(""));
        assert_eq!(request.payload, SignPayload::Bytes(b"abc"));
        assert_eq!(format!("{:?}", request.payload), "Bytes(3)");

        let hop = parse_url("https://[::1]/").unwrap();
        let request = sign_request(&put, &hop, SignPayload::Streamed);
        assert_eq!(request.authority, "[::1]");
        assert_eq!(request.path, "/");
        assert_eq!(request.query, None);
    }

    /// A signer that gives each `+` of a request path as `%2B`, as a signer of
    /// a canonical path does.
    #[derive(Debug)]
    struct PlusSigner;

    impl RequestSigner for PlusSigner {
        fn sign(&self, _request: &SignRequest<'_>) -> Result<Vec<(String, String)>> {
            Ok(Vec::new())
        }

        fn request_path<'a>(&self, path: &'a str) -> Result<Cow<'a, str>> {
            Ok(Cow::Owned(path.replace('+', "%2B")))
        }
    }

    /// The path of a path target goes through the signer, and the request
    /// target carries the form that the signer gives. The query does not.
    /// With no signer, or with a signer that keeps the default method, the
    /// path goes as written. A URL target goes as written with each signer.
    #[test]
    fn a_signer_gives_the_form_of_a_request_path() {
        rt::block_on(async {
            let cases: [(Option<Arc<dyn RequestSigner>>, &str); 3] = [
                (None, "/r/deltas/a+/b"),
                (fixed_signer(), "/r/deltas/a+/b"),
                (Some(Arc::new(PlusSigner)), "/r/deltas/a%2B/b"),
            ];
            for (signer, want) in cases {
                let fetcher = Fetcher::new(FetcherOptions {
                    signer,
                    allow_cleartext_credentials: true,
                    ..direct_options("http://example.com/r")
                })
                .await
                .unwrap();
                let targets = [
                    (Target::Path("deltas/a+/b"), want.to_owned()),
                    (
                        Target::PathQuery {
                            path: "deltas/a+/b",
                            query: "x=a+",
                        },
                        format!("{want}?x=a+"),
                    ),
                ];
                for (target, wire) in targets {
                    let Route::Mirrors { path, query } = fetcher.route(target).unwrap() else {
                        panic!("a path target goes to the mirrors");
                    };
                    let destination = fetcher.inner.mirrors[0].destination(&path, query);
                    let request = fetcher
                        .build_request(
                            &destination,
                            &FetchRequest::for_target(target),
                            &fetcher.inner.headers,
                            Protocol::Http11,
                            Via::Direct,
                        )
                        .unwrap();
                    assert_eq!(request.uri(), wire.as_str(), "{target:?}");
                }
                let Route::One(destination) = fetcher
                    .route(Target::Url("http://example.com/r/deltas/a+/b"))
                    .unwrap()
                else {
                    panic!("a url target names one destination");
                };
                assert_eq!(destination.target(), "/r/deltas/a+/b");
            }
        });
    }

    /// The headers of a signer replace the headers of the same name, and each
    /// of their values is marked sensitive, whatever its name is. A refused
    /// header and an error of the signer end the request.
    #[test]
    fn signed_headers_replace_their_names_and_failures_are_final() {
        let fetcher = rt::block_on(Fetcher::new(FetcherOptions {
            headers: vec![("x-amz-date".into(), "old".into())],
            ..tls_options("https://example.com")
        }))
        .unwrap();
        let hop = parse_url("https://example.com/x").unwrap();
        let signer = FixedSigner {
            header: Some(("x-amz-date".into(), "new".into())),
        };
        let merged = signed(
            &signer,
            &Method::GET,
            &hop,
            SignPayload::Empty,
            &fetcher.inner.headers,
        )
        .ok()
        .unwrap();
        let dates = merged
            .iter()
            .filter(|(name, _)| name == "x-amz-date")
            .map(|(_, value)| value.to_str().unwrap())
            .collect::<Vec<_>>();
        assert_eq!(dates, ["new"]);
        for (name, value) in merged.iter() {
            assert_eq!(value.is_sensitive(), name == "x-amz-date", "{name}");
        }

        // A header that is not a credential by its name is sensitive too, so
        // an HTTP/2 connection never indexes a session token.
        let signer = FixedSigner {
            header: Some(("X-Amz-Security-Token".into(), "session".into())),
        };
        let merged = signed(&signer, &Method::GET, &hop, SignPayload::Empty, &[])
            .ok()
            .unwrap();
        assert_eq!(merged.len(), 1);
        assert!(merged[0].1.is_sensitive());

        let signer = FixedSigner {
            header: Some(("authorization".into(), "Fixed x".into())),
        };
        let merged = signed(&signer, &Method::GET, &hop, SignPayload::Empty, &[])
            .ok()
            .unwrap();
        assert!(merged[0].1.is_sensitive());

        for signer in [
            FixedSigner { header: None },
            FixedSigner {
                header: Some(("host".into(), "elsewhere".into())),
            },
        ] {
            let failure = signed(&signer, &Method::GET, &hop, SignPayload::Empty, &[]);
            assert!(matches!(failure, Err(Failure::Final(Error::Fetch(_)))));
        }
    }

    #[test]
    fn fetcher_options_debug_holds_no_bearer_token() {
        let options = FetcherOptions {
            bearer_token: Some(BearerToken {
                token: "s3cret".into(),
            }),
            ..FetcherOptions::default()
        };
        let rendered = format!("{options:?}");
        assert!(!rendered.contains("s3cret"), "{rendered}");
        assert!(rendered.contains("redacted"), "{rendered}");
    }

    /// Headers whose values hold the marker `QXZ`, which no `Debug` output
    /// shows.
    fn secret_headers() -> Vec<(String, String)> {
        vec![
            ("Authorization".into(), "Basic val-QXZ-1".into()),
            ("proxy-authorization".into(), "Basic val-QXZ-2".into()),
            ("cookie".into(), "id=val-QXZ-3".into()),
            ("X-Trace".into(), "val-QXZ-4".into()),
        ]
    }

    /// The `Debug` output of the headers of [`secret_headers`].
    const SECRET_HEADERS_SHOWN: &str = r#"[("Authorization", "<redacted>"), ("proxy-authorization", "<redacted>"), ("cookie", "<redacted>"), ("X-Trace", "<redacted>")]"#;

    /// Checks that `rendered` shows each header of [`secret_headers`] with
    /// `<redacted>`, and no header value.
    fn assert_no_header_value(rendered: &str) {
        assert!(!rendered.contains("QXZ"), "{rendered}");
        for (name, _) in secret_headers() {
            let entry = format!(r#"("{name}", "<redacted>")"#);
            assert!(rendered.contains(&entry), "{rendered}");
        }
    }

    /// The `Debug` output of the options shows each header name and no header
    /// value. Each other field shows as its own `Debug` shows it, in the order
    /// of declaration.
    #[test]
    fn fetcher_options_debug_holds_no_header_value() {
        let options = FetcherOptions {
            headers: secret_headers(),
            ..FetcherOptions::new("https://example.com/")
        };
        let rendered = format!("{options:?}");
        assert_no_header_value(&rendered);
        assert_eq!(
            rendered,
            format!(
                "FetcherOptions {{ mirrors: [\"https://example.com/\"], \
                 headers: {SECRET_HEADERS_SHOWN}, basic_auth: None, bearer_token: None, \
                 signer: None, allow_cleartext_credentials: false, \
                 tls: TlsOptions {{ roots: System, client_identity: None }}, \
                 proxy: Environment, http2: true, max_retries: 5, max_redirects: 10, \
                 max_outstanding: 8, connect_timeout: 30s, progress_timeout: 60s, \
                 low_speed: None, fetch_timeout: Some(300s) }}"
            )
        );
    }

    /// The `Debug` output of a fetch request shows each header name and no
    /// header value. Each other field shows as its own `Debug` shows it.
    #[test]
    fn fetch_request_debug_holds_no_header_value() {
        let headers = secret_headers();
        let request = FetchRequest {
            headers: &headers,
            max_error_body: Some(4096),
            ..FetchRequest::path("summary")
        };
        let rendered = format!("{request:?}");
        assert_no_header_value(&rendered);
        assert_eq!(
            rendered,
            format!(
                "FetchRequest {{ target: Path(\"summary\"), priority: Normal, \
                 validators: None, max_size: None, headers: {SECRET_HEADERS_SHOWN}, \
                 basic_auth: None, allow_cleartext_credentials: false, \
                 max_error_body: Some(4096) }}"
            )
        );
    }

    /// The `Debug` output of a kept error response shows the name of each
    /// header and the length of the body. It shows no header value and no
    /// byte of the body, in the response and in the error that carries it.
    #[test]
    fn an_error_response_debug_holds_no_header_value_and_no_body() {
        let mut headers = hyper::HeaderMap::new();
        headers.insert(
            hyper::header::SET_COOKIE,
            HeaderValue::from_static("session=QXZ"),
        );
        let response = ErrorResponse::new(headers, Some(b"SECRETBODY".to_vec()));
        assert_eq!(
            format!("{response:?}"),
            "ErrorResponse { headers: [(\"set-cookie\", \"<redacted>\")], body: Some(10) }"
        );
        let error = Error::HttpStatus {
            status: 403,
            url: "https://h/x".into(),
            response: Some(Box::new(response)),
        };
        let rendered = format!("{error:?}");
        assert!(!rendered.contains("QXZ"), "{rendered}");
        assert!(!rendered.contains("SECRETBODY"), "{rendered}");
        assert_eq!(error.to_string(), "http status 403 for https://h/x");
    }

    /// An error response gives back the headers and the body that it was
    /// built from.
    #[test]
    fn an_error_response_keeps_its_parts() {
        let mut headers = hyper::HeaderMap::new();
        headers.insert("x-amz-bucket-region", HeaderValue::from_static("eu-west-1"));
        let response = ErrorResponse::new(headers.clone(), Some(b"<Error/>".to_vec()));
        assert_eq!(response.headers(), &headers);
        assert_eq!(response.body(), Some(&b"<Error/>"[..]));
        let headless = ErrorResponse::new(hyper::HeaderMap::new(), None);
        assert!(headless.headers().is_empty());
        assert_eq!(headless.body(), None);
    }

    /// A body of bytes has a copy that shares its buffer. A streamed body has
    /// no copy.
    #[test]
    fn a_bytes_body_clones_and_a_channel_body_does_not() {
        let body = UploadBody::bytes(b"part".to_vec());
        let copy = body.try_clone().expect("a bytes body has a copy");
        let (UploadForm::Bytes { bytes: first, .. }, UploadForm::Bytes { bytes: second, .. }) =
            (&body.form, &copy.form)
        else {
            panic!("the copy of a bytes body is a bytes body");
        };
        assert_eq!(first, second);
        assert_eq!(first.as_ptr(), second.as_ptr());
        let (channel, _writer) = UploadBody::channel();
        assert!(channel.try_clone().is_none());
    }

    /// A body of bytes gives its buffer back when no copy shares it. A
    /// shared body and a streamed body come back unchanged.
    #[test]
    fn a_bytes_body_gives_its_buffer_back_when_it_is_not_shared() {
        // A vector with spare capacity, and a vector with none.
        for mut bytes in [Vec::with_capacity(16), Vec::with_capacity(4)] {
            bytes.extend_from_slice(b"part");
            let (address, capacity) = (bytes.as_ptr(), bytes.capacity());
            let body = UploadBody::bytes(bytes);
            let copy = body.try_clone().expect("a bytes body has a copy");
            let Err(body) = body.into_vec() else {
                panic!("a shared body is not given back");
            };
            assert_eq!(body.as_bytes(), Some(&b"part"[..]));
            drop(copy);
            let Ok(bytes) = body.into_vec() else {
                panic!("a body with no copy gives its buffer back");
            };
            assert_eq!(bytes, b"part");
            assert_eq!(bytes.as_ptr(), address);
            assert_eq!(bytes.capacity(), capacity);
        }
        let (channel, _writer) = UploadBody::channel();
        let Err(channel) = channel.into_vec() else {
            panic!("a streamed body has no buffer");
        };
        assert!(channel.as_bytes().is_none());
    }

    /// A body with a digest keeps the digest in a copy and in a body that
    /// is not given back. The buffer that comes back holds no digest. The
    /// `Debug` output of its payload shows the length of the body.
    #[test]
    fn a_body_with_a_digest_keeps_it_in_a_copy_and_gives_the_buffer_back() {
        let digest = [7u8; 32];
        let mut bytes = Vec::with_capacity(16);
        bytes.extend_from_slice(b"part");
        let (address, capacity) = (bytes.as_ptr(), bytes.capacity());
        let body = UploadBody::bytes_with_sha256(bytes, digest);
        assert_eq!(body.sha256(), Some(digest));
        assert_eq!(body.as_bytes(), Some(&b"part"[..]));
        assert_eq!(format!("{body:?}"), "Bytes(4)");
        let copy = body.try_clone().expect("a bytes body has a copy");
        assert_eq!(copy.sha256(), Some(digest));
        assert_eq!(copy.as_bytes(), Some(&b"part"[..]));
        let Err(body) = body.into_vec() else {
            panic!("a shared body is not given back");
        };
        assert_eq!(body.sha256(), Some(digest));
        drop(copy);
        let Ok(bytes) = body.into_vec() else {
            panic!("a body with no copy gives its buffer back");
        };
        assert_eq!(bytes, b"part");
        assert_eq!(bytes.as_ptr(), address);
        assert_eq!(bytes.capacity(), capacity);
        assert_eq!(UploadBody::bytes(bytes).sha256(), None);
        let (channel, _writer) = UploadBody::channel();
        assert_eq!(channel.sha256(), None);

        let payload = SignPayload::Sha256 {
            bytes: b"abc",
            sha256: digest,
        };
        assert_eq!(format!("{payload:?}"), "Sha256(3)");
        assert_ne!(payload, SignPayload::Bytes(b"abc"));
    }

    /// The `Debug` output of an upload request shows each header name and no
    /// header value. Each other field shows as its own `Debug` shows it.
    #[test]
    fn upload_request_debug_holds_no_header_value() {
        let headers = secret_headers();
        let request = UploadRequest {
            headers: &headers,
            ..UploadRequest::path("objects", UploadBody::bytes(Vec::new()))
        };
        let rendered = format!("{request:?}");
        assert_no_header_value(&rendered);
        assert_eq!(
            rendered,
            format!(
                "UploadRequest {{ target: Path(\"objects\"), method: Post, body: Bytes(0), \
                 priority: Normal, max_response: 2097152, headers: {SECRET_HEADERS_SHOWN}, \
                 basic_auth: None, bearer_token: None, allow_cleartext_credentials: false, \
                 response_timeout: None }}"
            )
        );
    }

    /// Starts a raw socket peer that answers each request with fixed headers,
    /// and gives a fetcher with the peer as its mirror, and the port of the
    /// peer.
    ///
    /// Each header value holds the marker `QXZ`.
    async fn fixed_header_peer() -> (Fetcher, u16) {
        use futures_lite::io::{AsyncReadExt, AsyncWriteExt};

        let peer = rt::TcpListener::bind("127.0.0.1:0".parse().unwrap())
            .await
            .unwrap();
        let port = peer.local_addr().unwrap().port();
        drop(rt::spawn(async move {
            while let Ok((mut stream, _peer)) = peer.accept().await {
                let mut request = [0u8; 1024];
                let _ = stream.read(&mut request).await;
                let answer = "HTTP/1.1 200 OK\r\nSet-Cookie: a=val-QXZ-5\r\n\
                              Set-Cookie: b=val-QXZ-6\r\nContent-Type: text/val-QXZ-7\r\n\
                              Content-Length: 0\r\n\r\n";
                let _ = stream.write_all(answer.as_bytes()).await;
                let _ = stream.flush().await;
            }
        }));

        // With no redirect and a cleartext mirror, the fetcher reads no system
        // trust store.
        let fetcher = Fetcher::new(FetcherOptions {
            max_redirects: 0,
            ..direct_options(format!("http://127.0.0.1:{port}"))
        })
        .await
        .unwrap();
        (fetcher, port)
    }

    /// The `Debug` output of an upload answer shows the name of each response
    /// header, once for each value, and no header value.
    #[test]
    fn uploaded_debug_holds_no_header_value() {
        rt::block_on(async {
            let (fetcher, port) = fixed_header_peer().await;
            let uploaded = within(
                Duration::from_secs(5),
                fetcher.upload(UploadRequest::path(
                    "objects",
                    UploadBody::bytes(Vec::new()),
                )),
            )
            .await
            .expect("the upload ends within the limit")
            .unwrap();
            let rendered = format!("{uploaded:?}");
            assert!(!rendered.contains("QXZ"), "{rendered}");
            let headers = r#"[("set-cookie", "<redacted>"), ("set-cookie", "<redacted>"), ("content-type", "<redacted>"), ("content-length", "<redacted>")]"#;
            assert_eq!(
                rendered,
                format!(
                    "Uploaded {{ status: 200, headers: {headers}, \
                     url: \"http://127.0.0.1:{port}/objects\", body: Body {{ \
                     protocol: Http11, content_length: Some(0), received: 0, .. }} }}"
                )
            );
        });
    }

    /// The `Debug` output of an upload answer shows the host and the path of
    /// its URL, and `<redacted>` in place of the query.
    #[test]
    fn uploaded_debug_holds_no_url_query() {
        rt::block_on(async {
            let (fetcher, port) = fixed_header_peer().await;
            let url = format!("http://127.0.0.1:{port}/objects?X-Amz-Signature=sig-QXZ");
            let uploaded = within(
                Duration::from_secs(5),
                fetcher.upload(UploadRequest::url(&url, UploadBody::bytes(Vec::new()))),
            )
            .await
            .expect("the upload ends within the limit")
            .unwrap();
            assert_eq!(uploaded.url(), url);
            let rendered = format!("{uploaded:?}");
            assert!(!rendered.contains("QXZ"), "{rendered}");
            assert!(
                rendered.contains(&format!(
                    "url: \"http://127.0.0.1:{port}/objects?<redacted>\""
                )),
                "{rendered}"
            );
        });
    }

    /// For a text with no `@`, `?`, and `#`, the `Debug` output of a URL and
    /// of a path is the `Debug` output of the `str`.
    #[test]
    fn redacted_url_debug_quotes_as_str_debug() {
        let texts = [
            "https://example.com/it's",
            "a\"b\\c",
            "line\nnext\ttab\u{7f}",
            "caf\u{e9}",
            "\u{301}start",
            "mid\u{301}dle",
            "zero\u{200b}width",
            "emoji \u{1f980}",
            "",
        ];
        for text in texts {
            let expected = format!("{text:?}");
            assert_eq!(format!("{:?}", RedactedUrl::url(text)), expected);
            assert_eq!(format!("{:?}", RedactedUrl::path(text)), expected);
        }
    }

    /// The `Debug` output of a URL removes the text up to the last `@` after
    /// the scheme, and shows `<redacted>` in place of a query and a fragment.
    /// A path keeps each `@`.
    #[test]
    fn redacted_url_debug_removes_userinfo_query_and_fragment() {
        let urls = [
            ("https://example.com/repo", "https://example.com/repo"),
            (
                "https://user:pw@example.com/repo",
                "https://example.com/repo",
            ),
            (
                "https://u:pa#ss@example.com/repo",
                "https://example.com/repo",
            ),
            (
                "https://u:pa?ss@example.com/repo",
                "https://example.com/repo",
            ),
            (
                "https://u:p/ss@example.com/repo",
                "https://example.com/repo",
            ),
            ("https://example.com/a@b/c", "https://b/c"),
            (
                "https://example.com/s?a=1",
                "https://example.com/s?<redacted>",
            ),
            (
                "https://example.com/s#f",
                "https://example.com/s#<redacted>",
            ),
            (
                "https://u:p@example.com/s?a=1#f",
                "https://example.com/s?<redacted>#<redacted>",
            ),
            ("https://example.com/s?", "https://example.com/s?<redacted>"),
            ("https://example.com/s#", "https://example.com/s#<redacted>"),
            (
                "https://example.com/s?#",
                "https://example.com/s?<redacted>#<redacted>",
            ),
            ("https://example.com/s?a=x@y&b=1", "https://y&b=1"),
            ("example.com/s?a=1", "example.com/s?<redacted>"),
            ("u:p@example.com/s", "example.com/s"),
            ("u:a://b@h/p", "h/p"),
            ("https://[::1]:8/p?q", "https://[::1]:8/p?<redacted>"),
            ("https://h/p#a?b", "https://h/p#<redacted>"),
            ("é@é?é#é", "é?<redacted>#<redacted>"),
        ];
        for (url, shown) in urls {
            assert_eq!(
                format!("{:?}", RedactedUrl::url(url)),
                format!("\"{shown}\"")
            );
        }
        let paths = [
            ("objects/ab", "objects/ab"),
            ("a@b/c", "a@b/c"),
            ("summary?sig=1", "summary?<redacted>"),
            ("summary#f", "summary#<redacted>"),
            ("summary?sig=1#f", "summary?<redacted>#<redacted>"),
        ];
        for (path, shown) in paths {
            assert_eq!(
                format!("{:?}", RedactedUrl::path(path)),
                format!("\"{shown}\"")
            );
        }
    }

    /// The `Debug` output of a target alone shows the layout of a tuple
    /// variant, with the URL rule.
    #[test]
    fn target_debug_redacts_url_and_path() {
        let url = Target::Url("https://u:pw-QXZ@h/p?t=QXZ#QXZ");
        let path = Target::Path("summary?sig=QXZ#QXZ");
        assert_eq!(
            format!("{url:?}"),
            r#"Url("https://h/p?<redacted>#<redacted>")"#
        );
        assert_eq!(
            format!("{path:?}"),
            r#"Path("summary?<redacted>#<redacted>")"#
        );
        assert_eq!(
            format!("{url:#?}"),
            "Url(\n    \"https://h/p?<redacted>#<redacted>\",\n)"
        );
        assert_eq!(
            format!("{path:#?}"),
            "Path(\n    \"summary?<redacted>#<redacted>\",\n)"
        );
        let path_query = Target::PathQuery {
            path: "p?QXZ",
            query: "pattern=QXZ",
        };
        assert_eq!(
            format!("{path_query:?}"),
            r#"PathQuery { path: "p?<redacted>", query: "<redacted>" }"#
        );
        let rendered = format!("{:?}", FetchRequest::path_query("refs/v1", "pattern=QXZ"));
        assert!(!rendered.contains("QXZ"), "{rendered}");
        assert!(
            rendered.contains(r#"target: PathQuery { path: "refs/v1", query: "<redacted>" }"#),
            "{rendered}"
        );
    }

    /// A URL that holds the marker `QXZ` in its userinfo, query, and fragment.
    const SECRET_URL: &str =
        "https://user-QXZ:pw-QXZ@example.com/repo?X-Amz-Signature=sig-QXZ#frag-QXZ";

    /// The `Debug` output of the options shows the host and the path of each
    /// mirror, and no userinfo, query, or fragment.
    #[test]
    fn fetcher_options_debug_holds_no_mirror_secret() {
        let options = FetcherOptions {
            mirrors: vec![SECRET_URL.into(), "https://mirror.example.com/os".into()],
            ..FetcherOptions::default()
        };
        let rendered = format!("{options:?}");
        assert!(!rendered.contains("QXZ"), "{rendered}");
        assert!(
            rendered.contains(
                r#"mirrors: ["https://example.com/repo?<redacted>#<redacted>", "https://mirror.example.com/os"]"#
            ),
            "{rendered}"
        );
    }

    /// The `Debug` output of a fetch request shows the host and the path of
    /// its target, and no userinfo, query, or fragment.
    #[test]
    fn fetch_request_debug_holds_no_target_secret() {
        let rendered = format!("{:?}", FetchRequest::url(SECRET_URL));
        assert!(!rendered.contains("QXZ"), "{rendered}");
        assert!(
            rendered.contains(r#"target: Url("https://example.com/repo?<redacted>#<redacted>")"#),
            "{rendered}"
        );
        let rendered = format!("{:?}", FetchRequest::path("summary?sig=QXZ#f"));
        assert!(!rendered.contains("QXZ"), "{rendered}");
        assert!(
            rendered.contains(r#"target: Path("summary?<redacted>#<redacted>")"#),
            "{rendered}"
        );
    }

    /// The `Debug` output of an upload request shows the host and the path of
    /// its target, and no userinfo, query, or fragment.
    #[test]
    fn upload_request_debug_holds_no_target_secret() {
        let request = UploadRequest::url(SECRET_URL, UploadBody::bytes(Vec::new()));
        let rendered = format!("{request:?}");
        assert!(!rendered.contains("QXZ"), "{rendered}");
        assert!(
            rendered.contains(r#"target: Url("https://example.com/repo?<redacted>#<redacted>")"#),
            "{rendered}"
        );
    }

    /// A kept error body with no declared length grows as its frames arrive,
    /// and its capacity never passes the cap. The first frame of 10,000 bytes
    /// and the frames of 16 KiB after it do not fall on the cap.
    #[test]
    fn a_kept_error_body_holds_no_capacity_past_the_cap() {
        use futures_lite::io::{AsyncReadExt, AsyncWriteExt};

        const CAP: usize = 100_000;
        rt::block_on(async {
            let peer = rt::TcpListener::bind("127.0.0.1:0".parse().unwrap())
                .await
                .unwrap();
            let port = peer.local_addr().unwrap().port();
            drop(rt::spawn(async move {
                while let Ok((mut stream, _peer)) = peer.accept().await {
                    let mut request = [0u8; 1024];
                    let _ = stream.read(&mut request).await;
                    let mut answer =
                        b"HTTP/1.1 404 Not Found\r\nTransfer-Encoding: chunked\r\n\r\n".to_vec();
                    let mut left = CAP - 1;
                    let mut size = 10_000;
                    while left > 0 {
                        let n = size.min(left);
                        answer.extend_from_slice(format!("{n:x}\r\n").as_bytes());
                        answer.resize(answer.len() + n, b'e');
                        answer.extend_from_slice(b"\r\n");
                        left -= n;
                        size = 16 * 1024;
                    }
                    answer.extend_from_slice(b"0\r\n\r\n");
                    for piece in answer.chunks(size) {
                        if stream.write_all(piece).await.is_err() || stream.flush().await.is_err() {
                            break;
                        }
                    }
                }
            }));
            let fetcher = Fetcher::new(direct_options(format!("http://127.0.0.1:{port}")))
                .await
                .unwrap();
            let request = FetchRequest {
                max_error_body: Some(CAP as u64),
                ..FetchRequest::path("config")
            };
            let err = within(Duration::from_secs(10), fetcher.fetch(request))
                .await
                .expect("the fetch ends within the limit")
                .unwrap_err();
            let Error::HttpStatus {
                status: 404,
                response: Some(response),
                ..
            } = err
            else {
                panic!("{err}");
            };
            let body = response.body.as_ref().expect("the body is kept");
            assert_eq!(body.len(), CAP - 1);
            assert!(body.capacity() <= CAP, "capacity {}", body.capacity());
        });
    }
}