fast_down/http/
mod.rs

1mod prefetch;
2mod puller;
3pub use prefetch::*;
4pub use puller::*;
5
6use crate::url_info::FileId;
7use bytes::Bytes;
8use fast_pull::ProgressEntry;
9use std::{fmt::Debug, future::Future, time::Duration};
10use url::Url;
11
12pub trait HttpClient: Clone + Send + Sync + Unpin {
13    type RequestBuilder: HttpRequestBuilder;
14    fn get(&self, url: Url, range: Option<ProgressEntry>) -> Self::RequestBuilder;
15}
16pub trait HttpRequestBuilder {
17    type Response: HttpResponse;
18    type RequestError: Send + Debug;
19    fn send(
20        self,
21    ) -> impl Future<Output = Result<Self::Response, (Self::RequestError, Option<Duration>)>> + Send;
22}
23pub trait HttpResponse: Send + Unpin {
24    type Headers: HttpHeaders;
25    type ChunkError: Send + Debug;
26    fn headers(&self) -> &Self::Headers;
27    fn url(&self) -> &Url;
28    fn chunk(&mut self) -> impl Future<Output = Result<Option<Bytes>, Self::ChunkError>> + Send;
29}
30pub trait HttpHeaders {
31    type GetHeaderError: Send + Debug;
32    fn get(&self, header: &str) -> Result<&str, Self::GetHeaderError>;
33}
34
35pub type GetRequestBuilder<Client> = <Client as HttpClient>::RequestBuilder;
36pub type GetResponse<Client> = <GetRequestBuilder<Client> as HttpRequestBuilder>::Response;
37pub type GetRequestError<Client> = <GetRequestBuilder<Client> as HttpRequestBuilder>::RequestError;
38pub type GetChunkError<Client> = <GetResponse<Client> as HttpResponse>::ChunkError;
39pub type GetHeader<Client> = <GetResponse<Client> as HttpResponse>::Headers;
40pub type GetHeaderError<Client> = <GetHeader<Client> as HttpHeaders>::GetHeaderError;
41
42#[derive(thiserror::Error, Debug)]
43pub enum HttpError<Client: HttpClient> {
44    Request(GetRequestError<Client>),
45    Chunk(GetChunkError<Client>),
46    GetHeader(GetHeaderError<Client>),
47    Irrecoverable,
48    MismatchedBody(FileId),
49}