bytehaul 0.2.0

Async HTTP download library with resume, multi-connection, rate limiting, and checksum verification
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
mod multi;
mod range_validate;
mod resume;
pub(crate) mod retry;
mod single;

use std::path::{Path, PathBuf};
use std::time::Duration;

use tokio::sync::{mpsc, oneshot, watch};

use crate::checksum::verify_checksum;
use crate::config::{DownloadSpec, LogLevel};
use crate::error::DownloadError;
use crate::filename::{detect_filename, sanitize_relative_path};
use crate::http::response::ResponseMeta;
use crate::http::worker::HttpWorker;
use crate::http::HttpResponse;
use crate::network::BytehaulClient;
use crate::progress::{DownloadState, ProgressSnapshot};
use crate::rate_limiter::SpeedLimit;
use crate::storage::control::ControlSnapshot;
use crate::storage::segment::LeaseKey;
use crate::storage::writer::{FlushAllStats, WriterCommand};

use self::multi::run_multi_worker;
use self::range_validate::{
    validate_range_response, ExpectedRange, FreshRangeFallbackReason, RangeValidationDecision,
    RangeValidationMode,
};
use self::resume::try_resume_download;
use self::retry::retry_with_backoff;
use self::single::run_single_connection;

const SPEED_ESTIMATE_WINDOW: Duration = Duration::from_secs(5);
const MIN_SPEED_SAMPLE_SPAN: Duration = Duration::from_secs(1);
const MULTI_PROGRESS_INTERVAL: Duration = crate::progress::PROGRESS_REPORT_INTERVAL;

/// Attempt a range probe first; on failure fall back to a plain GET with retry.
async fn probe_or_fallback_get(
    worker: &HttpWorker,
    spec: &DownloadSpec,
    cancel_rx: &mut watch::Receiver<StopSignal>,
) -> Result<(HttpResponse, ResponseMeta, FreshResponseSource), DownloadError> {
    if spec.max_connections > 1 {
        let piece_end = spec.piece_size.saturating_sub(1);
        match retry_with_backoff(
            spec.max_retries,
            spec.retry_base_delay,
            spec.retry_max_delay,
            spec.max_retry_elapsed,
            cancel_rx,
            || worker.send_range(0, piece_end),
        )
        .await
        {
            Ok((resp, meta)) => {
                return Ok((resp, meta, FreshResponseSource::RangeProbe));
            }
            Err(error) if should_abort_range_probe_fallback(&error) => {
                return Err(error);
            }
            Err(_) => {}
        }
    }
    let (resp, meta) = retry_with_backoff(
        spec.max_retries,
        spec.retry_base_delay,
        spec.retry_max_delay,
        spec.max_retry_elapsed,
        cancel_rx,
        || worker.send_get(),
    )
    .await?;
    Ok((resp, meta, FreshResponseSource::FallbackGet))
}

fn should_abort_range_probe_fallback(error: &DownloadError) -> bool {
    matches!(
        error,
        DownloadError::Cancelled
            | DownloadError::Paused
            | DownloadError::RetryBudgetExceeded { .. }
            | DownloadError::HttpStatus {
                status: 429 | 500 | 502 | 503 | 504,
                ..
            }
    )
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StopSignal {
    Running,
    Cancel,
    Pause,
}

impl StopSignal {
    fn is_stop_requested(self) -> bool {
        !matches!(self, Self::Running)
    }
}

fn stop_signal_error(signal: StopSignal) -> Option<DownloadError> {
    match signal {
        StopSignal::Running => None,
        StopSignal::Cancel => Some(DownloadError::Cancelled),
        StopSignal::Pause => Some(DownloadError::Paused),
    }
}

fn stop_signal_state(signal: StopSignal) -> Option<DownloadState> {
    match signal {
        StopSignal::Running => None,
        StopSignal::Cancel => Some(DownloadState::Cancelled),
        StopSignal::Pause => Some(DownloadState::Paused),
    }
}

fn stop_signal_label(signal: StopSignal) -> &'static str {
    match signal {
        StopSignal::Running => "running",
        StopSignal::Cancel => "cancelled",
        StopSignal::Pause => "paused",
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ControlSaveReason {
    Autosave,
    Terminal,
}

impl ControlSaveReason {
    fn label(self) -> &'static str {
        match self {
            Self::Autosave => "autosave",
            Self::Terminal => "terminal",
        }
    }
}

#[derive(Debug, Clone)]
struct ControlSaveTracker {
    last_saved_downloaded_bytes: u64,
    autosave_ticks_since_save: u32,
}

impl ControlSaveTracker {
    fn new(initial_downloaded_bytes: u64) -> Self {
        Self {
            last_saved_downloaded_bytes: initial_downloaded_bytes,
            autosave_ticks_since_save: 0,
        }
    }

    fn should_save(
        &mut self,
        reason: ControlSaveReason,
        current_downloaded_bytes: u64,
        autosave_sync_every: u32,
    ) -> bool {
        if current_downloaded_bytes <= self.last_saved_downloaded_bytes {
            if matches!(reason, ControlSaveReason::Autosave) {
                self.autosave_ticks_since_save = 0;
            }
            return false;
        }

        match reason {
            ControlSaveReason::Autosave => {
                self.autosave_ticks_since_save = self.autosave_ticks_since_save.saturating_add(1);
                self.autosave_ticks_since_save >= autosave_sync_every
            }
            ControlSaveReason::Terminal => true,
        }
    }

    fn mark_saved(&mut self, downloaded_bytes: u64) {
        self.last_saved_downloaded_bytes = downloaded_bytes;
        self.autosave_ticks_since_save = 0;
    }

    fn last_saved_downloaded_bytes(&self) -> u64 {
        self.last_saved_downloaded_bytes
    }

    fn pending_autosaves(&self) -> u32 {
        self.autosave_ticks_since_save
    }
}

// 鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€
//  Entry point
// 鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€

#[derive(Debug, Clone, Copy)]
enum FreshResponseSource {
    RangeProbe,
    FallbackGet,
}

fn resolve_output_dir(spec: &DownloadSpec) -> Result<PathBuf, DownloadError> {
    let cwd = std::env::current_dir()?;
    let dir = spec.output_dir.clone().unwrap_or_else(|| cwd.clone());
    if dir.is_absolute() {
        Ok(dir)
    } else {
        Ok(cwd.join(dir))
    }
}

fn resolve_static_output_path(spec: &DownloadSpec) -> Result<Option<PathBuf>, DownloadError> {
    let Some(output_path) = &spec.output_path else {
        return Ok(None);
    };
    if output_path.is_absolute() {
        if spec.output_dir.is_some() {
            return Err(DownloadError::InvalidConfig(
                "output_path must be relative when output_dir is set".into(),
            ));
        }
        return Ok(Some(output_path.clone()));
    }
    let relative = sanitize_relative_path(output_path).ok_or_else(|| {
        DownloadError::InvalidConfig(
            "output_path must be a relative path without root prefixes or parent traversal".into(),
        )
    })?;
    Ok(Some(resolve_output_dir(spec)?.join(relative)))
}

fn resolve_auto_output_path(
    spec: &DownloadSpec,
    meta: &ResponseMeta,
    request_url: &str,
) -> Result<PathBuf, DownloadError> {
    Ok(resolve_output_dir(spec)?.join(detect_filename(
        meta.content_disposition.as_deref(),
        request_url,
    )))
}

#[allow(clippy::too_many_arguments)]
async fn run_fresh_from_response(
    client: BytehaulClient,
    spec: &DownloadSpec,
    request_url: &str,
    output_path: &Path,
    response: HttpResponse,
    meta: ResponseMeta,
    source: FreshResponseSource,
    progress_tx: &watch::Sender<ProgressSnapshot>,
    cancel_rx: watch::Receiver<StopSignal>,
    speed_limit: SpeedLimit,
    log_level: LogLevel,
    download_id: u64,
) -> Result<PathBuf, DownloadError> {
    let control_path = ControlSnapshot::control_path(output_path);
    let mut cancel_rx = cancel_rx;

    let mut response = response;
    let mut meta = meta;

    if spec.max_connections > 1 && matches!(source, FreshResponseSource::RangeProbe) {
        let validation = validate_range_response(
            response.status().as_u16(),
            response
                .headers()
                .get("retry-after")
                .and_then(|value| value.to_str().ok()),
            &meta,
            RangeValidationMode::FreshProbe,
            ExpectedRange {
                start: 0,
                end_inclusive: spec.piece_size.saturating_sub(1),
                total_size: None,
            },
        )?;

        match validation {
            RangeValidationDecision::Accept => {
                let total_size = meta
                    .content_range_total
                    .expect("fresh probe validation requires a total size");
                if total_size > spec.min_split_size {
                    log_info!(
                        log_level,
                        download_id,
                        strategy = "fresh multi",
                        total_size,
                        max_connections = spec.max_connections,
                        piece_size = spec.piece_size,
                        "download strategy selected"
                    );
                    let piece_map =
                        crate::storage::piece_map::PieceMap::new(total_size, spec.piece_size);
                    run_multi_worker(
                        client,
                        spec,
                        request_url,
                        output_path,
                        &meta,
                        total_size,
                        piece_map,
                        Some((response, meta.clone(), 0)),
                        progress_tx,
                        cancel_rx,
                        &control_path,
                        speed_limit,
                        log_level,
                        download_id,
                    )
                    .await?;
                    return Ok(output_path.to_path_buf());
                }

                let should_refetch_full_body = !range_probe_response_covers_full_file(&meta);
                log_info!(
                    log_level,
                    download_id,
                    strategy = "fresh single",
                    total_size,
                    reason = if should_refetch_full_body {
                        "file below min_split_size; refetching full body"
                    } else {
                        "file below min_split_size"
                    },
                    "download strategy selected"
                );

                if should_refetch_full_body {
                    (response, meta) =
                        retry_plain_get(client.clone(), spec, &mut cancel_rx).await?;
                }

                let total = single_response_total_size(response.status().as_u16(), &meta);
                run_single_connection(
                    response,
                    &meta,
                    request_url,
                    spec,
                    output_path,
                    0,
                    progress_tx,
                    cancel_rx,
                    &control_path,
                    total,
                    speed_limit,
                    log_level,
                    download_id,
                )
                .await?;
                return Ok(output_path.to_path_buf());
            }
            RangeValidationDecision::FallbackToSingle(reason) => {
                let log_reason = match reason {
                    FreshRangeFallbackReason::RangeNotSupported => {
                        "range not supported (200 response)"
                    }
                    FreshRangeFallbackReason::EncodedResponse => {
                        (response, meta) =
                            retry_plain_get(client.clone(), spec, &mut cancel_rx).await?;
                        "range probe returned encoded response; refetching full body"
                    }
                };

                log_info!(
                    log_level,
                    download_id,
                    strategy = "fresh single",
                    reason = log_reason,
                    "download strategy selected"
                );

                let total = single_response_total_size(response.status().as_u16(), &meta);
                run_single_connection(
                    response,
                    &meta,
                    request_url,
                    spec,
                    output_path,
                    0,
                    progress_tx,
                    cancel_rx,
                    &control_path,
                    total,
                    speed_limit,
                    log_level,
                    download_id,
                )
                .await?;
                return Ok(output_path.to_path_buf());
            }
        }
    }

    let reason = match source {
        FreshResponseSource::RangeProbe if response.status().as_u16() != 206 => {
            "range not supported (200 response)"
        }
        FreshResponseSource::RangeProbe => "file below min_split_size",
        FreshResponseSource::FallbackGet => {
            "fallback GET (single connection or range probe failed)"
        }
    };
    log_info!(
        log_level,
        download_id,
        strategy = "fresh single",
        reason,
        "download strategy selected"
    );
    let total = single_response_total_size(response.status().as_u16(), &meta);
    run_single_connection(
        response,
        &meta,
        request_url,
        spec,
        output_path,
        0,
        progress_tx,
        cancel_rx,
        &control_path,
        total,
        speed_limit,
        log_level,
        download_id,
    )
    .await?;
    Ok(output_path.to_path_buf())
}

pub(crate) async fn run_download(
    client: BytehaulClient,
    spec: DownloadSpec,
    log_level: LogLevel,
    download_id: u64,
    progress_tx: watch::Sender<ProgressSnapshot>,
    cancel_rx: watch::Receiver<StopSignal>,
) -> Result<(), DownloadError> {
    let checksum = spec.checksum.clone();
    let result = run_download_inner(
        client,
        spec,
        log_level,
        download_id,
        &progress_tx,
        cancel_rx,
    )
    .await;

    let output_path = match result {
        Ok(output_path) => output_path,
        Err(e) => {
            if !matches!(e, DownloadError::Cancelled | DownloadError::Paused) {
                progress_tx.send_modify(|progress| {
                    progress.state = DownloadState::Failed;
                    progress.eta_secs = None;
                });
            }
            log_error!(log_level, download_id, error = %e, "download failed");
            return Err(e);
        }
    };

    // Post-download checksum verification
    if let Some(ref expected) = checksum {
        log_info!(
            log_level,
            download_id,
            algorithm = "sha256",
            "checksum verification started"
        );
        match verify_checksum(&output_path, expected).await {
            Ok(()) => {
                log_info!(log_level, download_id, "checksum verification passed");
            }
            Err(e) => {
                log_error!(log_level, download_id, error = %e, "checksum verification failed");
                return Err(e);
            }
        }
    }

    log_info!(log_level, download_id, output = %output_path.display(), "download completed successfully");
    Ok(())
}

async fn run_download_inner(
    client: BytehaulClient,
    spec: DownloadSpec,
    log_level: LogLevel,
    download_id: u64,
    progress_tx: &watch::Sender<ProgressSnapshot>,
    cancel_rx: watch::Receiver<StopSignal>,
) -> Result<PathBuf, DownloadError> {
    let worker = HttpWorker::new(client.clone(), &spec);
    let mut cancel_rx = cancel_rx;
    let speed_limit = SpeedLimit::new(spec.max_download_speed);

    client.warm_resolution_for_url(&spec.url).await?;

    if let Some(output_path) = resolve_static_output_path(&spec)? {
        if let Some(resumed_path) = try_resume_download(
            client.clone(),
            &worker,
            &spec,
            &output_path,
            progress_tx,
            &mut cancel_rx,
            speed_limit.clone(),
            log_level,
            download_id,
        )
        .await?
        {
            return Ok(resumed_path);
        }

        return {
            let (resp, meta, source) =
                probe_or_fallback_get(&worker, &spec, &mut cancel_rx).await?;
            let request_url = worker.final_url().await?;
            run_fresh_from_response(
                client,
                &spec,
                &request_url,
                &output_path,
                resp,
                meta,
                source,
                progress_tx,
                cancel_rx,
                speed_limit,
                log_level,
                download_id,
            )
            .await
        };
    }

    let (initial_response, initial_meta, source) =
        probe_or_fallback_get(&worker, &spec, &mut cancel_rx).await?;

    let request_url = worker.final_url().await?;
    let output_path = resolve_auto_output_path(&spec, &initial_meta, &request_url)?;
    if let Some(resumed_path) = try_resume_download(
        client.clone(),
        &worker,
        &spec,
        &output_path,
        progress_tx,
        &mut cancel_rx,
        speed_limit.clone(),
        log_level,
        download_id,
    )
    .await?
    {
        return Ok(resumed_path);
    }

    run_fresh_from_response(
        client,
        &spec,
        &request_url,
        &output_path,
        initial_response,
        initial_meta,
        source,
        progress_tx,
        cancel_rx,
        speed_limit,
        log_level,
        download_id,
    )
    .await
}

// 鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€
//  Helpers
// 鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€鈹€

fn validate_metadata(meta: &ResponseMeta, ctrl: &ControlSnapshot) -> bool {
    if let Some(total) = meta.content_range_total {
        if total != ctrl.total_size {
            return false;
        }
    }
    if let Some(expected) = &ctrl.etag {
        if meta.etag.as_ref() != Some(expected) {
            return false;
        }
    }
    if let Some(expected) = &ctrl.last_modified {
        if meta.last_modified.as_ref() != Some(expected) {
            return false;
        }
    }
    true
}

fn range_probe_response_covers_full_file(meta: &ResponseMeta) -> bool {
    matches!(
        (
            meta.content_range_start,
            meta.content_range_end,
            meta.content_range_total,
        ),
        (Some(0), Some(end), Some(total)) if end.checked_add(1) == Some(total)
    )
}

fn single_response_total_size(status: u16, meta: &ResponseMeta) -> Option<u64> {
    if status == 206 {
        meta.content_range_total
    } else {
        meta.content_length
    }
}

async fn retry_plain_get(
    client: BytehaulClient,
    spec: &DownloadSpec,
    cancel_rx: &mut watch::Receiver<StopSignal>,
) -> Result<(HttpResponse, ResponseMeta), DownloadError> {
    let worker = HttpWorker::new(client, spec);
    retry_with_backoff(
        spec.max_retries,
        spec.retry_base_delay,
        spec.retry_max_delay,
        spec.max_retry_elapsed,
        cancel_rx,
        || worker.send_get(),
    )
    .await
}

async fn begin_lease_and_wait(
    write_tx: &mpsc::Sender<WriterCommand>,
    lease_key: LeaseKey,
) -> Result<(), DownloadError> {
    write_tx
        .send(WriterCommand::BeginLease { lease_key })
        .await
        .map_err(|_| DownloadError::ChannelClosed)
}

async fn flush_lease_and_wait(
    write_tx: &mpsc::Sender<WriterCommand>,
    lease_key: LeaseKey,
) -> Result<(), DownloadError> {
    let (ack_tx, ack_rx) = oneshot::channel();
    write_tx
        .send(WriterCommand::FlushLease {
            lease_key,
            ack: ack_tx,
        })
        .await
        .map_err(|_| DownloadError::ChannelClosed)?;
    ack_rx.await.map_err(|_| DownloadError::ChannelClosed)
}

async fn discard_lease_and_wait(
    write_tx: &mpsc::Sender<WriterCommand>,
    lease_key: LeaseKey,
) -> Result<usize, DownloadError> {
    let (ack_tx, ack_rx) = oneshot::channel();
    write_tx
        .send(WriterCommand::DiscardLease {
            lease_key,
            ack: ack_tx,
        })
        .await
        .map_err(|_| DownloadError::ChannelClosed)?;
    ack_rx.await.map_err(|_| DownloadError::ChannelClosed)
}

async fn flush_all_and_wait(
    write_tx: &mpsc::Sender<WriterCommand>,
    sync_data: bool,
) -> Result<FlushAllStats, DownloadError> {
    let (ack_tx, ack_rx) = oneshot::channel();
    write_tx
        .send(WriterCommand::FlushAll {
            sync_data,
            ack: ack_tx,
        })
        .await
        .map_err(|_| DownloadError::ChannelClosed)?;
    ack_rx.await.map_err(|_| DownloadError::ChannelClosed)
}

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

    fn worker_for(
        url: String,
        max_connections: u32,
        max_retries: u32,
    ) -> (HttpWorker, DownloadSpec) {
        let spec = DownloadSpec::new(url)
            .output_path("unused.bin")
            .max_connections(max_connections)
            .max_retries(max_retries);
        let client = crate::network::ClientNetworkConfig::default()
            .build_client()
            .unwrap();
        (HttpWorker::new(client, &spec), spec)
    }

    fn running_cancel_rx() -> watch::Receiver<StopSignal> {
        let (_cancel_tx, cancel_rx) = watch::channel(StopSignal::Running);
        cancel_rx
    }

    #[test]
    fn test_range_response_allowed_no_encoding() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: None,
            accept_ranges: false,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: None,
        };
        assert!(super::range_validate::range_response_allowed(&meta));
    }

    #[test]
    fn test_range_response_allowed_identity() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: None,
            accept_ranges: false,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: Some("identity".into()),
        };
        assert!(super::range_validate::range_response_allowed(&meta));
    }

    #[test]
    fn test_range_response_disallowed_gzip() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: None,
            accept_ranges: false,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: Some("gzip".into()),
        };
        assert!(!super::range_validate::range_response_allowed(&meta));
    }

    #[test]
    fn test_validate_metadata_matching() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: Some(1000),
            accept_ranges: true,
            etag: Some("\"abc\"".into()),
            last_modified: Some("Thu, 01 Jan 2026 00:00:00 GMT".into()),
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: Some("\"abc\"".into()),
            last_modified: Some("Thu, 01 Jan 2026 00:00:00 GMT".into()),
        };
        assert!(validate_metadata(&meta, &ctrl));
    }

    #[test]
    fn test_validate_metadata_size_mismatch() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: Some(2000),
            accept_ranges: true,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: None,
            last_modified: None,
        };
        assert!(!validate_metadata(&meta, &ctrl));
    }

    #[test]
    fn test_control_save_tracker_defers_autosaves_until_threshold() {
        let mut tracker = ControlSaveTracker::new(0);

        assert!(!tracker.should_save(ControlSaveReason::Autosave, 128, 2));
        assert_eq!(tracker.pending_autosaves(), 1);
        assert!(tracker.should_save(ControlSaveReason::Autosave, 128, 2));

        tracker.mark_saved(128);
        assert!(!tracker.should_save(ControlSaveReason::Autosave, 128, 2));
    }

    #[test]
    fn test_control_save_tracker_forces_terminal_save_on_new_progress() {
        let mut tracker = ControlSaveTracker::new(64);

        assert!(tracker.should_save(ControlSaveReason::Terminal, 96, 3));
        tracker.mark_saved(96);
        assert!(!tracker.should_save(ControlSaveReason::Terminal, 96, 3));
    }

    #[test]
    fn test_control_save_reason_labels() {
        assert_eq!(ControlSaveReason::Autosave.label(), "autosave");
        assert_eq!(ControlSaveReason::Terminal.label(), "terminal");
    }

    #[test]
    fn test_should_abort_range_probe_fallback_for_retryable_http_status() {
        assert!(should_abort_range_probe_fallback(
            &DownloadError::HttpStatus {
                status: 503,
                message: "retry-after:0".into(),
            }
        ));
        assert!(should_abort_range_probe_fallback(
            &DownloadError::HttpStatus {
                status: 429,
                message: "retry-after:1".into(),
            }
        ));
    }

    #[test]
    fn test_should_abort_range_probe_fallback_for_stop_or_budget() {
        assert!(should_abort_range_probe_fallback(&DownloadError::Cancelled));
        assert!(should_abort_range_probe_fallback(&DownloadError::Paused));
        assert!(should_abort_range_probe_fallback(
            &DownloadError::RetryBudgetExceeded {
                elapsed: Duration::from_secs(1),
                limit: Duration::from_millis(500),
            },
        ));
    }

    #[test]
    fn test_should_not_abort_range_probe_fallback_for_non_retryable_http_status() {
        assert!(!should_abort_range_probe_fallback(
            &DownloadError::HttpStatus {
                status: 416,
                message: "Range Not Satisfiable".into(),
            }
        ));
        assert!(!should_abort_range_probe_fallback(
            &DownloadError::InvalidConfig("bad url".into(),)
        ));
    }

    #[tokio::test]
    async fn test_probe_or_fallback_get_uses_range_probe_when_available() {
        let route = warp::path("probe-ok")
            .and(warp::header::optional::<String>("range"))
            .map(|range_header: Option<String>| match range_header {
                Some(_) => warp::http::Response::builder()
                    .status(206)
                    .header("content-length", "4")
                    .header("content-range", "bytes 0-3/8")
                    .body("test")
                    .unwrap(),
                None => warp::http::Response::builder()
                    .status(200)
                    .header("content-length", "8")
                    .body("testdata")
                    .unwrap(),
            });
        let (addr, server) = warp::serve(route).bind_ephemeral(([127, 0, 0, 1], 0));
        tokio::spawn(server);

        let (worker, spec) = worker_for(format!("http://{addr}/probe-ok"), 4, 0);
        let mut cancel_rx = running_cancel_rx();
        let (_, meta, source) = probe_or_fallback_get(&worker, &spec, &mut cancel_rx)
            .await
            .unwrap();

        assert!(matches!(source, FreshResponseSource::RangeProbe));
        assert_eq!(meta.content_range_start, Some(0));
        assert_eq!(meta.content_range_end, Some(3));
        assert_eq!(meta.content_range_total, Some(8));
    }

    #[tokio::test]
    async fn test_probe_or_fallback_get_returns_retryable_probe_error() {
        let route = warp::path("probe-503")
            .and(warp::header::optional::<String>("range"))
            .map(|range_header: Option<String>| match range_header {
                Some(_) => warp::http::Response::builder()
                    .status(503)
                    .header("retry-after", "0")
                    .body(String::new())
                    .unwrap(),
                None => warp::http::Response::builder()
                    .status(200)
                    .header("content-length", "8")
                    .body(String::from("testdata"))
                    .unwrap(),
            });
        let (addr, server) = warp::serve(route).bind_ephemeral(([127, 0, 0, 1], 0));
        tokio::spawn(server);

        let (worker, spec) = worker_for(format!("http://{addr}/probe-503"), 4, 0);
        let mut cancel_rx = running_cancel_rx();
        let err = probe_or_fallback_get(&worker, &spec, &mut cancel_rx)
            .await
            .unwrap_err();

        assert!(matches!(
            err,
            DownloadError::HttpStatus { status: 503, ref message } if message == "retry-after:0"
        ));
    }

    #[tokio::test]
    async fn test_probe_or_fallback_get_falls_back_after_non_abort_probe_error() {
        let route = warp::path("probe-fallback")
            .and(warp::header::optional::<String>("range"))
            .map(|range_header: Option<String>| match range_header {
                Some(_) => warp::http::Response::builder()
                    .status(416)
                    .body(String::new())
                    .unwrap(),
                None => warp::http::Response::builder()
                    .status(200)
                    .header("content-length", "8")
                    .body(String::from("testdata"))
                    .unwrap(),
            });
        let (addr, server) = warp::serve(route).bind_ephemeral(([127, 0, 0, 1], 0));
        tokio::spawn(server);

        let (worker, spec) = worker_for(format!("http://{addr}/probe-fallback"), 4, 0);
        let mut cancel_rx = running_cancel_rx();
        let (_, meta, source) = probe_or_fallback_get(&worker, &spec, &mut cancel_rx)
            .await
            .unwrap();

        assert!(matches!(source, FreshResponseSource::FallbackGet));
        assert_eq!(meta.content_length, Some(8));
        assert_eq!(meta.content_range_start, None);
    }

    #[test]
    fn test_validate_metadata_etag_mismatch() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: Some(1000),
            accept_ranges: true,
            etag: Some("\"new\"".into()),
            last_modified: None,
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: Some("\"old\"".into()),
            last_modified: None,
        };
        assert!(!validate_metadata(&meta, &ctrl));
    }

    #[test]
    fn test_validate_metadata_missing_expected_etag_is_mismatch() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: Some(1000),
            accept_ranges: true,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: Some("\"old\"".into()),
            last_modified: None,
        };
        assert!(!validate_metadata(&meta, &ctrl));
    }

    #[test]
    fn test_validate_metadata_last_modified_mismatch() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: Some(1000),
            accept_ranges: true,
            etag: None,
            last_modified: Some("Fri, 02 Jan 2026".into()),
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: None,
            last_modified: Some("Thu, 01 Jan 2026".into()),
        };
        assert!(!validate_metadata(&meta, &ctrl));
    }

    #[test]
    fn test_validate_metadata_missing_expected_last_modified_is_mismatch() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: Some(1000),
            accept_ranges: true,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: None,
            last_modified: Some("Thu, 01 Jan 2026".into()),
        };
        assert!(!validate_metadata(&meta, &ctrl));
    }

    #[test]
    fn test_validate_metadata_no_total_in_response() {
        let meta = ResponseMeta {
            content_length: None,
            content_range_start: None,
            content_range_end: None,
            content_range_total: None,
            accept_ranges: true,
            etag: None,
            last_modified: None,
            content_disposition: None,
            content_encoding: None,
        };
        let ctrl = ControlSnapshot {
            url: "https://example.com".into(),
            total_size: 1000,
            piece_size: 1000,
            piece_count: 1,
            completed_bitset: vec![0],
            downloaded_bytes: 0,
            etag: None,
            last_modified: None,
        };
        assert!(validate_metadata(&meta, &ctrl));
    }

    #[tokio::test]
    async fn test_flush_piece_and_wait_closed_channel() {
        let (tx, rx) = mpsc::channel::<WriterCommand>(1);
        drop(rx);
        let result = flush_lease_and_wait(
            &tx,
            LeaseKey {
                piece_id: 0,
                lease_id: 1,
            },
        )
        .await;
        assert!(matches!(result, Err(DownloadError::ChannelClosed)));
    }

    #[tokio::test]
    async fn test_discard_piece_and_wait_closed_channel() {
        let (tx, rx) = mpsc::channel::<WriterCommand>(1);
        drop(rx);
        let result = discard_lease_and_wait(
            &tx,
            LeaseKey {
                piece_id: 0,
                lease_id: 1,
            },
        )
        .await;
        assert!(matches!(result, Err(DownloadError::ChannelClosed)));
    }

    #[tokio::test]
    async fn test_flush_all_and_wait_closed_channel() {
        let (tx, rx) = mpsc::channel::<WriterCommand>(1);
        drop(rx);
        let result = flush_all_and_wait(&tx, false).await;
        assert!(matches!(result, Err(DownloadError::ChannelClosed)));
    }

    #[test]
    fn test_stop_signal_is_stop_requested() {
        assert!(!StopSignal::Running.is_stop_requested());
        assert!(StopSignal::Cancel.is_stop_requested());
        assert!(StopSignal::Pause.is_stop_requested());
    }

    #[test]
    fn test_stop_signal_error() {
        assert!(stop_signal_error(StopSignal::Running).is_none());
        assert!(matches!(
            stop_signal_error(StopSignal::Cancel),
            Some(DownloadError::Cancelled)
        ));
        assert!(matches!(
            stop_signal_error(StopSignal::Pause),
            Some(DownloadError::Paused)
        ));
    }

    #[test]
    fn test_stop_signal_state() {
        assert!(stop_signal_state(StopSignal::Running).is_none());
        assert_eq!(
            stop_signal_state(StopSignal::Cancel),
            Some(DownloadState::Cancelled)
        );
        assert_eq!(
            stop_signal_state(StopSignal::Pause),
            Some(DownloadState::Paused)
        );
    }

    #[test]
    fn test_stop_signal_label() {
        assert_eq!(stop_signal_label(StopSignal::Running), "running");
        assert_eq!(stop_signal_label(StopSignal::Cancel), "cancelled");
        assert_eq!(stop_signal_label(StopSignal::Pause), "paused");
    }

    #[test]
    fn test_resolve_output_dir_uses_cwd_when_none() {
        let spec =
            DownloadSpec::new("http://example.com/file").output_path(PathBuf::from("file.bin"));
        let dir = resolve_output_dir(&spec).unwrap();
        assert!(dir.is_absolute());
    }

    #[test]
    fn test_resolve_output_dir_absolute() {
        let abs_dir = std::env::temp_dir().join("bytehaul_test_resolve");
        let mut spec = DownloadSpec::new("http://example.com/file");
        spec.output_dir = Some(abs_dir.clone());
        let dir = resolve_output_dir(&spec).unwrap();
        assert_eq!(dir, abs_dir);
    }

    #[test]
    fn test_resolve_output_dir_relative() {
        let mut spec = DownloadSpec::new("http://example.com/file");
        spec.output_dir = Some(PathBuf::from("nested-output"));
        let dir = resolve_output_dir(&spec).unwrap();
        assert!(dir.is_absolute());
        assert!(dir.ends_with("nested-output"));
    }

    #[test]
    fn test_resolve_static_output_path_none() {
        let spec = DownloadSpec::new("http://example.com/file");
        let result = resolve_static_output_path(&spec).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_resolve_static_output_path_absolute() {
        let abs_path = std::env::temp_dir().join("bytehaul_test_abs.bin");
        let spec = DownloadSpec::new("http://example.com/file").output_path(abs_path.clone());
        let result = resolve_static_output_path(&spec).unwrap();
        assert_eq!(result, Some(abs_path));
    }

    #[test]
    fn test_resolve_static_output_path_absolute_with_output_dir_is_error() {
        let abs_path = std::env::temp_dir().join("bytehaul_test_abs.bin");
        let mut spec = DownloadSpec::new("http://example.com/file").output_path(abs_path);
        spec.output_dir = Some(std::env::temp_dir());
        let result = resolve_static_output_path(&spec);
        assert!(matches!(result, Err(DownloadError::InvalidConfig(_))));
    }

    #[test]
    fn test_resolve_static_output_path_relative() {
        let spec =
            DownloadSpec::new("http://example.com/file").output_path(PathBuf::from("data.bin"));
        let result = resolve_static_output_path(&spec).unwrap().unwrap();
        assert!(result.is_absolute());
        assert!(result.ends_with("data.bin"));
    }

    #[test]
    fn test_resolve_static_output_path_parent_traversal_is_error() {
        let spec = DownloadSpec::new("http://example.com/file")
            .output_path(PathBuf::from("../escape.bin"));
        let result = resolve_static_output_path(&spec);
        assert!(matches!(result, Err(DownloadError::InvalidConfig(_))));
    }
}