grit-lib 0.5.25

Core library for the grit Git implementation
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
//! Transfers that survive a dropped connection: downloads resumed with HTTP
//! `Range` requests, and uploads over the [TUS] resumable-upload protocol.
//!
//! Clone uses [`download_resumable`] for the bundle a server advertises with
//! `bundle-uri` ([`crate::bundle_uri`]); push uses [`TusUpload`] to send a large
//! bundle to the endpoint a server advertises with `pushBundle`
//! ([`crate::push`]). Both keep their progress on disk, so a transfer that is
//! interrupted (even by the process exiting) picks up where it stopped the next
//! time it runs, and both retry a dropped connection a few times before giving
//! up.
//!
//! Requests go through [`HttpClient::send`]; clients that do not implement it
//! cannot make these transfers. Progress is reported both as the
//! [`ProgressPhase::TransferringBundle`] phase and, in more detail, as
//! [`ResumableEvent`]s.
//!
//! [TUS]: https://tus.io/protocols/resumable-upload

use std::fs::{File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::fetch::Progress;
use crate::progress::{Cancel, ProgressPhase, ProgressUnit};
use crate::transport::http::{HttpClient, HttpMethod, HttpRequest, HttpResponse};

/// TUS protocol version this client and `grit serve` speak.
pub const TUS_VERSION: &str = "1.0.0";

/// Content type of a TUS `PATCH` body.
pub const TUS_PATCH_CONTENT_TYPE: &str = "application/offset+octet-stream";

/// Bytes sent per TUS `PATCH` request.
const PATCH_CHUNK: usize = 4 * 1024 * 1024;

/// Bytes read from a download body between progress reports.
const READ_CHUNK: usize = 64 * 1024;

/// Times a dropped connection is retried before the transfer gives up.
const MAX_ATTEMPTS: u32 = 5;

/// Which way a resumable transfer moves data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferDirection {
    /// Downloading a bundle from the server.
    Download,
    /// Uploading a bundle to the server.
    Upload,
}

/// Progress of a resumable transfer, reported through
/// [`Progress::resumable_transfer`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResumableEvent {
    /// The transfer began. `resumed_from` is how many bytes an earlier attempt
    /// had already moved (zero for a fresh transfer).
    Started {
        /// Download or upload.
        direction: TransferDirection,
        /// The URL being transferred.
        url: String,
        /// Total bytes, when known.
        total: Option<u64>,
        /// Bytes already in place before this run.
        resumed_from: u64,
    },
    /// More bytes arrived or were accepted.
    Advanced {
        /// Bytes in place so far, including `resumed_from`.
        done: u64,
        /// Total bytes, when known.
        total: Option<u64>,
    },
    /// The connection dropped; the transfer is picking up again at `done`.
    Reconnecting {
        /// Bytes in place when the connection dropped.
        done: u64,
        /// Which retry this is, starting at 1.
        attempt: u32,
    },
    /// Every byte is in place.
    Finished {
        /// Total bytes transferred.
        done: u64,
        /// Bytes that were already in place before this run.
        resumed_from: u64,
    },
}

/// Why a resumable transfer failed.
#[derive(Debug, thiserror::Error)]
pub enum ResumableError {
    /// The server answered with a status the transfer cannot continue from.
    #[error("HTTP {status} from {url}")]
    Status {
        /// The request URL.
        url: String,
        /// The response status.
        status: u16,
    },
    /// The server's answer did not follow the protocol.
    #[error("unexpected response from {url}: {detail}")]
    Protocol {
        /// The request URL.
        url: String,
        /// What was wrong.
        detail: String,
    },
    /// The connection kept dropping.
    #[error("the transfer of {url} kept failing after {attempts} attempts: {last}")]
    Exhausted {
        /// The URL being transferred.
        url: String,
        /// Attempts made.
        attempts: u32,
        /// The last failure.
        last: String,
    },
    /// The HTTP client failed (transport or authentication).
    #[error(transparent)]
    Http(Box<crate::error::Error>),
    /// Reading or writing the local file failed.
    #[error(transparent)]
    Io(#[from] std::io::Error),
    /// [`Cancel`] asked the download to stop. The partial file is kept, so
    /// the next call resumes it.
    #[error("operation cancelled")]
    Cancelled,
}

impl From<crate::error::Error> for ResumableError {
    fn from(e: crate::error::Error) -> Self {
        Self::Http(Box::new(e))
    }
}

/// Result alias for resumable transfers.
pub type Result<T> = std::result::Result<T, ResumableError>;

/// What [`download_resumable`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DownloadOutcome {
    /// Size of the downloaded file.
    pub bytes: u64,
    /// Bytes that were already on disk from an earlier, interrupted run.
    pub resumed_from: u64,
}

/// What is known about a partial download, kept next to it as JSON.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
struct DownloadState {
    /// The URL the bytes came from; another URL starts over.
    url: String,
    /// The server's `ETag` (or `Last-Modified`), sent back as `If-Range` so a
    /// changed file is downloaded afresh instead of spliced.
    validator: Option<String>,
    /// Total size, once the server said.
    total: Option<u64>,
    /// Whether every byte is in place (the file is at its final path).
    complete: bool,
}

/// The files a download to `dest` keeps: the bytes so far and their state.
fn download_paths(dest: &Path) -> (PathBuf, PathBuf) {
    let name = dest
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    (
        dest.with_file_name(format!("{name}.part")),
        dest.with_file_name(format!("{name}.json")),
    )
}

fn load_state(path: &Path) -> Option<DownloadState> {
    serde_json::from_slice(&std::fs::read(path).ok()?).ok()
}

fn save_state(path: &Path, state: &DownloadState) -> Result<()> {
    let tmp = path.with_extension("json.tmp");
    let bytes = serde_json::to_vec(state).map_err(|e| std::io::Error::other(e.to_string()))?;
    std::fs::write(&tmp, bytes)?;
    std::fs::rename(&tmp, path)?;
    Ok(())
}

/// Remove `dest` and everything [`download_resumable`] keeps for it.
///
/// # Errors
///
/// A removal failure other than the file not existing.
pub fn discard_download(dest: &Path) -> std::io::Result<()> {
    let (part, state) = download_paths(dest);
    for path in [dest, part.as_path(), state.as_path()] {
        match std::fs::remove_file(path) {
            Ok(()) => {}
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => return Err(e),
        }
    }
    Ok(())
}

/// Download `url` to `dest`, continuing an earlier interrupted download.
///
/// Bytes arrive in `<dest>.part`, with what is known about them in
/// `<dest>.json`; the file is renamed to `dest` once complete. When a
/// previous run left a partial file for the same URL, only the rest is asked
/// for (`Range: bytes=<n>-`, with `If-Range` so a file that changed on the
/// server starts over). A connection that drops mid-download is retried a few
/// times from where it stopped. When `dest` is already complete for `url`,
/// nothing is downloaded.
///
/// # Parameters
///
/// - `client`: sends the requests ([`HttpClient::send`]).
/// - `url`: what to download.
/// - `dest`: where the finished file goes; its directory must exist.
/// - `progress`: receives [`ResumableEvent`]s.
/// - `cancel`: checked before each request and between body chunks.
///
/// # Errors
///
/// [`ResumableError::Status`] when the server refuses the download,
/// [`ResumableError::Exhausted`] when the connection keeps dropping,
/// [`ResumableError::Cancelled`] when `cancel` fires, and I/O errors from the
/// local files. The partial file is kept so a later call can resume.
pub fn download_resumable(
    client: &dyn HttpClient,
    url: &str,
    dest: &Path,
    progress: &mut dyn Progress,
    cancel: &dyn Cancel,
) -> Result<DownloadOutcome> {
    let (part, state_path) = download_paths(dest);
    let mut state = load_state(&state_path).unwrap_or_default();
    if state.url != url {
        discard_download(dest)?;
        state = DownloadState {
            url: url.to_owned(),
            ..DownloadState::default()
        };
    }
    if state.complete {
        if let (Some(total), Ok(meta)) = (state.total, std::fs::metadata(dest)) {
            if meta.len() == total {
                return Ok(DownloadOutcome {
                    bytes: total,
                    resumed_from: total,
                });
            }
        }
        discard_download(dest)?;
        state = DownloadState {
            url: url.to_owned(),
            ..DownloadState::default()
        };
    }

    let resumed_from = std::fs::metadata(&part).map(|m| m.len()).unwrap_or(0);
    let mut started = false;
    let mut attempt = 0;
    let mut last_failure = String::new();
    loop {
        if cancel.is_cancelled() {
            return Err(ResumableError::Cancelled);
        }
        if attempt > 0 {
            let done = std::fs::metadata(&part).map(|m| m.len()).unwrap_or(0);
            if attempt >= MAX_ATTEMPTS {
                return Err(ResumableError::Exhausted {
                    url: url.to_owned(),
                    attempts: attempt,
                    last: last_failure,
                });
            }
            progress.resumable_transfer(&ResumableEvent::Reconnecting { done, attempt });
        }
        attempt += 1;
        match download_attempt(
            client,
            url,
            &part,
            &mut state,
            &state_path,
            progress,
            cancel,
            |p, s| {
                if !started {
                    started = true;
                    p.start_with_unit(
                        ProgressPhase::TransferringBundle,
                        ProgressUnit::Bytes,
                        s.total,
                    );
                    p.set(s.resumed_from);
                    p.resumable_transfer(&ResumableEvent::Started {
                        direction: TransferDirection::Download,
                        url: url.to_owned(),
                        total: s.total,
                        resumed_from: s.resumed_from,
                    });
                }
            },
        ) {
            Ok(AttemptOutcome::Complete(bytes)) => {
                std::fs::rename(&part, dest)?;
                state.complete = true;
                state.total = Some(bytes);
                save_state(&state_path, &state)?;
                progress.set(bytes);
                progress.finish();
                progress.resumable_transfer(&ResumableEvent::Finished {
                    done: bytes,
                    resumed_from: resumed_from.min(bytes),
                });
                return Ok(DownloadOutcome {
                    bytes,
                    resumed_from: resumed_from.min(bytes),
                });
            }
            Ok(AttemptOutcome::Dropped(why)) => last_failure = why,
            Err(e) => return Err(e),
        }
    }
}

/// Where a download attempt starts, reported once when the first one answers.
struct AttemptStart {
    total: Option<u64>,
    resumed_from: u64,
}

enum AttemptOutcome {
    /// Every byte is in the partial file; the value is its size.
    Complete(u64),
    /// The connection dropped before the end.
    Dropped(String),
}

/// One request of a download: ask for the bytes not yet on disk and append
/// them to `part`.
#[allow(clippy::too_many_arguments)]
fn download_attempt(
    client: &dyn HttpClient,
    url: &str,
    part: &Path,
    state: &mut DownloadState,
    state_path: &Path,
    progress: &mut dyn Progress,
    cancel: &dyn Cancel,
    mut on_start: impl FnMut(&mut dyn Progress, &AttemptStart),
) -> Result<AttemptOutcome> {
    let have = std::fs::metadata(part).map(|m| m.len()).unwrap_or(0);
    let mut request = HttpRequest::new(HttpMethod::Get, url).long_running();
    if have > 0 {
        request = request.header("Range", format!("bytes={have}-"));
        if let Some(validator) = &state.validator {
            request = request.header("If-Range", validator.clone());
        }
    }
    let response = match client.send(&request) {
        Ok(r) => r,
        Err(e) => return Ok(AttemptOutcome::Dropped(e.to_string())),
    };
    let (mut offset, total) =
        match response.status {
            206 if have > 0 => {
                let (start, total) = parse_content_range(response.header("content-range"))
                    .ok_or_else(|| ResumableError::Protocol {
                        url: url.to_owned(),
                        detail: "206 without a usable Content-Range".to_owned(),
                    })?;
                if start != have {
                    return Err(ResumableError::Protocol {
                        url: url.to_owned(),
                        detail: format!("asked for bytes from {have}, got bytes from {start}"),
                    });
                }
                (have, total)
            }
            200 | 206 => {
                // A full answer: the server ignored the range or the file changed.
                let total = response
                    .header("content-length")
                    .and_then(|v| v.trim().parse().ok());
                (0, total)
            }
            416 if have > 0 && state.total == Some(have) => {
                on_start(
                    progress,
                    &AttemptStart {
                        total: state.total,
                        resumed_from: have,
                    },
                );
                return Ok(AttemptOutcome::Complete(have));
            }
            416 => {
                // The partial file is longer than the server's: start over.
                File::create(part)?;
                return Ok(AttemptOutcome::Dropped(
                    "the server's file is shorter than the partial download".to_owned(),
                ));
            }
            status => {
                return Err(ResumableError::Status {
                    url: url.to_owned(),
                    status,
                })
            }
        };
    state.total = total;
    state.validator = validator(&response);
    save_state(state_path, state)?;
    on_start(
        progress,
        &AttemptStart {
            total,
            resumed_from: offset,
        },
    );

    let mut file = if offset == 0 {
        File::create(part)?
    } else {
        OpenOptions::new().append(true).open(part)?
    };
    let mut body = response.body;
    let mut buf = vec![0u8; READ_CHUNK];
    loop {
        if cancel.is_cancelled() {
            file.flush()?;
            return Err(ResumableError::Cancelled);
        }
        let n = match body.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => n,
            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(e) => {
                file.flush()?;
                return Ok(AttemptOutcome::Dropped(e.to_string()));
            }
        };
        file.write_all(&buf[..n])?;
        offset += n as u64;
        progress.set(offset);
        progress.resumable_transfer(&ResumableEvent::Advanced {
            done: offset,
            total,
        });
    }
    file.flush()?;
    file.sync_all()?;
    match total {
        Some(total) if offset < total => Ok(AttemptOutcome::Dropped(format!(
            "the connection closed after {offset} of {total} bytes"
        ))),
        _ => Ok(AttemptOutcome::Complete(offset)),
    }
}

/// The value to send back as `If-Range`: a strong `ETag`, else `Last-Modified`.
fn validator(response: &HttpResponse) -> Option<String> {
    response
        .header("etag")
        .filter(|v| !v.starts_with("W/"))
        .or_else(|| response.header("last-modified"))
        .map(str::to_owned)
}

/// Parse `bytes <start>-<end>/<total>` into the start and (known) total.
fn parse_content_range(value: Option<&str>) -> Option<(u64, Option<u64>)> {
    let rest = value?.trim().strip_prefix("bytes ")?;
    let (range, total) = rest.split_once('/')?;
    let (start, _end) = range.split_once('-')?;
    let start = start.trim().parse().ok()?;
    let total = match total.trim() {
        "*" => None,
        t => Some(t.parse().ok()?),
    };
    Some((start, total))
}

/// An upload to a TUS endpoint.
///
/// [`TusUpload::create`] asks the endpoint for a new upload of a known length
/// and returns its URL; [`TusUpload::send_file`] sends a file to it, starting
/// at whatever offset the server already has, so calling it again after an
/// interruption (with the same upload URL) only sends the rest.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TusUpload {
    /// Absolute URL of the upload.
    pub url: String,
    /// Total bytes of the upload.
    pub length: u64,
}

impl TusUpload {
    /// Create an upload of `length` bytes at `endpoint` (TUS creation
    /// extension).
    ///
    /// # Errors
    ///
    /// [`ResumableError::Status`] when the server refuses, and
    /// [`ResumableError::Protocol`] when it answers without a `Location`.
    pub fn create(client: &dyn HttpClient, endpoint: &str, length: u64) -> Result<Self> {
        let request = HttpRequest::new(HttpMethod::Post, endpoint)
            .header("Tus-Resumable", TUS_VERSION)
            .header("Upload-Length", length.to_string());
        let response = client.send(&request)?;
        if response.status != 201 {
            return Err(ResumableError::Status {
                url: endpoint.to_owned(),
                status: response.status,
            });
        }
        let location = response
            .header("location")
            .ok_or_else(|| ResumableError::Protocol {
                url: endpoint.to_owned(),
                detail: "no Location for the new upload".to_owned(),
            })?;
        let url = resolve_url(endpoint, location).ok_or_else(|| ResumableError::Protocol {
            url: endpoint.to_owned(),
            detail: format!("bad Location {location:?}"),
        })?;
        Ok(Self { url, length })
    }

    /// How many bytes the server has, or `None` when it no longer knows the
    /// upload (expired or already used).
    ///
    /// # Errors
    ///
    /// [`ResumableError::Status`] for other refusals, and
    /// [`ResumableError::Protocol`] for an answer without `Upload-Offset`.
    pub fn offset(&self, client: &dyn HttpClient) -> Result<Option<u64>> {
        let request =
            HttpRequest::new(HttpMethod::Head, &self.url).header("Tus-Resumable", TUS_VERSION);
        let response = client.send(&request)?;
        match response.status {
            404 | 410 => Ok(None),
            s if (200..300).contains(&s) => upload_offset(&response, &self.url).map(Some),
            status => Err(ResumableError::Status {
                url: self.url.clone(),
                status,
            }),
        }
    }

    /// Send `file` (exactly [`Self::length`] bytes) to the upload, starting at
    /// the server's offset and resynchronizing after each failed request.
    ///
    /// # Errors
    ///
    /// [`ResumableError::Status`] when the server no longer knows the upload
    /// (`404`/`410`) or refuses it, [`ResumableError::Exhausted`] when requests
    /// keep failing, and I/O errors reading `file`.
    pub fn send_file(
        &self,
        client: &dyn HttpClient,
        file: &Path,
        progress: &mut dyn Progress,
    ) -> Result<()> {
        let gone = |status| ResumableError::Status {
            url: self.url.clone(),
            status,
        };
        let mut offset = self.offset(client)?.ok_or_else(|| gone(404))?;
        let resumed_from = offset;
        progress.start_with_unit(
            ProgressPhase::TransferringBundle,
            ProgressUnit::Bytes,
            Some(self.length),
        );
        progress.set(offset);
        progress.resumable_transfer(&ResumableEvent::Started {
            direction: TransferDirection::Upload,
            url: self.url.clone(),
            total: Some(self.length),
            resumed_from,
        });
        let mut input = File::open(file)?;
        let mut buf = vec![0u8; PATCH_CHUNK];
        let mut failures = 0;
        while offset < self.length {
            let n = usize::try_from(self.length - offset)
                .map_or(PATCH_CHUNK, |left| left.min(PATCH_CHUNK));
            input.seek(SeekFrom::Start(offset))?;
            input.read_exact(&mut buf[..n])?;
            let request = HttpRequest::new(HttpMethod::Patch, &self.url)
                .header("Tus-Resumable", TUS_VERSION)
                .header("Upload-Offset", offset.to_string())
                .header("Content-Type", TUS_PATCH_CONTENT_TYPE)
                .body(&buf[..n])
                .long_running();
            let failure = match client.send(&request) {
                Ok(r) if r.status == 204 || r.status == 200 => {
                    offset = upload_offset(&r, &self.url)?;
                    failures = 0;
                    progress.set(offset);
                    progress.resumable_transfer(&ResumableEvent::Advanced {
                        done: offset,
                        total: Some(self.length),
                    });
                    continue;
                }
                Ok(r) if r.status == 404 || r.status == 410 => return Err(gone(r.status)),
                Ok(r) if r.status == 409 => format!("offset {offset} was out of date"),
                Ok(r) if (400..500).contains(&r.status) => return Err(gone(r.status)),
                Ok(r) => format!("HTTP {}", r.status),
                Err(e) => e.to_string(),
            };
            failures += 1;
            if failures >= MAX_ATTEMPTS {
                return Err(ResumableError::Exhausted {
                    url: self.url.clone(),
                    attempts: failures,
                    last: failure,
                });
            }
            progress.resumable_transfer(&ResumableEvent::Reconnecting {
                done: offset,
                attempt: failures,
            });
            // Ask the server where it got to; it may have kept part of the chunk.
            if let Ok(Some(server)) = self.offset(client) {
                offset = server;
            }
        }
        progress.set(self.length);
        progress.finish();
        progress.resumable_transfer(&ResumableEvent::Finished {
            done: self.length,
            resumed_from,
        });
        Ok(())
    }
}

fn upload_offset(response: &HttpResponse, url: &str) -> Result<u64> {
    response
        .header("upload-offset")
        .and_then(|v| v.trim().parse().ok())
        .ok_or_else(|| ResumableError::Protocol {
            url: url.to_owned(),
            detail: "no Upload-Offset".to_owned(),
        })
}

/// Resolve `reference` (absolute, or relative to `base`) to an absolute URL.
#[must_use]
pub fn resolve_url(base: &str, reference: &str) -> Option<String> {
    if let Ok(absolute) = url::Url::parse(reference) {
        return Some(absolute.to_string());
    }
    url::Url::parse(base)
        .ok()?
        .join(reference)
        .ok()
        .map(|u| u.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fetch::NoProgress;
    use crate::progress::{FlagCancel, NeverCancel};
    use std::collections::VecDeque;
    use std::sync::Mutex;

    /// Serves one file from memory, honoring `Range`, and can cut a body short.
    struct FileServer {
        data: Vec<u8>,
        etag: String,
        /// Bytes to send before dropping, one entry per GET (then full bodies).
        cuts: Mutex<VecDeque<usize>>,
        ranges: Mutex<Vec<Option<String>>>,
    }

    struct Cut {
        data: std::io::Cursor<Vec<u8>>,
        fail_after: bool,
    }

    impl Read for Cut {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            let n = self.data.read(buf)?;
            if n == 0 && self.fail_after {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::ConnectionReset,
                    "reset",
                ));
            }
            Ok(n)
        }
    }

    impl HttpClient for FileServer {
        fn get(&self, _: &str, _: Option<&str>) -> crate::error::Result<Vec<u8>> {
            unreachable!()
        }
        fn post(
            &self,
            _: &str,
            _: &str,
            _: &str,
            _: &[u8],
            _: Option<&str>,
        ) -> crate::error::Result<Vec<u8>> {
            unreachable!()
        }
        fn send(&self, request: &HttpRequest<'_>) -> crate::error::Result<HttpResponse> {
            let range = request
                .headers
                .iter()
                .find(|(n, _)| *n == "Range")
                .map(|(_, v)| v.clone());
            let if_range = request
                .headers
                .iter()
                .find(|(n, _)| *n == "If-Range")
                .map(|(_, v)| v.clone());
            self.ranges
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .push(range.clone());
            let start = range
                .as_deref()
                .and_then(|r| r.strip_prefix("bytes="))
                .and_then(|r| r.strip_suffix('-'))
                .and_then(|r| r.parse::<usize>().ok())
                .filter(|_| if_range.as_deref().is_none_or(|v| v == self.etag));
            let total = self.data.len();
            let (status, mut headers, body) = match start {
                Some(s) if s >= total => (416, vec![], vec![]),
                Some(s) => (
                    206,
                    vec![(
                        "content-range".to_owned(),
                        format!("bytes {s}-{}/{total}", total - 1),
                    )],
                    self.data[s..].to_vec(),
                ),
                None => (
                    200,
                    vec![("content-length".to_owned(), total.to_string())],
                    self.data.clone(),
                ),
            };
            headers.push(("etag".to_owned(), self.etag.clone()));
            let cut = self
                .cuts
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .pop_front();
            let (body, fail_after) = match cut {
                Some(n) if n < body.len() => (body[..n].to_vec(), true),
                _ => (body, false),
            };
            Ok(HttpResponse {
                status,
                headers,
                body: Box::new(Cut {
                    data: std::io::Cursor::new(body),
                    fail_after,
                }),
            })
        }
    }

    #[derive(Default)]
    struct Events(Vec<ResumableEvent>);
    impl crate::progress::ProgressSink for Events {}
    impl Progress for Events {
        fn resumable_transfer(&mut self, event: &ResumableEvent) {
            if !matches!(event, ResumableEvent::Advanced { .. }) {
                self.0.push(event.clone());
            }
        }
    }

    fn server(cuts: &[usize]) -> FileServer {
        FileServer {
            data: (0..200_000u32).map(|i| (i % 251) as u8).collect(),
            etag: "\"v1\"".to_owned(),
            cuts: Mutex::new(cuts.iter().copied().collect()),
            ranges: Mutex::new(Vec::new()),
        }
    }

    fn tempdir() -> tempfile::TempDir {
        match tempfile::tempdir() {
            Ok(d) => d,
            Err(e) => panic!("tempdir: {e}"),
        }
    }

    #[test]
    fn dropped_downloads_resume_in_the_same_run() {
        let dir = tempdir();
        let dest = dir.path().join("clone.bundle");
        let srv = server(&[70_000, 10_000]);
        let mut events = Events::default();
        let outcome = download_resumable(&srv, "http://h/b", &dest, &mut events, &NeverCancel);
        assert_eq!(
            outcome.ok(),
            Some(DownloadOutcome {
                bytes: 200_000,
                resumed_from: 0
            })
        );
        assert_eq!(std::fs::read(&dest).ok(), Some(srv.data.clone()));
        let ranges = srv.ranges.lock().unwrap_or_else(|e| e.into_inner()).clone();
        assert_eq!(
            ranges,
            [
                None,
                Some("bytes=70000-".to_owned()),
                Some("bytes=80000-".to_owned())
            ]
        );
        assert!(matches!(
            events.0.as_slice(),
            [
                ResumableEvent::Started {
                    resumed_from: 0,
                    total: Some(200_000),
                    ..
                },
                ResumableEvent::Reconnecting {
                    done: 70_000,
                    attempt: 1
                },
                ResumableEvent::Reconnecting {
                    done: 80_000,
                    attempt: 2
                },
                ResumableEvent::Finished {
                    done: 200_000,
                    resumed_from: 0
                },
            ]
        ));
    }

    #[test]
    fn a_later_run_resumes_a_partial_download() {
        let dir = tempdir();
        let dest = dir.path().join("clone.bundle");
        // The first run's connection keeps dropping until it gives up.
        let failing = server(&[50_000, 0, 0, 0, 0, 0]);
        assert!(matches!(
            download_resumable(&failing, "http://h/b", &dest, &mut NoProgress, &NeverCancel),
            Err(ResumableError::Exhausted { .. })
        ));
        assert!(!dest.exists());

        let srv = server(&[]);
        let mut events = Events::default();
        let outcome = download_resumable(&srv, "http://h/b", &dest, &mut events, &NeverCancel);
        assert_eq!(outcome.ok().map(|o| o.resumed_from), Some(50_000));
        assert_eq!(std::fs::read(&dest).ok(), Some(srv.data.clone()));
        assert!(matches!(
            events.0.first(),
            Some(ResumableEvent::Started {
                resumed_from: 50_000,
                ..
            })
        ));

        // Complete already: nothing is requested.
        let again = server(&[]);
        let outcome =
            download_resumable(&again, "http://h/b", &dest, &mut NoProgress, &NeverCancel);
        assert_eq!(outcome.ok().map(|o| o.resumed_from), Some(200_000));
        assert!(again
            .ranges
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .is_empty());
    }

    #[test]
    fn a_changed_file_or_url_starts_over() {
        let dir = tempdir();
        let dest = dir.path().join("clone.bundle");
        let failing = server(&[50_000, 0, 0, 0, 0, 0]);
        let _ = download_resumable(&failing, "http://h/b", &dest, &mut NoProgress, &NeverCancel);

        let mut changed = server(&[]);
        changed.etag = "\"v2\"".to_owned();
        let outcome =
            download_resumable(&changed, "http://h/b", &dest, &mut NoProgress, &NeverCancel);
        assert_eq!(outcome.ok().map(|o| o.bytes), Some(200_000));
        assert_eq!(std::fs::read(&dest).ok(), Some(changed.data.clone()));

        let other = server(&[]);
        let outcome = download_resumable(
            &other,
            "http://h/other",
            &dest,
            &mut NoProgress,
            &NeverCancel,
        );
        assert_eq!(outcome.ok().map(|o| o.resumed_from), Some(0));
        assert_eq!(
            other
                .ranges
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .as_slice(),
            [None]
        );
    }

    /// Cancels once `after` bytes of the download are on disk.
    struct CancelAfter {
        flag: FlagCancel,
        after: u64,
    }
    impl crate::progress::ProgressSink for CancelAfter {}
    impl Progress for CancelAfter {
        fn resumable_transfer(&mut self, event: &ResumableEvent) {
            if let ResumableEvent::Advanced { done, .. } = event {
                if *done >= self.after {
                    self.flag.cancel();
                }
            }
        }
    }

    #[test]
    fn a_cancelled_download_stops_mid_body_and_resumes_next_time() {
        let dir = tempdir();
        let dest = dir.path().join("clone.bundle");
        let srv = server(&[]);
        let flag = FlagCancel::new();
        let mut progress = CancelAfter {
            flag: flag.clone(),
            after: 1,
        };
        assert!(matches!(
            download_resumable(&srv, "http://h/b", &dest, &mut progress, &flag),
            Err(ResumableError::Cancelled)
        ));
        assert!(!dest.exists());
        let (part, _) = download_paths(&dest);
        let kept = std::fs::metadata(&part).map(|m| m.len()).unwrap_or(0);
        assert!(
            kept > 0 && kept < 200_000,
            "one chunk is kept, got {kept} bytes"
        );

        let outcome = download_resumable(&srv, "http://h/b", &dest, &mut NoProgress, &NeverCancel);
        assert_eq!(outcome.ok().map(|o| o.resumed_from), Some(kept));
        assert_eq!(std::fs::read(&dest).ok(), Some(srv.data.clone()));
    }

    #[test]
    fn an_already_cancelled_download_sends_no_request() {
        let dir = tempdir();
        let dest = dir.path().join("clone.bundle");
        let srv = server(&[]);
        let flag = FlagCancel::new();
        flag.cancel();
        assert!(matches!(
            download_resumable(&srv, "http://h/b", &dest, &mut NoProgress, &flag),
            Err(ResumableError::Cancelled)
        ));
        assert!(srv
            .ranges
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .is_empty());
    }

    #[test]
    fn content_ranges_parse() {
        assert_eq!(
            parse_content_range(Some("bytes 10-99/100")),
            Some((10, Some(100)))
        );
        assert_eq!(parse_content_range(Some("bytes 10-99/*")), Some((10, None)));
        assert_eq!(parse_content_range(Some("items 1-2/3")), None);
        assert_eq!(parse_content_range(None), None);
    }

    #[test]
    fn urls_resolve_against_the_endpoint() {
        assert_eq!(
            resolve_url("http://h:1/repo/push", "/repo/push/abc").as_deref(),
            Some("http://h:1/repo/push/abc")
        );
        assert_eq!(
            resolve_url("http://h/repo/push", "https://other/x").as_deref(),
            Some("https://other/x")
        );
    }

    /// A TUS server in memory that drops some PATCHes after keeping part of them.
    struct TusServer {
        stored: Mutex<Vec<u8>>,
        /// Per PATCH: bytes to keep before failing (`None` = accept all).
        patch_cuts: Mutex<VecDeque<Option<usize>>>,
        length: Mutex<u64>,
    }

    impl HttpClient for TusServer {
        fn get(&self, _: &str, _: Option<&str>) -> crate::error::Result<Vec<u8>> {
            unreachable!()
        }
        fn post(
            &self,
            _: &str,
            _: &str,
            _: &str,
            _: &[u8],
            _: Option<&str>,
        ) -> crate::error::Result<Vec<u8>> {
            unreachable!()
        }
        fn send(&self, request: &HttpRequest<'_>) -> crate::error::Result<HttpResponse> {
            let header = |name: &str| {
                request
                    .headers
                    .iter()
                    .find(|(n, _)| n.eq_ignore_ascii_case(name))
                    .map(|(_, v)| v.clone())
            };
            let mut stored = self.stored.lock().unwrap_or_else(|e| e.into_inner());
            let reply = |status: u16, headers: Vec<(String, String)>| HttpResponse {
                status,
                headers,
                body: Box::new(std::io::empty()),
            };
            let offset = |n: usize| vec![("upload-offset".to_owned(), n.to_string())];
            Ok(match request.method {
                HttpMethod::Post => {
                    *self.length.lock().unwrap_or_else(|e| e.into_inner()) =
                        header("Upload-Length")
                            .and_then(|v| v.parse().ok())
                            .unwrap_or(0);
                    reply(201, vec![("location".to_owned(), "/push/u1".to_owned())])
                }
                HttpMethod::Head => reply(200, offset(stored.len())),
                HttpMethod::Patch => {
                    let at: usize = header("Upload-Offset")
                        .and_then(|v| v.parse().ok())
                        .unwrap_or(usize::MAX);
                    if at != stored.len() {
                        return Ok(reply(409, vec![]));
                    }
                    let cut = self
                        .patch_cuts
                        .lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .pop_front()
                        .flatten();
                    match cut {
                        Some(keep) => {
                            stored.extend_from_slice(&request.body[..keep]);
                            return Err(crate::error::Error::Message("reset".to_owned()));
                        }
                        None => {
                            stored.extend_from_slice(request.body);
                            reply(204, offset(stored.len()))
                        }
                    }
                }
                HttpMethod::Get | HttpMethod::Put | HttpMethod::Delete => reply(405, vec![]),
            })
        }
    }

    #[test]
    fn tus_uploads_resume_from_the_server_offset() {
        let dir = tempdir();
        let file = dir.path().join("push.bundle");
        let data: Vec<u8> = (0..(PATCH_CHUNK * 2 + 1000))
            .map(|i| (i % 253) as u8)
            .collect();
        assert!(std::fs::write(&file, &data).is_ok());
        let srv = TusServer {
            stored: Mutex::new(Vec::new()),
            patch_cuts: Mutex::new([None, Some(1000)].into_iter().collect()),
            length: Mutex::new(0),
        };
        let upload = match TusUpload::create(&srv, "http://h/push", data.len() as u64) {
            Ok(u) => u,
            Err(e) => panic!("create: {e}"),
        };
        assert_eq!(upload.url, "http://h/push/u1");
        let mut events = Events::default();
        assert!(upload.send_file(&srv, &file, &mut events).is_ok());
        assert_eq!(*srv.stored.lock().unwrap_or_else(|e| e.into_inner()), data);
        assert!(matches!(
            events.0.as_slice(),
            [
                ResumableEvent::Started {
                    resumed_from: 0,
                    ..
                },
                ResumableEvent::Reconnecting { attempt: 1, .. },
                ResumableEvent::Finished { .. },
            ]
        ));
        assert_eq!(upload.offset(&srv).ok().flatten(), Some(data.len() as u64));
    }
}