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};
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(self) -> impl Future<Output = Result<Self::Response, Self::RequestError>> + Send;
20}
21pub trait HttpResponse: Send + Unpin {
22    type Headers: HttpHeaders;
23    type ChunkError: Send + Debug;
24    fn headers(&self) -> &Self::Headers;
25    fn url(&self) -> &Url;
26    fn chunk(&mut self) -> impl Future<Output = Result<Option<Bytes>, Self::ChunkError>> + Send;
27}
28pub trait HttpHeaders {
29    type GetHeaderError: Send + Debug;
30    fn get(&self, header: &str) -> Result<&str, Self::GetHeaderError>;
31}
32
33pub type GetResponse<Client> =
34    <<Client as HttpClient>::RequestBuilder as HttpRequestBuilder>::Response;
35pub type GetRequestError<Client> =
36    <<Client as HttpClient>::RequestBuilder as HttpRequestBuilder>::RequestError;
37pub type GetChunkError<Client> = <GetResponse<Client> as HttpResponse>::ChunkError;
38pub type GetHeader<Client> = <GetResponse<Client> as HttpResponse>::Headers;
39pub type GetHeaderError<Client> = <GetHeader<Client> as HttpHeaders>::GetHeaderError;
40
41#[derive(thiserror::Error, Debug)]
42pub enum HttpError<Client: HttpClient> {
43    Request(GetRequestError<Client>),
44    Chunk(GetChunkError<Client>),
45    GetHeader(GetHeaderError<Client>),
46    Irrecoverable,
47    MismatchedBody(FileId),
48}