qk-cli 0.22.0

A fast standalone task runner that reads Nx workspaces
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
//! `qk store proxy`: a store server in front of another, for machines on one
//! network. Blobs never change once stored, so the proxy keeps those it passes
//! on its own disk and serves them from there. It takes uploads of blobs,
//! entries and warm state at the network's speed: it keeps them, queues them
//! on its disk and answers at once, sending them upstream in the background.
//! Every other request goes to the upstream server as it came, so reads of
//! records that change and the index stay the upstream's. Runners point
//! `QK_STORE_SERVER_URL` at the proxy and keep their tokens, which the
//! upstream checks.

use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant, SystemTime};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

/// The response headers the protocol gives meaning to, passed on as they came.
const PASSED_HEADERS: [&str; 4] = [
    "content-type",
    "retry-after",
    "qk-index-cursor",
    "server-timing",
];

/// The largest request body the proxy takes, and the longest request or
/// header line, past which a request is refused rather than buffered.
const MAX_BODY: usize = 1 << 30;
const MAX_LINE: u64 = 64 << 10;
const MAX_HEADERS: usize = 100;
/// How long a connection may wait for a client to send or take bytes.
const IDLE: Duration = Duration::from_secs(60);
/// The largest bundle a store server takes, as the protocol bounds an upload
/// body.
const LARGEST_BUNDLE: usize = 100 << 20;
/// Queued uploads sent upstream at once.
const UPLOADERS: usize = 32;
/// Blobs asked about in one `POST blobs/missing`, of the 4,096 the protocol
/// allows.
const MISSING_BATCH: usize = 1024;
/// The longest pause before sending a queued upload again; the pause doubles
/// from a second up to it.
const RETRY_PAUSE: Duration = Duration::from_secs(60);
/// Times the upstream may answer that it lacks a record's blobs, each sent
/// again, before the record is given up.
const MISSING_ROUNDS: u32 = 3;

pub struct Options {
    pub upstream: String,
    pub directory: PathBuf,
    pub listen: String,
    pub max_size: Option<u64>,
}

struct Proxy {
    agent: ureq::Agent,
    upstream: String,
    blobs: PathBuf,
    scratch: PathBuf,
    /// Uploads answered for and not yet sent upstream, each a job file named
    /// by its place in the queue and, for a record, its body beside it.
    queue: PathBuf,
    /// The newest upload of each warm record sent upstream, named by a hash
    /// of its path, which the proxy serves without asking the upstream: warm
    /// state is never current, only recent, so a save the proxy holds serves
    /// as well as a newer one elsewhere.
    records: PathBuf,
    next_job: AtomicU64,
    jobs: Mutex<VecDeque<u64>>,
    job_ready: Condvar,
    /// Blobs queued for upload, which eviction leaves alone, with the ids of
    /// the jobs that send them.
    queued_blobs: Mutex<HashMap<String, BTreeSet<u64>>>,
    /// Signalled as each queued blob is sent or given up.
    blob_done: Condvar,
    /// The newest queued upload of each entry and warm record, by path, which
    /// the proxy serves until the upstream has it.
    queued_records: Mutex<HashMap<String, u64>>,
    /// The paths of records being sent now, which another upload of the same
    /// path waits for, so that the upstream takes them in order.
    sending_records: Mutex<HashSet<String>>,
    record_sent: Condvar,
    /// Whether the upstream holds queued blobs, as asked for many at once.
    upstream_holds: Mutex<HashMap<String, bool>>,
    /// Whether the upstream turned a bundle away, as one that takes none.
    /// Bundles go to it as they came from then on, so that clients see its
    /// refusal and send their blobs one each.
    upstream_refuses_bundles: AtomicBool,
    max_size: Option<u64>,
    /// Tokens the upstream accepted, which may read blobs held here.
    trusted: Mutex<HashSet<String>>,
    /// Tokens the upstream took a write from, whose uploads the proxy answers
    /// for and queues.
    writers: Mutex<HashSet<String>>,
    /// Held while blobs are evicted, so two connections do not both evict.
    evicting: Mutex<()>,
    size: AtomicU64,
    stats: Stats,
}

#[derive(Default)]
struct Stats {
    local_hits: AtomicU64,
    local_bytes: AtomicU64,
    upstream_requests: AtomicU64,
    upstream_bytes: AtomicU64,
    stored: AtomicU64,
    evicted: AtomicU64,
    queued: AtomicU64,
    uploaded: AtomicU64,
    dropped: AtomicU64,
    superseded: AtomicU64,
}

pub fn serve(options: Options) -> Result<()> {
    let proxy = Proxy::open(&options)?;
    let resumed = proxy.resume_queue()?;
    // Within a limit lowered since it last ran, the queue held.
    proxy.evict();
    for _ in 0..UPLOADERS {
        let proxy = proxy.clone();
        std::thread::spawn(move || proxy.upload_queued());
    }
    let listener = TcpListener::bind(&options.listen)
        .with_context(|| format!("cannot listen on {}", options.listen))?;
    eprintln!(
        "qk: store proxy for {} listening on http://{}, holding {} in {}, {resumed} uploads queued",
        proxy.upstream,
        listener.local_addr()?,
        bytes(proxy.size.load(Ordering::Relaxed)),
        options.directory.display()
    );
    for stream in listener.incoming() {
        let Ok(stream) = stream else { continue };
        let proxy = proxy.clone();
        std::thread::spawn(move || {
            let _ = connection(stream, &proxy);
        });
    }
    Ok(())
}

impl Proxy {
    /// The proxy keeping its blobs and queue in `options.directory`.
    fn open(options: &Options) -> Result<Arc<Self>> {
        let blobs = options.directory.join("blobs");
        let scratch = options.directory.join("tmp");
        let queue = options.directory.join("queue");
        let records = options.directory.join("records");
        fs::create_dir_all(&blobs).with_context(|| format!("cannot create {}", blobs.display()))?;
        fs::create_dir_all(&scratch)?;
        fs::create_dir_all(&queue)?;
        fs::create_dir_all(&records)?;
        // Partial downloads of an earlier run are no blob.
        for entry in fs::read_dir(&scratch)?.flatten() {
            let _ = fs::remove_file(entry.path());
        }
        let size = [&blobs, &records]
            .into_iter()
            .flat_map(|directory| fs::read_dir(directory).into_iter().flatten().flatten())
            .filter_map(|entry| entry.metadata().ok())
            .map(|metadata| metadata.len())
            .sum();
        let agent: ureq::Agent = ureq::Agent::config_builder()
            .http_status_as_error(false)
            .max_redirects(0)
            .timeout_connect(Some(Duration::from_secs(10)))
            // A queued upload the upstream never answers holds an uploader no
            // longer than this.
            .timeout_recv_response(Some(Duration::from_secs(300)))
            .build()
            .into();
        let proxy = Arc::new(Proxy {
            agent,
            upstream: options.upstream.trim_end_matches('/').to_owned(),
            blobs,
            scratch,
            queue,
            records,
            next_job: AtomicU64::new(0),
            jobs: Mutex::new(VecDeque::new()),
            job_ready: Condvar::new(),
            queued_blobs: Mutex::new(HashMap::new()),
            blob_done: Condvar::new(),
            queued_records: Mutex::new(HashMap::new()),
            sending_records: Mutex::new(HashSet::new()),
            record_sent: Condvar::new(),
            upstream_holds: Mutex::new(HashMap::new()),
            upstream_refuses_bundles: AtomicBool::new(false),
            max_size: options.max_size,
            trusted: Mutex::new(HashSet::new()),
            writers: Mutex::new(HashSet::new()),
            evicting: Mutex::new(()),
            size: AtomicU64::new(size),
            stats: Stats::default(),
        });
        Ok(proxy)
    }
}

#[derive(Debug)]
struct Request {
    method: String,
    /// The path and query after the host, such as `/v4/blobs/<hash>`.
    target: String,
    authorization: Option<String>,
    content_type: Option<String>,
    body: Vec<u8>,
    close: bool,
}

/// Serves the requests of one connection until the client closes it.
fn connection(stream: TcpStream, proxy: &Proxy) -> Result<()> {
    stream.set_nodelay(true)?;
    // A client that stalls holds a thread no longer than this.
    stream.set_read_timeout(Some(IDLE))?;
    stream.set_write_timeout(Some(IDLE))?;
    let mut reader = BufReader::new(stream.try_clone()?);
    let mut writer = io::BufWriter::new(stream);
    while let Some(request) = read_request(&mut reader)? {
        let started = Instant::now();
        let (status, source, sent) = handle(proxy, &request, &mut writer)?;
        writer.flush()?;
        eprintln!(
            "qk: {} {} {status} {source} {} {}ms",
            request.method,
            request.target,
            bytes(sent),
            started.elapsed().as_millis()
        );
        if request.close {
            break;
        }
    }
    Ok(())
}

/// One line of the request, without its line ending; `None` at the end of
/// the stream. A line longer than [`MAX_LINE`] is refused.
fn read_line(reader: &mut impl BufRead) -> Result<Option<String>> {
    let mut line = String::new();
    if reader.by_ref().take(MAX_LINE + 1).read_line(&mut line)? == 0 {
        return Ok(None);
    }
    if !line.ends_with('\n') {
        bail!("a request line is too long or cut short");
    }
    Ok(Some(line.trim_end_matches(['\r', '\n']).to_owned()))
}

/// The next request on a connection. Framing that two readers could take
/// differently is refused, as a front proxy might frame the same bytes
/// otherwise and smuggle a request past it: conflicting lengths, a length
/// beside a transfer encoding, or any transfer encoding but `chunked`.
fn read_request(reader: &mut impl BufRead) -> Result<Option<Request>> {
    let Some(line) = read_line(reader)? else {
        return Ok(None);
    };
    let mut parts = line.split_whitespace();
    let (Some(method), Some(target)) = (parts.next(), parts.next()) else {
        bail!("malformed request line");
    };
    // Only a path: the target is appended to the upstream's address, where
    // anything else could name another host.
    if !target.starts_with('/') {
        bail!("unsupported request target {target:?}");
    }
    let (method, target) = (method.to_owned(), target.to_owned());
    let mut length: Option<usize> = None;
    let mut encoding: Option<String> = None;
    let mut authorization = None;
    let mut content_type = None;
    let mut close = false;
    for count in 0.. {
        if count == MAX_HEADERS {
            bail!("too many headers");
        }
        let header = read_line(reader)?.context("the request ends in its headers")?;
        if header.is_empty() {
            break;
        }
        // A line without a colon, as a folded continuation, or a name with
        // whitespace before its colon, could be read otherwise by a proxy in
        // front, which would frame the body differently (RFC 9112 5.1).
        let Some((name, value)) = header.split_once(':') else {
            bail!("malformed header line");
        };
        if name.is_empty() || name.bytes().any(|byte| byte.is_ascii_whitespace()) {
            bail!("malformed header name {name:?}");
        }
        let value = value.trim();
        match name.to_ascii_lowercase().as_str() {
            "content-length" => {
                let parsed: usize = value.parse().context("bad content-length")?;
                if length.is_some_and(|known| known != parsed) {
                    bail!("conflicting content-length headers");
                }
                length = Some(parsed);
            }
            "transfer-encoding" => {
                if encoding.is_some() {
                    bail!("more than one transfer-encoding header");
                }
                encoding = Some(value.to_owned());
            }
            "authorization" => authorization = Some(value.to_owned()),
            "content-type" => content_type = Some(value.to_owned()),
            "connection" => close = value.eq_ignore_ascii_case("close"),
            _ => {}
        }
    }
    let body = match (encoding, length) {
        (Some(_), Some(_)) => bail!("both content-length and transfer-encoding"),
        (Some(encoding), None) if encoding.eq_ignore_ascii_case("chunked") => read_chunked(reader)?,
        (Some(encoding), None) => bail!("unsupported transfer-encoding {encoding:?}"),
        (None, length) => {
            let length = length.unwrap_or(0);
            if length > MAX_BODY {
                bail!("a request body of {length} bytes is larger than the proxy takes");
            }
            read_body(reader, length)?
        }
    };
    Ok(Some(Request {
        method,
        target,
        authorization,
        content_type,
        body,
        close,
    }))
}

/// Exactly `length` bytes, the buffer growing only as they arrive.
fn read_body(reader: &mut impl BufRead, length: usize) -> Result<Vec<u8>> {
    let mut body = Vec::new();
    reader.by_ref().take(length as u64).read_to_end(&mut body)?;
    if body.len() != length {
        bail!("the request body ends early");
    }
    Ok(body)
}

fn read_chunked(reader: &mut impl BufRead) -> Result<Vec<u8>> {
    let mut body = Vec::new();
    loop {
        let line = read_line(reader)?.context("the request ends in a chunk")?;
        let size = usize::from_str_radix(line.trim().split(';').next().unwrap_or(""), 16)
            .context("bad chunk size")?;
        if size == 0 {
            // Trailers, then the blank line that ends them.
            loop {
                let line = read_line(reader)?.context("the request ends in its trailers")?;
                if line.trim().is_empty() {
                    return Ok(body);
                }
            }
        }
        if body.len().saturating_add(size) > MAX_BODY {
            bail!("a chunked request body is larger than the proxy takes");
        }
        body.extend(read_body(reader, size)?);
        read_line(reader)?.context("the request ends after a chunk")?;
    }
}

/// Answers one request, and says where the answer came from and how many
/// bytes of body it sent.
fn handle(
    proxy: &Proxy,
    request: &Request,
    writer: &mut impl Write,
) -> Result<(u16, &'static str, u64)> {
    let path = record_path(&request.target);
    let record = path.starts_with("/v4/entries/") || path.starts_with("/v4/warm/");
    // Many small blobs as one upload body, which the proxy takes as a record
    // whose blobs records citing them wait for.
    let bundle = path.starts_with("/v4/bundles/");
    if request.method == "GET" && path == "/proxy/stats" {
        let body = proxy.stats_json().into_bytes();
        head(
            writer,
            200,
            &[("content-type", "application/json")],
            Some(body.len() as u64),
        )?;
        writer.write_all(&body)?;
        return Ok((200, "proxy", body.len() as u64));
    }
    let blob = path
        .strip_prefix("/v4/blobs/")
        .filter(|hash| hash.len() == 64 && hash.bytes().all(|byte| byte.is_ascii_hexdigit()));
    if let Some(hash) = blob
        && matches!(request.method.as_str(), "GET" | "HEAD")
        && proxy.trusts(request)
        && let Ok(file) = File::open(proxy.blobs.join(hash))
    {
        let length = file.metadata()?.len();
        // A hit counts as a use, which eviction goes by.
        let _ = file.set_modified(SystemTime::now());
        head(
            writer,
            200,
            &[("content-type", "application/zstd")],
            Some(length),
        )?;
        let mut sent = 0;
        if request.method == "GET" {
            sent = io::copy(&mut BufReader::new(file), writer)?;
            proxy.stats.local_hits.fetch_add(1, Ordering::Relaxed);
            proxy.stats.local_bytes.fetch_add(sent, Ordering::Relaxed);
        }
        return Ok((200, "local", sent));
    }
    // An entry or warm record still queued is the newest; the upstream has
    // an older one or none. A warm record sent on since is kept and served
    // too, without asking the upstream for a newer one.
    if record
        && matches!(request.method.as_str(), "GET" | "HEAD")
        && proxy.trusts(request)
        && let Some((pack, source)) = proxy
            .queued_pack(path)
            .map(|pack| (pack, "queued"))
            .or_else(|| {
                path.starts_with("/v4/warm/")
                    .then(|| proxy.kept_pack(path))
                    .flatten()
                    .map(|pack| (pack, "kept"))
            })
    {
        head(
            writer,
            200,
            &[("content-type", "application/x-qk-pack")],
            Some(pack.len() as u64),
        )?;
        let mut sent = 0;
        if request.method == "GET" {
            writer.write_all(&pack)?;
            sent = pack.len() as u64;
            proxy.stats.local_hits.fetch_add(1, Ordering::Relaxed);
            proxy.stats.local_bytes.fetch_add(sent, Ordering::Relaxed);
        }
        return Ok((200, source, sent));
    }
    // A client asks before uploading a blob. Uploading one the upstream has
    // costs only the local network, and the proxy asks the upstream itself
    // before sending it on.
    if blob.is_some() && request.method == "HEAD" && proxy.trusts(request) {
        head(writer, 404, &[], Some(0))?;
        return Ok((404, "local", 0));
    }
    // An upload from a token the upstream accepted is kept, queued and
    // answered at once. Others go upstream as they came, the answer being
    // the upstream's to give.
    if request.method == "PUT" && proxy.writes(request) && (blob.is_some() || record || bundle) {
        let queued = match blob {
            Some(hash) => proxy.queue_blob(request, hash).unwrap_or(false),
            // A bundle is queued first, which holds its blobs against
            // eviction while they are kept; one that does not parse, or is
            // past what a store server takes, goes upstream as it came, for
            // the upstream to refuse.
            None if bundle && proxy.upstream_refuses_bundles.load(Ordering::Relaxed) => false,
            None if bundle => match bundle_blobs(path, &request.body) {
                Some(blobs) => {
                    let queued = proxy.enqueue(request, None).is_ok();
                    if queued {
                        for (hash, frame) in blobs {
                            proxy.keep(&hash, frame);
                        }
                    }
                    queued
                }
                None => false,
            },
            None => {
                for (hash, frame) in inline_blobs(&request.body).unwrap_or_default() {
                    proxy.keep(&hash, frame);
                }
                proxy.enqueue(request, None).is_ok()
            }
        };
        if queued {
            head(writer, 204, &[], Some(0))?;
            return Ok((204, "queued", 0));
        }
    }
    let keep = match (request.method.as_str(), blob) {
        ("GET", Some(hash)) => Some(hash),
        _ => None,
    };
    let (status, sent) = proxy.forward(request, writer, keep)?;
    if request.method == "PUT"
        && (200..300).contains(&status)
        && let Some(token) = &request.authorization
    {
        proxy.writers.lock().unwrap().insert(token.clone());
    }
    // An upload the upstream took is kept too, for the runners that fetch it:
    // a blob on its own, or those a result or warm record carries inline.
    // The client has its answer first.
    if request.method == "PUT" && status == 204 {
        writer.flush()?;
        if let Some(hash) = blob {
            proxy.keep(hash, &request.body);
        } else if record || bundle {
            for (hash, frame) in inline_blobs(&request.body).unwrap_or_default() {
                proxy.keep(&hash, frame);
            }
            // An older one kept is no longer what the upstream holds. Not
            // replaced, as two such uploads may be answered in either order;
            // the next read goes upstream.
            if path.starts_with("/v4/warm/") {
                proxy.forget_record(path);
            }
        }
    }
    Ok((status, "upstream", sent))
}

impl Proxy {
    fn trusts(&self, request: &Request) -> bool {
        request
            .authorization
            .as_ref()
            .is_some_and(|token| self.trusted.lock().unwrap().contains(token))
    }

    /// Whether the upstream has taken a write from the request's token. A
    /// token that may only read is trusted all the same, and the upstream
    /// would refuse its uploads after the proxy had answered for them.
    fn writes(&self, request: &Request) -> bool {
        request
            .authorization
            .as_ref()
            .is_some_and(|token| self.writers.lock().unwrap().contains(token))
    }

    /// Sends the request upstream and passes its answer back, keeping the body
    /// as the blob `keep` when the upstream has it.
    fn forward(
        &self,
        request: &Request,
        writer: &mut impl Write,
        keep: Option<&str>,
    ) -> Result<(u16, u64)> {
        self.stats.upstream_requests.fetch_add(1, Ordering::Relaxed);
        let mut builder = ureq::http::Request::builder()
            .method(request.method.as_str())
            .uri(format!("{}{}", self.upstream, request.target));
        if let Some(token) = &request.authorization {
            builder = builder.header("authorization", token);
        }
        if let Some(content_type) = &request.content_type {
            builder = builder.header("content-type", content_type);
        }
        let response = self.agent.run(builder.body(request.body.clone())?);
        let mut response = match response {
            Ok(response) => response,
            Err(error) => {
                let body = format!(r#"{{"error":"busy","message":"upstream: {error}"}}"#);
                head(
                    writer,
                    503,
                    &[("content-type", "application/json"), ("retry-after", "5")],
                    Some(body.len() as u64),
                )?;
                writer.write_all(body.as_bytes())?;
                return Ok((503, body.len() as u64));
            }
        };
        let status = response.status().as_u16();
        if status != 401
            && let Some(token) = &request.authorization
        {
            self.trusted.lock().unwrap().insert(token.clone());
        }
        let headers: Vec<(&str, String)> = PASSED_HEADERS
            .iter()
            .filter_map(|name| {
                response
                    .headers()
                    .get(*name)
                    .and_then(|value| value.to_str().ok())
                    .map(|value| (*name, value.to_owned()))
            })
            .collect();
        let headers: Vec<(&str, &str)> = headers
            .iter()
            .map(|(name, value)| (*name, value.as_str()))
            .collect();
        if request.method == "HEAD" {
            head(writer, status, &headers, Some(0))?;
            return Ok((status, 0));
        }
        let mut body = response.body_mut().as_reader();
        let mut copy = match keep {
            Some(_) if status == 200 => tempfile::NamedTempFile::new_in(&self.scratch).ok(),
            _ => None,
        };
        // Sent in chunks as it arrives: the upstream's length may be unknown.
        head(writer, status, &headers, None)?;
        let mut buffer = vec![0; 256 << 10];
        let mut sent = 0;
        loop {
            let read = body.read(&mut buffer)?;
            if read == 0 {
                break;
            }
            write!(writer, "{read:x}\r\n")?;
            writer.write_all(&buffer[..read])?;
            writer.write_all(b"\r\n")?;
            if let Some(file) = &mut copy
                && file.write_all(&buffer[..read]).is_err()
            {
                copy = None;
            }
            sent += read as u64;
        }
        writer.write_all(b"0\r\n\r\n")?;
        // The client has its answer before the blob is checked and kept.
        writer.flush()?;
        self.stats.upstream_bytes.fetch_add(sent, Ordering::Relaxed);
        if let (Some(hash), Some(file)) = (keep, copy) {
            self.store(hash, file);
        }
        Ok((status, sent))
    }

    /// Whether the proxy holds the blob already, which then counts as a use.
    fn held(&self, hash: &str) -> bool {
        File::options()
            .write(true)
            .open(self.blobs.join(hash))
            .is_ok_and(|file| file.set_modified(SystemTime::now()).is_ok())
    }

    /// Keeps a blob the proxy holds in memory, answering whether it holds it.
    fn keep(&self, hash: &str, frame: &[u8]) -> bool {
        if self.held(hash) {
            return true;
        }
        let Ok(mut file) = tempfile::NamedTempFile::new_in(&self.scratch) else {
            return false;
        };
        file.write_all(frame).is_ok() && self.store(hash, file)
    }

    /// Moves a downloaded frame into place once it decodes to its hash: the
    /// proxy serves what it keeps without asking the upstream again. Answers
    /// whether it holds the blob.
    fn store(&self, hash: &str, file: tempfile::NamedTempFile) -> bool {
        // Kept meanwhile, as by another connection: counted once.
        if self.held(hash) {
            return true;
        }
        let verified = File::open(file.path()).ok().is_some_and(|frame| {
            let mut hasher = blake3::Hasher::new();
            zstd::stream::read::Decoder::new(frame)
                .and_then(|mut decoder| io::copy(&mut decoder, &mut hasher))
                .is_ok()
                && hasher.finalize().to_hex().as_str() == hash
        });
        if !verified {
            return false;
        }
        let length = file
            .as_file()
            .metadata()
            .map(|metadata| metadata.len())
            .unwrap_or(0);
        if file.persist(self.blobs.join(hash)).is_err() {
            return false;
        }
        self.stats.stored.fetch_add(1, Ordering::Relaxed);
        let size = self.size.fetch_add(length, Ordering::Relaxed) + length;
        if self.max_size.is_some_and(|max| size > max) {
            self.evict();
        }
        true
    }

    /// Removes the least recently used blobs until the proxy holds at most 90%
    /// of its limit.
    fn evict(&self) {
        let Some(max) = self.max_size else { return };
        let Ok(_guard) = self.evicting.try_lock() else {
            return;
        };
        // Blobs and kept records together, the least recently used first.
        let mut blobs: Vec<(SystemTime, u64, PathBuf)> = [&self.blobs, &self.records]
            .into_iter()
            .flat_map(|directory| fs::read_dir(directory).into_iter().flatten().flatten())
            .filter_map(|entry| {
                let metadata = entry.metadata().ok()?;
                Some((metadata.modified().ok()?, metadata.len(), entry.path()))
            })
            .collect();
        blobs.sort();
        let mut size: u64 = blobs.iter().map(|(_, length, _)| length).sum();
        let target = max / 10 * 9;
        for (_, length, path) in blobs {
            if size <= target {
                break;
            }
            // Its upload is still to be sent from here. Held while the blob is
            // removed, so that an upload queuing it meanwhile either finds it
            // gone, and keeps its own, or holds it before it can go.
            let queued = self.queued_blobs.lock().unwrap();
            let held = path
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|name| queued.contains_key(name));
            if held {
                continue;
            }
            if fs::remove_file(&path).is_ok() {
                size -= length;
                self.stats.evicted.fetch_add(1, Ordering::Relaxed);
            }
            drop(queued);
        }
        self.size.store(size, Ordering::Relaxed);
    }

    fn stats_json(&self) -> String {
        let stats = &self.stats;
        let load = |counter: &AtomicU64| counter.load(Ordering::Relaxed);
        serde_json::json!({
            "localHits": load(&stats.local_hits),
            "localBytes": load(&stats.local_bytes),
            "upstreamRequests": load(&stats.upstream_requests),
            "upstreamBytes": load(&stats.upstream_bytes),
            "stored": load(&stats.stored),
            "evicted": load(&stats.evicted),
            "queued": load(&stats.queued),
            "uploaded": load(&stats.uploaded),
            "dropped": load(&stats.dropped),
            "superseded": load(&stats.superseded),
            "size": load(&self.size),
        })
        .to_string()
    }
}

/// An upload the proxy answered for and sends upstream in the background.
#[derive(Serialize, Deserialize)]
struct Job {
    /// The path it is sent to, such as `/v4/blobs/<hash>`.
    target: String,
    authorization: String,
    content_type: Option<String>,
    /// The blob it sends, which the proxy holds; a record's body is kept
    /// beside the job instead.
    blob: Option<String>,
}

/// One upload's turn to send the record at `path`.
struct RecordTurn<'a> {
    proxy: &'a Proxy,
    path: String,
}

impl Drop for RecordTurn<'_> {
    fn drop(&mut self) {
        self.proxy
            .sending_records
            .lock()
            .unwrap()
            .remove(&self.path);
        self.proxy.record_sent.notify_all();
    }
}

/// What sending a queued upload came to.
enum Sent {
    Done(u16),
    /// The upstream will not take it, as for a token it refuses.
    Refused(String),
    /// The upstream lacked blobs the record cites; those the proxy held are
    /// sent now, and the record is to be sent again.
    Missing,
    /// It may take it later, as when it could not be reached.
    Later(String),
}

impl Proxy {
    fn job_path(&self, id: u64) -> PathBuf {
        self.queue.join(format!("{id:020}.json"))
    }

    fn body_path(&self, id: u64) -> PathBuf {
        self.queue.join(format!("{id:020}.body"))
    }

    /// Queues `request` to be sent upstream, with the body kept beside it
    /// unless it sends the held `blob`. On disk before the client is answered,
    /// so a proxy that stops sends it once started again.
    fn enqueue(&self, request: &Request, blob: Option<&str>) -> Result<()> {
        if let Some(hash) = blob
            && self.queued_blobs.lock().unwrap().contains_key(hash)
        {
            return Ok(());
        }
        let job = Job {
            target: request.target.clone(),
            authorization: request
                .authorization
                .clone()
                .context("an upload without a token")?,
            content_type: request.content_type.clone(),
            blob: blob.map(str::to_owned),
        };
        let id = self.next_job.fetch_add(1, Ordering::Relaxed);
        self.write_job(id, &job, blob.is_none().then_some(&request.body[..]))?;
        self.admit(id, &job);
        Ok(())
    }

    /// Keeps the blob `request` uploads and queues sending it on, answering
    /// whether it is queued. The blob is held against eviction from before it
    /// is kept, as keeping it can evict and so can another connection meanwhile,
    /// which would lose an upload the client was told the proxy took.
    fn queue_blob(&self, request: &Request, hash: &str) -> Result<bool> {
        let id = {
            let mut queued = self.queued_blobs.lock().unwrap();
            if queued.contains_key(hash) {
                // Queued already, and so held.
                drop(queued);
                return Ok(self.keep(hash, &request.body));
            }
            let id = self.next_job.fetch_add(1, Ordering::Relaxed);
            queued.entry(hash.to_owned()).or_default().insert(id);
            id
        };
        let unpin = || {
            let mut queued = self.queued_blobs.lock().unwrap();
            if let Some(ids) = queued.get_mut(hash) {
                ids.remove(&id);
                if ids.is_empty() {
                    queued.remove(hash);
                }
            }
            drop(queued);
            self.blob_done.notify_all();
        };
        if !self.keep(hash, &request.body) {
            unpin();
            return Ok(false);
        }
        let job = Job {
            target: request.target.clone(),
            authorization: request
                .authorization
                .clone()
                .context("an upload without a token")?,
            content_type: request.content_type.clone(),
            blob: Some(hash.to_owned()),
        };
        if let Err(error) = self.write_job(id, &job, None) {
            unpin();
            return Err(error);
        }
        self.admit_held(id, &job);
        Ok(true)
    }

    /// Writes the job file of `id`, and the record body it sends unless it
    /// sends a held blob. Only its owner may read a job file, which holds the
    /// client's token.
    fn write_job(&self, id: u64, job: &Job, body: Option<&[u8]>) -> Result<()> {
        let write = |path: PathBuf, bytes: &[u8]| -> Result<()> {
            let mut file = tempfile::NamedTempFile::new_in(&self.queue)?;
            file.write_all(bytes)?;
            file.persist(path)?;
            Ok(())
        };
        if let Some(body) = body {
            write(self.body_path(id), body)?;
        }
        write(self.job_path(id), &serde_json::to_vec(job)?)
    }

    fn admit(&self, id: u64, job: &Job) {
        let mut queued = self.queued_blobs.lock().unwrap();
        for hash in self.job_blobs(id, job) {
            queued.entry(hash).or_default().insert(id);
        }
        drop(queued);
        self.admit_held(id, job);
    }

    /// The blobs job `id` sends: the one it uploads on its own, or those its
    /// bundle carries. A record citing them waits for it.
    fn job_blobs(&self, id: u64, job: &Job) -> Vec<String> {
        match &job.blob {
            Some(hash) => vec![hash.clone()],
            None if is_bundle(&job.target) => fs::read(self.body_path(id))
                .ok()
                .and_then(|body| {
                    inline_blobs(&body)
                        .map(|blobs| blobs.into_iter().map(|(hash, _)| hash).collect())
                })
                .unwrap_or_default(),
            None => Vec::new(),
        }
    }

    /// Queues `id`, whose blob, if it sends one, is held already.
    fn admit_held(&self, id: u64, job: &Job) {
        // Queued in order, so the last admitted is the newest.
        if job.blob.is_none() && !is_bundle(&job.target) {
            self.queued_records
                .lock()
                .unwrap()
                .insert(record_path(&job.target).to_owned(), id);
        }
        self.stats.queued.fetch_add(1, Ordering::Relaxed);
        self.jobs.lock().unwrap().push_back(id);
        self.job_ready.notify_one();
    }

    /// Queues again, in their order, the uploads an earlier proxy left, and
    /// answers how many.
    fn resume_queue(&self) -> Result<usize> {
        let mut ids = Vec::new();
        for entry in fs::read_dir(&self.queue)?.flatten() {
            let name = entry.file_name();
            let name = name.to_string_lossy();
            match name
                .strip_suffix(".json")
                .and_then(|id| id.parse::<u64>().ok())
            {
                Some(id) => ids.push(id),
                // Bodies whose jobs are gone, and files never put in place.
                None if name.ends_with(".body")
                    && self.queue.join(name.replace(".body", ".json")).is_file() => {}
                None => {
                    let _ = fs::remove_file(entry.path());
                }
            }
        }
        ids.sort_unstable();
        let mut resumed = 0;
        for &id in &ids {
            let job = fs::read(self.job_path(id))
                .ok()
                .and_then(|bytes| serde_json::from_slice::<Job>(&bytes).ok());
            match job {
                Some(job) if job.blob.is_some() || self.body_path(id).is_file() => {
                    self.admit(id, &job);
                    resumed += 1;
                }
                _ => {
                    let _ = fs::remove_file(self.job_path(id));
                    let _ = fs::remove_file(self.body_path(id));
                }
            }
        }
        let next = ids.last().map_or(0, |id| id + 1);
        self.next_job.store(next, Ordering::Relaxed);
        Ok(resumed)
    }

    /// Sends queued uploads upstream, one after another, for as long as the
    /// proxy runs.
    fn upload_queued(&self) {
        loop {
            let id = {
                let mut jobs = self.jobs.lock().unwrap();
                loop {
                    if let Some(id) = jobs.pop_front() {
                        break id;
                    }
                    jobs = self.job_ready.wait(jobs).unwrap();
                }
            };
            self.upload(id);
        }
    }

    /// Sends one queued upload, again after a pause while the upstream may
    /// take it later, and removes it once sent or refused.
    fn upload(&self, id: u64) {
        let job = fs::read(self.job_path(id))
            .ok()
            .and_then(|bytes| serde_json::from_slice::<Job>(&bytes).ok());
        if let Some(job) = &job {
            if job.blob.is_none() {
                self.await_blobs(id);
            }
            let started = Instant::now();
            let mut pause = Duration::from_secs(1);
            let mut missing = 0;
            let outcome = loop {
                // A record is sent on its path's turn, unless a newer upload
                // of it has been queued since, which replaces it. A bundle is
                // named by what it holds, so it takes no turn: one sent again
                // is the same, and records waiting for the first keep waiting.
                let turn = match job.blob {
                    Some(_) => None,
                    None if is_bundle(&job.target) => None,
                    None => match self.record_turn(record_path(&job.target), id) {
                        Some(turn) => Some(turn),
                        None => {
                            self.stats.superseded.fetch_add(1, Ordering::Relaxed);
                            break "superseded by a newer upload".into();
                        }
                    },
                };
                let sent = self.send(job, id);
                // A warm record sent upstream is kept, so the proxy serves it
                // once it is no longer queued; on its turn, so that a newer
                // upload of it is kept after.
                if matches!(sent, Sent::Done(_))
                    && job.blob.is_none()
                    && record_path(&job.target).starts_with("/v4/warm/")
                {
                    self.keep_record(record_path(&job.target), &self.body_path(id));
                }
                drop(turn);
                match sent {
                    Sent::Done(status) => {
                        self.stats.uploaded.fetch_add(1, Ordering::Relaxed);
                        break status.to_string();
                    }
                    Sent::Refused(why) => {
                        self.stats.dropped.fetch_add(1, Ordering::Relaxed);
                        break format!("dropped: {why}");
                    }
                    Sent::Missing => {
                        missing += 1;
                        if missing == MISSING_ROUNDS {
                            self.stats.dropped.fetch_add(1, Ordering::Relaxed);
                            break "dropped: the upstream still lacks blobs it cites".into();
                        }
                    }
                    Sent::Later(why) => {
                        eprintln!(
                            "qk: queued PUT {} failed, again in {}s: {why}",
                            job.target,
                            pause.as_secs()
                        );
                        std::thread::sleep(pause);
                        pause = (pause * 2).min(RETRY_PAUSE);
                    }
                }
            };
            eprintln!(
                "qk: queued PUT {} {outcome} {}ms",
                job.target,
                started.elapsed().as_millis()
            );
            let blobs = self.job_blobs(id, job);
            if !blobs.is_empty() {
                let mut queued = self.queued_blobs.lock().unwrap();
                let mut sent_on = Vec::new();
                for hash in &blobs {
                    if let Some(ids) = queued.get_mut(hash) {
                        ids.remove(&id);
                        if ids.is_empty() {
                            queued.remove(hash);
                            sent_on.push(hash);
                        }
                    }
                }
                drop(queued);
                let mut holds = self.upstream_holds.lock().unwrap();
                for hash in sent_on {
                    holds.remove(hash);
                }
                drop(holds);
                self.blob_done.notify_all();
            }
            if job.blob.is_none() {
                let path = record_path(&job.target);
                let mut records = self.queued_records.lock().unwrap();
                if records.get(path) == Some(&id) {
                    records.remove(path);
                }
            }
        }
        let _ = fs::remove_file(self.job_path(id));
        let _ = fs::remove_file(self.body_path(id));
        self.stats.queued.fetch_sub(1, Ordering::Relaxed);
    }

    /// Waits until the jobs queued before the record queued as `id` that send
    /// blobs it cites stored apart are done, so the upstream does not refuse
    /// it for lacking them. A client uploads those before the record, and the
    /// queue is taken in order, so other uploaders have those jobs already. A
    /// job queued after the record, as for the same blob uploaded again, may
    /// wait for an uploader the record holds, so it is not waited for.
    fn await_blobs(&self, id: u64) {
        let Ok(body) = fs::read(self.body_path(id)) else {
            return;
        };
        let apart = apart_blobs(&body).unwrap_or_default();
        let mut queued = self.queued_blobs.lock().unwrap();
        let ahead = |queued: &HashMap<String, BTreeSet<u64>>| {
            apart.iter().any(|hash| {
                queued
                    .get(hash)
                    .and_then(|ids| ids.first())
                    .is_some_and(|first| *first < id)
            })
        };
        while ahead(&queued) {
            queued = self.blob_done.wait(queued).unwrap();
        }
    }

    /// Waits for no other upload of the record at `path` to be on its way,
    /// and takes the turn, which ends when it is dropped; or answers `None`
    /// once `id` is not the newest upload of it, as when a newer one is
    /// queued, or was sent and so left the queue.
    fn record_turn(&self, path: &str, id: u64) -> Option<RecordTurn<'_>> {
        let mut sending = self.sending_records.lock().unwrap();
        loop {
            if self.queued_records.lock().unwrap().get(path) != Some(&id) {
                return None;
            }
            if sending.insert(path.to_owned()) {
                return Some(RecordTurn {
                    proxy: self,
                    path: path.to_owned(),
                });
            }
            sending = self.record_sent.wait(sending).unwrap();
        }
    }

    /// Sends each blob the bundle of job `id` carries on its own, as an
    /// upstream that takes no bundles has them.
    fn send_bundled(&self, job: &Job, id: u64) -> Sent {
        let Ok(body) = fs::read(self.body_path(id)) else {
            return Sent::Refused("its body is gone".into());
        };
        for (hash, frame) in inline_blobs(&body).unwrap_or_default() {
            let file = tempfile::NamedTempFile::new_in(&self.scratch).and_then(|mut file| {
                file.write_all(frame)?;
                file.reopen()
            });
            let Ok(file) = file else {
                return Sent::Later(format!("blob {hash} could not be written to send"));
            };
            let response = self.put(
                &format!("/v4/blobs/{hash}"),
                &job.authorization,
                Some("application/zstd"),
                file,
            );
            match response.map(|response| response.status().as_u16()) {
                Ok(200..=299) => {}
                Ok(status) => return sent(status),
                Err(error) => return Sent::Later(error.to_string()),
            }
        }
        Sent::Done(204)
    }

    fn send(&self, job: &Job, id: u64) -> Sent {
        if let Some(hash) = &job.blob {
            return self.send_blob(hash, &job.authorization, true);
        }
        let Ok(body) = File::open(self.body_path(id)) else {
            return Sent::Refused("its body is gone".into());
        };
        let response = self.put(
            &job.target,
            &job.authorization,
            job.content_type.as_deref(),
            body,
        );
        let mut response = match response {
            Ok(response) => response,
            Err(error) => return Sent::Later(error.to_string()),
        };
        match response.status().as_u16() {
            // An entry another run committed first.
            409 if job.target.starts_with("/v4/entries/") => Sent::Done(409),
            // An upstream that takes no bundles takes their blobs one each.
            404 if is_bundle(&job.target) => {
                self.upstream_refuses_bundles.store(true, Ordering::Relaxed);
                self.send_bundled(job, id)
            }
            422 => {
                let answer: serde_json::Value = response
                    .body_mut()
                    .read_to_string()
                    .ok()
                    .and_then(|text| serde_json::from_str(&text).ok())
                    .unwrap_or(serde_json::Value::Null);
                let missing = answer["blobs"].as_array().cloned().unwrap_or_default();
                for hash in missing.iter().filter_map(serde_json::Value::as_str) {
                    let valid =
                        hash.len() == 64 && hash.bytes().all(|byte| byte.is_ascii_hexdigit());
                    if !valid || !self.blobs.join(hash).is_file() {
                        return Sent::Refused(format!("the upstream lacks blob {hash}"));
                    }
                    // The upstream just said it lacks it, whatever it said before.
                    match self.send_blob(hash, &job.authorization, false) {
                        Sent::Done(_) => {}
                        other => return other,
                    }
                }
                Sent::Missing
            }
            status => sent(status),
        }
    }

    /// Sends the held blob `hash`, unless asking the upstream finds it has it.
    fn send_blob(&self, hash: &str, authorization: &str, ask: bool) -> Sent {
        let target = format!("/v4/blobs/{hash}");
        if ask {
            match self.upstream_has(hash, authorization) {
                Ok(Ok(true)) => return Sent::Done(200),
                Ok(Ok(false)) => {}
                Ok(Err(status)) => return sent(status),
                Err(error) => return Sent::Later(error.to_string()),
            }
        }
        let Ok(file) = File::open(self.blobs.join(hash)) else {
            return Sent::Refused("the blob is gone".into());
        };
        match self.put(&target, authorization, Some("application/zstd"), file) {
            Ok(response) => sent(response.status().as_u16()),
            Err(error) => Sent::Later(error.to_string()),
        }
    }

    /// Whether the upstream has the blob `hash`. Asked for many queued blobs at
    /// once, so most of a queue's blobs cost no request of their own; an
    /// upstream without `POST blobs/missing` is asked about each.
    ///
    /// `Ok(Err(status))` when the upstream answered with a status other than
    /// a yes or a no, as for a token it refuses, which the caller weighs as it
    /// does an upload's answer; `Err` only when it could not be asked.
    fn upstream_has(&self, hash: &str, authorization: &str) -> Result<Result<bool, u16>> {
        if let Some(&has) = self.upstream_holds.lock().unwrap().get(hash) {
            return Ok(Ok(has));
        }
        // Never with both locks held at once, which `upload` takes the other
        // way round.
        let queued: Vec<String> = self.queued_blobs.lock().unwrap().keys().cloned().collect();
        let mut asked = vec![hash.to_owned()];
        {
            let known = self.upstream_holds.lock().unwrap();
            asked.extend(
                queued
                    .into_iter()
                    .filter(|queued| queued != hash && !known.contains_key(queued))
                    .take(MISSING_BATCH - 1),
            );
        }
        self.stats.upstream_requests.fetch_add(1, Ordering::Relaxed);
        let mut response = self
            .agent
            .post(format!("{}/v4/blobs/missing", self.upstream))
            .header("authorization", authorization)
            .header("content-type", "application/json")
            .send(serde_json::json!({ "blobs": asked }).to_string())?;
        if response.status().as_u16() != 200 {
            self.stats.upstream_requests.fetch_add(1, Ordering::Relaxed);
            let held = self
                .agent
                .head(format!("{}/v4/blobs/{hash}", self.upstream))
                .header("authorization", authorization)
                .call()?;
            return Ok(match held.status().as_u16() {
                200 => Ok(true),
                404 => Ok(false),
                status => Err(status),
            });
        }
        let answer: serde_json::Value =
            serde_json::from_str(&response.body_mut().read_to_string()?)?;
        let missing: HashSet<&str> = answer["missing"]
            .as_array()
            .context("no missing blobs in the upstream's answer")?
            .iter()
            .filter_map(serde_json::Value::as_str)
            .collect();
        let mut known = self.upstream_holds.lock().unwrap();
        for blob in &asked {
            known.insert(blob.clone(), !missing.contains(blob.as_str()));
        }
        Ok(Ok(!missing.contains(hash)))
    }

    /// The pack the upstream would answer for the entry or warm record queued
    /// at `path`, built from its upload: the table as one frame, then each
    /// blob the upload carried as the frame it came in. The rest are apart,
    /// as the proxy holds them.
    fn queued_pack(&self, path: &str) -> Option<Vec<u8>> {
        let id = *self.queued_records.lock().unwrap().get(path)?;
        let body = fs::read(self.body_path(id)).ok()?;
        pack_of_upload(&body)
    }

    fn record_file(&self, path: &str) -> PathBuf {
        self.records
            .join(blake3::hash(path.as_bytes()).to_hex().as_str())
    }

    /// Moves `body`, a record the upstream took, to where the proxy keeps the
    /// record at `path`, in place of an older one.
    fn keep_record(&self, path: &str, body: &Path) {
        let destination = self.record_file(path);
        {
            // Not while eviction counts what the proxy holds, which would
            // store its total over this change.
            let _evicting = self.evicting.lock().unwrap();
            let previous = fs::metadata(&destination).map_or(0, |metadata| metadata.len());
            let Ok(length) = fs::metadata(body).map(|metadata| metadata.len()) else {
                return;
            };
            if fs::rename(body, &destination).is_err() {
                return;
            }
            self.size.fetch_add(length, Ordering::Relaxed);
            self.size.fetch_sub(previous, Ordering::Relaxed);
        }
        if self
            .max_size
            .is_some_and(|max| self.size.load(Ordering::Relaxed) > max)
        {
            self.evict();
        }
    }

    /// Removes the warm record kept for `path`, if any.
    fn forget_record(&self, path: &str) {
        let _evicting = self.evicting.lock().unwrap();
        let file = self.record_file(path);
        if let Ok(metadata) = fs::metadata(&file)
            && fs::remove_file(&file).is_ok()
        {
            self.size.fetch_sub(metadata.len(), Ordering::Relaxed);
        }
    }

    /// The pack of the warm record at `path` the proxy keeps, as the upstream
    /// would send it. Reading it counts as a use, which eviction goes by.
    fn kept_pack(&self, path: &str) -> Option<Vec<u8>> {
        let file = self.record_file(path);
        let body = fs::read(&file).ok()?;
        let _ = File::options()
            .write(true)
            .open(&file)
            .and_then(|file| file.set_modified(SystemTime::now()));
        pack_of_upload(&body)
    }

    fn put(
        &self,
        target: &str,
        authorization: &str,
        content_type: Option<&str>,
        body: File,
    ) -> Result<ureq::http::Response<ureq::Body>, ureq::Error> {
        self.stats.upstream_requests.fetch_add(1, Ordering::Relaxed);
        let mut request = self
            .agent
            .put(format!("{}{target}", self.upstream))
            .header("authorization", authorization);
        if let Some(content_type) = content_type {
            request = request.header("content-type", content_type);
        }
        request.send(body)
    }
}

/// What a status the upstream answered an upload with comes to.
/// Whether an upload to `target` is a bundle of blobs.
fn is_bundle(target: &str) -> bool {
    record_path(target).starts_with("/v4/bundles/")
}

fn sent(status: u16) -> Sent {
    match status {
        200..=299 => Sent::Done(status),
        429 | 500.. => Sent::Later(format!("the upstream answered {status}")),
        _ => Sent::Refused(format!("the upstream answered {status}")),
    }
}

/// The blobs the bundle uploaded to `path` carries, by hash, when it is one a
/// store server takes, as `tools/store-server/PROTOCOL.md` describes: within
/// the largest upload body, named by the SHA-256 of its header, its record
/// empty, and every blob it lists, sorted and once each, carried inline as a
/// zstd frame.
fn bundle_blobs<'a>(path: &str, body: &'a [u8]) -> Option<Vec<(String, &'a [u8])>> {
    use sha2::Digest;
    if body.len() > LARGEST_BUNDLE {
        return None;
    }
    let length = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize;
    let header = body.get(4..4 + length)?;
    let named: String = sha2::Sha256::digest(header)
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect();
    if path.strip_prefix("/v4/bundles/") != Some(named.as_str()) {
        return None;
    }
    let parsed: serde_json::Value = serde_json::from_slice(header).ok()?;
    if parsed["record"].as_u64()? != 0 {
        return None;
    }
    let listed = parsed["blobs"].as_array()?;
    let mut at = 4 + length;
    let mut blobs: Vec<(String, &[u8])> = Vec::new();
    for blob in listed {
        let hash = blob[0].as_str()?;
        let valid = hash.len() == 64
            && hash
                .bytes()
                .all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f'));
        let sorted = blobs.last().is_none_or(|(last, _)| last.as_str() < hash);
        blob[1].as_u64()?;
        let size = usize::try_from(blob[2].as_u64()?).ok()?;
        let frame = body.get(at..at.checked_add(size)?)?;
        if !valid || !sorted || !frame.starts_with(&[0x28, 0xb5, 0x2f, 0xfd]) {
            return None;
        }
        blobs.push((hash.to_owned(), frame));
        at += size;
    }
    (!blobs.is_empty() && at == body.len()).then_some(blobs)
}

/// The blobs an upload body carries inline, by hash: after the header and the
/// record, each blob with a compressed length is a zstd frame of that length,
/// in the header's order.
fn inline_blobs(body: &[u8]) -> Option<Vec<(String, &[u8])>> {
    let length = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize;
    let header: serde_json::Value = serde_json::from_slice(body.get(4..4 + length)?).ok()?;
    let mut at = 4 + length + usize::try_from(header["record"].as_u64()?).ok()?;
    let mut blobs = Vec::new();
    for blob in header["blobs"].as_array()? {
        let Some(size) = blob[2].as_u64() else {
            continue;
        };
        let size = usize::try_from(size).ok()?;
        let hash = blob[0].as_str()?.to_owned();
        blobs.push((hash, body.get(at..at.checked_add(size)?)?));
        at += size;
    }
    Some(blobs)
}

/// A request target without its query.
fn record_path(target: &str) -> &str {
    target.split('?').next().unwrap_or("")
}

/// The pack a store server sends for the upload body `body`: what precedes
/// the inline blobs as one frame, then each inline blob's frame unchanged.
/// Its layout is in `tools/store-server/PROTOCOL.md`.
fn pack_of_upload(body: &[u8]) -> Option<Vec<u8>> {
    let length = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize;
    let header: serde_json::Value = serde_json::from_slice(body.get(4..4 + length)?).ok()?;
    let record_length = usize::try_from(header["record"].as_u64()?).ok()?;
    let record = body.get(4 + length..4 + length + record_length)?;
    let blobs = header["blobs"].as_array()?;
    let mut table = b"qkp4".to_vec();
    table.extend((record.len() as u64).to_le_bytes());
    table.extend(record);
    table.extend(u32::try_from(blobs.len()).ok()?.to_le_bytes());
    for blob in blobs {
        table.push(u8::from(blob[2].as_u64().is_some()));
        table.extend(blob[1].as_u64()?.to_le_bytes());
    }
    let mut pack = zstd::bulk::compress(&table, 3).ok()?;
    pack.extend(body.get(4 + length + record_length..)?);
    Some(pack)
}

/// The blobs an upload body cites without carrying them, those stored apart:
/// the header's blobs without a compressed length.
fn apart_blobs(body: &[u8]) -> Option<Vec<String>> {
    let length = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize;
    let header: serde_json::Value = serde_json::from_slice(body.get(4..4 + length)?).ok()?;
    Some(
        header["blobs"]
            .as_array()?
            .iter()
            .filter(|blob| blob[2].as_u64().is_none())
            .filter_map(|blob| blob[0].as_str().map(str::to_owned))
            .collect(),
    )
}

/// The status line and headers; a body of unknown length is sent in chunks.
fn head(
    writer: &mut impl Write,
    status: u16,
    headers: &[(&str, &str)],
    length: Option<u64>,
) -> io::Result<()> {
    write!(writer, "HTTP/1.1 {status} {}\r\n", reason(status))?;
    for (name, value) in headers {
        write!(writer, "{name}: {value}\r\n")?;
    }
    match length {
        Some(length) => write!(writer, "content-length: {length}\r\n\r\n"),
        None => write!(writer, "transfer-encoding: chunked\r\n\r\n"),
    }
}

fn reason(status: u16) -> &'static str {
    match status {
        200 => "OK",
        201 => "Created",
        204 => "No Content",
        400 => "Bad Request",
        401 => "Unauthorized",
        403 => "Forbidden",
        404 => "Not Found",
        409 => "Conflict",
        413 => "Payload Too Large",
        422 => "Unprocessable Content",
        429 => "Too Many Requests",
        503 => "Service Unavailable",
        _ => "",
    }
}

fn bytes(count: u64) -> String {
    match count {
        count if count >= 1 << 30 => format!("{:.1}GB", count as f64 / (1u64 << 30) as f64),
        count if count >= 1 << 20 => format!("{:.1}MB", count as f64 / (1u64 << 20) as f64),
        count if count >= 1 << 10 => format!("{:.1}KB", count as f64 / 1024.0),
        count => format!("{count}B"),
    }
}

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

    #[test]
    fn framing_two_readers_could_take_differently_is_refused() {
        let refused = |request: &str| {
            format!(
                "{:#}",
                read_request(&mut io::Cursor::new(request.as_bytes().to_vec())).unwrap_err()
            )
        };
        assert!(
            refused(
                "PUT /v4/blobs/x HTTP/1.1\r\nContent-Length: 4\r\nContent-Length: 5\r\n\r\nabcd"
            )
            .contains("conflicting content-length")
        );
        assert!(
            refused("PUT /v4/blobs/x HTTP/1.1\r\nContent-Length: 4\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n")
                .contains("both content-length and transfer-encoding")
        );
        assert!(
            refused(
                "PUT /v4/blobs/x HTTP/1.1\r\nTransfer-Encoding: gzip, chunked\r\n\r\n0\r\n\r\n"
            )
            .contains("unsupported transfer-encoding")
        );
        assert!(
            refused("PUT /v4/blobs/x HTTP/1.1\r\nTransfer-Encoding: chunked\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n")
                .contains("more than one transfer-encoding")
        );
        // A name with whitespace before its colon, which a lenient reader
        // could take as the length, and a line that is no header at all.
        assert!(
            refused("PUT /v4/blobs/x HTTP/1.1\r\nContent-Length : 4\r\n\r\nabcd")
                .contains("malformed header name")
        );
        assert!(
            refused("PUT /v4/blobs/x HTTP/1.1\r\nContent-Length: 4\r\n folded\r\n\r\nabcd")
                .contains("malformed header line")
        );
        // A repeated length that agrees is no conflict.
        let agreed = read_request(&mut io::Cursor::new(
            b"PUT /v4/blobs/x HTTP/1.1\r\nContent-Length: 4\r\ncontent-length: 4\r\n\r\nabcd"
                .to_vec(),
        ))
        .unwrap()
        .unwrap();
        assert_eq!(agreed.body, b"abcd");
    }

    #[test]
    fn only_a_path_may_be_requested() {
        let error = read_request(&mut io::Cursor::new(
            b"GET @internal-host:8080/x HTTP/1.1\r\n\r\n".to_vec(),
        ))
        .unwrap_err();
        assert!(format!("{error:#}").contains("unsupported request target"));
    }

    #[test]
    fn sizes_past_the_limits_are_refused_before_they_are_read() {
        let refused = |request: Vec<u8>| {
            format!(
                "{:#}",
                read_request(&mut io::Cursor::new(request)).unwrap_err()
            )
        };
        let huge = format!(
            "PUT /v4/blobs/x HTTP/1.1\r\nContent-Length: {}\r\n\r\n",
            MAX_BODY + 1
        );
        assert!(refused(huge.into_bytes()).contains("larger than the proxy takes"));
        let chunk = format!(
            "PUT /v4/blobs/x HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n{:x}\r\n",
            MAX_BODY + 1
        );
        assert!(refused(chunk.into_bytes()).contains("larger than the proxy takes"));
        let mut long = b"GET /".to_vec();
        long.extend(vec![b'a'; MAX_LINE as usize + 10]);
        long.extend(b" HTTP/1.1\r\n\r\n");
        assert!(refused(long).contains("too long"));
        assert!(
            refused(b"PUT /v4/blobs/x HTTP/1.1\r\nContent-Length: 10\r\n\r\nshort".to_vec())
                .contains("ends early")
        );
    }

    #[test]
    fn chunked_bodies_are_read_whole() {
        let mut reader = io::Cursor::new(b"4\r\nwiki\r\n5;x=y\r\npedia\r\n0\r\n\r\n".to_vec());
        assert_eq!(read_chunked(&mut reader).unwrap(), b"wikipedia");
    }

    #[test]
    fn a_request_without_a_body_ends_at_its_headers() {
        let mut reader = io::Cursor::new(
            b"HEAD /v4/blobs/x HTTP/1.1\r\nAuthorization: Bearer t\r\n\r\nGET /v4/index HTTP/1.1\r\nConnection: close\r\n\r\n"
                .to_vec(),
        );
        let first = read_request(&mut reader).unwrap().unwrap();
        assert_eq!(
            (first.method.as_str(), first.target.as_str()),
            ("HEAD", "/v4/blobs/x")
        );
        assert_eq!(first.authorization.as_deref(), Some("Bearer t"));
        assert!(!first.close);
        let second = read_request(&mut reader).unwrap().unwrap();
        assert!(second.close);
        assert!(read_request(&mut reader).unwrap().is_none());
    }

    #[test]
    fn a_record_waits_only_for_the_jobs_of_its_blobs_queued_before_it() {
        let directory = tempfile::tempdir().unwrap();
        let proxy = Proxy::open(&Options {
            upstream: "http://127.0.0.1:9".into(),
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: None,
        })
        .unwrap();
        let blob = "b".repeat(64);
        let request = |target: String, body: Vec<u8>| Request {
            method: "PUT".into(),
            target,
            authorization: Some("Bearer token".into()),
            content_type: None,
            body,
            close: false,
        };
        // A record citing the blob stored apart, as an upload body.
        let record = b"{}";
        let header = serde_json::json!({ "record": record.len(), "blobs": [[blob, 1, null]] });
        let header = serde_json::to_vec(&header).unwrap();
        let mut body = (header.len() as u32).to_le_bytes().to_vec();
        body.extend(&header);
        body.extend(record);
        let waits = |id: u64| {
            let (done, finished) = std::sync::mpsc::channel();
            let proxy = proxy.clone();
            std::thread::spawn(move || {
                proxy.await_blobs(id);
                let _ = done.send(());
            });
            finished
        };

        // The same blob uploaded again after the record is not waited for.
        proxy
            .enqueue(&request("/v4/entries/first".into(), body.clone()), None)
            .unwrap();
        proxy
            .enqueue(
                &request(format!("/v4/blobs/{blob}"), Vec::new()),
                Some(&blob),
            )
            .unwrap();
        assert!(waits(0).recv_timeout(Duration::from_secs(5)).is_ok());

        // A record after the blob's job waits until that job is done.
        proxy
            .enqueue(&request("/v4/entries/second".into(), body), None)
            .unwrap();
        let finished = waits(2);
        assert!(finished.recv_timeout(Duration::from_millis(200)).is_err());
        proxy.queued_blobs.lock().unwrap().remove(&blob);
        proxy.blob_done.notify_all();
        assert!(finished.recv_timeout(Duration::from_secs(5)).is_ok());
    }

    #[test]
    fn only_bundles_a_store_server_takes_are_queued() {
        use sha2::Digest;
        let frame = zstd::stream::encode_all(&b"x"[..], 1).unwrap();
        // A bundle of `listed`, each carried as `frame` when its third field
        // is a length, and its path named by its header unless `path` says.
        let bundle = |record: u64, listed: serde_json::Value, path: Option<&str>| {
            let header =
                serde_json::to_vec(&serde_json::json!({"record": record, "blobs": listed}))
                    .unwrap();
            let mut body = (header.len() as u32).to_le_bytes().to_vec();
            body.extend(&header);
            body.extend(vec![b'r'; record as usize]);
            for blob in listed.as_array().unwrap() {
                if blob[2].is_u64() {
                    body.extend(&frame);
                }
            }
            let named: String = sha2::Sha256::digest(&header)
                .iter()
                .map(|byte| format!("{byte:02x}"))
                .collect();
            let path = path.map_or(format!("/v4/bundles/{named}"), str::to_owned);
            (path, body)
        };
        let (a, b) = ("a".repeat(64), "b".repeat(64));
        let size = frame.len();
        let takes = |(path, body): (String, Vec<u8>)| bundle_blobs(&path, &body).is_some();
        assert!(takes(bundle(
            0,
            serde_json::json!([[a, 1, size], [b, 1, size]]),
            None
        )));
        // Named otherwise, carrying a record, listing a blob it does not
        // carry, unsorted, or carrying nothing.
        let other = format!("/v4/bundles/{}", "c".repeat(64));
        assert!(!takes(bundle(
            0,
            serde_json::json!([[a, 1, size]]),
            Some(&other)
        )));
        assert!(!takes(bundle(2, serde_json::json!([[a, 1, size]]), None)));
        assert!(!takes(bundle(
            0,
            serde_json::json!([[a, 1, size], [b, 1, null]]),
            None
        )));
        assert!(!takes(bundle(
            0,
            serde_json::json!([[b, 1, size], [a, 1, size]]),
            None
        )));
        assert!(!takes(bundle(0, serde_json::json!([]), None)));
    }

    #[test]
    fn a_record_waits_for_a_bundle_queued_before_it_carrying_its_blobs() {
        let directory = tempfile::tempdir().unwrap();
        let proxy = Proxy::open(&Options {
            upstream: "http://127.0.0.1:9".into(),
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: None,
        })
        .unwrap();
        let blob = "b".repeat(64);
        let request = |target: String, body: Vec<u8>| Request {
            method: "PUT".into(),
            target,
            authorization: Some("Bearer token".into()),
            content_type: None,
            body,
            close: false,
        };
        let body = |record: &[u8], frame: Option<&[u8]>| {
            let header = serde_json::json!({
                "record": record.len(),
                "blobs": [[blob, 1, frame.map(<[u8]>::len)]],
            });
            let header = serde_json::to_vec(&header).unwrap();
            let mut body = (header.len() as u32).to_le_bytes().to_vec();
            body.extend(&header);
            body.extend(record);
            body.extend(frame.unwrap_or_default());
            body
        };
        let frame = zstd::stream::encode_all(&b"x"[..], 1).unwrap();
        proxy
            .enqueue(
                &request(
                    format!("/v4/bundles/{}", "c".repeat(64)),
                    body(b"", Some(&frame)),
                ),
                None,
            )
            .unwrap();
        // A record citing the bundled blob stored apart waits for the bundle.
        proxy
            .enqueue(&request("/v4/entries/key".into(), body(b"{}", None)), None)
            .unwrap();
        let (done, finished) = std::sync::mpsc::channel();
        let waiting = proxy.clone();
        std::thread::spawn(move || {
            waiting.await_blobs(1);
            let _ = done.send(());
        });
        assert!(finished.recv_timeout(Duration::from_millis(200)).is_err());
        proxy.queued_blobs.lock().unwrap().remove(&blob);
        proxy.blob_done.notify_all();
        assert!(finished.recv_timeout(Duration::from_secs(5)).is_ok());
    }

    #[test]
    fn a_blob_the_upstream_refuses_to_answer_about_is_given_up() {
        // An upstream that refuses every request, as one whose token was
        // revoked after the proxy queued its uploads.
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let upstream = format!("http://{}", listener.local_addr().unwrap());
        std::thread::spawn(move || {
            for stream in listener.incoming().flatten() {
                std::thread::spawn(move || {
                    let mut reader = BufReader::new(stream.try_clone().unwrap());
                    let mut writer = stream;
                    while let Ok(Some(_)) = read_request(&mut reader) {
                        let refused = "HTTP/1.1 401 Unauthorized\r\ncontent-length: 0\r\n\r\n";
                        if writer.write_all(refused.as_bytes()).is_err() {
                            return;
                        }
                    }
                });
            }
        });
        let directory = tempfile::tempdir().unwrap();
        let proxy = Proxy::open(&Options {
            upstream,
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: None,
        })
        .unwrap();
        let blob = "c".repeat(64);
        match proxy.send_blob(&blob, "Bearer revoked", true) {
            Sent::Refused(why) => assert!(why.contains("401"), "{why}"),
            Sent::Done(status) => panic!("sent: {status}"),
            Sent::Missing => panic!("missing"),
            Sent::Later(why) => panic!("left to send again: {why}"),
        }
    }

    #[test]
    fn an_uploaded_blob_is_queued_without_eviction_taking_it_first() {
        let upload = |content: &[u8]| {
            let hash = blake3::hash(content).to_hex().to_string();
            let request = Request {
                method: "PUT".into(),
                target: format!("/v4/blobs/{hash}"),
                authorization: Some("Bearer token".into()),
                content_type: Some("application/zstd".into()),
                body: zstd::stream::encode_all(content, 3).unwrap(),
                close: false,
            };
            (hash, request)
        };
        let (first, first_request) = upload(b"the first upload, queued and so held");
        let (second, second_request) =
            upload(b"the second upload, which keeping goes past the limit");
        let directory = tempfile::tempdir().unwrap();
        // Room for the first and not both, so keeping the second evicts.
        let room = first_request.body.len() + second_request.body.len() - 1;
        let proxy = Proxy::open(&Options {
            upstream: "http://127.0.0.1:9".into(),
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: Some(room as u64),
        })
        .unwrap();
        assert!(proxy.queue_blob(&first_request, &first).unwrap());
        assert!(proxy.queue_blob(&second_request, &second).unwrap());
        assert!(proxy.blobs.join(&first).is_file());
        assert!(
            proxy.blobs.join(&second).is_file(),
            "the queued blob was evicted"
        );
        assert_eq!(proxy.jobs.lock().unwrap().len(), 2);
    }

    #[test]
    fn kept_records_are_evicted_least_recently_used_first() {
        let directory = tempfile::tempdir().unwrap();
        // Room for two records of 100 bytes and not three.
        let proxy = Proxy::open(&Options {
            upstream: "http://127.0.0.1:9".into(),
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: Some(250),
        })
        .unwrap();
        let keep = |path: &str, age: u64| {
            let body = directory.path().join("body");
            fs::write(&body, [0; 100]).unwrap();
            proxy.keep_record(path, &body);
            File::options()
                .write(true)
                .open(proxy.record_file(path))
                .unwrap()
                .set_modified(SystemTime::now() - Duration::from_secs(age))
                .unwrap();
        };
        keep("/v4/warm/a/first", 30);
        keep("/v4/warm/a/second", 20);
        // Read since, and so used more recently than the second.
        proxy.kept_pack("/v4/warm/a/first");
        keep("/v4/warm/a/third", 0);
        assert!(proxy.record_file("/v4/warm/a/first").is_file());
        assert!(!proxy.record_file("/v4/warm/a/second").exists());
        assert!(proxy.record_file("/v4/warm/a/third").is_file());
        assert_eq!(proxy.size.load(Ordering::Relaxed), 200);
        // And one the upstream took a newer upload of directly is forgotten.
        proxy.forget_record("/v4/warm/a/third");
        assert!(!proxy.record_file("/v4/warm/a/third").exists());
        assert_eq!(proxy.size.load(Ordering::Relaxed), 100);
    }

    #[test]
    fn records_of_one_path_are_sent_one_at_a_time_and_the_newest_wins() {
        let directory = tempfile::tempdir().unwrap();
        let proxy = Proxy::open(&Options {
            upstream: "http://127.0.0.1:9".into(),
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: None,
        })
        .unwrap();
        let path = "/v4/warm/a/b";
        let request = |body: &[u8]| Request {
            method: "PUT".into(),
            target: path.into(),
            authorization: Some("Bearer token".into()),
            content_type: None,
            body: body.to_vec(),
            close: false,
        };
        proxy.enqueue(&request(b"older"), None).unwrap();
        proxy.enqueue(&request(b"newer"), None).unwrap();
        // The older upload gives way to the newer, which takes the turn.
        assert!(proxy.record_turn(path, 0).is_none());
        // And still once the newer was sent and so left the queue.
        let newest = proxy.queued_records.lock().unwrap().remove(path).unwrap();
        assert!(proxy.record_turn(path, 0).is_none());
        proxy
            .queued_records
            .lock()
            .unwrap()
            .insert(path.to_owned(), newest);
        let turn = proxy.record_turn(path, 1).unwrap();
        // Another send of the path waits for the turn to end.
        let (taken, waiting) = std::sync::mpsc::channel();
        std::thread::scope(|scope| {
            scope.spawn(|| {
                let second = proxy.record_turn(path, 1);
                taken.send(second.is_some()).unwrap();
            });
            assert!(waiting.recv_timeout(Duration::from_millis(200)).is_err());
            drop(turn);
            assert!(waiting.recv_timeout(Duration::from_secs(5)).unwrap());
        });
    }

    #[test]
    fn a_proxy_started_again_resumes_the_uploads_it_queued_in_order() {
        let directory = tempfile::tempdir().unwrap();
        let options = Options {
            upstream: "http://127.0.0.1:9".into(),
            directory: directory.path().to_owned(),
            listen: String::new(),
            max_size: None,
        };
        let upload = |target: &str, body: &[u8]| Request {
            method: "PUT".into(),
            target: target.into(),
            authorization: Some("Bearer token".into()),
            content_type: Some("application/x-qk-upload".into()),
            body: body.to_vec(),
            close: false,
        };
        let blob = "a".repeat(64);
        let first = Proxy::open(&options).unwrap();
        first
            .enqueue(&upload(&format!("/v4/blobs/{blob}"), b""), Some(&blob))
            .unwrap();
        // Queued once while it waits to be sent.
        first
            .enqueue(&upload(&format!("/v4/blobs/{blob}"), b""), Some(&blob))
            .unwrap();
        first
            .enqueue(&upload("/v4/entries/key", b"record"), None)
            .unwrap();
        drop(first);
        // A file a stop cut short of being put in place.
        fs::write(directory.path().join("queue/.tmp123"), b"").unwrap();

        let second = Proxy::open(&options).unwrap();
        assert_eq!(second.resume_queue().unwrap(), 2);
        assert_eq!(*second.jobs.lock().unwrap(), [0, 1]);
        assert_eq!(second.queued_blobs.lock().unwrap()[&blob].len(), 1);
        assert_eq!(fs::read(second.body_path(1)).unwrap(), b"record");
        assert!(!directory.path().join("queue/.tmp123").exists());
        // Jobs queued after continue the order.
        second
            .enqueue(&upload("/v4/entries/other", b"record"), None)
            .unwrap();
        assert_eq!(*second.jobs.lock().unwrap(), [0, 1, 2]);
    }
}