tilo-net 0.5.1

The network capability on Linux, macOS and the web: async HTTP with streaming bodies, WebSocket, WebTransport, network status, raw sockets on the desktop, and Tilo's one TLS provider and trust source.
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
//! One download from a trusted origin: the exact bytes, whole, verified.
//!
//! Bytes go into a file the caller already opened and owns, from wherever that
//! file currently ends, so an interrupted download resumes instead of starting
//! again. The response must come from the trusted origin, and every redirect
//! hop is checked against it and bounded. The body must be framed and encoded
//! in a way whose length means the bytes themselves: no transfer coding beyond
//! a single HTTP/1.1 `chunked`, and no content coding other than `identity`,
//! because a digest covers the bytes and not their compression.
//!
//! A download tries a bounded number of times, and only for failures that can
//! differ next time: connection and timeout errors, `429`, and `5xx`. A short
//! body, a refused range and an interrupted read resume from the bytes already
//! held; a mismatched size, digest or framing is final. The caller's cancel
//! flag stops it at any point, and its optional pause flag stops it where the
//! held bytes can be written down for a later run.
//!
//! [`Hooks`] is how a caller that must survive a crash writes down what it has
//! proven: it receives every chunk after it was written and every boundary
//! where the held bytes are about to change state.

use reqwest::{Url, blocking::Response, header};
use sha2::{Digest, Sha256};
use std::{
    fs::File,
    io::{self, Read, Seek, SeekFrom, Write},
    sync::atomic::{AtomicBool, Ordering},
    time::Duration,
};

/// Largest piece of a response body read at once.
const BUFFER: usize = 64 * 1024;
/// Attempts one download makes before it reports itself incomplete.
const ATTEMPTS: usize = 3;
/// Redirects one request follows before it refuses the chain.
const REDIRECTS: usize = 5;
/// Interrupted file writes one chunk survives before they are a failure.
const INTERRUPTED_WRITES: u8 = 8;
/// How long connecting to the origin may take.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// What went wrong, in terms a caller can map onto what it tells a person.
#[derive(Debug)]
pub enum Error {
    /// The caller's cancel flag was set.
    Cancelled,
    /// The caller's pause flag was set, after [`Hooks::pausing`] ran.
    Paused,
    /// A URL, redirect or response left the trusted origin's rules.
    Trusted(String),
    /// A response or the caller's file broke the download's bounds.
    Protocol(String),
    /// The download could not finish in its bounded attempts.
    Incomplete(String),
    /// The held bytes were not the expected size.
    Size { expected: u64, found: u64 },
    /// The held bytes were not the expected SHA-256.
    Digest,
    /// Reading, seeking or sizing the caller's file failed.
    Io(io::Error),
    /// A [`Hooks`] method refused.
    Hook(String),
    /// The HTTP client could not be built.
    Client(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Cancelled => formatter.write_str("the download was cancelled"),
            Self::Paused => formatter.write_str("the download was paused"),
            Self::Trusted(detail) => write!(formatter, "untrusted download: {detail}"),
            Self::Protocol(detail) => write!(formatter, "refused response: {detail}"),
            Self::Incomplete(detail) => write!(formatter, "incomplete download: {detail}"),
            Self::Size { expected, found } => {
                write!(formatter, "downloaded {found} bytes instead of {expected}")
            }
            Self::Digest => formatter.write_str("the download did not match its digest"),
            Self::Io(error) => write!(formatter, "download file: {error}"),
            Self::Hook(detail) => write!(formatter, "download hook: {detail}"),
            Self::Client(detail) => write!(formatter, "HTTP client: {detail}"),
        }
    }
}

impl std::error::Error for Error {}

/// Where a download is, and how many bytes of the file are in hand.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Phase {
    /// Deciding what to ask for next.
    Requesting,
    /// Appending the response body.
    Receiving,
    /// Asking again after a failure that can differ next time.
    Retrying,
    /// Discarding the held bytes and asking for the whole file.
    Resetting,
    /// Hashing the whole file against its expected digest.
    Verifying,
    /// The file holds exactly the expected, verified bytes.
    Complete,
}

/// One report from a download.
#[derive(Clone, Copy, Debug)]
pub struct Progress {
    pub phase: Phase,
    /// Bytes held in the file. While [`Phase::Verifying`] runs the file already
    /// holds all of them, so this is the finished size.
    pub received: u64,
    /// The exact size the finished file has.
    pub total: u64,
}

/// What a download is asked for.
pub struct Request<'a> {
    /// The absolute URL on the downloader's trusted origin.
    pub url: Url,
    /// The exact number of bytes the finished file holds.
    pub size: u64,
    /// SHA-256 of the finished file, as lowercase hex, when the caller knows it.
    pub sha256: Option<&'a str>,
    pub cancel: &'a AtomicBool,
    /// Stops at the next boundary where the held bytes can be written down.
    pub pause: Option<&'a AtomicBool>,
}

/// What a caller does with the bytes as they arrive.
///
/// Every method may refuse, which ends the download with [`Error::Hook`]; a
/// caller that has written nothing refuses rather than continue with a file it
/// cannot vouch for. `file` is the caller's working file, positioned at the
/// end of what it already wrote.
pub trait Hooks {
    /// An attempt starts, with `offset` bytes already in the file. Whatever the
    /// caller knows about those bytes can be checked now, before anything is
    /// sent for them.
    fn attempt(&mut self, _file: &mut File, _offset: u64) -> Result<(), String> {
        Ok(())
    }

    /// Append `bytes` to the file. The default appends them with the write
    /// retries a filesystem needs; a caller that records a recoverable
    /// checkpoint writes them and records where they end.
    fn write(&mut self, file: &mut File, bytes: &[u8]) -> Result<(), String> {
        append(file, bytes)
    }

    /// The response ended short of the file, and the next request resumes from
    /// the held bytes: make them durable before it is sent.
    fn before_retry(&mut self, _file: &mut File) -> Result<(), String> {
        Ok(())
    }

    /// The held bytes are about to be discarded.
    fn before_reset(&mut self, _file: &mut File) -> Result<(), String> {
        Ok(())
    }

    /// The file is empty again.
    fn reset(&mut self, _file: &mut File) -> Result<(), String> {
        Ok(())
    }

    /// The caller asked to pause: settle the held bytes before returning
    /// [`Error::Paused`].
    fn pausing(&mut self, _file: &mut File) -> Result<(), String> {
        Ok(())
    }

    /// The whole file matched its expected size and digest.
    fn verified(&mut self, _file: &mut File) -> Result<(), String> {
        Ok(())
    }
}

impl Hooks for () {}

/// Append `bytes` to `file`, retrying interrupted writes, and fail rather than
/// report an append that only partly happened.
pub fn append(file: &mut impl Write, bytes: &[u8]) -> Result<(), String> {
    let mut remaining = bytes;
    let mut interrupted: u8 = 0;
    while !remaining.is_empty() {
        let count = match file.write(remaining) {
            Ok(count) => count,
            Err(error) if error.kind() == io::ErrorKind::Interrupted => {
                interrupted = interrupted.checked_add(1).ok_or_else(|| {
                    "the file write was interrupted more often than it can be".to_string()
                })?;
                if interrupted >= INTERRUPTED_WRITES {
                    return Err(format!(
                        "the file write was interrupted {interrupted} times"
                    ));
                }
                continue;
            }
            Err(error) => return Err(format!("the file write failed: {error}")),
        };
        if count == 0 {
            return Err(format!(
                "the file write accepted no bytes: {}",
                io::Error::from(io::ErrorKind::WriteZero)
            ));
        }
        interrupted = 0;
        remaining = remaining
            .get(count..)
            .ok_or_else(|| "the file write reported more bytes than it was given".to_string())?;
    }
    Ok(())
}

/// Whether `actual` names the same origin as `trusted`: one scheme, host and
/// port, with no credentials, query or fragment to make it a different address.
#[must_use]
pub fn same_origin(actual: &Url, trusted: &Url) -> bool {
    actual.scheme() == trusted.scheme()
        && actual.host_str() == trusted.host_str()
        && actual.port_or_known_default() == trusted.port_or_known_default()
        && actual.username().is_empty()
        && actual.password().is_none()
        && actual.query().is_none()
        && actual.fragment().is_none()
}

/// One trusted origin's downloads.
pub struct Downloader {
    client: reqwest::blocking::Client,
    origin: Url,
}

impl Downloader {
    /// A downloader for `origin`. `timeout` bounds one request including its
    /// body, so a download that outlasts it resumes where it stopped rather
    /// than fail.
    pub fn new(origin: Url, timeout: Duration, user_agent: Option<&str>) -> Result<Self, Error> {
        let mut builder = super::blocking_client()
            .connect_timeout(CONNECT_TIMEOUT)
            .timeout(timeout)
            .no_gzip()
            .no_brotli()
            .no_deflate()
            .no_zstd()
            .retry(reqwest::retry::never())
            .redirect(reqwest::redirect::Policy::none());
        if let Some(user_agent) = user_agent {
            builder = builder.user_agent(user_agent);
        }
        let client = builder
            .build()
            .map_err(|error| Error::Client(error.to_string()))?;
        Ok(Self { client, origin })
    }

    /// Download `request` into `file`, resuming from its current length, and
    /// leave it holding exactly `size` verified bytes.
    #[allow(
        clippy::too_many_lines,
        reason = "one attempt's steps read in the order they run: ask, read, settle"
    )]
    pub fn fetch(
        &self,
        request: Request<'_>,
        file: &mut File,
        hooks: &mut impl Hooks,
        mut progress: impl FnMut(Progress),
    ) -> Result<(), Error> {
        let Request {
            url,
            size,
            sha256,
            cancel,
            pause,
        } = request;
        stop(cancel)?;
        check_size(size)?;
        if !same_origin(&url, &self.origin) {
            return Err(Error::Trusted(
                "the download URL is not on the trusted origin".into(),
            ));
        }
        let mut last = "no response was received".to_owned();
        for attempt in 0..ATTEMPTS {
            stop(cancel)?;
            if paused(pause) {
                hooks.pausing(file).map_err(Error::Hook)?;
                return Err(Error::Paused);
            }
            let offset = held_length(file, size, cancel)?;
            hooks.attempt(file, offset).map_err(Error::Hook)?;
            if held_length(file, size, cancel)? != offset {
                return Err(Error::Protocol(
                    "the held bytes changed while an attempt started".into(),
                ));
            }
            if offset == size {
                return verify(file, size, sha256, cancel, pause, hooks, &mut progress);
            }
            report(&mut progress, Phase::Requesting, offset, size);
            let mut response = match self.request(url.clone(), offset, cancel, pause) {
                Ok(response) => response,
                Err(RequestError::Paused) => {
                    hooks.pausing(file).map_err(Error::Hook)?;
                    return Err(Error::Paused);
                }
                Err(RequestError::Fatal(detail)) => {
                    stop(cancel)?;
                    return Err(Error::Trusted(detail));
                }
                Err(RequestError::Retry(detail)) => {
                    stop(cancel)?;
                    last = detail;
                    report(&mut progress, Phase::Retrying, offset, size);
                    continue;
                }
            };
            let status = response.status();
            if status == reqwest::StatusCode::TOO_MANY_REQUESTS || status.is_server_error() {
                stop(cancel)?;
                last = format!("the origin answered {status}");
                report(&mut progress, Phase::Retrying, offset, size);
                continue;
            }
            let expected = match expected(&response, offset, size) {
                Ok(Expected::Bytes(count)) => count,
                Ok(Expected::Refused(detail)) => {
                    stop(cancel)?;
                    last = detail.into();
                    discard(file, size, cancel, pause, hooks, &mut progress)?;
                    continue;
                }
                Err(error) => {
                    stop(cancel)?;
                    return Err(error);
                }
            };
            stop(cancel)?;
            file.seek(SeekFrom::Start(offset)).map_err(Error::Io)?;
            let mut written = offset;
            report(&mut progress, Phase::Receiving, written, size);
            let end = read_body(
                &mut response,
                expected,
                cancel,
                || paused(pause),
                |chunk| {
                    hooks.write(file, chunk).map_err(Error::Hook)?;
                    let length = held_length(file, size, cancel)?;
                    if written.checked_add(u64::try_from(chunk.len()).unwrap_or(u64::MAX))
                        != Some(length)
                    {
                        return Err(Error::Protocol(
                            "a chunk was not written at the end of the file".into(),
                        ));
                    }
                    written = length;
                    report(&mut progress, Phase::Receiving, written, size);
                    Ok(())
                },
            )?;
            drop(response);
            let length = held_length(file, size, cancel)?;
            match end {
                BodyEnd::Complete if length == size => {
                    return verify(file, size, sha256, cancel, pause, hooks, &mut progress);
                }
                BodyEnd::Paused => {
                    hooks.pausing(file).map_err(Error::Hook)?;
                    return Err(Error::Paused);
                }
                BodyEnd::Complete => {
                    last = "the response ended before the file was complete".into();
                }
                BodyEnd::Interrupted(detail) => last = detail,
            }
            if length == size {
                // The response did not finish, so its bytes are not trusted even
                // though they fill the file: fetch the whole file again.
                discard(file, size, cancel, pause, hooks, &mut progress)?;
            } else if attempt.saturating_add(1) < ATTEMPTS && length > offset {
                hooks.before_retry(file).map_err(Error::Hook)?;
            }
            report(&mut progress, Phase::Retrying, length, size);
        }
        stop(cancel)?;
        if paused(pause) {
            hooks.pausing(file).map_err(Error::Hook)?;
            return Err(Error::Paused);
        }
        Err(Error::Incomplete(format!(
            "after {ATTEMPTS} attempts: {last}"
        )))
    }

    /// Download `request` whole into memory. A manifest is small and always
    /// fetched again, so this resumes nothing.
    pub fn fetch_bytes(&self, request: Request<'_>) -> Result<Vec<u8>, Error> {
        let Request {
            url,
            size,
            sha256,
            cancel,
            pause,
        } = request;
        stop(cancel)?;
        check_size(size)?;
        if !same_origin(&url, &self.origin) {
            return Err(Error::Trusted(
                "the download URL is not on the trusted origin".into(),
            ));
        }
        let mut response = match self.request(url, 0, cancel, pause) {
            Ok(response) => response,
            Err(RequestError::Paused) => return Err(Error::Paused),
            Err(RequestError::Fatal(detail)) => return Err(Error::Trusted(detail)),
            Err(RequestError::Retry(detail)) => return Err(Error::Incomplete(detail)),
        };
        let expected = match expected(&response, 0, size) {
            Ok(Expected::Bytes(count)) => count,
            Ok(Expected::Refused(detail)) => return Err(Error::Protocol(detail.into())),
            Err(error) => return Err(error),
        };
        let mut bytes = Vec::new();
        let end = read_body(
            &mut response,
            expected,
            cancel,
            || paused(pause),
            |chunk| {
                bytes.extend_from_slice(chunk);
                Ok(())
            },
        )?;
        match end {
            BodyEnd::Complete => {}
            BodyEnd::Paused => return Err(Error::Paused),
            BodyEnd::Interrupted(detail) => return Err(Error::Incomplete(detail)),
        }
        stop(cancel)?;
        if bytes.len() as u64 != size {
            return Err(Error::Size {
                expected: size,
                found: bytes.len() as u64,
            });
        }
        if sha256.is_some_and(|expected| !digest_matches(&bytes, expected)) {
            return Err(Error::Digest);
        }
        stop(cancel)?;
        Ok(bytes)
    }

    /// One response for `url`, following at most [`REDIRECTS`] same-origin
    /// redirects, with its origin and framing already checked.
    fn request(
        &self,
        mut url: Url,
        offset: u64,
        cancel: &AtomicBool,
        pause: Option<&AtomicBool>,
    ) -> Result<Response, RequestError> {
        for hop in 0..=REDIRECTS {
            cancelled(cancel).map_err(RequestError::Fatal)?;
            if paused(pause) {
                return Err(RequestError::Paused);
            }
            if !same_origin(&url, &self.origin) {
                return Err(RequestError::Fatal(
                    "the request URL is not on the trusted origin".into(),
                ));
            }
            let mut request = self
                .client
                .get(url.clone())
                .header(header::ACCEPT_ENCODING, "identity");
            if offset > 0 {
                request = request.header(header::RANGE, format!("bytes={offset}-"));
            }
            let sent = request.send();
            cancelled(cancel).map_err(RequestError::Fatal)?;
            if paused(pause) {
                return Err(RequestError::Paused);
            }
            let response = sent.map_err(|error| {
                // A header or protocol error is not a transport failure that can
                // differ next time; only connecting and timeouts are retried.
                if error.is_connect() || error.is_timeout() {
                    RequestError::Retry(error.to_string())
                } else {
                    RequestError::Fatal(error.to_string())
                }
            })?;
            identity(&self.origin, &response).map_err(RequestError::Fatal)?;
            if !matches!(response.status().as_u16(), 301 | 302 | 303 | 307 | 308) {
                return Ok(response);
            }
            let locations: Vec<_> = response
                .headers()
                .get_all(header::LOCATION)
                .iter()
                .collect();
            if hop == REDIRECTS || locations.len() != 1 {
                return Err(RequestError::Fatal(
                    "invalid redirect or too many redirects".into(),
                ));
            }
            let location = locations
                .first()
                .ok_or_else(|| RequestError::Fatal("invalid redirect Location".into()))?
                .to_str()
                .map_err(|_| RequestError::Fatal("invalid redirect Location".into()))?;
            url = url
                .join(location)
                .map_err(|_| RequestError::Fatal("invalid redirect URL".into()))?;
            if !same_origin(&url, &self.origin) {
                return Err(RequestError::Fatal(
                    "the redirect left the trusted origin".into(),
                ));
            }
        }
        Err(RequestError::Fatal(
            "the redirect chain exceeded its bound".into(),
        ))
    }
}

/// What one response offers a download that holds `offset` bytes.
enum Expected {
    /// Read this many body bytes from `offset`.
    Bytes(u64),
    /// The origin did not honour the requested range.
    Refused(&'static str),
}

/// The bytes a response promises, checked against what was asked for.
fn expected(response: &Response, offset: u64, size: u64) -> Result<Expected, Error> {
    let status = response.status();
    if offset > 0 {
        if status == reqwest::StatusCode::PARTIAL_CONTENT {
            return Ok(match range_size(response.headers(), offset, size) {
                Some(count) => Expected::Bytes(count),
                None => Expected::Refused("the origin sent a range the request did not ask for"),
            });
        }
        if status == reqwest::StatusCode::OK || status == reqwest::StatusCode::RANGE_NOT_SATISFIABLE
        {
            return Ok(Expected::Refused(
                "the origin ignored or rejected the requested range",
            ));
        }
        return Err(Error::Protocol(format!(
            "unexpected status {status} for a resumed download"
        )));
    }
    if status != reqwest::StatusCode::OK {
        return Err(Error::Protocol(format!(
            "the first response must be 200, got {status}"
        )));
    }
    declared_length(response.headers(), size).map_err(Error::Protocol)?;
    if response.headers().contains_key(header::CONTENT_RANGE) {
        return Err(Error::Protocol(
            "a full response carried a Content-Range".into(),
        ));
    }
    Ok(Expected::Bytes(size))
}

/// Discard the held bytes and settle that decision with the caller's hooks, so
/// the next request asks for the whole file.
fn discard(
    file: &mut File,
    total: u64,
    cancel: &AtomicBool,
    pause: Option<&AtomicBool>,
    hooks: &mut impl Hooks,
    progress: &mut impl FnMut(Progress),
) -> Result<(), Error> {
    stop(cancel)?;
    if paused(pause) {
        hooks.pausing(file).map_err(Error::Hook)?;
        return Err(Error::Paused);
    }
    hooks.before_reset(file).map_err(Error::Hook)?;
    file.set_len(0).map_err(Error::Io)?;
    file.rewind().map_err(Error::Io)?;
    hooks.reset(file).map_err(Error::Hook)?;
    report(progress, Phase::Resetting, 0, total);
    stop(cancel)
}

/// Hash the whole file and check it against the size and digest it was
/// downloaded for.
fn verify(
    file: &mut File,
    size: u64,
    sha256: Option<&str>,
    cancel: &AtomicBool,
    pause: Option<&AtomicBool>,
    hooks: &mut impl Hooks,
    progress: &mut impl FnMut(Progress),
) -> Result<(), Error> {
    stop(cancel)?;
    if paused(pause) {
        hooks.pausing(file).map_err(Error::Hook)?;
        return Err(Error::Paused);
    }
    report(progress, Phase::Verifying, size, size);
    file.flush().map_err(Error::Io)?;
    stop(cancel)?;
    file.rewind().map_err(Error::Io)?;
    let mut hash = Sha256::new();
    let end = read_body(
        file,
        size,
        cancel,
        || paused(pause),
        |chunk| {
            hash.update(chunk);
            Ok(())
        },
    )?;
    match end {
        BodyEnd::Complete => {}
        BodyEnd::Paused => {
            hooks.pausing(file).map_err(Error::Hook)?;
            return Err(Error::Paused);
        }
        BodyEnd::Interrupted(detail) => return Err(Error::Incomplete(detail)),
    }
    stop(cancel)?;
    if held_length(file, size, cancel)? != size {
        return Err(Error::Protocol(
            "the held file changed while it was verified".into(),
        ));
    }
    let found = hex::encode(hash.finalize());
    if sha256.is_some_and(|expected| !found.eq_ignore_ascii_case(expected)) {
        return Err(Error::Digest);
    }
    report(progress, Phase::Complete, size, size);
    stop(cancel)?;
    if paused(pause) {
        hooks.pausing(file).map_err(Error::Hook)?;
        return Err(Error::Paused);
    }
    hooks.verified(file).map_err(Error::Hook)?;
    stop(cancel)?;
    if paused(pause) {
        hooks.pausing(file).map_err(Error::Hook)?;
        return Err(Error::Paused);
    }
    Ok(())
}

/// What a request failed as: settled, worth another try, or paused.
enum RequestError {
    Paused,
    Fatal(String),
    Retry(String),
}

fn report(progress: &mut impl FnMut(Progress), phase: Phase, received: u64, total: u64) {
    progress(Progress {
        phase,
        received,
        total,
    });
}

fn stop(cancel: &AtomicBool) -> Result<(), Error> {
    if cancel.load(Ordering::Acquire) {
        Err(Error::Cancelled)
    } else {
        Ok(())
    }
}

fn cancelled(cancel: &AtomicBool) -> Result<(), String> {
    if cancel.load(Ordering::Acquire) {
        Err("the download was cancelled".into())
    } else {
        Ok(())
    }
}

fn paused(pause: Option<&AtomicBool>) -> bool {
    pause.is_some_and(|pause| pause.load(Ordering::Acquire))
}

fn check_size(size: u64) -> Result<(), Error> {
    if size == 0 {
        Err(Error::Protocol("a download has a nonzero size".into()))
    } else {
        Ok(())
    }
}

fn digest_matches(bytes: &[u8], expected: &str) -> bool {
    hex::encode(Sha256::digest(bytes)).eq_ignore_ascii_case(expected)
}

fn held_length(file: &File, size: u64, cancel: &AtomicBool) -> Result<u64, Error> {
    stop(cancel)?;
    let metadata = file.metadata().map_err(Error::Io)?;
    stop(cancel)?;
    if !metadata.is_file() || metadata.len() > size {
        return Err(Error::Protocol(
            "the download must be written to a regular file no longer than its expected size"
                .into(),
        ));
    }
    Ok(metadata.len())
}

/// The declared length of a response, when it declares one, is exactly
/// `expected`; two declarations are always ambiguous.
fn declared_length(headers: &header::HeaderMap, expected: u64) -> Result<(), String> {
    let values: Vec<_> = headers.get_all(header::CONTENT_LENGTH).iter().collect();
    if values.len() > 1
        || values
            .first()
            .is_some_and(|value| value.to_str().ok().and_then(decimal) != Some(expected))
    {
        return Err("unexpected or ambiguous Content-Length".into());
    }
    Ok(())
}

fn decimal(value: &str) -> Option<u64> {
    if value.is_empty() || !value.bytes().all(|byte| byte.is_ascii_digit()) {
        None
    } else {
        value.parse().ok()
    }
}

/// The bytes a `206` response carries, checked against what the request asked
/// for: it starts where the file ends, stays inside the file, declares the
/// whole file's size, and its `Content-Length` agrees with its range.
fn range_size(headers: &header::HeaderMap, offset: u64, total: u64) -> Option<u64> {
    let values: Vec<_> = headers.get_all(header::CONTENT_RANGE).iter().collect();
    if values.len() != 1 {
        return None;
    }
    let value = values.first()?.to_str().ok()?.strip_prefix("bytes ")?;
    let (range, full) = value.split_once('/')?;
    let (start, end) = range.split_once('-')?;
    let (start, end, full) = (decimal(start)?, decimal(end)?, decimal(full)?);
    if start != offset || end < start || end >= total || full != total {
        return None;
    }
    let count = end.checked_sub(start)?.checked_add(1)?;
    declared_length(headers, count).ok()?;
    Some(count)
}

/// The response came from the trusted origin, and its framing means its bytes
/// are the body: one HTTP/1.1 `chunked` transfer coding with no length beside
/// it, or no transfer coding at all, and no content coding but `identity`.
fn identity(origin: &Url, response: &Response) -> Result<(), String> {
    if !same_origin(response.url(), origin) {
        return Err("the response came from an untrusted origin".into());
    }
    let mut framing = response.headers().get_all(header::TRANSFER_ENCODING).iter();
    if let Some(value) = framing.next() {
        let ambiguous = framing.next().is_some()
            || response.version() != reqwest::Version::HTTP_11
            || response.headers().contains_key(header::CONTENT_LENGTH)
            || !value
                .to_str()
                .is_ok_and(|value| value.trim().eq_ignore_ascii_case("chunked"));
        if ambiguous {
            return Err("unsupported or ambiguous Transfer-Encoding framing".into());
        }
    }
    let encodings: Vec<_> = response
        .headers()
        .get_all(header::CONTENT_ENCODING)
        .iter()
        .collect();
    if encodings.len() > 1 || encodings.first().is_some_and(|value| **value != "identity") {
        return Err("unsupported or ambiguous Content-Encoding".into());
    }
    Ok(())
}

/// How a body ended.
enum BodyEnd {
    /// Exactly the expected bytes were read.
    Complete,
    /// The caller asked to pause.
    Paused,
    /// The response ended or failed before the expected bytes arrived.
    Interrupted(String),
}

/// Read exactly `expected` bytes, allowing one more so a body that overruns is
/// noticed rather than trusted.
fn read_body(
    reader: &mut impl Read,
    expected: u64,
    cancel: &AtomicBool,
    pause: impl Fn() -> bool,
    mut chunk: impl FnMut(&[u8]) -> Result<(), Error>,
) -> Result<BodyEnd, Error> {
    let mut buffer = vec![0_u8; BUFFER];
    let mut received = 0_u64;
    loop {
        stop(cancel)?;
        if pause() {
            return Ok(BodyEnd::Paused);
        }
        let remaining = expected.saturating_sub(received);
        let limit =
            usize::try_from(remaining.saturating_add(1).min(BUFFER as u64)).unwrap_or(BUFFER);
        let target = buffer
            .get_mut(..limit)
            .ok_or_else(|| Error::Protocol("a read bound exceeded its buffer".into()))?;
        let read = reader.read(target);
        stop(cancel)?;
        if pause() {
            return Ok(BodyEnd::Paused);
        }
        let count = match read {
            Ok(count) => count,
            Err(error) => {
                return Ok(BodyEnd::Interrupted(format!(
                    "the response read failed: {error}"
                )));
            }
        };
        if count == 0 {
            return Ok(if received == expected {
                BodyEnd::Complete
            } else {
                BodyEnd::Interrupted("the response was truncated".into())
            });
        }
        let allowed = usize::try_from(remaining).unwrap_or(usize::MAX);
        if count > allowed {
            return Err(Error::Protocol(
                "the response exceeded its expected byte boundary".into(),
            ));
        }
        let got = buffer
            .get(..count)
            .ok_or_else(|| Error::Protocol("a read exceeded its buffer".into()))?;
        chunk(got)?;
        stop(cancel)?;
        received = received.saturating_add(u64::try_from(count).unwrap_or(u64::MAX));
    }
}
#[cfg(test)]
mod tests {
    #![allow(
        clippy::arithmetic_side_effects,
        reason = "test fixtures: arithmetic on small fixed lengths cannot overflow"
    )]

    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::AtomicU64;
    use test_http::{Reply, Responder};

    const BODY: &[u8] = b"the bytes of one verified download";

    fn digest(bytes: &[u8]) -> String {
        hex::encode(Sha256::digest(bytes))
    }

    fn url(responder: &Responder) -> Url {
        Url::parse(&format!("{}/file", responder.url())).unwrap()
    }

    fn origin(responder: &Responder) -> Url {
        Url::parse(&format!("{}/", responder.url())).unwrap()
    }

    fn downloader(responder: &Responder) -> Downloader {
        Downloader::new(
            origin(responder),
            Duration::from_secs(10),
            Some("Tilo-Test/1"),
        )
        .unwrap()
    }

    fn contents(file: &mut File) -> Vec<u8> {
        file.rewind().unwrap();
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes).unwrap();
        bytes
    }

    fn file_holding(prefix: &[u8]) -> File {
        let mut file = tempfile::tempfile().unwrap();
        file.write_all(prefix).unwrap();
        file
    }

    fn fetch(
        responder: &Responder,
        file: &mut File,
        size: u64,
        sha256: Option<&str>,
    ) -> Result<(), Error> {
        let cancel = AtomicBool::new(false);
        downloader(responder).fetch(
            Request {
                url: url(responder),
                size,
                sha256,
                cancel: &cancel,
                pause: None,
            },
            file,
            &mut (),
            |_| {},
        )
    }

    /// Counts what a download told its caller, and keeps the default write.
    #[derive(Default)]
    struct Recorded {
        attempts: Vec<u64>,
        chunks: u64,
        resets: u32,
        pauses: u32,
        verified: u32,
    }

    impl Hooks for Recorded {
        fn attempt(&mut self, _file: &mut File, offset: u64) -> Result<(), String> {
            self.attempts.push(offset);
            Ok(())
        }
        fn write(&mut self, file: &mut File, bytes: &[u8]) -> Result<(), String> {
            self.chunks += 1;
            append(file, bytes)
        }
        fn reset(&mut self, _file: &mut File) -> Result<(), String> {
            self.resets += 1;
            Ok(())
        }
        fn pausing(&mut self, _file: &mut File) -> Result<(), String> {
            self.pauses += 1;
            Ok(())
        }
        fn verified(&mut self, _file: &mut File) -> Result<(), String> {
            self.verified += 1;
            Ok(())
        }
    }

    #[test]
    fn a_full_download_writes_the_verified_bytes() {
        let responder = Responder::start(vec![Reply::status(200).body(BODY)]);
        let mut file = tempfile::tempfile().unwrap();
        let mut hooks = Recorded::default();
        let mut seen = Vec::new();
        let cancel = AtomicBool::new(false);
        downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size: BODY.len() as u64,
                    sha256: Some(&digest(BODY)),
                    cancel: &cancel,
                    pause: None,
                },
                &mut file,
                &mut hooks,
                |progress| seen.push(progress),
            )
            .unwrap();
        assert_eq!(contents(&mut file), BODY);
        assert_eq!(hooks.verified, 1);
        assert_eq!(hooks.attempts, vec![0]);
        assert!(hooks.chunks > 0);
        assert!(hooks.resets == 0 && hooks.pauses == 0);
        assert_eq!(seen.last().unwrap().phase, Phase::Complete);
        assert_eq!(seen.last().unwrap().received, BODY.len() as u64);
        assert!(
            seen.iter()
                .any(|progress| progress.phase == Phase::Verifying)
        );
        let request = responder.next_request();
        assert_eq!(request.header("accept-encoding"), Some("identity"));
        assert_eq!(request.header("range"), None);
    }

    #[test]
    fn a_download_resumes_from_the_bytes_it_holds() {
        let split = 8;
        let responder = Responder::start(vec![
            Reply::status(206)
                .header(
                    "Content-Range",
                    &format!("bytes {split}-{}/{}", BODY.len() - 1, BODY.len()),
                )
                .body(&BODY[split..]),
        ]);
        let mut file = file_holding(&BODY[..split]);
        let mut hooks = Recorded::default();
        let cancel = AtomicBool::new(false);
        downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size: BODY.len() as u64,
                    sha256: Some(&digest(BODY)),
                    cancel: &cancel,
                    pause: None,
                },
                &mut file,
                &mut hooks,
                |_| {},
            )
            .unwrap();
        assert_eq!(contents(&mut file), BODY);
        assert_eq!(hooks.attempts, vec![split as u64]);
        assert_eq!(
            responder.next_request().header("range"),
            Some(format!("bytes={split}-").as_str())
        );
    }

    #[test]
    fn a_server_that_ignores_the_range_restarts_from_zero() {
        let split = 8;
        let responder = Responder::start(vec![
            Reply::status(200).body(BODY),
            Reply::status(200).body(BODY),
        ]);
        let mut file = file_holding(&BODY[..split]);
        let mut hooks = Recorded::default();
        let cancel = AtomicBool::new(false);
        downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size: BODY.len() as u64,
                    sha256: Some(&digest(BODY)),
                    cancel: &cancel,
                    pause: None,
                },
                &mut file,
                &mut hooks,
                |_| {},
            )
            .unwrap();
        assert_eq!(contents(&mut file), BODY);
        assert_eq!(hooks.resets, 1);
        assert!(responder.next_request().header("range").is_some());
        assert_eq!(responder.next_request().header("range"), None);
    }

    #[test]
    fn a_416_restarts_from_zero() {
        let split = 8;
        let responder = Responder::start(vec![Reply::status(416), Reply::status(200).body(BODY)]);
        let mut file = file_holding(&BODY[..split]);
        let cancel = AtomicBool::new(false);
        downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size: BODY.len() as u64,
                    sha256: Some(&digest(BODY)),
                    cancel: &cancel,
                    pause: None,
                },
                &mut file,
                &mut (),
                |_| {},
            )
            .unwrap();
        assert_eq!(contents(&mut file), BODY);
        assert!(responder.next_request().header("range").is_some());
        assert_eq!(responder.next_request().header("range"), None);
    }

    #[test]
    fn a_wrong_content_range_restarts_from_zero() {
        let split = 8;
        let responder = Responder::start(vec![
            Reply::status(206)
                .header(
                    "Content-Range",
                    &format!("bytes {}-{}/{}", split - 1, BODY.len() - 1, BODY.len()),
                )
                .body(&BODY[split - 1..]),
            Reply::status(200).body(BODY),
        ]);
        let mut file = file_holding(&BODY[..split]);
        let mut hooks = Recorded::default();
        let cancel = AtomicBool::new(false);
        downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size: BODY.len() as u64,
                    sha256: Some(&digest(BODY)),
                    cancel: &cancel,
                    pause: None,
                },
                &mut file,
                &mut hooks,
                |_| {},
            )
            .unwrap();
        assert_eq!(contents(&mut file), BODY);
        assert_eq!(hooks.resets, 1);
        assert!(
            responder
                .drain_requests()
                .iter()
                .skip(1)
                .all(|request| request.header("range").is_none())
        );
    }

    #[test]
    fn a_redirect_to_another_origin_is_refused() {
        let elsewhere = Responder::silent();
        let escape = format!("{}/file", elsewhere.url());
        let responder = Responder::start(vec![
            Reply::status(302).header("Location", &escape),
            Reply::status(200).body(BODY),
        ]);
        let mut file = tempfile::tempfile().unwrap();
        let error = fetch(&responder, &mut file, BODY.len() as u64, None).unwrap_err();
        assert!(matches!(error, Error::Trusted(_)), "{error}");
        assert!(contents(&mut file).is_empty());
        elsewhere.assert_untouched();
    }

    #[test]
    fn a_same_origin_redirect_is_followed_up_to_five_hops() {
        for hops in [REDIRECTS, REDIRECTS + 1] {
            let mut replies = vec![Reply::status(302).header("Location", "/next"); hops];
            replies.push(Reply::status(200).body(BODY));
            let responder = Responder::start(replies);
            let mut file = tempfile::tempfile().unwrap();
            let result = fetch(&responder, &mut file, BODY.len() as u64, None);
            assert_eq!(result.is_ok(), hops == REDIRECTS, "hops {hops}");
            if hops == REDIRECTS {
                assert_eq!(contents(&mut file), BODY);
            }
            let sent = responder.drain_requests().len();
            assert_eq!(sent, hops + usize::from(hops == REDIRECTS), "hops {hops}");
        }
    }

    #[test]
    fn an_encoded_response_is_refused() {
        for encoding in ["gzip", "br", "zstd"] {
            let responder = Responder::start(vec![
                Reply::status(200)
                    .header("Content-Encoding", encoding)
                    .body(BODY),
            ]);
            let mut file = tempfile::tempfile().unwrap();
            let error = fetch(&responder, &mut file, BODY.len() as u64, None).unwrap_err();
            assert!(matches!(error, Error::Trusted(_)), "{encoding}: {error}");
            assert_eq!(responder.drain_requests().len(), 1);
        }
    }

    #[test]
    fn a_body_that_announces_a_length_and_codes_it_is_refused() {
        let mut wire = format!("{:x}\r\n", BODY.len()).into_bytes();
        wire.extend_from_slice(BODY);
        wire.extend_from_slice(b"\r\n0\r\n\r\n");
        let responder = Responder::start(vec![
            Reply::status(200)
                .header("Transfer-Encoding", "chunked")
                .body(wire),
        ]);
        let mut file = tempfile::tempfile().unwrap();
        let error = fetch(&responder, &mut file, BODY.len() as u64, None).unwrap_err();
        assert!(matches!(error, Error::Trusted(_)), "{error}");
        assert!(contents(&mut file).is_empty());
    }

    #[test]
    fn a_missing_length_is_read_to_eof() {
        let responder =
            Responder::start(vec![Reply::status(200).body(BODY).without_content_length()]);
        let mut file = tempfile::tempfile().unwrap();
        fetch(
            &responder,
            &mut file,
            BODY.len() as u64,
            Some(&digest(BODY)),
        )
        .unwrap();
        assert_eq!(contents(&mut file), BODY);
    }

    #[test]
    fn a_short_body_is_never_complete() {
        let responder = Responder::start(vec![
            Reply::status(200)
                .without_content_length()
                .header("Content-Length", &BODY.len().to_string())
                .body(&BODY[..8]);
            3
        ]);
        let mut file = tempfile::tempfile().unwrap();
        let error = fetch(&responder, &mut file, BODY.len() as u64, None).unwrap_err();
        assert!(matches!(error, Error::Incomplete(_)), "{error}");
        assert_eq!(contents(&mut file), BODY[..8]);
        assert_eq!(responder.drain_requests().len(), ATTEMPTS);
    }

    #[test]
    fn a_size_mismatch_is_final() {
        let responder = Responder::start(vec![
            Reply::status(200)
                .without_content_length()
                .header("Content-Length", &(BODY.len() + 1).to_string())
                .body(BODY),
        ]);
        let mut file = tempfile::tempfile().unwrap();
        let error = fetch(&responder, &mut file, BODY.len() as u64, None).unwrap_err();
        assert!(matches!(error, Error::Protocol(_)), "{error}");
        assert_eq!(responder.drain_requests().len(), 1);
        assert!(contents(&mut file).is_empty());
    }

    #[test]
    fn a_digest_mismatch_is_final() {
        let responder = Responder::start(vec![Reply::status(200).body(BODY)]);
        let mut file = tempfile::tempfile().unwrap();
        let error = fetch(
            &responder,
            &mut file,
            BODY.len() as u64,
            Some(&digest(b"other")),
        )
        .unwrap_err();
        assert!(matches!(error, Error::Digest), "{error}");
        assert_eq!(responder.drain_requests().len(), 1);
    }

    #[test]
    fn an_already_complete_file_is_verified_without_a_request() {
        let responder = Responder::silent();
        let mut file = file_holding(BODY);
        fetch(
            &responder,
            &mut file,
            BODY.len() as u64,
            Some(&digest(BODY)),
        )
        .unwrap();
        assert_eq!(contents(&mut file), BODY);
        let mut wrong = file_holding(BODY);
        let error = fetch(
            &responder,
            &mut wrong,
            BODY.len() as u64,
            Some(&digest(b"other")),
        )
        .unwrap_err();
        assert!(matches!(error, Error::Digest), "{error}");
        responder.assert_untouched();
    }

    #[test]
    fn a_held_file_longer_than_the_download_is_refused() {
        let responder = Responder::silent();
        let mut file = file_holding(&[b'x'; 4]);
        let error = fetch(&responder, &mut file, 3, None).unwrap_err();
        assert!(matches!(error, Error::Protocol(_)), "{error}");
        responder.assert_untouched();
    }

    #[test]
    fn a_cancel_stops_the_download_where_the_bytes_arrive() {
        let body = vec![b'E'; 200_000];
        let responder = Responder::start(vec![Reply::status(200).body(body.clone())]);
        let mut file = tempfile::tempfile().unwrap();
        let cancel = Arc::new(AtomicBool::new(false));
        let signal = Arc::clone(&cancel);
        let size = body.len() as u64;
        let error = downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size,
                    sha256: None,
                    cancel: &cancel,
                    pause: None,
                },
                &mut file,
                &mut (),
                move |progress| {
                    if progress.phase == Phase::Receiving && progress.received > 0 {
                        signal.store(true, Ordering::Release);
                    }
                },
            )
            .unwrap_err();
        assert!(matches!(error, Error::Cancelled), "{error}");
        assert!(file.metadata().unwrap().len() < size);
    }

    #[test]
    fn a_pause_settles_the_held_bytes_before_it_returns() {
        let body = vec![b'E'; 200_000];
        let responder = Responder::start(vec![Reply::status(200).body(body.clone())]);
        let mut file = tempfile::tempfile().unwrap();
        let pause = Arc::new(AtomicBool::new(false));
        let signal = Arc::clone(&pause);
        let cancel = AtomicBool::new(false);
        let mut hooks = Recorded::default();
        let size = body.len() as u64;
        let error = downloader(&responder)
            .fetch(
                Request {
                    url: url(&responder),
                    size,
                    sha256: None,
                    cancel: &cancel,
                    pause: Some(&pause),
                },
                &mut file,
                &mut hooks,
                move |progress| {
                    if progress.phase == Phase::Receiving && progress.received > 0 {
                        signal.store(true, Ordering::Release);
                    }
                },
            )
            .unwrap_err();
        assert!(matches!(error, Error::Paused), "{error}");
        assert_eq!(hooks.pauses, 1);
        let held = file.metadata().unwrap().len();
        assert!(held > 0 && held < size, "held {held}");
    }

    #[test]
    fn a_body_that_overruns_its_boundary_is_refused() {
        let responder =
            Responder::start(vec![Reply::status(200).without_content_length().body(BODY)]);
        let mut file = tempfile::tempfile().unwrap();
        let error = fetch(&responder, &mut file, (BODY.len() - 1) as u64, None).unwrap_err();
        assert!(matches!(error, Error::Protocol(_)), "{error}");
    }

    #[test]
    fn fetch_bytes_verifies_a_whole_in_memory_download() {
        let responder = Responder::start(vec![Reply::status(200).body(BODY)]);
        let cancel = AtomicBool::new(false);
        let bytes = downloader(&responder)
            .fetch_bytes(Request {
                url: url(&responder),
                size: BODY.len() as u64,
                sha256: Some(&digest(BODY)),
                cancel: &cancel,
                pause: None,
            })
            .unwrap();
        assert_eq!(bytes, BODY);
        assert_eq!(responder.next_request().header("range"), None);
    }

    #[test]
    fn fetch_bytes_refuses_a_digest_mismatch_and_a_short_body() {
        let responder = Responder::start(vec![Reply::status(200).body(BODY)]);
        let cancel = AtomicBool::new(false);
        let error = downloader(&responder)
            .fetch_bytes(Request {
                url: url(&responder),
                size: BODY.len() as u64,
                sha256: Some(&digest(b"other")),
                cancel: &cancel,
                pause: None,
            })
            .unwrap_err();
        assert!(matches!(error, Error::Digest), "{error}");
        let responder = Responder::start(vec![
            Reply::status(200)
                .without_content_length()
                .header("Content-Length", &BODY.len().to_string())
                .body(&BODY[..8]),
        ]);
        let error = downloader(&responder)
            .fetch_bytes(Request {
                url: url(&responder),
                size: BODY.len() as u64,
                sha256: None,
                cancel: &cancel,
                pause: None,
            })
            .unwrap_err();
        assert!(matches!(error, Error::Incomplete(_)), "{error}");
    }

    #[test]
    fn append_retries_interruptions_and_reports_a_partial_write() {
        struct Writer {
            written: Vec<u8>,
            interrupted: u8,
            limit: usize,
        }
        impl Write for Writer {
            fn flush(&mut self) -> io::Result<()> {
                Ok(())
            }
            fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
                if self.interrupted > 0 {
                    self.interrupted -= 1;
                    return Err(io::Error::from(io::ErrorKind::Interrupted));
                }
                if self.written.len() >= self.limit {
                    return Err(io::Error::other("injected write failure"));
                }
                let count = bytes.len().min(4).min(self.limit - self.written.len());
                self.written.extend_from_slice(&bytes[..count]);
                Ok(count)
            }
        }
        let mut writer = Writer {
            written: Vec::new(),
            interrupted: 3,
            limit: usize::MAX,
        };
        append(&mut writer, b"eightbyt").unwrap();
        assert_eq!(writer.written, b"eightbyt");

        let mut failing = Writer {
            written: Vec::new(),
            interrupted: 0,
            limit: 5,
        };
        let error = append(&mut failing, b"eightbyt").unwrap_err();
        assert!(error.contains("injected write failure"), "{error}");
        assert_eq!(failing.written, b"eight");
    }

    #[test]
    fn a_write_that_is_always_interrupted_stops_in_a_bounded_way() {
        struct Interrupted(AtomicU64);
        impl Write for Interrupted {
            fn flush(&mut self) -> io::Result<()> {
                Ok(())
            }
            fn write(&mut self, _bytes: &[u8]) -> io::Result<usize> {
                self.0.fetch_add(1, Ordering::Relaxed);
                Err(io::Error::from(io::ErrorKind::Interrupted))
            }
        }
        let mut writer = Interrupted(AtomicU64::new(0));
        let error = append(&mut writer, b"bytes").unwrap_err();
        assert_eq!(
            writer.0.load(Ordering::Relaxed),
            u64::from(INTERRUPTED_WRITES)
        );
        assert!(error.contains("interrupted"), "{error}");
    }
}