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
use std::{
    io::Error as IoError,
    pin::Pin,
    task::{ready, Context, Poll},
};

use futures_util::stream::Stream;
use hyper::body::{Bytes, Frame};
use tokio::{
    fs::File,
    io::{AsyncRead, AsyncSeek},
};

use crate::util::{FileBytesStream, FileBytesStreamMultiRange, FileBytesStreamRange};

/// Hyper Body implementation for the various types of streams used in static serving.
pub enum Body<F = File> {
    /// No response body.
    Empty,
    /// Serve a complete file.
    Full(FileBytesStream<F>),
    /// Serve a range from a file.
    Range(FileBytesStreamRange<F>),
    /// Serve multiple ranges from a file.
    MultiRange(FileBytesStreamMultiRange<F>),
}

impl<F> hyper::body::Body for Body<F>
where
    F: AsyncRead + AsyncSeek + Unpin,
{
    type Data = Bytes;
    type Error = IoError;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>, IoError>>> {
        let opt = ready!(match *self {
            Body::Empty => return Poll::Ready(None),
            Body::Full(ref mut stream) => Pin::new(stream).poll_next(cx),
            Body::Range(ref mut stream) => Pin::new(stream).poll_next(cx),
            Body::MultiRange(ref mut stream) => Pin::new(stream).poll_next(cx),
        });
        Poll::Ready(opt.map(|res| res.map(Frame::data)))
    }
}