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
use crate::error::{HttpError, Result};
use bytes::Bytes;

#[cfg(feature = "reqwest-async")]
pub(crate) mod nonblocking {
    use super::*;
    use crate::range_client::AsyncHttpRangeClient;
    use async_trait::async_trait;

    #[cfg(not(target_arch = "wasm32"))]
    #[async_trait]
    impl AsyncHttpRangeClient for reqwest::Client {
        async fn get_range(&self, url: &str, range: &str) -> Result<Bytes> {
            let response = self.get(url).header("Range", range).send().await?;
            if !response.status().is_success() {
                return Err(HttpError::HttpStatus(response.status().as_u16()));
            }
            response
                .bytes()
                .await
                .map_err(|e| HttpError::HttpError(e.to_string()))
        }
        async fn head_response_header(&self, url: &str, header: &str) -> Result<Option<String>> {
            let response = self.head(url).send().await?;
            if let Some(val) = response.headers().get(header) {
                let v = val
                    .to_str()
                    .map_err(|e| HttpError::HttpError(e.to_string()))?;
                Ok(Some(v.to_string()))
            } else {
                Ok(None)
            }
        }
    }

    #[cfg(target_arch = "wasm32")]
    #[async_trait(?Send)]
    impl AsyncHttpRangeClient for reqwest::Client {
        async fn get_range(&self, url: &str, range: &str) -> Result<Bytes> {
            let response = self.get(url).header("Range", range).send().await?;
            if !response.status().is_success() {
                return Err(HttpError::HttpStatus(response.status().as_u16()));
            }
            response
                .bytes()
                .await
                .map_err(|e| HttpError::HttpError(e.to_string()))
        }
        async fn head_response_header(&self, url: &str, header: &str) -> Result<Option<String>> {
            let response = self.head(url).send().await?;
            if let Some(val) = response.headers().get(header) {
                let v = val
                    .to_str()
                    .map_err(|e| HttpError::HttpError(e.to_string()))?;
                Ok(Some(v.to_string()))
            } else {
                Ok(None)
            }
        }
    }

    /// Async HTTP client for HTTP Range requests with a buffer optimized for sequential reading.
    pub type BufferedHttpRangeClient = crate::AsyncBufferedHttpRangeClient<reqwest::Client>;

    impl BufferedHttpRangeClient {
        pub fn new(url: &str) -> Self {
            Self::with(reqwest::Client::new(), url)
        }
    }
}

#[cfg(feature = "reqwest-sync")]
pub(crate) mod sync {
    use super::*;
    use crate::range_client::SyncHttpRangeClient;

    impl SyncHttpRangeClient for reqwest::blocking::Client {
        fn get_range(&self, url: &str, range: &str) -> Result<Bytes> {
            let response = self.get(url).header("Range", range).send()?;
            if !response.status().is_success() {
                return Err(HttpError::HttpStatus(response.status().as_u16()));
            }
            response
                .bytes()
                .map_err(|e| HttpError::HttpError(e.to_string()))
        }
        fn head_response_header(&self, url: &str, header: &str) -> Result<Option<String>> {
            let response = self.head(url).send()?;
            if let Some(val) = response.headers().get(header) {
                let v = val
                    .to_str()
                    .map_err(|e| HttpError::HttpError(e.to_string()))?;
                Ok(Some(v.to_string()))
            } else {
                Ok(None)
            }
        }
    }

    /// Sync HTTP client for HTTP Range requests with a buffer optimized for sequential reading.
    pub type HttpReader = crate::SyncBufferedHttpRangeClient<reqwest::blocking::Client>;

    impl HttpReader {
        pub fn new(url: &str) -> Self {
            Self::with(reqwest::blocking::Client::new(), url)
        }
    }
}

impl From<reqwest::Error> for HttpError {
    fn from(error: reqwest::Error) -> Self {
        if let Some(status) = error.status() {
            HttpError::HttpStatus(status.as_u16())
        } else {
            HttpError::HttpError(error.to_string())
        }
    }
}