ostrya-remote 0.14.0

The push and pull protocol of ostrya: the wire codec, the push client session, and the ssh and HTTP transports
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
//! The client of a bucket repository.

use std::collections::HashSet;
use std::io::ErrorKind;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::task::{Context, Poll};
use std::time::Duration;

use futures_io::AsyncRead;
use futures_lite::io::AsyncReadExt;
use ostrya_fetch::{
    Body, ErrorResponse, FetchRequest, Fetched, Fetcher, Target, UploadBody, UploadMethod,
    UploadRequest, Uploaded,
};
use quick_xml::escape::partial_escape;
use sha2::{Digest, Sha256};

use super::key::{
    check_key, check_list_prefix, decode_listed, encode_path, encode_query, is_repository_key,
};
use super::sigv4::valid_region;
use super::xml::{self, Doc, ErrorDoc, MAX_BODY, shown};
use super::{Location, S3Endpoint};
use crate::{Error, Result};

/// The largest listing body and error body that the client reads: the limit
/// of the XML reader, 4 MiB.
const XML_CAP: u64 = MAX_BODY as u64;

/// The largest number of truncated pages in a row, with no key and no common
/// prefix, that a list follows.
///
/// S3 can give a truncated page of `ListObjectsV2` with no key and no common
/// prefix, for example over many delete markers of a versioned bucket. The
/// list follows such a page if its continuation token is new since the last
/// page with a key or a common prefix. A run of more than this number of such
/// pages fails the list, so a server that gives empty pages with new tokens
/// and no end cannot keep the list open.
const MAX_EMPTY_PAGES: usize = 1000;

/// The largest body of one `PUT`: 5 GiB, the S3 limit. It is also the
/// largest part of a multipart upload.
const MAX_PUT: u64 = 5 * 1024 * 1024 * 1024;

/// One MiB.
const MIB: u64 = 1024 * 1024;

/// The smallest part size of [`Bucket::put_stream`]: 5 MiB, the S3 limit of
/// each part other than the last part.
const MIN_PART: u64 = 5 * MIB;

/// The smallest part size that [`part_size`] gives: 8 MiB.
const DEFAULT_PART: u64 = 8 * MIB;

/// The largest number of parts of one multipart upload: 10,000, the S3
/// limit.
const MAX_PARTS: u32 = 10_000;

/// The largest upload id and the largest `ETag` of a part that the client
/// takes: 1 KiB. So the `ETag` values of an upload and the document that
/// completes it have a limit.
const MAX_UPLOAD_TOKEN: usize = 1024;

/// The namespace of the S3 documents.
const S3_NAMESPACE: &str = "http://s3.amazonaws.com/doc/2006-03-01/";

/// The largest capacity that a whole read of a listing body or of an error
/// body reserves before the body arrives. A larger body grows the buffer as
/// it arrives.
const MAX_RESERVE: u64 = XML_CAP;

/// The capacity that a whole read of a body with no declared length reserves
/// before the body arrives: 8 KiB.
const FIRST_READ: u64 = 8 * 1024;

/// The size of the chunks in which the compare of a conditional `PUT` reads
/// the object of the key: 64 KiB.
const COMPARE_CHUNK: usize = 64 * 1024;

/// The size above which the client hashes a body on the blocking pool, once
/// before the first request: 1 MiB. The signer hashes a smaller body on the
/// task of each request.
const UNBLOCK_SIZE: usize = 1024 * 1024;

/// The client of a bucket repository.
///
/// [`Bucket::new`] opens it from an [`S3Endpoint`], and
/// [`Bucket::from_fetcher`] opens it over a fetcher that the caller built.
/// [`Bucket::counted`] gives a client whose fetcher adds byte counters.
/// Each method takes a key or a list prefix relative to the prefix of the
/// repository, and the client adds `PREFIX/`. A key in a result is relative
/// to the prefix of the repository too.
///
/// # Keys
///
/// The client refuses a key that is not a key of a bucket repository, before
/// it sends a request:
///
/// - `config`, `summary`, `summary.sig`, `extensions/ostrya/lock`, and
///   `extensions/ostrya/lock-probe`
/// - `objects/XX/<62 hex digits>.<ext>`, in lowercase hex, with the
///   extension `commit`, `commitmeta`, `dirtree`, `dirmeta`, `filez`, or
///   `tombstone-commit`
/// - `refs/heads/<name>`, with a ref name that
///   [`is_ref_name`](ostrya_core::is_ref_name) accepts
/// - `refs/mirrors/<id>/<name>`, with an id of at most 255 bytes that
///   [`is_collection_id`](ostrya_core::is_collection_id) accepts, and a ref
///   name that [`is_ref_name`](ostrya_core::is_ref_name) accepts
/// - `deltas/<2>/<41>/<target>` and `deltas/<2>/<41>-<43>/<target>`, with
///   checksums in modified base64, and the target `superblock` or a part
///   number with no leading zero
/// - `delta-indexes/<2>/<41>.index`.
///
/// A list prefix ends in `/`, so a list for `s3://b/repo` never gives the
/// keys of `s3://b/repo2`. Its segments are not empty and are not `.` or
/// `..`.
///
/// The request path of a key encodes each byte other than `A-Z`, `a-z`,
/// `0-9`, `-`, `.`, `_`, `~`, and `/` as `%XX`. So a `+` of a delta key goes
/// as `%2B`.
///
/// # Requests
///
/// The `GET` requests (get, get stream, the checks for an object, and the
/// lists) are fetches. The fetcher repeats a fetch after a transport failure
/// or a 408, 429, or 5xx, as [`Fetcher::fetch`] states, and fetches a failed
/// body of a whole read again. A check for an object is a `GET` with
/// `Range: bytes=0-0`, as [`exists`](Bucket::exists) states. The signature
/// does not cover the `Range` header.
///
/// The client sends a `PUT`, a `DELETE`, and the `POST` that starts a
/// multipart upload again after
/// [`Error::UploadInterrupted`](ostrya_fetch::Error::UploadInterrupted) and
/// after a 408, 429, or 5xx, at most
/// [`max_retries`](ostrya_fetch::FetcherOptions::max_retries) times. The
/// delay starts at 250 ms, doubles each time, and stops at 2 s.
/// [`put_if_absent`](Bucket::put_if_absent) and
/// [`delete_if_match`](Bucket::delete_if_match) state their own rules, and
/// [`put_stream`](Bucket::put_stream) states more rules for the `POST`
/// requests of a multipart upload.
///
/// The client follows no redirect. The body of each listing, of each answer
/// to a request of a multipart upload, and of each error response is at most
/// 4 MiB.
///
/// # Errors of the server
///
/// An unsuccessful status gives [`Error::S3`], with the code and the message
/// of the S3 `Error` document in the body. A response whose
/// `x-amz-bucket-region` header or whose `Region` element names a region
/// other than the region of the location gives [`Error::S3Region`]. An
/// absent key is not an error, whatever region its response names.
///
/// The body of an error response to a `PUT` or a `DELETE` that declares a
/// coding, or a length over 4 MiB, is not read. The status then gives
/// [`Error::S3`] with no code, and the client does not send a definitive
/// status again.
///
/// The [`Debug`] output shows the location and the mirror of the fetcher.
pub struct Bucket {
    fetcher: Fetcher,
    location: Location,
    max_retries: u32,
    /// The bucket URL, if each request names its URL. It is `None` if the
    /// request paths go under the one mirror of the fetcher, the bucket URL.
    base: Option<String>,
}

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

/// An object that [`Bucket::get`] read whole.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Object {
    /// The bytes of the object.
    pub bytes: Vec<u8>,
    /// The `ETag` of the response, as the server wrote it.
    pub etag: Option<String>,
}

/// The body of an object that [`Bucket::get_stream`] and
/// [`Bucket::get_stream_strict`] give.
///
/// A read gives the bytes in bounded chunks, as a [`Body`] does. The stream
/// holds an admission permit of the fetcher until it ends or is dropped. A
/// failure in transit fails the read, and the caller reads the object
/// again.
#[derive(Debug)]
pub struct ObjectStream {
    body: Body,
    etag: Option<String>,
}

impl ObjectStream {
    /// Returns the `ETag` of the response, as the server wrote it.
    pub fn etag(&self) -> Option<&str> {
        self.etag.as_deref()
    }

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

impl AsyncRead for ObjectStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        Pin::new(&mut self.body).poll_read(cx, buf)
    }
}

/// The read trait of tokio.
#[cfg(feature = "tokio")]
impl ostrya_rt::tokio_io::AsyncRead for ObjectStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ostrya_rt::tokio_io::ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        ostrya_rt::tokio_io::AsyncRead::poll_read(Pin::new(&mut self.body), cx, buf)
    }
}

/// The outcome of [`Bucket::put_if_absent`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PutIfAbsent {
    /// A request of this call wrote the object: the server answered it with
    /// a 2xx.
    Created {
        /// The `ETag` of the response, as the server wrote it.
        etag: Option<String>,
    },
    /// The key held an object, and the call wrote nothing. The server
    /// answered 412 to the first request, or a read of the key after a
    /// request with an unknown outcome found other bytes. Or a read after a
    /// 412 that followed a request with an unknown outcome found no object.
    Present,
    /// The key holds the bytes of the body, and no answer of the server
    /// states which writer put them there.
    ///
    /// A request of this call had an unknown outcome. Then a failed request
    /// followed, and a read of the key found the same bytes. A request of
    /// this call can have written them, or an earlier writer of the same
    /// bytes.
    Matches {
        /// The `ETag` of the read of the key, as the server wrote it.
        etag: Option<String>,
    },
    /// The key holds the bytes of the body, and the server answered 412 to
    /// a request of this call.
    ///
    /// A request of this call had an unknown outcome. Then a request got
    /// 412, and a read of the key found the same bytes. A request of this
    /// call can have written them, or an earlier writer of the same bytes.
    /// The 412 shows that the server honors `If-None-Match`.
    Refused {
        /// The `ETag` of the read of the key, as the server wrote it.
        etag: Option<String>,
    },
}

/// The outcome of [`Bucket::delete_if_match`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DeleteIfMatch {
    /// The key holds no object after the call. A request of the call
    /// deleted it, or the key was absent at a read after a request with an
    /// unknown outcome.
    Deleted,
    /// The key did not hold an object with the `ETag` at the first request:
    /// the server answered 412, or the key was absent. Or the read after a
    /// request with an unknown outcome found an object.
    NotMatched,
}

/// The keys and the common prefixes of a list.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Listing {
    /// The objects, in the order of the server.
    pub objects: Vec<Listed>,
    /// The common prefixes of a list with a delimiter, relative to the prefix
    /// of the repository, each with its trailing `/`.
    pub common_prefixes: Vec<String>,
}

/// One object of a [`Listing`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Listed {
    /// The key, relative to the prefix of the repository.
    ///
    /// The key is not checked. A key that is not a key of a bucket
    /// repository is a stray key.
    pub key: String,
    /// The size of the object in bytes.
    pub size: u64,
}

/// One incomplete multipart upload of a key of the repository.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Upload {
    /// The key, relative to the prefix of the repository.
    pub key: String,
    /// The upload id.
    pub upload_id: String,
    /// The `Initiated` time, as the server wrote it.
    pub initiated: String,
}

/// The answer to one upload request, read whole.
struct Reply {
    status: u16,
    etag: Option<String>,
    region: Option<String>,
    /// The body, or `None` if it is larger than 4 MiB or failed in transit.
    body: Option<Vec<u8>>,
}

/// The outcome of one upload request.
enum Sent {
    /// The server answered.
    Reply(Reply),
    /// The request failed after the fetcher sent it, so the outcome is not
    /// known.
    Interrupted(ostrya_fetch::Error),
}

/// What a key holds, compared with the body of a request.
enum Compared {
    /// The same bytes, with the `ETag` of the read.
    Same(Option<String>),
    /// Other bytes.
    Other,
    /// No object.
    Absent,
}

impl Bucket {
    /// Opens the client of the bucket repository of `endpoint`.
    ///
    /// The call builds the fetcher. It sends no request.
    /// [`S3Endpoint`] states the HTTP options that the client takes.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if [`SigV4::new`](super::SigV4::new) refuses
    ///   the region.
    /// - [`Error::Fetch`] if [`Fetcher::new`] refuses the options. An example
    ///   is an `http://` endpoint with
    ///   [`allow_cleartext_credentials`](ostrya_fetch::FetcherOptions::allow_cleartext_credentials)
    ///   set to `false`, because the signer is a credential. Another is
    ///   [`basic_auth`](ostrya_fetch::FetcherOptions::basic_auth), a bearer
    ///   token, or an `Authorization` header in the options.
    pub async fn new(endpoint: &S3Endpoint) -> Result<Bucket> {
        let location = endpoint.location.clone();
        let options = endpoint.signed_options(location.bucket_url())?;
        let fetcher = Fetcher::new(options).await.map_err(Error::Fetch)?;
        Ok(Bucket {
            fetcher,
            location,
            max_retries: endpoint.http.max_retries,
            base: None,
        })
    }

    /// Opens the client of the bucket repository of `endpoint` over
    /// `fetcher`.
    ///
    /// The call builds no fetcher and sends no request. The client shares
    /// the connection pool, the admission limit, and the counters of
    /// `fetcher`. The client names each request by its URL under the
    /// [`bucket_url`](Location::bucket_url) of the location, so it does not
    /// use the mirrors of `fetcher`. An example of `fetcher` is the fetcher
    /// of a pull, from the options that
    /// [`pull_options`](S3Endpoint::pull_options) gives, whose mirror is the
    /// [`repository_url`](Location::repository_url).
    ///
    /// The client takes the location and the retry count of `endpoint`.
    /// `fetcher` must sign each request with the credentials and the region
    /// of `endpoint`, and follow no redirect, as the fetcher of
    /// [`Bucket::new`] does. The call cannot check this.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ostrya_fetch::Fetcher;
    /// use ostrya_remote::s3::{Bucket, Environment, S3Endpoint, Settings};
    /// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
    /// let endpoint =
    ///     S3Endpoint::resolve("s3://builds/exampleos", &Settings::default(), &Environment::Process)
    ///         .await?;
    /// let fetcher = Fetcher::new(endpoint.pull_options()?).await?;
    /// let bucket = Bucket::from_fetcher(&endpoint, fetcher.clone());
    /// // The fetcher reads the objects under the repository URL. The client
    /// // reads `summary` with the S3 rules of an absent key and of an error.
    /// let summary = bucket.get("summary", 1 << 20).await?;
    /// # Ok(()) }
    /// ```
    pub fn from_fetcher(endpoint: &S3Endpoint, fetcher: Fetcher) -> Bucket {
        Bucket {
            fetcher,
            location: endpoint.location.clone(),
            max_retries: endpoint.http.max_retries,
            base: Some(endpoint.location.bucket_url()),
        }
    }

    /// Returns a client of the same bucket repository whose fetcher adds
    /// counters of received body bytes.
    ///
    /// The fetcher of the new client is [`Fetcher::counted`] of the fetcher
    /// of this client with `received`, so it has the counters of this client
    /// and the counters of `received`. [`Fetcher::counted`] states which
    /// bytes each counter gets. The new client has the location, the retry
    /// count, and the request URLs of this client. The counters of this
    /// client do not change.
    pub fn counted(&self, received: Vec<Arc<AtomicU64>>) -> Bucket {
        Bucket {
            fetcher: self.fetcher.counted(received),
            location: self.location.clone(),
            max_retries: self.max_retries,
            base: self.base.clone(),
        }
    }

    /// Returns the location of the bucket repository.
    pub fn location(&self) -> &Location {
        &self.location
    }

    /// Reads the object of `key` whole, or returns `None` if the key holds no
    /// object.
    ///
    /// A 404 is an absent key if its body arrived whole and holds the code
    /// `NoSuchKey`, or holds no S3 code: an empty body, or a body that is
    /// not an S3 `Error` document. A 404 with another code, for example
    /// `NoSuchBucket`, is an error, so a missing bucket never reads as an
    /// empty repository. A 404 whose body did not arrive is an error too,
    /// with no code. An absent key is not an error, whatever region its
    /// response names. [`get_strict`](Bucket::get_strict) reads a key by a
    /// strict rule.
    ///
    /// The bytes have the capacity of the object and one byte, if the
    /// response declares the length of the object.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository.
    /// - [`Error::LimitExceeded`] if the object is larger than `max_size`
    ///   bytes. The message names the key.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response.
    /// - [`Error::Transport`] if the server answers with 304. The request
    ///   sends no validators, so the answer cannot be used.
    /// - [`Error::Fetch`] for a failure of the fetcher, and [`Error::Io`] for
    ///   a body that failed in transit after the last refetch.
    pub async fn get(&self, key: &str, max_size: u64) -> Result<Option<Object>> {
        self.get_whole(key, max_size, is_absent).await
    }

    /// Reads the object of `key` whole, or returns `None` if the key holds no
    /// object, by a strict rule of an absent key.
    ///
    /// A 404 is an absent key only if its body arrived whole and holds the
    /// code `NoSuchKey`. Each other 404 is an error, also a 404 with an empty
    /// body or with a body that is not an S3 `Error` document, for example a
    /// page of a proxy. Use this call where a false absent key causes a loss
    /// of data. Else the call is [`get`](Bucket::get).
    ///
    /// # Errors
    ///
    /// The errors of [`get`](Bucket::get). A 404 that is not an absent key
    /// gives [`Error::S3`] with the status 404, and with no code if the body
    /// holds no S3 code.
    pub async fn get_strict(&self, key: &str, max_size: u64) -> Result<Option<Object>> {
        self.get_whole(key, max_size, is_no_such_key).await
    }

    /// Reads the object of `key` whole, as [`get`](Bucket::get) does, with
    /// `absent` as the rule of an absent key.
    async fn get_whole(
        &self,
        key: &str,
        max_size: u64,
        absent: fn(u16, Option<&[u8]>) -> bool,
    ) -> Result<Option<Object>> {
        check_key(key)?;
        let full = self.full_key(key);
        let path = encode_path(&full);
        let target = self.target(&path, "");
        let mut refetch = self.fetcher.refetching(FetchRequest {
            max_size: Some(max_size),
            max_error_body: Some(XML_CAP),
            ..target.fetch()
        });
        loop {
            let mut body = match refetch.fetch().await {
                Ok(Fetched::Body(body)) => body,
                Ok(Fetched::NotModified) => return Err(not_modified(&full)),
                Err(e) => {
                    self.absent_or_error(e, &full, max_size, absent)?;
                    return Ok(None);
                }
            };
            let etag = body.validators().etag.clone();
            let declared = body.content_length();
            // The fetcher refuses a declared length over `max_size`, so the
            // read reserves a declared length whole.
            match read_whole(&mut body, declared, max_size, max_size).await {
                Ok(bytes) => return Ok(Some(Object { bytes, etag })),
                Err(e) if e.kind() == ErrorKind::FileTooLarge => {
                    return Err(too_large(&full, max_size));
                }
                Err(e) => {
                    drop(body);
                    refetch.retry(e).await?;
                }
            }
        }
    }

    /// Opens the body of the object of `key` as a stream, or returns `None`
    /// if the key holds no object.
    ///
    /// The stream holds no more than one chunk of the object in memory.
    /// [`get`](Bucket::get) states the rule of an absent key.
    ///
    /// # Errors
    ///
    /// The errors of [`get`](Bucket::get), other than the size limit: also
    /// [`Error::Transport`] for a 304. A read of the stream fails with an I/O
    /// error if the body fails in transit.
    pub async fn get_stream(&self, key: &str) -> Result<Option<ObjectStream>> {
        self.open_stream(key, is_absent).await
    }

    /// Opens the body of the object of `key` as a stream, or returns `None`
    /// if the key holds no object, by the strict rule of an absent key of
    /// [`get_strict`](Bucket::get_strict).
    ///
    /// The stream holds no more than one chunk of the object in memory.
    ///
    /// # Errors
    ///
    /// The errors of [`get_stream`](Bucket::get_stream). A 404 that is not an
    /// absent key gives [`Error::S3`] with the status 404, and with no code
    /// if the body holds no S3 code.
    pub async fn get_stream_strict(&self, key: &str) -> Result<Option<ObjectStream>> {
        self.open_stream(key, is_no_such_key).await
    }

    /// Opens the body of the object of `key` as a stream, as
    /// [`get_stream`](Bucket::get_stream) does, with `absent` as the rule of
    /// an absent key.
    async fn open_stream(
        &self,
        key: &str,
        absent: fn(u16, Option<&[u8]>) -> bool,
    ) -> Result<Option<ObjectStream>> {
        check_key(key)?;
        let full = self.full_key(key);
        let path = encode_path(&full);
        let target = self.target(&path, "");
        let fetched = self
            .fetcher
            .fetch(FetchRequest {
                max_error_body: Some(XML_CAP),
                ..target.fetch()
            })
            .await;
        match fetched {
            Ok(Fetched::Body(body)) => {
                let etag = body.validators().etag.clone();
                Ok(Some(ObjectStream { body, etag }))
            }
            Ok(Fetched::NotModified) => Err(not_modified(&full)),
            Err(e) => {
                self.absent_or_error(e, &full, u64::MAX, absent)?;
                Ok(None)
            }
        }
    }

    /// Returns `true` if the key `key` holds an object.
    ///
    /// The request is `GET KEY` with `Range: bytes=0-0`, and the client reads
    /// at most one byte of the object. A 206 and a 200 are an object. A 416
    /// is an object too, because a server gives it for an object of zero
    /// bytes. A server that ignores the range answers 200 with the whole
    /// object. The client then refuses a declared length over one byte, and
    /// reads no byte of it. The fetcher reads a declared body of at most
    /// 64 KiB to its end, so the connection goes back to the pool. A larger
    /// body, or a body with no declared length, closes the connection.
    ///
    /// The client reads a body that declares a length of at most one byte to
    /// its end, so the connection goes back to the pool. If the body fails
    /// after the status, the status decides, and the key holds an object. The
    /// client does not read a body with no declared length, and its
    /// connection closes. So a server that stalls such a body does not hold
    /// the check until the progress timeout.
    ///
    /// [`get`](Bucket::get) states the rule of an absent key.
    /// [`exists_strict`](Bucket::exists_strict) reads a key by the strict
    /// rule.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response.
    /// - [`Error::Transport`] if the server answers with 304. The request
    ///   sends no validators, so the answer cannot be used.
    /// - [`Error::Fetch`] for a failure of the fetcher.
    pub async fn exists(&self, key: &str) -> Result<bool> {
        self.probe(key, is_absent).await
    }

    /// Returns `true` if the key `key` holds an object, by the strict rule of
    /// an absent key of [`get_strict`](Bucket::get_strict).
    ///
    /// [`exists`](Bucket::exists) states the request and the rules of an
    /// object.
    ///
    /// # Errors
    ///
    /// The errors of [`exists`](Bucket::exists). A 404 that is not an absent
    /// key gives [`Error::S3`] with the status 404, and with no code if the
    /// body holds no S3 code.
    pub async fn exists_strict(&self, key: &str) -> Result<bool> {
        self.probe(key, is_no_such_key).await
    }

    /// Returns `true` if the key `key` holds an object, as
    /// [`exists`](Bucket::exists) does, with `absent` as the rule of an
    /// absent key.
    async fn probe(&self, key: &str, absent: fn(u16, Option<&[u8]>) -> bool) -> Result<bool> {
        check_key(key)?;
        let full = self.full_key(key);
        let path = encode_path(&full);
        let target = self.target(&path, "");
        let range = [("Range".to_owned(), "bytes=0-0".to_owned())];
        let fetched = self
            .fetcher
            .fetch(FetchRequest {
                headers: &range,
                max_size: Some(1),
                max_error_body: Some(XML_CAP),
                ..target.fetch()
            })
            .await;
        match fetched {
            Ok(Fetched::Body(mut body)) => {
                // The end of a declared body returns the connection to the
                // pool. A failure drops the connection, and the status still
                // decides. A body with no declared length is not read, so a
                // server that stalls it does not hold the check, and the drop
                // closes the connection.
                if body.content_length().is_some_and(|length| length <= 1) {
                    let mut bytes = [0u8; 8];
                    while let Ok(1..) = body.read(&mut bytes).await {}
                }
                Ok(true)
            }
            Ok(Fetched::NotModified) => Err(not_modified(&full)),
            Err(ostrya_fetch::Error::FetchTooLarge { .. }) => Ok(true),
            Err(ostrya_fetch::Error::HttpStatus { status: 416, .. }) => Ok(true),
            Err(e) => {
                self.absent_or_error(e, &full, 1, absent)?;
                Ok(false)
            }
        }
    }

    /// Writes `body` to `key`, and returns the `ETag` of the response.
    ///
    /// The request is idempotent, so the client sends it again by the rule
    /// of [`Bucket`]. A second request shares the bytes of the first one, so
    /// the client holds one copy of the body.
    ///
    /// The client hashes a body of more than 1 MiB once, on the blocking pool
    /// of the runtime, before the first request. Each request signs that
    /// digest. The signer hashes a smaller body on the task of each request.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository,
    ///   or if `body` is larger than 5 GiB, the limit of one S3 `PUT`.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response.
    /// - [`Error::Fetch`] for a failure of the fetcher, also after the last
    ///   request that the client sent again.
    pub async fn put(&self, key: &str, body: Vec<u8>) -> Result<Option<String>> {
        check_key(key)?;
        check_put_size(key, &body)?;
        let full = self.full_key(key);
        let path = encode_path(&full);
        self.put_body(&full, &path, upload_body(body).await).await
    }

    /// Writes the bytes that `body` gives to `key`, and returns the `ETag`
    /// of the answer that wrote the object, as the server wrote it.
    ///
    /// Servers give different `ETag` values for one object of several
    /// parts. Do not compare the result with the `ETag` of a later read of
    /// the key.
    ///
    /// The call reads `body` in parts of `part_size` bytes. [`part_size`]
    /// gives the part size of an object of a known size. A body of at most
    /// one part goes in one `PUT`. A longer body goes in a multipart upload:
    ///
    /// 1. `POST KEY?uploads` starts the upload and gives the upload id.
    /// 2. `PUT KEY?partNumber=N&uploadId=ID` sends each part, one part after
    ///    the other. The part numbers start at 1 and have no gap. Each part
    ///    other than the last one holds `part_size` bytes.
    /// 3. `POST KEY?uploadId=ID` with a `CompleteMultipartUpload` document
    ///    completes the upload. The document gives each part number with the
    ///    `ETag` of its answer, as the server wrote it.
    ///
    /// The call holds one part in memory, with the `ETag` of each part that
    /// it sent and the document that completes the upload. The first part
    /// starts at 8 KiB and doubles up to `part_size` while the bytes arrive,
    /// so a small body takes a small buffer. After each part, the call takes
    /// the buffer back and reads the next part into it. If the fetcher still
    /// holds the bytes of a part, the next part takes a new buffer. After a
    /// full part, the call reads one byte more to find if the body goes on.
    /// The call hashes each part of more than 1 MiB once, on the blocking
    /// pool of the runtime, before the first request of the part. The part
    /// moves to the pool and back with no copy, and each request of the part
    /// signs that digest. The signer hashes a smaller part on the task of
    /// each request.
    ///
    /// The client takes an upload id and an `ETag` of a part of at most
    /// 1 KiB each. So the `ETag` values of an upload and the document that
    /// completes it have a limit.
    ///
    /// The client sends each part again by the rule of [`Bucket`]. A part
    /// has the same bytes in each request. The `POST` that starts the upload
    /// is sent again by the same rule, and also after a 2xx whose body did
    /// not arrive. The `POST` that completes the upload is sent again after
    /// an interrupted request, after a 408, 429, or 5xx, after a 2xx whose
    /// body cannot be read, and after a 2xx whose body is an S3 `Error`
    /// document. S3 can send such a document after the head of a 200.
    ///
    /// After an interrupted request, a 408, 429, or 5xx, or an unusable 2xx
    /// to the `POST` that completes the upload, the server can have
    /// completed the upload. If a later request then gets a 404 with the code
    /// `NoSuchUpload`, the client reads the key as
    /// [`get_stream`](Bucket::get_stream) does. A `Content-Length` equal to
    /// the number of bytes of the body gives success with the `ETag` of the
    /// read, and the client reads no byte of the object. An absent object and
    /// an object of another length give an error.
    ///
    /// This check compares the length alone. It is sound for a key whose
    /// name sets its content, such as a content-addressed object or a static
    /// delta. For a key whose content changes, an older object of the same
    /// length passes the check.
    ///
    /// After each failure that comes after the start of the upload, the
    /// client sends one `DELETE KEY?uploadId=ID` to abort the upload, and
    /// ignores its outcome. Then it returns the error of the failure. If the
    /// last answer to a request of the upload is a 404 with the code
    /// `NoSuchUpload`, the server holds no such upload, and the client sends
    /// no abort.
    ///
    /// A dropped future sends no abort, so the upload stays open, and it
    /// holds storage in the bucket. [`list_uploads`](Bucket::list_uploads)
    /// gives each such upload, and [`abort_upload`](Bucket::abort_upload)
    /// aborts it. The prune of a bucket repository of the `ostrya` crate
    /// aborts each such upload. To remove such uploads with no call of the
    /// client, set a lifecycle rule on the bucket that aborts incomplete
    /// multipart uploads.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository,
    ///   or if `part_size` is less than 5 MiB or more than 5 GiB, the S3
    ///   limits of a part. The call then reads no byte of `body`.
    /// - [`Error::Io`] if a read of `body` fails.
    /// - [`Error::LimitExceeded`] if the body is longer than 10,000 parts of
    ///   `part_size` bytes, the S3 limit of the parts of an upload. The
    ///   message names the key and the part size.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response,
    ///   and [`Error::S3`] for an S3 `Error` document with a 2xx after the
    ///   last request that the client sent again.
    /// - [`Error::Transport`] if the answer that starts the upload has no
    ///   usable upload id or an upload id of more than 1 KiB, if its body did
    ///   not arrive after the last request that the client sent again, if the
    ///   answer to a part has no `ETag` or an `ETag` of more than 1 KiB, or if
    ///   no answer that completes the upload can be read. Also if the key
    ///   holds no object or an object of another length after a 404
    ///   `NoSuchUpload` to a completion whose outcome is not known. The
    ///   message then states what the key holds.
    /// - The errors of [`get_stream`](Bucket::get_stream) for the read of
    ///   the key after such a 404 `NoSuchUpload`.
    /// - [`Error::Fetch`] for a failure of the fetcher, also after the last
    ///   request that the client sent again.
    pub async fn put_stream<R>(&self, key: &str, body: R, part_size: u64) -> Result<Option<String>>
    where
        R: AsyncRead + Unpin + Send,
    {
        check_key(key)?;
        let size = usize::try_from(part_size)
            .ok()
            .filter(|_| (MIN_PART..=MAX_PUT).contains(&part_size))
            .ok_or_else(|| {
                Error::InvalidInput(format!(
                    "S3: the part size {part_size} of {key} is outside the range of \
                     {MIN_PART} to {MAX_PUT} bytes of a part"
                ))
            })?;
        self.put_parts(key, Parts::new(body, size, MAX_PARTS)).await
    }

    /// Writes `body` to `key` if the key holds no object.
    ///
    /// The request carries `If-None-Match: *`. A 2xx gives
    /// [`PutIfAbsent::Created`]. A 412 to the first request gives
    /// [`PutIfAbsent::Present`]. A 409 with the code
    /// `ConditionalRequestConflict` is sent again, at most
    /// [`max_retries`](ostrya_fetch::FetcherOptions::max_retries) times.
    ///
    /// After an interrupted request, the outcome is not known. The client
    /// then reads the key and compares the SHA-256 of what it holds with the
    /// SHA-256 of `body`:
    ///
    /// - The same bytes give [`PutIfAbsent::Matches`].
    /// - Other bytes give [`PutIfAbsent::Present`].
    /// - An absent key sends the request again, within the retry count.
    ///
    /// After a 408, 429, or 5xx, the client sends the request again within
    /// the retry count. A 412 after such a status, or after an interrupted
    /// request, can answer a request of this call. So the client then reads
    /// the key and compares it in the same way: the same bytes give
    /// [`PutIfAbsent::Refused`], and other bytes or an absent key give
    /// [`PutIfAbsent::Present`].
    ///
    /// The compare reads the object as a stream, in chunks of 64 KiB. A
    /// response that declares a length other than the length of `body`
    /// holds other bytes, and the client reads none of it. The client
    /// computes the SHA-256 of `body` on the blocking pool of the runtime:
    /// for a body of more than 1 MiB once before the first request, which
    /// each request signs, and for a smaller body at the first compare.
    ///
    /// A server that does not honor `If-None-Match` writes the object over
    /// the one that the key holds, and gives [`PutIfAbsent::Created`].
    ///
    /// # Errors
    ///
    /// The errors of [`put`](Bucket::put), and the errors of
    /// [`get`](Bucket::get) for the read of the key.
    pub async fn put_if_absent(&self, key: &str, body: Vec<u8>) -> Result<PutIfAbsent> {
        check_key(key)?;
        check_put_size(key, &body)?;
        let full = self.full_key(key);
        let path = encode_path(&full);
        let body = upload_body(body).await;
        let headers = [("If-None-Match".to_owned(), "*".to_owned())];
        let mut round = 0;
        // `true` after a request whose outcome is not known.
        let mut unsure = false;
        // The SHA-256 of `body`: the digest of a large body, or else computed
        // at the first compare that needs it.
        let mut digest = body.sha256();
        loop {
            match self
                .send(UploadMethod::Put, &path, "", copy(&body), &headers)
                .await?
            {
                Sent::Reply(reply) if is_success(reply.status) => {
                    return Ok(PutIfAbsent::Created { etag: reply.etag });
                }
                Sent::Reply(reply) if reply.status == 412 => {
                    if !unsure {
                        return Ok(PutIfAbsent::Present);
                    }
                    return Ok(match self.compare(&full, &body, &mut digest).await? {
                        Compared::Same(etag) => PutIfAbsent::Refused { etag },
                        Compared::Other | Compared::Absent => PutIfAbsent::Present,
                    });
                }
                Sent::Reply(reply)
                    if reply.status == 409
                        && reply_code(&reply).as_deref() == Some("ConditionalRequestConflict")
                        && round < self.max_retries => {}
                Sent::Reply(reply) if is_retryable(reply.status) && round < self.max_retries => {
                    unsure = true;
                }
                Sent::Reply(reply) => return Err(self.reply_error(reply, Some(&full))),
                Sent::Interrupted(e) => match self.compare(&full, &body, &mut digest).await? {
                    Compared::Same(etag) => return Ok(PutIfAbsent::Matches { etag }),
                    Compared::Other => return Ok(PutIfAbsent::Present),
                    Compared::Absent if round < self.max_retries => unsure = true,
                    Compared::Absent => return Err(Error::Fetch(e)),
                },
            }
            round += 1;
            ostrya_rt::Timer::after(backoff(round)).await;
        }
    }

    /// Deletes the object of `key`.
    ///
    /// A 2xx and an absent key are success. [`get`](Bucket::get) states the
    /// rule of an absent key. The client sends the request again by the rule
    /// of [`Bucket`].
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response.
    /// - [`Error::Fetch`] for a failure of the fetcher, also after the last
    ///   request that the client sent again.
    pub async fn delete(&self, key: &str) -> Result<()> {
        check_key(key)?;
        let full = self.full_key(key);
        let path = encode_path(&full);
        let mut round = 0;
        loop {
            let empty = UploadBody::bytes(Vec::new());
            match self
                .send(UploadMethod::Delete, &path, "", empty, &[])
                .await?
            {
                Sent::Reply(reply) if is_success(reply.status) => return Ok(()),
                Sent::Reply(reply) if is_absent(reply.status, reply.body.as_deref()) => {
                    return Ok(());
                }
                Sent::Reply(reply) if is_retryable(reply.status) && round < self.max_retries => {}
                Sent::Reply(reply) => return Err(self.reply_error(reply, Some(&full))),
                Sent::Interrupted(_) if round < self.max_retries => {}
                Sent::Interrupted(e) => return Err(Error::Fetch(e)),
            }
            round += 1;
            ostrya_rt::Timer::after(backoff(round)).await;
        }
    }

    /// Deletes the object of `key` if its `ETag` is `etag`.
    ///
    /// The request is `DELETE KEY` with `If-Match: ETAG`. `etag` goes as the
    /// server wrote it, for example the [`etag`](Object::etag) of a read. A
    /// 2xx gives [`DeleteIfMatch::Deleted`]. A 412 or an absent key to the
    /// first request gives [`DeleteIfMatch::NotMatched`].
    /// [`get`](Bucket::get) states the rule of an absent key. SeaweedFS 4.36
    /// answers 412 for an absent key.
    ///
    /// The client sends the request again by the rule of [`Bucket`]. A 412
    /// or an absent key after a request with an unknown outcome can answer a
    /// request of this call. So the client then reads the key as
    /// [`exists`](Bucket::exists) does. An absent key gives
    /// [`DeleteIfMatch::Deleted`], and an object gives
    /// [`DeleteIfMatch::NotMatched`].
    ///
    /// A server that does not honor `If-Match` deletes the object whatever
    /// its `ETag` is, and gives [`DeleteIfMatch::Deleted`].
    /// `rclone serve s3` 1.75.1 is such a server.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository,
    ///   or if `etag` is empty or longer than 1 KiB. The client then sends no
    ///   request.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response.
    /// - [`Error::Fetch`] for a failure of the fetcher, also after the last
    ///   request that the client sent again.
    /// - The errors of [`exists`](Bucket::exists) for the read of the key.
    pub async fn delete_if_match(&self, key: &str, etag: &str) -> Result<DeleteIfMatch> {
        check_key(key)?;
        if etag.is_empty() || etag.len() > MAX_UPLOAD_TOKEN {
            return Err(Error::InvalidInput(format!(
                "S3: the ETag of a conditional delete of {key} is empty or longer than \
                 {MAX_UPLOAD_TOKEN} bytes"
            )));
        }
        let full = self.full_key(key);
        let path = encode_path(&full);
        let headers = [("If-Match".to_owned(), etag.to_owned())];
        let mut round = 0;
        // `true` after a request whose outcome is not known.
        let mut unsure = false;
        loop {
            let empty = UploadBody::bytes(Vec::new());
            match self
                .send(UploadMethod::Delete, &path, "", empty, &headers)
                .await?
            {
                Sent::Reply(reply) if is_success(reply.status) => {
                    return Ok(DeleteIfMatch::Deleted);
                }
                Sent::Reply(reply)
                    if reply.status == 412 || is_absent(reply.status, reply.body.as_deref()) =>
                {
                    if !unsure {
                        return Ok(DeleteIfMatch::NotMatched);
                    }
                    return Ok(if self.exists(key).await? {
                        DeleteIfMatch::NotMatched
                    } else {
                        DeleteIfMatch::Deleted
                    });
                }
                Sent::Reply(reply) if is_retryable(reply.status) && round < self.max_retries => {
                    unsure = true;
                }
                Sent::Reply(reply) => return Err(self.reply_error(reply, Some(&full))),
                Sent::Interrupted(_) if round < self.max_retries => unsure = true,
                Sent::Interrupted(e) => return Err(Error::Fetch(e)),
            }
            round += 1;
            ostrya_rt::Timer::after(backoff(round)).await;
        }
    }

    /// Aborts the multipart upload `upload_id` of `key`.
    ///
    /// The request is `DELETE KEY?uploadId=ID`. A 2xx and a 404 with the
    /// code `NoSuchUpload` are success, so an upload that a server already
    /// removed is not an error. The client sends the request again by the
    /// rule of [`Bucket`].
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `key` is not a key of a bucket repository,
    ///   or if `upload_id` is empty.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response.
    /// - [`Error::Fetch`] for a failure of the fetcher, also after the last
    ///   request that the client sent again.
    pub async fn abort_upload(&self, key: &str, upload_id: &str) -> Result<()> {
        check_key(key)?;
        if upload_id.is_empty() {
            return Err(Error::InvalidInput(format!(
                "S3: the upload id of an abort of {key} is empty"
            )));
        }
        let full = self.full_key(key);
        let path = encode_path(&full);
        let query = upload_query(upload_id);
        let empty = UploadBody::bytes(Vec::new());
        let reply = self
            .send_retrying(UploadMethod::Delete, &path, &query, &empty)
            .await?;
        if is_success(reply.status) || is_no_such_upload(&reply) {
            return Ok(());
        }
        Err(self.reply_error(reply, Some(&full)))
    }

    /// Lists each object under `prefix`, over all pages.
    ///
    /// `prefix` is relative to the prefix of the repository and ends in `/`,
    /// for example `objects/ab/` or `refs/heads/`. The request is
    /// `ListObjectsV2` with the encoding type `url`, and the client decodes
    /// each key.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidInput`] if `prefix` does not end in `/`, or holds an
    ///   empty segment, `.`, `..`, or a character outside the alphabet of a
    ///   key.
    /// - [`Error::Transport`] if a page cannot be used: a body that the XML
    ///   reader refuses, a truncated page with no continuation token, a key
    ///   outside the prefix, or a page that does not move forward. Each key
    ///   and common prefix of a page comes after each one of the earlier
    ///   pages, in the byte order of UTF-8. A truncated page that holds no
    ///   key and no common prefix gives a continuation token that no page
    ///   gave after the last page with a key or a common prefix. More than
    ///   1000 truncated pages in a row with no key and no common prefix fail
    ///   the list. So a server that gives the same page again, or empty
    ///   pages with no end, fails the list.
    /// - [`Error::Transport`] if the server answers with 304.
    /// - [`Error::LimitExceeded`] if a page is larger than 4 MiB.
    /// - [`Error::S3`] and [`Error::S3Region`] for an unsuccessful response,
    ///   or for an S3 `Error` document with a 200.
    /// - [`Error::Fetch`] and [`Error::Io`] as for [`get`](Bucket::get).
    pub async fn list(&self, prefix: &str) -> Result<Listing> {
        self.list_pages(prefix, false).await
    }

    /// Lists the objects and the common prefixes under `prefix`, with the
    /// delimiter `/`, over all pages.
    ///
    /// Each key and each common prefix is relative to the prefix of the
    /// repository.
    ///
    /// # Errors
    ///
    /// The errors of [`list`](Bucket::list).
    pub async fn list_delimited(&self, prefix: &str) -> Result<Listing> {
        self.list_pages(prefix, true).await
    }

    /// Lists the incomplete multipart uploads of the keys of the repository,
    /// over all pages.
    ///
    /// The request is `ListMultipartUploads` with the prefix `PREFIX/`. With
    /// no prefix, the list covers the whole bucket, and the client keeps an
    /// upload only if its key is a key of a bucket repository. A 404 with
    /// the code `NoSuchUpload` is an empty list, because a server can answer
    /// a bucket with no upload in this way.
    ///
    /// # Errors
    ///
    /// The errors of [`list`](Bucket::list), with the markers of the next
    /// page in place of the continuation token. The rule of progress is
    /// different:
    ///
    /// - The `NextKeyMarker` of each truncated page does not come before the
    ///   key marker of the page before it, in the byte order of UTF-8.
    /// - While the key marker stays the same, the `NextUploadIdMarker` of
    ///   each truncated page is one that no earlier page gave for that key
    ///   marker. S3 sorts the uploads of one key by the time of their start,
    ///   and not by the upload id, so the upload id markers of one key have
    ///   no order.
    ///
    /// So a truncated page that holds no upload must move the markers.
    pub async fn list_uploads(&self) -> Result<Vec<Upload>> {
        let repository = self.repository_prefix();
        let mut uploads = Vec::new();
        let mut markers: Option<(String, String)> = None;
        // The upload id markers of the key marker of `markers`.
        let mut ids: HashSet<String> = HashSet::new();
        loop {
            let mut query = "uploads".to_owned();
            if !repository.is_empty() {
                query.push_str("&prefix=");
                query.push_str(&encode_query(&repository));
            }
            query.push_str("&encoding-type=url");
            if let Some((key_marker, id_marker)) = &markers {
                query.push_str("&key-marker=");
                query.push_str(&encode_query(key_marker));
                query.push_str("&upload-id-marker=");
                query.push_str(&encode_query(id_marker));
            }
            let body = match self.fetch_listing(&query).await {
                Ok(body) => body,
                Err(Error::S3 {
                    status: 404,
                    code: Some(code),
                    ..
                }) if code == "NoSuchUpload" => return Ok(uploads),
                Err(e) => return Err(e),
            };
            let page = match xml::list_uploads(&body).map_err(|why| self.unusable(&why))? {
                Doc::Result(page) => page,
                Doc::Error(doc) => return Err(self.answer_error(200, None, Some(doc), None)),
            };
            for (key, upload_id, initiated) in page.uploads {
                let key = self.listed(&key, page.encoded, &repository, repository.len())?;
                if is_repository_key(&key) {
                    uploads.push(Upload {
                        key,
                        upload_id,
                        initiated,
                    });
                }
            }
            if !page.truncated {
                return Ok(uploads);
            }
            let key_marker = page
                .next_key_marker
                .filter(|marker| !marker.is_empty())
                .map(|marker| decode_listed(&marker, page.encoded));
            let id_marker = page.next_upload_id_marker.filter(|id| !id.is_empty());
            let (Some(key_marker), Some(id_marker)) = (key_marker, id_marker) else {
                return Err(
                    self.unusable("a truncated page has no NextKeyMarker or no NextUploadIdMarker")
                );
            };
            match &markers {
                Some((earlier, _)) if key_marker < *earlier => {
                    return Err(self.unusable(
                        "the markers of a page do not move past the markers of the page \
                         before it: its NextKeyMarker comes before the key marker",
                    ));
                }
                Some((earlier, _)) if key_marker == *earlier => {
                    if !ids.insert(id_marker.clone()) {
                        return Err(self.unusable(
                            "the markers of a page do not move past the markers of the earlier \
                             pages: an earlier page gave the same NextUploadIdMarker for the \
                             same NextKeyMarker",
                        ));
                    }
                }
                _ => {
                    ids.clear();
                    ids.insert(id_marker.clone());
                }
            }
            markers = Some((key_marker, id_marker));
        }
    }

    /// Lists the objects under `prefix`, with the delimiter `/` if
    /// `delimited` is `true`.
    async fn list_pages(&self, prefix: &str, delimited: bool) -> Result<Listing> {
        check_list_prefix(prefix)?;
        let strip = self.repository_prefix().len();
        let full = self.full_key(prefix);
        let mut listing = Listing::default();
        let mut token: Option<String> = None;
        // The last key or common prefix of the earlier pages.
        let mut last: Option<String> = None;
        // The continuation tokens of the pages after the last page with a
        // key or a common prefix, and of that page.
        let mut tokens: HashSet<String> = HashSet::new();
        // The number of truncated pages in a row with no key and no common
        // prefix.
        let mut empty_run = 0usize;
        loop {
            let mut query = format!(
                "list-type=2&prefix={}&encoding-type=url",
                encode_query(&full)
            );
            if delimited {
                query.push_str("&delimiter=%2F");
            }
            if let Some(token) = &token {
                query.push_str("&continuation-token=");
                query.push_str(&encode_query(token));
            }
            let body = self.fetch_listing(&query).await?;
            let page = match xml::list_objects(&body).map_err(|why| self.unusable(&why))? {
                Doc::Result(page) => page,
                Doc::Error(doc) => return Err(self.answer_error(200, None, Some(doc), None)),
            };
            let (objects, prefixes) = (listing.objects.len(), listing.common_prefixes.len());
            for (key, size) in page.contents {
                let key = self.listed(&key, page.encoded, &full, strip)?;
                listing.objects.push(Listed { key, size });
            }
            for common in page.prefixes {
                let common = self.listed(&common, page.encoded, &full, strip)?;
                listing.common_prefixes.push(common);
            }
            let entries = listing.objects[objects..]
                .iter()
                .map(|listed| listed.key.as_str())
                .chain(
                    listing.common_prefixes[prefixes..]
                        .iter()
                        .map(String::as_str),
                );
            let (first, page_last) = entries.fold((None, None), |(low, high), entry| {
                (
                    low.filter(|low| low <= &entry).or(Some(entry)),
                    high.filter(|high| high >= &entry).or(Some(entry)),
                )
            });
            if let (Some(first), Some(last)) = (first, &last)
                && first <= last.as_str()
            {
                return Err(self.unusable(&format!(
                    "a page does not move past the keys of the earlier pages: it holds {:?} \
                     after {:?}",
                    shown(first),
                    shown(last)
                )));
            }
            if let Some(page_last) = page_last {
                last = Some(page_last.to_owned());
            }
            if !page.truncated {
                return Ok(listing);
            }
            let Some(next) = page.token.filter(|token| !token.is_empty()) else {
                return Err(self.unusable("a truncated page has no NextContinuationToken"));
            };
            if page_last.is_some() {
                tokens.clear();
                empty_run = 0;
            } else {
                empty_run += 1;
                if empty_run > MAX_EMPTY_PAGES {
                    return Err(self.unusable(&format!(
                        "more than {MAX_EMPTY_PAGES} truncated pages in a row hold no key and \
                         no common prefix"
                    )));
                }
            }
            if !tokens.insert(next.clone()) {
                return Err(self.unusable(
                    "a truncated page holds no key and no common prefix, and gives the \
                     continuation token of an earlier page",
                ));
            }
            token = Some(next);
        }
    }

    /// Fetches the listing of `query` on the bucket, and returns its body.
    async fn fetch_listing(&self, query: &str) -> Result<Vec<u8>> {
        let target = self.target("", query);
        let mut refetch = self.fetcher.refetching(FetchRequest {
            max_size: Some(XML_CAP),
            max_error_body: Some(XML_CAP),
            ..target.fetch()
        });
        loop {
            let mut body = match refetch.fetch().await {
                Ok(Fetched::Body(body)) => body,
                Ok(Fetched::NotModified) => {
                    return Err(self.unusable("the server answered a list with 304"));
                }
                Err(ostrya_fetch::Error::FetchTooLarge { .. }) => {
                    return Err(self.listing_too_large());
                }
                Err(ostrya_fetch::Error::HttpStatus {
                    status, response, ..
                }) => return Err(self.status_error(status, response.as_deref(), None)),
                Err(e) => return Err(Error::Fetch(e)),
            };
            let declared = body.content_length();
            match read_whole(&mut body, declared, XML_CAP, MAX_RESERVE).await {
                Ok(bytes) => return Ok(bytes),
                Err(e) if e.kind() == ErrorKind::FileTooLarge => {
                    return Err(self.listing_too_large());
                }
                Err(e) => {
                    drop(body);
                    refetch.retry(e).await?;
                }
            }
        }
    }

    /// Sends one upload request of `method` to `path` with the query
    /// `query`, `body`, and `headers`, and reads the answer whole. An empty
    /// query is no query.
    async fn send(
        &self,
        method: UploadMethod,
        path: &str,
        query: &str,
        body: UploadBody,
        headers: &[(String, String)],
    ) -> Result<Sent> {
        let target = self.target(path, query);
        let request = UploadRequest {
            method,
            max_response: XML_CAP,
            headers,
            ..target.upload(body)
        };
        match self.fetcher.upload(request).await {
            Ok(uploaded) => Ok(Sent::Reply(read_reply(uploaded).await)),
            Err(e) if !e.is_unsent() => Ok(Sent::Interrupted(e)),
            Err(e) => Err(Error::Fetch(e)),
        }
    }

    /// Sends one request of `method` to `path` with the query `query` and
    /// `body` by the rule of [`Bucket`], and returns the last answer.
    ///
    /// The client sends the request again after an interrupted request and
    /// after a 408, 429, or 5xx, at most `max_retries` times. Each request
    /// shares the bytes of `body`.
    async fn send_retrying(
        &self,
        method: UploadMethod,
        path: &str,
        query: &str,
        body: &UploadBody,
    ) -> Result<Reply> {
        let mut round = 0;
        loop {
            match self.send(method, path, query, copy(body), &[]).await? {
                Sent::Reply(reply) if is_retryable(reply.status) && round < self.max_retries => {}
                Sent::Reply(reply) => return Ok(reply),
                Sent::Interrupted(_) if round < self.max_retries => {}
                Sent::Interrupted(e) => return Err(Error::Fetch(e)),
            }
            round += 1;
            ostrya_rt::Timer::after(backoff(round)).await;
        }
    }

    /// Writes `body` to the full key `full`, whose encoded path is `path`,
    /// with one `PUT`, and returns the `ETag` of the answer.
    async fn put_body(&self, full: &str, path: &str, body: UploadBody) -> Result<Option<String>> {
        let reply = self
            .send_retrying(UploadMethod::Put, path, "", &body)
            .await?;
        if is_success(reply.status) {
            return Ok(reply.etag);
        }
        Err(self.reply_error(reply, Some(full)))
    }

    /// Writes the parts that `parts` reads to `key`, with one `PUT` for a
    /// body of one part and a multipart upload for a longer body.
    async fn put_parts<R: AsyncRead + Unpin>(
        &self,
        key: &str,
        mut parts: Parts<R>,
    ) -> Result<Option<String>> {
        let full = self.full_key(key);
        let path = encode_path(&full);
        let first = parts.next().await.map_err(|stop| stop.error(&full))?;
        if first.last {
            return self
                .put_body(&full, &path, upload_body(first.bytes).await)
                .await;
        }
        let upload_id = self.create_upload(&full, &path).await?;
        let query = upload_query(&upload_id);
        match self
            .send_parts(key, &full, &path, &query, first, parts)
            .await
        {
            Ok(etag) => Ok(etag),
            // The server holds no such upload, so the client sends no abort.
            Err(Failed { error, gone: true }) => Err(error),
            Err(Failed { error, gone: false }) => {
                // One abort, sent once. Its outcome does not change the
                // error of the call.
                let empty = UploadBody::bytes(Vec::new());
                let _ = self
                    .send(UploadMethod::Delete, &path, &query, empty, &[])
                    .await;
                Err(error)
            }
        }
    }

    /// Sends `first` and each later part that `parts` reads to the upload
    /// of the query `query`, and completes the upload.
    ///
    /// After each part other than the last one, `parts` takes the buffer of
    /// the part back for the next part.
    async fn send_parts<R: AsyncRead + Unpin>(
        &self,
        key: &str,
        full: &str,
        path: &str,
        query: &str,
        first: Part,
        mut parts: Parts<R>,
    ) -> std::result::Result<Option<String>, Failed> {
        let mut etags = Vec::new();
        let mut sent = 0u64;
        let mut part = first;
        loop {
            sent += part.bytes.len() as u64;
            let last = part.last;
            let (etag, body) = self.upload_part(full, path, query, part).await?;
            etags.push(etag);
            if last {
                break;
            }
            parts.recycle(body);
            part = parts.next().await.map_err(|stop| stop.error(full))?;
        }
        let body = upload_body(complete_body(&etags)).await;
        drop(etags);
        self.complete_upload(key, full, path, query, body, sent)
            .await
    }

    /// Starts a multipart upload of the full key `full`, and returns its
    /// upload id.
    ///
    /// The client sends the request again by the rule of [`Bucket`], and
    /// also after a 2xx whose body did not arrive.
    async fn create_upload(&self, full: &str, path: &str) -> Result<String> {
        let unusable = |why: &str| {
            self.unusable(&format!(
                "the answer that starts the multipart upload of {full}: {why}"
            ))
        };
        let mut round = 0;
        loop {
            let empty = UploadBody::bytes(Vec::new());
            let failure = match self
                .send(UploadMethod::Post, path, "uploads", empty, &[])
                .await?
            {
                Sent::Reply(reply) if is_success(reply.status) => match reply.body.as_deref() {
                    // A body that failed in transit counts as an interrupted
                    // request.
                    None => unusable("the body did not arrive"),
                    Some(body) => {
                        return match xml::initiate(body).map_err(|why| unusable(&why))? {
                            Doc::Result(upload_id) if upload_id.len() > MAX_UPLOAD_TOKEN => {
                                Err(unusable(&format!(
                                    "the UploadId has more than {MAX_UPLOAD_TOKEN} bytes"
                                )))
                            }
                            Doc::Result(upload_id) => Ok(upload_id),
                            Doc::Error(doc) => Err(self.answer_error(
                                reply.status,
                                reply.region.as_deref(),
                                Some(doc),
                                Some(full),
                            )),
                        };
                    }
                },
                Sent::Reply(reply) if is_retryable(reply.status) => {
                    self.reply_error(reply, Some(full))
                }
                Sent::Reply(reply) => return Err(self.reply_error(reply, Some(full))),
                Sent::Interrupted(e) => Error::Fetch(e),
            };
            if round >= self.max_retries {
                return Err(failure);
            }
            round += 1;
            ostrya_rt::Timer::after(backoff(round)).await;
        }
    }

    /// Sends `part` to the upload of the query `query`, and returns the
    /// `ETag` of the answer with the body of the part.
    async fn upload_part(
        &self,
        full: &str,
        path: &str,
        query: &str,
        part: Part,
    ) -> std::result::Result<(String, UploadBody), Failed> {
        let number = part.number;
        let query = format!("partNumber={number}&{query}");
        let body = upload_body(part.bytes).await;
        let reply = self
            .send_retrying(UploadMethod::Put, path, &query, &body)
            .await?;
        if !is_success(reply.status) {
            return Err(self.upload_failed(reply, full));
        }
        let why = match reply.etag {
            Some(etag) if etag.len() <= MAX_UPLOAD_TOKEN => return Ok((etag, body)),
            Some(_) => format!("an ETag of more than {MAX_UPLOAD_TOKEN} bytes"),
            None => "no ETag".to_owned(),
        };
        Err(self
            .unusable(&format!("the answer to part {number} of {full} has {why}"))
            .into())
    }

    /// Completes the upload of the query `query` with the
    /// `CompleteMultipartUpload` document `body`, and returns the `ETag` of
    /// the object. `sent` is the number of bytes of the parts.
    ///
    /// After an unsure request, a 404 with the code `NoSuchUpload` makes the
    /// client read the key `key`, by the rule of [`Bucket::put_stream`].
    async fn complete_upload(
        &self,
        key: &str,
        full: &str,
        path: &str,
        query: &str,
        body: UploadBody,
        sent: u64,
    ) -> std::result::Result<Option<String>, Failed> {
        let mut round = 0;
        // `true` after a request whose outcome is not known.
        let mut unsure = false;
        loop {
            let failure = match self
                .send(UploadMethod::Post, path, query, copy(&body), &[])
                .await?
            {
                Sent::Reply(reply) if is_success(reply.status) => {
                    unsure = true;
                    match reply.body.as_deref().map(xml::complete) {
                        Some(Ok(Doc::Result(etag))) => return Ok(etag.or(reply.etag)),
                        Some(Ok(Doc::Error(doc))) => self.answer_error(
                            reply.status,
                            reply.region.as_deref(),
                            Some(doc),
                            Some(full),
                        ),
                        Some(Err(why)) => self.unusable(&format!(
                            "the answer that completes the multipart upload of {full}: {why}"
                        )),
                        None => self.unusable(&format!(
                            "the answer that completes the multipart upload of {full}: the \
                             body did not arrive"
                        )),
                    }
                }
                Sent::Reply(reply) if unsure && is_no_such_upload(&reply) => {
                    return self
                        .completed_object(key, full, sent)
                        .await
                        .map_err(|error| Failed { error, gone: true });
                }
                Sent::Reply(reply) if is_retryable(reply.status) => {
                    unsure = true;
                    self.reply_error(reply, Some(full))
                }
                Sent::Reply(reply) => return Err(self.upload_failed(reply, full)),
                Sent::Interrupted(e) => {
                    unsure = true;
                    Error::Fetch(e)
                }
            };
            if round >= self.max_retries {
                return Err(failure.into());
            }
            round += 1;
            ostrya_rt::Timer::after(backoff(round)).await;
        }
    }

    /// Reads the key `key`, whose full key is `full`, after a 404 with the
    /// code `NoSuchUpload` to the completion of its upload, and returns the
    /// `ETag` of the object if its declared length is `sent`.
    ///
    /// The client reads no byte of the object.
    async fn completed_object(&self, key: &str, full: &str, sent: u64) -> Result<Option<String>> {
        let held = match self.get_stream(key).await? {
            Some(stream) => match stream.content_length() {
                Some(length) if length == sent => return Ok(stream.etag),
                Some(length) => format!("an object of {length} bytes"),
                None => "an object of no declared length".to_owned(),
            },
            None => "no object".to_owned(),
        };
        Err(self.unusable(&format!(
            "the answer that completes the multipart upload of {full} is 404 NoSuchUpload \
             after a request with no known outcome, and the key holds {held}, not the \
             {sent} bytes of the parts"
        )))
    }

    /// Returns the failure of an unsuccessful `reply` to a request of the
    /// multipart upload of the full key `full`.
    fn upload_failed(&self, reply: Reply, full: &str) -> Failed {
        let gone = is_no_such_upload(&reply);
        Failed {
            error: self.reply_error(reply, Some(full)),
            gone,
        }
    }

    /// Reads the object of the full key `full` as a stream, and compares it
    /// with `body`.
    ///
    /// A declared length other than the length of `body` gives
    /// [`Compared::Other`] with no read of the object. Else the function
    /// hashes the object in chunks of [`COMPARE_CHUNK`] bytes, and stops at
    /// the first byte past the length of `body`. `digest` keeps the SHA-256
    /// of `body` for a later compare.
    async fn compare(
        &self,
        full: &str,
        body: &UploadBody,
        digest: &mut Option<[u8; 32]>,
    ) -> Result<Compared> {
        let size = body.as_bytes().map_or(0, <[u8]>::len) as u64;
        let path = encode_path(full);
        let target = self.target(&path, "");
        let mut refetch = self.fetcher.refetching(FetchRequest {
            max_error_body: Some(XML_CAP),
            ..target.fetch()
        });
        let (stored, etag) = loop {
            let mut stream = match refetch.fetch().await {
                Ok(Fetched::Body(stream)) => stream,
                Ok(Fetched::NotModified) => return Err(not_modified(full)),
                Err(e) => {
                    self.absent_or_error(e, full, u64::MAX, is_absent)?;
                    return Ok(Compared::Absent);
                }
            };
            if stream.content_length().is_some_and(|length| length != size) {
                return Ok(Compared::Other);
            }
            let etag = stream.validators().etag.clone();
            match hash_exact(&mut stream, size).await {
                Ok(Some(stored)) => break (stored, etag),
                Ok(None) => return Ok(Compared::Other),
                Err(e) => {
                    drop(stream);
                    refetch.retry(e).await?;
                }
            }
        };
        let ours = match *digest {
            Some(ours) => ours,
            None => {
                let copy = copy(body);
                let ours: [u8; 32] = ostrya_rt::unblock(move || {
                    Sha256::digest(copy.as_bytes().unwrap_or_default()).into()
                })
                .await;
                *digest = Some(ours);
                ours
            }
        };
        Ok(if stored == ours {
            Compared::Same(etag)
        } else {
            Compared::Other
        })
    }

    /// Returns `Ok` if the failed fetch `e` of the object `full` states that
    /// the key holds no object by the rule `absent`, and the error of the
    /// fetch otherwise.
    ///
    /// The absent-key rule runs before the region check, so an absent key is
    /// never a region error.
    fn absent_or_error(
        &self,
        e: ostrya_fetch::Error,
        full: &str,
        max_size: u64,
        absent: fn(u16, Option<&[u8]>) -> bool,
    ) -> Result<()> {
        match e {
            ostrya_fetch::Error::FetchTooLarge { .. } => Err(too_large(full, max_size)),
            ostrya_fetch::Error::HttpStatus {
                status, response, ..
            } => {
                let response = response.as_deref();
                if absent(status, response.and_then(ErrorResponse::body)) {
                    Ok(())
                } else {
                    Err(self.status_error(status, response, Some(full)))
                }
            }
            other => Err(Error::Fetch(other)),
        }
    }

    /// Returns the error of an unsuccessful fetch with `status` and the kept
    /// `response`.
    fn status_error(
        &self,
        status: u16,
        response: Option<&ErrorResponse>,
        key: Option<&str>,
    ) -> Error {
        let region = response
            .and_then(|response| response.headers().get("x-amz-bucket-region"))
            .and_then(|value| value.to_str().ok());
        let doc = response.and_then(ErrorResponse::body).and_then(xml::error);
        self.answer_error(status, region, doc, key)
    }

    /// Returns the error of an unsuccessful upload `reply`.
    fn reply_error(&self, reply: Reply, key: Option<&str>) -> Error {
        let doc = reply.body.as_deref().and_then(xml::error);
        self.answer_error(reply.status, reply.region.as_deref(), doc, key)
    }

    /// Returns the error of an answer with `status`, the region header
    /// `region`, and the S3 `Error` document `doc`.
    ///
    /// A region of the header, else of the `Region` element, that differs
    /// from the region of the location gives [`Error::S3Region`]. A value
    /// that is not a region counts as absent.
    fn answer_error(
        &self,
        status: u16,
        region: Option<&str>,
        doc: Option<ErrorDoc>,
        key: Option<&str>,
    ) -> Error {
        let doc = doc.unwrap_or_default();
        let bucket_region = region
            .map(str::trim)
            .filter(|region| valid_region(region))
            .or_else(|| {
                doc.region
                    .as_deref()
                    .map(str::trim)
                    .filter(|region| valid_region(region))
            });
        if let Some(bucket_region) = bucket_region
            && bucket_region != self.location.region()
        {
            return Error::S3Region {
                bucket: self.location.address().bucket().to_owned(),
                region: self.location.region().to_owned(),
                bucket_region: bucket_region.to_owned(),
            };
        }
        Error::S3 {
            status,
            code: doc.code.as_deref().map(str::trim).map(shown),
            message: doc.message.as_deref().map(shown),
            key: key.map(str::to_owned),
        }
    }

    /// Returns the key or the common prefix `text` of a listing, relative to
    /// the prefix of the repository, which is the first `strip` bytes.
    ///
    /// `encoded` states that the listing has the encoding type `url`. A text
    /// outside `requested`, the prefix of the request, fails the listing.
    /// `requested` starts with the prefix of the repository.
    fn listed(&self, text: &str, encoded: bool, requested: &str, strip: usize) -> Result<String> {
        let mut decoded = decode_listed(text, encoded);
        if !decoded.starts_with(requested) {
            return Err(self.unusable(&format!(
                "the listing holds the key {:?}, which is outside the prefix {requested:?}",
                shown(&decoded)
            )));
        }
        decoded.drain(..strip);
        Ok(decoded)
    }

    /// Returns the target of a request to the encoded path `path`, relative
    /// to the bucket URL, with the query `query`. An empty query is no query.
    fn target<'a>(&self, path: &'a str, query: &'a str) -> RequestTarget<'a> {
        match &self.base {
            None => RequestTarget::Mirror { path, query },
            Some(base) if query.is_empty() => RequestTarget::Url(format!("{base}/{path}")),
            Some(base) => RequestTarget::Url(format!("{base}/{path}?{query}")),
        }
    }

    /// Returns the prefix of each key of the repository: `PREFIX/`, or empty
    /// for an address with no prefix.
    fn repository_prefix(&self) -> String {
        match self.location.address().prefix() {
            "" => String::new(),
            prefix => format!("{prefix}/"),
        }
    }

    /// Returns the full key of `key`, which is relative to the prefix of the
    /// repository.
    fn full_key(&self, key: &str) -> String {
        format!("{}{key}", self.repository_prefix())
    }

    /// Returns the error of a listing that the client cannot use.
    fn unusable(&self, why: &str) -> Error {
        Error::Transport(format!(
            "S3: the answer of {} cannot be used: {why}",
            self.location.address()
        ))
    }

    /// Returns the error of a listing page over the limit of the reader.
    fn listing_too_large(&self) -> Error {
        Error::LimitExceeded(format!(
            "S3: a listing page of {} is larger than {XML_CAP} bytes",
            self.location.address()
        ))
    }
}

/// The target of one request of the client.
enum RequestTarget<'a> {
    /// A path and a query under the one mirror of the fetcher, the bucket
    /// URL. An empty query is no query.
    Mirror {
        /// The encoded path.
        path: &'a str,
        /// The query.
        query: &'a str,
    },
    /// The URL of the request, under the bucket URL.
    Url(String),
}

impl RequestTarget<'_> {
    /// Returns a fetch of the target with the default of each other field.
    fn fetch(&self) -> FetchRequest<'_> {
        match self {
            RequestTarget::Mirror { path, query: "" } => FetchRequest::path(path),
            RequestTarget::Mirror { path, query } => FetchRequest::path_query(path, query),
            RequestTarget::Url(url) => FetchRequest::url(url),
        }
    }

    /// Returns an upload of `body` to the target with the default of each
    /// other field.
    fn upload(&self, body: UploadBody) -> UploadRequest<'_> {
        match self {
            RequestTarget::Mirror { path, query: "" } => UploadRequest::path(path, body),
            RequestTarget::Mirror { path, query } => UploadRequest {
                target: Target::PathQuery { path, query },
                ..UploadRequest::path(path, body)
            },
            RequestTarget::Url(url) => UploadRequest::url(url, body),
        }
    }
}

/// Returns the part size of a multipart upload of an object of `size`
/// bytes.
///
/// The part size is the larger of 8 MiB and `(2 * size + 1 MiB) / 10,000`,
/// rounded up to a whole MiB. So 10,000 parts hold `2 * size + 1 MiB` bytes,
/// more than the bytes of a `.filez` object whose file has `size` bytes of
/// content. The part size is 8 MiB up to a size of 41,942,515,712 bytes, and
/// 9 MiB from one byte more.
///
/// # Errors
///
/// [`Error::LimitExceeded`] if the part size is more than 5 GiB, the S3 limit
/// of a part. This is the case for a size of more than 26,843,545,075,712
/// bytes.
pub fn part_size(size: u64) -> Result<u64> {
    let too_large = || {
        Error::LimitExceeded(format!(
            "S3: an object of {size} bytes needs parts of more than {MAX_PUT} bytes, the limit \
             of a part"
        ))
    };
    let span = size
        .checked_mul(2)
        .and_then(|span| span.checked_add(MIB))
        .ok_or_else(too_large)?;
    let part = span
        .div_ceil(u64::from(MAX_PARTS) * MIB)
        .checked_mul(MIB)
        .ok_or_else(too_large)?
        .max(DEFAULT_PART);
    if part > MAX_PUT {
        return Err(too_large());
    }
    Ok(part)
}

/// One part of a body that [`Parts`] read.
struct Part {
    /// The part number, from 1.
    number: u32,
    /// The bytes of the part.
    bytes: Vec<u8>,
    /// `true` if the body ends with this part.
    last: bool,
}

/// A failure of a multipart upload after its start.
struct Failed {
    /// The error of the call.
    error: Error,
    /// `true` if the last answer to a request of the upload is a 404 with
    /// the code `NoSuchUpload`. The server then holds no such upload, so the
    /// client sends no abort.
    gone: bool,
}

impl From<Error> for Failed {
    fn from(error: Error) -> Failed {
        Failed { error, gone: false }
    }
}

/// Why [`Parts::next`] gave no part.
enum Stop {
    /// A read of the body failed.
    Io(std::io::Error),
    /// The part with the largest number is full, and the body goes on.
    TooMany {
        /// The largest number of parts.
        max_parts: u32,
        /// The part size.
        part_size: usize,
    },
}

impl Stop {
    /// Returns the error of a stop of the parts of the full key `full`.
    fn error(self, full: &str) -> Error {
        match self {
            Stop::Io(e) => Error::Io(e),
            Stop::TooMany {
                max_parts,
                part_size,
            } => Error::LimitExceeded(format!(
                "S3: the body of {full} is longer than {max_parts} parts of {part_size} bytes"
            )),
        }
    }
}

/// The reader of the parts of a body, one part after the other.
struct Parts<R> {
    body: R,
    part_size: usize,
    max_parts: u32,
    /// The number of the last part that [`Parts::next`] gave.
    number: u32,
    /// The first byte of the next part, which the read after a full part
    /// gave.
    carry: Option<u8>,
    /// The buffer of an earlier part, which [`Parts::recycle`] took back.
    spare: Option<Vec<u8>>,
}

impl<R: AsyncRead + Unpin> Parts<R> {
    /// Returns the reader of the parts of `body`, each of `part_size` bytes
    /// other than the last one, at most `max_parts` parts.
    fn new(body: R, part_size: usize, max_parts: u32) -> Parts<R> {
        Parts {
            body,
            part_size,
            max_parts,
            number: 0,
            carry: None,
            spare: None,
        }
    }

    /// Reads the next part.
    ///
    /// The first part starts with no capacity and grows as [`fill`] states.
    /// A later part reads into the buffer that [`Parts::recycle`] took back,
    /// with no fill of zeros. Else it starts with the capacity of a whole
    /// part. After a full part, the reader reads one byte more to find if the
    /// body goes on, and keeps it for the next part. A full part with the
    /// number `max_parts` and a byte after it gives [`Stop::TooMany`].
    async fn next(&mut self) -> std::result::Result<Part, Stop> {
        let mut bytes = self.spare.take().unwrap_or_default();
        let mut filled = 0;
        if let Some(byte) = self.carry.take() {
            match bytes.first_mut() {
                Some(first) => *first = byte,
                None => {
                    bytes.reserve_exact(self.part_size);
                    bytes.push(byte);
                }
            }
            filled = 1;
        }
        let filled = fill(&mut self.body, &mut bytes, filled, self.part_size)
            .await
            .map_err(Stop::Io)?;
        bytes.truncate(filled);
        self.number += 1;
        let mut last = true;
        if bytes.len() == self.part_size {
            let mut probe = [0u8; 1];
            if self.body.read(&mut probe).await.map_err(Stop::Io)? == 1 {
                if self.number >= self.max_parts {
                    return Err(Stop::TooMany {
                        max_parts: self.max_parts,
                        part_size: self.part_size,
                    });
                }
                self.carry = Some(probe[0]);
                last = false;
            }
        }
        Ok(Part {
            number: self.number,
            bytes,
            last,
        })
    }

    /// Takes back the buffer of `body`, the body of a part that the upload
    /// sent, for the next part. A buffer that another body still shares
    /// stays with that body.
    fn recycle(&mut self, body: UploadBody) {
        self.spare = body.into_vec().ok();
    }
}

/// Reads from `body` into `buffer`, from the byte `filled`, until the
/// buffer holds `limit` bytes or the body ends, and returns the number of
/// bytes that the buffer holds. `buffer` has a length and a capacity of at
/// most `limit` bytes, and `filled` is at most its length.
///
/// The read overwrites the bytes of `buffer` past `filled`, with no fill of
/// zeros. When the read reaches the length of `buffer`, the length doubles,
/// from [`FIRST_READ`] bytes, up to `limit`, and only the new bytes are
/// zeros. The capacity grows with the length when it must, and never passes
/// `limit`.
async fn fill(
    body: &mut (impl AsyncRead + Unpin),
    buffer: &mut Vec<u8>,
    mut filled: usize,
    limit: usize,
) -> std::io::Result<usize> {
    loop {
        if filled == limit {
            return Ok(filled);
        }
        if filled == buffer.len() {
            let grown = filled.saturating_mul(2).max(FIRST_READ as usize).min(limit);
            buffer.reserve_exact(grown - filled);
            buffer.resize(grown, 0);
        }
        match body.read(&mut buffer[filled..]).await? {
            0 => return Ok(filled),
            n => filled += n,
        }
    }
}

/// Returns the query `uploadId=ID` of the upload `upload_id`.
fn upload_query(upload_id: &str) -> String {
    format!("uploadId={}", encode_query(upload_id))
}

/// Returns the `CompleteMultipartUpload` document of the parts whose `ETag`
/// values are `etags`, the first one for part 1.
///
/// Each `ETag` goes as the server wrote it, with `&`, `<`, and `>` escaped.
fn complete_body(etags: &[String]) -> Vec<u8> {
    let mut body = format!("<CompleteMultipartUpload xmlns=\"{S3_NAMESPACE}\">");
    for (number, etag) in (1u32..).zip(etags) {
        body.push_str(&format!(
            "<Part><PartNumber>{number}</PartNumber><ETag>{}</ETag></Part>",
            partial_escape(etag.as_str())
        ));
    }
    body.push_str("</CompleteMultipartUpload>");
    body.into_bytes()
}

/// Returns the body of `bytes`.
///
/// A body of more than [`UNBLOCK_SIZE`] bytes moves to the blocking pool,
/// which hashes it and gives it back with its SHA-256, with no copy of its
/// bytes. A smaller body has no digest, and the signer hashes it.
async fn upload_body(bytes: Vec<u8>) -> UploadBody {
    if bytes.len() <= UNBLOCK_SIZE {
        return UploadBody::bytes(bytes);
    }
    ostrya_rt::unblock(move || {
        let sha256 = Sha256::digest(&bytes).into();
        UploadBody::bytes_with_sha256(bytes, sha256)
    })
    .await
}

/// Returns a second body with the bytes of `body`.
fn copy(body: &UploadBody) -> UploadBody {
    body.try_clone()
        .expect("a body of bytes has a copy that shares its bytes")
}

/// Refuses a body larger than one S3 `PUT` takes.
fn check_put_size(key: &str, body: &[u8]) -> Result<()> {
    if body.len() as u64 > MAX_PUT {
        return Err(Error::InvalidInput(format!(
            "S3: the body of {key} has {} bytes, more than the {MAX_PUT}-byte limit of one PUT",
            body.len()
        )));
    }
    Ok(())
}

/// Reads the status, the `ETag`, the region header, and the body of
/// `uploaded`.
async fn read_reply(uploaded: Uploaded) -> Reply {
    let header = |name: &str| {
        uploaded
            .headers()
            .get(name)
            .and_then(|value| value.to_str().ok())
            .map(str::to_owned)
    };
    let status = uploaded.status();
    let etag = header("etag");
    let region = header("x-amz-bucket-region");
    let withheld = uploaded.body_withheld();
    let mut body = uploaded.into_body();
    let declared = body.content_length();
    let body = if withheld {
        None
    } else {
        read_whole(&mut body, declared, XML_CAP, MAX_RESERVE)
            .await
            .ok()
    };
    Reply {
        status,
        etag,
        region,
        body,
    }
}

/// Returns the `Code` of the S3 `Error` document of `reply`.
fn reply_code(reply: &Reply) -> Option<String> {
    reply.body.as_deref().and_then(xml::error)?.code
}

/// Returns `true` if `reply` is a 404 with the code `NoSuchUpload`.
fn is_no_such_upload(reply: &Reply) -> bool {
    reply.status == 404 && reply_code(reply).is_some_and(|code| code.trim() == "NoSuchUpload")
}

/// Returns `true` for a 2xx status.
fn is_success(status: u16) -> bool {
    (200..300).contains(&status)
}

/// Returns `true` for a status that a later request can avoid: 408, 429,
/// and 5xx.
fn is_retryable(status: u16) -> bool {
    status == 408 || status == 429 || (500..600).contains(&status)
}

/// Returns `true` if an answer with `status` and the whole body `body`
/// states an absent key: a 404 whose body holds the code `NoSuchKey`, or no
/// S3 code.
///
/// `body` is `None` if the body did not arrive whole. Such a 404 can come
/// from a missing bucket, so it is not an absent key.
fn is_absent(status: u16, body: Option<&[u8]>) -> bool {
    status == 404
        && body.is_some_and(|body| {
            xml::error(body)
                .and_then(|doc| doc.code)
                .is_none_or(|code| code.trim() == "NoSuchKey")
        })
}

/// Returns `true` if an answer with `status` and the whole body `body`
/// states an absent key by the strict rule: a 404 whose body holds the code
/// `NoSuchKey`.
///
/// `body` is `None` if the body did not arrive whole. A 404 with no S3 code
/// can come from a proxy or from a missing bucket, so it is not an absent
/// key.
fn is_no_such_key(status: u16, body: Option<&[u8]>) -> bool {
    status == 404
        && body
            .and_then(xml::error)
            .and_then(|doc| doc.code)
            .is_some_and(|code| code.trim() == "NoSuchKey")
}

/// Reads `body` to its end, and refuses a body larger than `limit` bytes
/// with [`ErrorKind::FileTooLarge`].
///
/// The vector reserves the `declared` length and one byte for the end of
/// the body, up to `reserve` bytes before the body arrives. With no
/// declared length, it reserves [`FIRST_READ`] bytes, up to `reserve`. It
/// then doubles as the bytes arrive, and its capacity never passes the
/// declared length or `limit` by more than one byte.
async fn read_whole(
    body: &mut (impl AsyncRead + Unpin),
    declared: Option<u64>,
    limit: u64,
    reserve: u64,
) -> std::io::Result<Vec<u8>> {
    let most = declared.map_or(limit, |declared| declared.min(limit));
    let ceiling = usize::try_from(most.saturating_add(1)).unwrap_or(usize::MAX);
    let first = usize::try_from(declared.unwrap_or(FIRST_READ).min(most).min(reserve))
        .unwrap_or(0)
        .saturating_add(1);
    let mut bytes = vec![0u8; first];
    let mut filled = 0;
    loop {
        if filled == bytes.len() {
            if bytes.len() >= ceiling {
                return Err(std::io::Error::new(
                    ErrorKind::FileTooLarge,
                    format!("the body is larger than {limit} bytes"),
                ));
            }
            let grown = bytes.len().saturating_mul(2).min(ceiling);
            bytes.reserve_exact(grown - bytes.len());
            bytes.resize(grown, 0);
        }
        let n = body.read(&mut bytes[filled..]).await?;
        if n == 0 {
            bytes.truncate(filled);
            return Ok(bytes);
        }
        filled += n;
    }
}

/// Hashes the stream `body` with SHA-256, in chunks of [`COMPARE_CHUNK`]
/// bytes, if it holds exactly `size` bytes.
///
/// Returns `None` at the first byte past `size`, or at an end before `size`.
async fn hash_exact(
    body: &mut (impl AsyncRead + Unpin),
    size: u64,
) -> std::io::Result<Option<[u8; 32]>> {
    let chunk =
        usize::try_from(size.saturating_add(1)).map_or(COMPARE_CHUNK, |len| len.min(COMPARE_CHUNK));
    let mut buf = vec![0u8; chunk];
    let mut hasher = Sha256::new();
    let mut read = 0u64;
    loop {
        let n = body.read(&mut buf).await?;
        if n == 0 {
            return Ok((read == size).then(|| hasher.finalize().into()));
        }
        read += n as u64;
        if read > size {
            return Ok(None);
        }
        hasher.update(&buf[..n]);
    }
}

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

/// Returns the error of an object over the limit of the caller.
fn too_large(key: &str, max_size: u64) -> Error {
    Error::LimitExceeded(format!(
        "S3: the object {key} is larger than the limit of {max_size} bytes"
    ))
}

/// Returns the error of a 304 to a request with no validators.
fn not_modified(key: &str) -> Error {
    Error::Transport(format!(
        "S3: the server answered the read of {key}, which sent no validators, with 304"
    ))
}

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

    #[test]
    fn the_backoff_doubles_and_stops_at_two_seconds() {
        let delays: Vec<u64> = (1..=6)
            .map(|round| backoff(round).as_millis() as u64)
            .collect();
        assert_eq!(delays, [250, 500, 1000, 2000, 2000, 2000]);
    }

    #[test]
    fn the_retryable_statuses_are_408_429_and_5xx() {
        for status in [408, 429, 500, 503, 599] {
            assert!(is_retryable(status), "{status}");
        }
        for status in [200, 301, 400, 403, 404, 409, 412] {
            assert!(!is_retryable(status), "{status}");
        }
    }

    #[test]
    fn an_absent_key_is_a_404_whose_body_has_no_code_or_no_such_key() {
        let coded = |code: &str| format!("<Error><Code>{code}</Code></Error>").into_bytes();
        assert!(is_absent(404, Some(b"")));
        assert!(is_absent(404, Some(b"not found")));
        assert!(is_absent(404, Some(b"<Error><Message>m</Message></Error>")));
        assert!(is_absent(404, Some(&coded("NoSuchKey"))));
        assert!(!is_absent(404, Some(&coded("NoSuchBucket"))));
        assert!(!is_absent(404, None));
        assert!(!is_absent(403, Some(b"")));
    }

    #[test]
    fn a_strict_absent_key_is_a_404_whose_body_has_no_such_key() {
        let coded = |code: &str| format!("<Error><Code>{code}</Code></Error>").into_bytes();
        assert!(is_no_such_key(404, Some(&coded("NoSuchKey"))));
        assert!(is_no_such_key(404, Some(&coded(" NoSuchKey\n"))));
        assert!(!is_no_such_key(404, Some(b"")));
        assert!(!is_no_such_key(404, Some(b"<html>Not Found</html>")));
        assert!(!is_no_such_key(
            404,
            Some(b"<Error><Message>m</Message></Error>")
        ));
        assert!(!is_no_such_key(404, Some(&coded("NoSuchBucket"))));
        assert!(!is_no_such_key(404, None));
        assert!(!is_no_such_key(403, Some(&coded("NoSuchKey"))));
    }

    #[test]
    fn a_whole_read_holds_no_capacity_past_its_limit() {
        ostrya_rt::block_on(async {
            let data = vec![b'x'; XML_CAP as usize];
            // No declared length: the vector doubles up to the limit and one
            // byte.
            let mut reader = futures_lite::io::Cursor::new(data.clone());
            let bytes = read_whole(&mut reader, None, XML_CAP, MAX_RESERVE)
                .await
                .unwrap();
            assert_eq!(bytes.len(), data.len());
            assert!(bytes.capacity() <= data.len() + 1, "{}", bytes.capacity());
            // A declared length: the vector reserves it and one byte.
            let mut reader = futures_lite::io::Cursor::new(data[..3000].to_vec());
            let bytes = read_whole(&mut reader, Some(3000), XML_CAP, MAX_RESERVE)
                .await
                .unwrap();
            assert_eq!(bytes.len(), 3000);
            assert!(bytes.capacity() <= 3001, "{}", bytes.capacity());
            // A body past the limit fails.
            let mut reader = futures_lite::io::Cursor::new(vec![b'x'; 11]);
            let err = read_whole(&mut reader, None, 10, MAX_RESERVE)
                .await
                .unwrap_err();
            assert_eq!(err.kind(), ErrorKind::FileTooLarge);
        });
    }

    /// A read with a reserve over 4 MiB reserves a declared length whole, at
    /// once. With no declared length, it reserves 8 KiB first.
    #[test]
    fn a_whole_read_reserves_a_declared_length_at_once() {
        ostrya_rt::block_on(async {
            let size = 2 * XML_CAP as usize + 5;
            let limit = 4 * XML_CAP;
            /// A reader that records the length of the buffer of its first
            /// read.
            struct First {
                data: futures_lite::io::Cursor<Vec<u8>>,
                first: Option<usize>,
            }
            impl AsyncRead for First {
                fn poll_read(
                    mut self: Pin<&mut Self>,
                    cx: &mut Context<'_>,
                    buf: &mut [u8],
                ) -> Poll<std::io::Result<usize>> {
                    self.first.get_or_insert(buf.len());
                    Pin::new(&mut self.data).poll_read(cx, buf)
                }
            }
            let mut reader = First {
                data: futures_lite::io::Cursor::new(vec![b'x'; size]),
                first: None,
            };
            let bytes = read_whole(&mut reader, Some(size as u64), limit, limit)
                .await
                .unwrap();
            assert_eq!(bytes.len(), size);
            assert_eq!(reader.first, Some(size + 1));
            assert!(bytes.capacity() <= size + 1, "{}", bytes.capacity());
            // The listing reserve caps the first read at 4 MiB.
            let mut reader = First {
                data: futures_lite::io::Cursor::new(vec![b'x'; size]),
                first: None,
            };
            read_whole(&mut reader, Some(size as u64), limit, MAX_RESERVE)
                .await
                .unwrap();
            assert_eq!(reader.first, Some(XML_CAP as usize + 1));
            // No declared length: 8 KiB first, whatever the reserve.
            let mut reader = First {
                data: futures_lite::io::Cursor::new(vec![b'x'; size]),
                first: None,
            };
            read_whole(&mut reader, None, limit, limit).await.unwrap();
            assert_eq!(reader.first, Some(FIRST_READ as usize + 1));
        });
    }

    #[test]
    fn the_part_size_grows_by_whole_mib_past_8_mib() {
        assert_eq!(part_size(0).unwrap(), 8 * MIB);
        assert_eq!(part_size(41_942_515_712).unwrap(), 8 * MIB);
        assert_eq!(part_size(41_942_515_713).unwrap(), 9 * MIB);
        assert_eq!(part_size(26_843_545_075_712).unwrap(), MAX_PUT);
        for size in [26_843_545_075_713, u64::MAX / 2, u64::MAX] {
            let err = part_size(size).unwrap_err();
            assert!(matches!(err, Error::LimitExceeded(_)), "{size}: {err}");
        }
    }

    #[test]
    fn the_complete_document_lists_each_part_with_its_escaped_etag() {
        let etags = [
            "\"5d41402abc4b2a76b9719d911017c592\"".to_owned(),
            "\"a&b<c>\"".to_owned(),
        ];
        assert_eq!(
            String::from_utf8(complete_body(&etags)).unwrap(),
            "<CompleteMultipartUpload xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\
             <Part><PartNumber>1</PartNumber>\
             <ETag>\"5d41402abc4b2a76b9719d911017c592\"</ETag></Part>\
             <Part><PartNumber>2</PartNumber><ETag>\"a&amp;b&lt;c&gt;\"</ETag></Part>\
             </CompleteMultipartUpload>"
        );
    }

    /// Reads each part of `data` with parts of `part_size` bytes, at most
    /// `max_parts` parts, and returns the parts or the stop.
    fn read_parts(
        data: &[u8],
        part_size: usize,
        max_parts: u32,
    ) -> std::result::Result<Vec<Part>, Stop> {
        ostrya_rt::block_on(async {
            let mut parts = Parts::new(futures_lite::io::Cursor::new(data), part_size, max_parts);
            let mut read = Vec::new();
            loop {
                let part = parts.next().await?;
                let last = part.last;
                read.push(part);
                if last {
                    return Ok(read);
                }
            }
        })
    }

    #[test]
    fn a_body_reads_as_numbered_parts_with_a_probe_byte_between_them() {
        let data: Vec<u8> = (0..10u8).collect();
        let Ok(parts) = read_parts(&data, 4, 3) else {
            panic!("three parts of four bytes hold ten bytes");
        };
        let found: Vec<(u32, &[u8], bool)> = parts
            .iter()
            .map(|part| (part.number, part.bytes.as_slice(), part.last))
            .collect();
        assert_eq!(
            found,
            [
                (1, &data[..4], false),
                (2, &data[4..8], false),
                (3, &data[8..], true)
            ]
        );
        // A body of whole parts ends with a full part, and an empty body is
        // one empty part.
        let parts = read_parts(&data[..8], 4, 3).ok().unwrap();
        assert_eq!(parts.len(), 2);
        assert!(parts[1].last && parts[1].bytes == data[4..8]);
        let parts = read_parts(&[], 4, 3).ok().unwrap();
        assert_eq!(parts.len(), 1);
        assert!(parts[0].last && parts[0].bytes.is_empty());
    }

    #[test]
    fn a_body_past_the_last_part_stops_at_the_probe_byte() {
        assert!(read_parts(&[7u8; 12], 4, 3).is_ok());
        match read_parts(&[7u8; 13], 4, 3) {
            Err(Stop::TooMany {
                max_parts: 3,
                part_size: 4,
            }) => {}
            Err(Stop::TooMany { .. }) => panic!("another limit"),
            Err(Stop::Io(e)) => panic!("{e}"),
            Ok(parts) => panic!("{} parts", parts.len()),
        }
        let err = Stop::TooMany {
            max_parts: 3,
            part_size: 4,
        }
        .error("repo/config");
        assert!(matches!(err, Error::LimitExceeded(_)), "{err}");
        let text = err.to_string();
        assert!(
            text.contains("repo/config") && text.contains("3 parts of 4 bytes"),
            "{text}"
        );
    }

    #[test]
    fn a_part_holds_no_capacity_past_the_part_size() {
        // The first part grows from 8 KiB: a small body takes a small
        // buffer.
        let small = read_parts(&[1u8; 100], 1 << 20, 3).ok().unwrap();
        assert_eq!(small[0].bytes.len(), 100);
        assert_eq!(small[0].bytes.capacity(), FIRST_READ as usize);
        // A part size that is not a power of two: the doubling stops at the
        // part size, and a later part with no buffer to take back takes the
        // capacity of a whole part at once.
        let size = 3 * FIRST_READ as usize + 5;
        let data = vec![2u8; 2 * size + 1];
        let parts = read_parts(&data, size, 3).ok().unwrap();
        assert_eq!(parts.len(), 3);
        for part in &parts[..2] {
            assert_eq!(part.bytes.len(), size);
            assert_eq!(part.bytes.capacity(), size);
        }
        assert_eq!(parts[2].bytes, [2u8]);
        assert_eq!(parts[2].bytes.capacity(), size);
    }

    #[test]
    fn a_later_part_reads_into_the_buffer_that_came_back() {
        /// A reader of `data` that counts the zero bytes of each buffer of
        /// more than one byte that it gets. The probe after a full part
        /// reads into a buffer of one byte.
        struct Counting {
            data: futures_lite::io::Cursor<Vec<u8>>,
            zeros: usize,
        }
        impl AsyncRead for Counting {
            fn poll_read(
                mut self: Pin<&mut Self>,
                cx: &mut Context<'_>,
                buf: &mut [u8],
            ) -> Poll<std::io::Result<usize>> {
                if buf.len() > 1 {
                    self.zeros += buf.iter().filter(|byte| **byte == 0).count();
                }
                Pin::new(&mut self.data).poll_read(cx, buf)
            }
        }
        let size = 3 * FIRST_READ as usize + 5;
        let data: Vec<u8> = (0..3 * size + 1).map(|at| (at % 255) as u8 + 1).collect();
        ostrya_rt::block_on(async {
            let mut parts = Parts::new(
                Counting {
                    data: futures_lite::io::Cursor::new(data.clone()),
                    zeros: 0,
                },
                size,
                4,
            );
            // The first part grows, and the fill of zeros comes with the
            // growth.
            let first = parts.next().await.ok().unwrap();
            let zeros = parts.body.zeros;
            assert!(zeros > 0);
            let address = first.bytes.as_ptr();
            // No copy shares the body, so the second part reads into the
            // same buffer with no fill of zeros.
            parts.recycle(UploadBody::bytes(first.bytes));
            let second = parts.next().await.ok().unwrap();
            assert_eq!(second.bytes, data[size..2 * size]);
            assert_eq!(second.bytes.as_ptr(), address);
            assert_eq!(second.bytes.capacity(), size);
            assert_eq!(parts.body.zeros, zeros);
            // A copy shares the body, so the third part takes a new buffer.
            let body = UploadBody::bytes(second.bytes);
            let shared = body.try_clone().unwrap();
            parts.recycle(body);
            let third = parts.next().await.ok().unwrap();
            assert_eq!(third.bytes, data[2 * size..3 * size]);
            assert_ne!(third.bytes.as_ptr(), address);
            assert_eq!(third.bytes.capacity(), size);
            assert_eq!(shared.as_bytes(), Some(&data[size..2 * size]));
            let zeros = parts.body.zeros;
            // A short last part reads into the buffer that came back, with
            // no fill of zeros.
            let address = third.bytes.as_ptr();
            parts.recycle(UploadBody::bytes(third.bytes));
            let last = parts.next().await.ok().unwrap();
            assert!(last.last);
            assert_eq!(last.bytes, data[3 * size..]);
            assert_eq!(last.bytes.as_ptr(), address);
            assert_eq!(last.bytes.capacity(), size);
            assert_eq!(parts.body.zeros, zeros);
        });
    }

    #[test]
    fn a_body_over_the_limit_comes_back_from_the_pool_with_its_digest() {
        ostrya_rt::block_on(async {
            let small = upload_body(vec![1; UNBLOCK_SIZE]).await;
            assert_eq!(small.sha256(), None);
            assert_eq!(small.as_bytes().map(<[u8]>::len), Some(UNBLOCK_SIZE));

            let mut bytes = Vec::with_capacity(UNBLOCK_SIZE + 8);
            bytes.resize(UNBLOCK_SIZE + 1, 2);
            let digest: [u8; 32] = Sha256::digest(&bytes).into();
            let (address, capacity) = (bytes.as_ptr(), bytes.capacity());
            let body = upload_body(bytes).await;
            assert_eq!(body.sha256(), Some(digest));
            assert_eq!(body.as_bytes().map(<[u8]>::as_ptr), Some(address));
            // The buffer of the body comes back for the next part.
            let mut parts = Parts::new(&[][..], UNBLOCK_SIZE + 1, 2);
            parts.recycle(body);
            let spare = parts.spare.take().expect("the buffer comes back");
            assert_eq!(spare.as_ptr(), address);
            assert_eq!(spare.capacity(), capacity);
        });
    }

    #[test]
    fn a_failed_read_of_the_body_stops_the_parts() {
        /// A reader that gives four bytes and then fails.
        struct Failing {
            given: bool,
        }
        impl AsyncRead for Failing {
            fn poll_read(
                mut self: Pin<&mut Self>,
                _: &mut Context<'_>,
                buf: &mut [u8],
            ) -> Poll<std::io::Result<usize>> {
                if self.given {
                    return Poll::Ready(Err(std::io::Error::other("the source failed")));
                }
                self.given = true;
                let n = buf.len().min(4);
                buf[..n].fill(9);
                Poll::Ready(Ok(n))
            }
        }
        ostrya_rt::block_on(async {
            let mut parts = Parts::new(Failing { given: false }, 4, 3);
            // The probe after the full first part fails.
            let Err(Stop::Io(e)) = parts.next().await else {
                panic!("a failed read stops the parts");
            };
            assert_eq!(e.to_string(), "the source failed");
            assert!(matches!(Stop::Io(e).error("k"), Error::Io(_)));
        });
    }

    #[test]
    fn a_stream_hashes_only_at_its_exact_length() {
        ostrya_rt::block_on(async {
            let data = vec![b'h'; 3 * COMPARE_CHUNK + 7];
            let size = data.len() as u64;
            let digest: [u8; 32] = Sha256::digest(&data).into();
            let mut reader = futures_lite::io::Cursor::new(data.clone());
            assert_eq!(hash_exact(&mut reader, size).await.unwrap(), Some(digest));
            let mut reader = futures_lite::io::Cursor::new(data.clone());
            assert_eq!(hash_exact(&mut reader, size - 1).await.unwrap(), None);
            let mut reader = futures_lite::io::Cursor::new(data);
            assert_eq!(hash_exact(&mut reader, size + 1).await.unwrap(), None);
        });
    }
}