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
//! This library provides the [`RequestFile`] type that
//! provides an asynchronous file-like interface to a web resource.
//!
//! # Features
//!
//!   * No unsafe code (`#[forbid(unsafe_code)]`)
//!   * Tested; code coverage: 100%
//!
//! # Examples
//!
//! ```
//! # tokio_test::block_on(async {
//! use reqwest_file::RequestFile;
//! use tokio::io::{AsyncReadExt, AsyncSeekExt};
//!
//! let client = reqwest::Client::new();
//! let request = client.get("http://httpbin.org/base64/aGVsbG8gd29ybGQ=");
//! let mut file: RequestFile = RequestFile::new(request);
//!
//! let mut buffer = [0; 5];
//! assert_eq!(file.read(&mut buffer).await.unwrap(), 5);
//! assert_eq!(&buffer, b"hello");
//!
//! let mut buffer = [0; 5];
//! assert_eq!(file.seek(std::io::SeekFrom::Current(1)).await.unwrap(), 6);
//! assert_eq!(file.read(&mut buffer).await.unwrap(), 5);
//! assert_eq!(&buffer, b"world");
//! # })
//! ```

#![feature(type_alias_impl_trait, mixed_integer_ops, io_error_more)]
#![forbid(unsafe_code)]

use std::future::Future;
use std::io::{Error as IoError, ErrorKind, SeekFrom};
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::Bytes;
use futures_util::{FutureExt, Stream, StreamExt};
use pin_project::pin_project;
use reqwest::{RequestBuilder, StatusCode};
use tokio::io::{AsyncRead, AsyncSeek, ReadBuf};
use tokio_util::io::StreamReader;

fn to_io_error(e: impl std::error::Error + Send + Sync + 'static) -> IoError {
    IoError::new(ErrorKind::Other, e)
}

#[derive(Debug)]
struct HttpResponseStatusError(StatusCode);

impl std::fmt::Display for HttpResponseStatusError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "HTTP status error: {}", self.0)
    }
}

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

// Unfortunately, `reqwest::Response::bytes_stream()` returns
// an unnamed type. Since we need to store this type,
// we need to be able to name it, thus requiring the TAIT here.
type RequestStream = impl Stream<Item = Result<Bytes, IoError>>;
type StreamData = (Option<u64>, RequestStream);
type RequestStreamFuture = impl Future<Output = Result<StreamData, IoError>>;

fn parse_content_length(headers: &reqwest::header::HeaderMap) -> Option<u64> {
    headers
        .get(reqwest::header::CONTENT_LENGTH)?
        // convert to str (eg. "123")
        .to_str()
        .ok()?
        // convert to an integer
        .parse::<u64>()
        .ok()
}

fn parse_range_length(headers: &reqwest::header::HeaderMap) -> Option<u64> {
    headers
        .get(reqwest::header::CONTENT_RANGE)?
        // convert to str (eg. "bytes 3-5/7")
        .to_str()
        .ok()?
        // take the part after '/'
        .split_once('/')?
        .1
        // convert to an integer
        .parse::<u64>()
        .ok()
}

fn determine_size(offset: u64, response: &reqwest::Response) -> Option<u64> {
    if response.status() == StatusCode::OK {
        parse_content_length(response.headers())
    } else if response.status() == StatusCode::PARTIAL_CONTENT {
        parse_range_length(response.headers()).or_else(|| {
            parse_content_length(response.headers()).map(|size| offset + size)
        })
    } else {
        unreachable!()
    }
}

/// Send a request with a `Range` header,
/// returning a future for the response stream.
fn send_request(request: &RequestBuilder, offset: u64) -> RequestStreamFuture {
    let request = request
        .try_clone()
        .expect("request contains streaming body");
    request
        .header(reqwest::header::RANGE, format!("bytes={offset}-"))
        .send()
        .map(move |result| {
            result
                .and_then(|response| response.error_for_status())
                .map_err(to_io_error)
                .and_then(|response| {
                    if response.status() == StatusCode::OK {
                        if offset != 0 {
                            return Err(ErrorKind::NotSeekable.into());
                        }
                    } else if response.status() != StatusCode::PARTIAL_CONTENT {
                        let error = HttpResponseStatusError(response.status());
                        return Err(to_io_error(error));
                    }
                    let size = determine_size(offset, &response);
                    let stream = response
                        .bytes_stream()
                        .map(|result| result.map_err(to_io_error));

                    Ok((size, stream))
                })
        })
}

/// Try to read `delta` bytes from a stream,
/// returning how many bytes were read and remain to be read.
///
/// Return `Ok` if the fastforward is complete,
/// or if EOF is reached (at the first empty read).
fn fastforward<R: AsyncRead, const BUFFER: usize>(
    mut reader: Pin<&mut R>,
    delta: u64,
    context: &mut Context<'_>,
) -> (u64, u64, Poll<Result<(), IoError>>) {
    let mut array = [std::mem::MaybeUninit::uninit(); BUFFER];
    let mut remaining = delta;
    let poll = loop {
        assert!(remaining > 0);
        let buffer_size = (remaining as usize).min(BUFFER);
        let mut buffer = ReadBuf::uninit(&mut array[0..buffer_size]);
        match reader.as_mut().poll_read(context, &mut buffer) {
            Poll::Ready(Ok(())) => {
                let read = buffer.filled().len() as u64;
                if read == 0 {
                    // reached EOF
                    break Poll::Ready(Ok(()));
                } else {
                    remaining = remaining.checked_sub(read).unwrap();
                    if remaining == 0 {
                        break Poll::Ready(Ok(()));
                    } else {
                        continue;
                    }
                }
            }
            other => break other,
        }
    };
    let read = delta.checked_sub(remaining).unwrap();
    (read, remaining, poll)
}

/// The maximum fast-forward read length.
const DEFAULT_FF_WINDOW: u64 = 128 * 1024;

/// The size of the fast-forward read buffer.
const DEFAULT_FF_BUFFER: usize = 4096;

/// State of the request file.
enum State<P, R> {
    /// The initial state, before a request is sent.
    Initial,
    /// The request is being sent.
    Pending(Pin<Box<P>>),
    /// The request is finished, the response stream is ready.
    Ready(Pin<Box<R>>),
    /// The response body is fast-forward seeking.
    Seeking(Pin<Box<R>>, u64),
    /// This state should never persist after a function call,
    /// it exists solely to please the borrow-checker.
    Transient,
}

/// Type that provides an asynchronous file-like interface to a web resource.
///
/// This type implements [`AsyncRead`] and [`AsyncSeek`],
/// so it can be used like an asynchronous file.
///
/// Seeking is implemented as sending out a new request
/// with a `Range` header.
/// All http requests made by this type include this header.
///
/// If the webserver does not support range requests,
/// seeking to anything other than the start of the file
/// will return a [`NotSeekable`] error
/// (excluding [fast-forwards](#fast-forward)).
/// Note that the `Accept-Ranges` response header is not
/// used to check range request support before sending one.
///
/// If the webserver does not provide the `Content-Length` header,
/// and no size was given using [`RequestFile::with_size()`],
/// then seeking relative to the file end
/// will return an [`Unsupported`] error.
///
/// If the http request fails during a `read` or `seek`
/// operation, it returns an [`Other`] error that wraps the
/// original http error.
/// If the webserver returns anything other than status code
/// `206 Partial Content`, or `200 Ok` for responses
/// starting at the first byte, that is also considered a failure.
///
/// For transient errors that require a new HTTP request,
/// the [`reset()`](RequestFile::reset) method can be used.
///
/// # Assumptions
///
/// This type assumes that the http resource
/// is of constant size, and thus that the separate requests
/// performed while seeking are all consistent.
///
/// # Reading
///
/// Reads are implemented by wrapping the response body
/// stream in [`StreamReader`].
///
/// # Seeking
///
/// Seeking a position before the first byte will
/// return an [`InvalidInput`] error.
///
/// This type performs no special handling of seeking
/// beyond the end of the file (EOF), so what happens in this case
/// depends on the webserver.
///
/// # Optimizations
///
/// ## Fast-Forward
///
/// As an optimization, seeking forward by a small amount
/// (by default up to 128KiB) will not perform
/// a new request, but rather fast-forward through
/// the response body.
///
/// This type of seek is always allowed,
/// even if the webserver does not support `Range` requests.
///
/// ###### Customization
///
/// The settings for fast-forwards can be changed through
/// two constant parameters.
///
///   * `FF_WINDOW` limits how much can be fast-forwarded;
///     only seeking up to this number of bytes forwards
///     will read through the request (discarding the data)
///     to avoid sending out a new request.
///
///   * `FF_BUFFER` defines the internal buffer size
///     used to read into during a fast-foward.
///
/// [`NotSeekable`]: std::io::ErrorKind::NotSeekable
/// [`Unsupported`]: std::io::ErrorKind::Unsupported
/// [`InvalidInput`]: std::io::ErrorKind::InvalidInput
/// [`Other`]: std::io::ErrorKind::Other
#[pin_project(project = RequestFileProjection)]
pub struct RequestFile<
    const FF_WINDOW: u64 = DEFAULT_FF_WINDOW,
    const FF_BUFFER: usize = DEFAULT_FF_BUFFER,
> {
    /// The request template.
    request: RequestBuilder,
    /// The state of the HTTP request.
    state: State<RequestStreamFuture, StreamReader<RequestStream, Bytes>>,
    /// Track the size of the response body.
    size_: Option<u64>,
    /// Track the current position in the response body.
    position: u64,
}

impl<const FF_WINDOW: u64, const FF_BUFFER: usize>
    RequestFile<FF_WINDOW, FF_BUFFER>
{
    /// Create a new file-like object for a web resource.
    ///
    /// # Panics
    ///
    /// This function panics if the request:
    ///  * already includes a `Range` header
    ///  * contains a streaming body
    ///  * building the request fails.
    pub fn new(request: RequestBuilder) -> Self {
        Self::with_size(request, None)
    }

    /// Create a new file-like object for a web resource.
    ///
    /// This function allows you to set the response body size
    /// if you happen to know it before performing the request.
    /// This value must be known---either through this method
    /// or via the `Content-Length` header on the response---to
    /// seek relative to the file end.
    ///
    /// If the response specifies a different size using the `Content-Length`
    /// header, the value given here is overwritten.
    ///
    /// # Panics
    ///
    /// This function panics if the request:
    ///  * already includes a `Range` header
    ///  * contains a streaming body
    ///  * building the request fails.
    pub fn with_size(
        request: RequestBuilder,
        size: impl Into<Option<u64>>,
    ) -> Self {
        request
            // Ensure it can be cloned.
            .try_clone()
            .expect("request contains streaming body")
            // Ensure is valid.
            .build()
            .expect("invalid request")
            // Ensure it does not already contain a range header.
            .headers()
            .contains_key(reqwest::header::RANGE)
            .then(|| panic!("request already has range header set"));
        Self {
            request,
            state: State::Initial,
            size_: size.into(),
            position: 0,
        }
    }

    /// Check the size of this file, which may be unknown.
    pub fn size(&self) -> Option<u64> {
        self.size_
    }

    /// Force a new HTTP request to begin,
    /// without changing the current position.
    ///
    /// This method can be used if the request is broken
    /// in a way that can be fixed by restarting it,
    /// for example due to a transient network issue.
    pub fn reset(&mut self) {
        self.state = State::Initial;
    }

    /// Perform the HTTP request so the response is ready to be read.
    pub async fn prepare(&mut self) -> Result<(), IoError> {
        use tokio::io::AsyncSeekExt;
        self.seek(SeekFrom::Current(0)).await.map(|_| ())
    }
}

impl<const FF_WINDOW: u64, const FF_BUFFER: usize>
    RequestFileProjection<'_, FF_WINDOW, FF_BUFFER>
{
    /// Compute the absolute seek position.
    fn resolve_seek_position(
        &self,
        position: SeekFrom,
    ) -> Result<u64, IoError> {
        match position {
            SeekFrom::Start(position) => Ok(position),
            SeekFrom::End(delta) => {
                if let Some(size) = &self.size_ {
                    if let Some(position) = size.checked_add_signed(delta) {
                        Ok(position)
                    } else if delta > 0 {
                        // seek overflow; wrap to maximum
                        Ok(u64::MAX)
                    } else {
                        // seek to negative position
                        Err(ErrorKind::InvalidInput.into())
                    }
                } else {
                    // size not known
                    Err(ErrorKind::Unsupported.into())
                }
            }
            SeekFrom::Current(delta) => {
                if let Some(position) = self.position.checked_add_signed(delta)
                {
                    Ok(position)
                } else if delta > 0 {
                    // seek overflow; wrap to maximum
                    Ok(u64::MAX)
                } else {
                    // seek to negative position
                    Err(ErrorKind::InvalidInput.into())
                }
            }
        }
    }

    /// Drive the state from `State::Initial` to `State::Pending`.
    fn drive_initial(&mut self) {
        if let State::Initial = self.state {
            let future = Box::pin(send_request(self.request, *self.position));
            *self.state = State::Pending(future);
        }
    }

    /// Drive the state from `State::Pending` to `State::Ready`.
    fn poll_drive_pending(
        &mut self,
        context: &mut Context<'_>,
    ) -> Poll<Result<(), IoError>> {
        if let State::Pending(future) = self.state {
            future.as_mut().poll(context).map_ok(move |(size, stream)| {
                *self.size_ = self.size_.or(size);
                *self.state = State::Ready(Box::pin(StreamReader::new(stream)));
            })
        } else {
            Poll::Ready(Ok(()))
        }
    }

    /// Drive the state from `State::Seeking` to `State::Ready`.
    fn poll_drive_seeking(
        &mut self,
        context: &mut Context<'_>,
    ) -> Poll<Result<(), IoError>> {
        match std::mem::replace(self.state, State::Transient) {
            State::Seeking(mut reader, delta) => {
                let (read, remaining, poll) = fastforward::<_, { FF_BUFFER }>(
                    reader.as_mut(),
                    delta,
                    context,
                );
                *self.position = self.position.saturating_add(read);
                match poll {
                    Poll::Ready(Ok(())) => {
                        // If EOF is reached, there may be a remaining delta.
                        *self.position = self.position.wrapping_add(remaining);
                        *self.state = State::Ready(reader);
                        Poll::Ready(Ok(()))
                    }
                    other => {
                        *self.state = State::Seeking(reader, remaining);
                        other
                    }
                }
            }
            state => {
                *self.state = state;
                Poll::Ready(Ok(()))
            }
        }
    }

    /// Drive the state to `State::Ready`.
    fn poll_drive<'a>(
        &'a mut self,
        context: &mut Context<'_>,
    ) -> Poll<Result<(), IoError>> {
        use futures_util::ready;

        self.drive_initial();
        assert!(!matches!(self.state, State::Initial));

        ready!(self.poll_drive_pending(context))?;
        assert!(!matches!(self.state, State::Initial));
        assert!(!matches!(self.state, State::Pending(_)));

        ready!(self.poll_drive_seeking(context))?;
        assert!(!matches!(self.state, State::Initial));
        assert!(!matches!(self.state, State::Pending(_)));
        assert!(!matches!(self.state, State::Seeking(_, _)));

        assert!(matches!(self.state, State::Ready(_)));
        Poll::Ready(Ok(()))
    }
}

impl<const FF_WINDOW: u64, const FF_BUFFER: usize> AsyncRead
    for RequestFile<FF_WINDOW, FF_BUFFER>
{
    fn poll_read(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
        buffer: &mut ReadBuf<'_>,
    ) -> Poll<Result<(), IoError>> {
        let mut this = self.project();
        let reader = match this.poll_drive(context) {
            Poll::Ready(Ok(())) => match this.state {
                State::Ready(reader) => reader.as_mut(),
                _ => unreachable!(),
            },
            other => return other.map_ok(|_| ()),
        };
        let initial_size = buffer.filled().len();
        reader.poll_read(context, buffer).map_ok(|_| {
            let delta =
                buffer.filled().len().checked_sub(initial_size).unwrap();
            *this.position = this.position.saturating_add(delta as u64);
        })
    }
}

impl<const FF_WINDOW: u64, const FF_BUFFER: usize> AsyncSeek
    for RequestFile<FF_WINDOW, FF_BUFFER>
{
    fn start_seek(
        self: Pin<&mut Self>,
        position: SeekFrom,
    ) -> Result<(), IoError> {
        let this = self.project();
        let initial_position = *this.position;
        let final_position = this.resolve_seek_position(position)?;
        if initial_position != final_position {
            let delta_forward = final_position.saturating_sub(initial_position);
            if 0 < delta_forward && delta_forward <= FF_WINDOW {
                // seeking forwards by a small leap
                if let State::Ready(reader) =
                    std::mem::replace(this.state, State::Transient)
                {
                    *this.state = State::Seeking(reader, delta_forward);
                } else {
                    *this.position = final_position;
                    *this.state = State::Initial;
                }
            } else {
                // seeking backwards or a large leap forwards
                *this.position = final_position;
                *this.state = State::Initial;
            }
        }
        Ok(())
    }

    fn poll_complete(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<Result<u64, IoError>> {
        let mut this = self.project();
        this.poll_drive(context).map_ok(|_| *this.position)
    }
}

#[cfg(test)]
mod tests {
    use super::RequestFile;
    use std::io::SeekFrom;
    use tokio::io::{AsyncReadExt, AsyncSeekExt};

    #[derive(Debug, serde::Deserialize)]
    pub struct QueryParams {
        data: String,
        content_length: Option<bool>,
        content_range: Option<bool>,
        status: Option<u16>,
    }

    async fn web_page_main(
        range_header: axum::extract::TypedHeader<axum::headers::Range>,
        axum::extract::Query(query): axum::extract::Query<QueryParams>,
    ) -> (
        axum::http::StatusCode,
        axum::http::header::HeaderMap,
        impl axum::response::IntoResponse,
    ) {
        use axum::http::{
            self,
            header::{HeaderMap, HeaderValue},
            StatusCode,
        };

        let mut headers = HeaderMap::new();

        let range = range_header.iter().next().expect("empty range header");
        let data_len = query.data.len();
        let offset = if let std::ops::Bound::Included(offset) = range.0 {
            offset
        } else {
            unreachable!()
        };

        let response =
            query.data.chars().skip(offset as usize).collect::<String>();
        if query.content_range.unwrap_or(false) {
            let content_range = format!("bytes {offset}-{data_len}/{data_len}");
            headers.insert(
                http::header::CONTENT_RANGE,
                HeaderValue::from_str(&content_range).unwrap(),
            );
        }
        if query.content_length.unwrap_or(false) {
            let content_length = response.len().to_string();
            headers.insert(
                http::header::CONTENT_LENGTH,
                HeaderValue::from_str(&content_length).unwrap(),
            );
        }
        let default_status = StatusCode::PARTIAL_CONTENT.as_u16();
        let status = query.status.unwrap_or(default_status);
        let status = StatusCode::from_u16(status).expect("invalid status code");
        // Stream the response so axum won't add a content-length header.
        let response = axum::body::StreamBody::new(futures_util::stream::iter(
            std::iter::once(std::io::Result::Ok(response)),
        ));
        (status, headers, response)
    }

    async fn web_page_no_range_support() -> &'static str {
        "xyz"
    }

    fn start_server() -> String {
        let app = axum::Router::new()
            .route("/", axum::routing::get(web_page_main))
            .route(
                "/no-range-support",
                axum::routing::get(web_page_no_range_support),
            );
        let server = axum::Server::bind(&"0.0.0.0:0".parse().unwrap())
            .serve(app.into_make_service());
        let address = server.local_addr();
        tokio::spawn(server);
        format!("http://{address}")
    }

    async fn read(file: &mut RequestFile) -> Result<String, std::io::Error> {
        let mut response_data = String::new();
        file.read_to_string(&mut response_data)
            .await
            .map(|_| response_data)
    }

    macro_rules! test {
        (
            $name:ident
            $( $token:tt )*
        ) => {
            // Make versions of the test for various const param combos.
            test!(
                @concrete
                $name
                $( $token )*
            );
            test!(
                @concrete
                @ff_window 2
                @ff_buffer 1
                $name
                $( $token )*
            );
        };
        (
            @concrete
            $( @ff_window $ff_window:literal )?
            $( @ff_buffer $ff_buffer:literal )?
            $name:ident
            [
                $( SeekFrom::$seek_from:ident($offset:literal)
                => ( $($tell:tt)* ) );* $(;)?
            ]
            $( Content-Length = $content_length:literal )?
            $( Content-Range = $content_range:literal )?
            $data:literal => $result:tt
        ) => { paste::paste! {
            #[tokio::test]
            async fn [<
                $name
                $( _ff_window_ $ff_window )?
                $( _ff_buffer_ $ff_buffer )?
            >]() {
                let url = start_server();
                let data = $data;
                let client = reqwest::Client::new();

                #[allow(unused_mut)]
                let mut params = String::new();
                $(
                    if $content_length {
                        params.push_str("&content_length=true");
                    }
                )?
                $(
                    if $content_range {
                        params.push_str("&content_range=true");
                    }
                )?

                let request = client.get(format!("{url}/?data={data}{params}"));
                const FF_WINDOW: u64 = super::DEFAULT_FF_WINDOW
                    $( + $ff_window - super::DEFAULT_FF_WINDOW )?;
                const FF_BUFFER: usize = super::DEFAULT_FF_BUFFER
                    $( + $ff_buffer - super::DEFAULT_FF_BUFFER )?;
                let mut file: RequestFile::<FF_WINDOW, FF_BUFFER>
                    = RequestFile::new(request);

                $(
                    let seek_from = SeekFrom::$seek_from($offset);
                    let seek_result = file.seek(seek_from).await;
                    test!(@check_seek seek_result $($tell)*);
                )*

                let mut response_data = String::new();
                let read_result = file.read_to_string(&mut response_data).await;
                test!(@check_read read_result response_data $result);
            }
        }};
        (
            @check_seek $seek:ident $result:literal
        ) => {
            let pos = $seek.expect("seek error");
            assert_eq!(pos, $result,
                "Seek fail: {pos} != {}", $result);
        };
        (
            @check_seek $seek:ident ErrorKind::$kind:ident
        ) => {
            if let Err(error) = $seek {
                use std::io::ErrorKind;
                let kind = error.kind();
                assert_eq!(kind, ErrorKind::$kind,
                    "Seek fail: {kind} != ErrorKind::{}", ErrorKind::$kind);
            } else {
                panic!("expected seek error")
            }
        };
        (
            @check_read $read:ident $data:ident $result:literal
        ) => {
            $read.expect("read error");
            assert_eq!($data, $result,
                "Read fail: {} != {}", $data, $result);
        };
        (
            @check_read $read:ident $data:ident ErrorKind::$kind:ident
        ) => {
            if let Err(error) = $read {
                use std::io::ErrorKind;
                let kind = error.kind();
                assert_eq!(kind, ErrorKind::$kind
                    "Read fail: {kind} != ErrorKind::{}", ErrorKind::$kind);
            } else {
                panic!("expected read error")
            }
        }
    }

    test! {
        test_read
        []
        "abc" => "abc"
    }

    test! {
        test_from_start_first
        [
            SeekFrom::Start(0) => (0)
        ]
        "abc" => "abc"
    }
    test! {
        test_from_start_middle
        [
            SeekFrom::Start(3) => (3)
        ]
        "abcde" => "de"
    }
    test! {
        test_from_start_last
        [
            SeekFrom::Start(4) => (4)
        ]
        "abcd" => ""
    }
    test! {
        test_from_start_beyond
        [
            SeekFrom::Start(9) => (9)
        ]
        "abcd" => ""
    }

    test! {
        test_from_current_before
        [
            SeekFrom::Start(3) => (3);
            SeekFrom::Current(-4) => (ErrorKind::InvalidInput);
        ]
        "abcd" => "d"
    }
    test! {
        test_from_current_first
        [
            SeekFrom::Start(3) => (3);
            SeekFrom::Current(-3) => (0);
        ]
        "abcd" => "abcd"
    }
    test! {
        test_from_current_middle_backward
        [
            SeekFrom::Start(4) => (4);
            SeekFrom::Current(-2) => (2);
        ]
        "abcdef" => "cdef"
    }
    test! {
        test_from_current_middle_forward
        [
            SeekFrom::Start(3) => (3);
            SeekFrom::Current(3) => (6);
        ]
        "abcdefgh" => "gh"
    }
    test! {
        test_from_current_last
        [
            SeekFrom::Start(3) => (3);
            SeekFrom::Current(3) => (6);
        ]
        "abcd" => ""
    }
    test! {
        test_from_current_beyond
        [
            SeekFrom::Start(3) => (3);
            SeekFrom::Current(4) => (7);
        ]
        "abcd" => ""
    }

    test! {
        test_from_end_before
        [
            SeekFrom::End(-6) => (ErrorKind::InvalidInput)
        ]
        Content-Length = true
        "abcd" => "abcd"
    }
    test! {
        test_from_end_first
        [
            SeekFrom::End(-4) => (0)
        ]
        Content-Range = true
        "abcd" => "abcd"
    }
    test! {
        test_from_end_middle
        [
            SeekFrom::End(-2) => (2)
        ]
        Content-Length = true
        "abcd" => "cd"
    }
    test! {
        test_from_end_last
        [
            SeekFrom::End(0) => (4)
        ]
        Content-Range = true
        "abcd" => ""
    }
    test! {
        test_from_end_beyond
        [
            SeekFrom::End(2) => (6)
        ]
        Content-Length = true
        "abcd" => ""
    }

    test! {
        test_from_end_uknown_size
        [
            SeekFrom::End(2) => (ErrorKind::Unsupported)
        ]
        "abc" => "abc"
    }

    #[tokio::test]
    async fn test_seek_at_initial_state() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/?data=abc"));
        let file: RequestFile = RequestFile::new(request);

        // NOTE: We cannot use AsyncReadExt.seek() here
        // since that does a `poll_complete()` before `start_seek()`
        // which leaves the file in the `Ready` state.
        use futures_util::future::poll_fn;
        use tokio::io::AsyncSeek;
        tokio::pin!(file);
        file.as_mut()
            .start_seek(SeekFrom::Start(4))
            .expect("start seek error");
        let pos = poll_fn(|context| file.as_mut().poll_complete(context))
            .await
            .expect("complete seek error");
        assert_eq!(pos, 4);
    }

    /// Test that the file supports reading the stream
    /// when it is already at EOF.
    #[tokio::test]
    async fn test_read_at_end() {
        let url = start_server();
        let client = reqwest::Client::new();
        let data = "abc";
        let request = client.get(format!("{url}/?data={data}"));
        let mut file: RequestFile = RequestFile::new(request);

        let mut response_data = String::new();
        file.read_to_string(&mut response_data)
            .await
            .expect("read error");
        assert_eq!(response_data, data);
        response_data.clear();

        file.read_to_string(&mut response_data)
            .await
            .expect("read error");
        assert_eq!(response_data, "");
    }

    #[tokio::test]
    async fn test_size_empty() {
        let url = start_server();
        let client = reqwest::Client::new();
        let data = "abcd";
        let request = client.get(format!("{url}/?data={data}"));
        let mut file: RequestFile = RequestFile::new(request);

        assert_eq!(file.size(), None);
        file.prepare().await.unwrap();
        assert_eq!(file.size(), None);
    }

    #[tokio::test]
    async fn test_size_from_content_length() {
        let url = start_server();
        let client = reqwest::Client::new();
        let data = "abcd";
        let url = format!("{url}/?data={data}&content_length=true");
        let request = client.get(url);
        let mut file: RequestFile = RequestFile::new(request);

        assert_eq!(file.size(), None);
        file.seek(SeekFrom::Start(2)).await.unwrap();
        assert_eq!(file.size(), Some(data.len() as u64));
    }

    #[tokio::test]
    async fn test_size_from_content_range() {
        let url = start_server();
        let client = reqwest::Client::new();
        let data = "abcd";
        let url = format!("{url}/?data={data}&content_range=true");
        let request = client.get(url);
        let mut file: RequestFile = RequestFile::new(request);

        assert_eq!(file.size(), None);
        file.seek(SeekFrom::Start(2)).await.unwrap();
        assert_eq!(file.size(), Some(data.len() as u64));
    }

    #[tokio::test]
    #[should_panic(expected = "HTTP status client error (404 Not Found)")]
    async fn test_status_404() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/404"));
        let mut file: RequestFile = RequestFile::new(request);

        match file.prepare().await {
            Ok(()) => unreachable!(),
            Err(e) => {
                assert_eq!(e.kind(), std::io::ErrorKind::Other);
                if let Some(e) = e.into_inner() {
                    panic!("{e}")
                } else {
                    unreachable!()
                }
            }
        }
    }

    #[tokio::test]
    #[should_panic(expected = "HTTP status error: 204 No Content")]
    async fn test_status_204_no_content() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/?data=abc&status=204"));
        let mut file: RequestFile = RequestFile::new(request);

        match file.prepare().await {
            Ok(()) => unreachable!(),
            Err(e) => {
                assert_eq!(e.kind(), std::io::ErrorKind::Other);
                if let Some(e) = e.into_inner() {
                    panic!("{e}")
                } else {
                    unreachable!()
                }
            }
        }
    }

    #[tokio::test]
    async fn test_read_no_range_support() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/no-range-support"));
        let mut file: RequestFile = RequestFile::new(request);

        file.seek(SeekFrom::Start(0)).await.expect("seek error");
        let data = read(&mut file).await.expect("read error");
        assert_eq!(data, "xyz");
    }

    #[tokio::test]
    #[should_panic(expected = "seek error: Kind(NotSeekable)")]
    async fn test_seek_no_range_support() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/no-range-support"));
        let mut file: RequestFile = RequestFile::new(request);

        // seek beyond the fastforward window
        file.seek(SeekFrom::Start(1_000_000_000))
            .await
            .expect("seek error");
    }

    #[tokio::test]
    async fn test_seek_current_overflow() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/?data=abc"));
        let mut file: RequestFile = RequestFile::new(request);

        let seek_from = SeekFrom::Start(u64::MAX - 10);
        let pos = file.seek(seek_from).await.expect("seek error");
        assert_eq!(pos, u64::MAX - 10);

        let pos = file.seek(SeekFrom::Current(20)).await.expect("seek error");
        assert_eq!(pos, u64::MAX);
    }

    #[tokio::test]
    async fn test_seek_end_overflow() {
        let url = start_server();
        let client = reqwest::Client::new();
        let request = client.get(format!("{url}/?data=abc"));
        let mut file: RequestFile =
            RequestFile::with_size(request, u64::MAX - 10);

        let pos = file.seek(SeekFrom::End(20)).await.expect("seek error");
        assert_eq!(pos, u64::MAX);
    }

    #[tokio::test]
    async fn test_reset() {
        let client = reqwest::Client::new();
        let request = client.get("http://example.com/");
        let mut file: RequestFile = RequestFile::new(request);
        file.reset();
    }
}