Skip to main content

aria2_protocol/http/
client.rs

1use std::sync::Once;
2use std::time::Duration;
3
4use reqwest::{Certificate, Client, ClientBuilder, redirect};
5use tracing::{debug, info};
6
7use crate::http::request::HttpRequest;
8use crate::http::response::HttpResponse;
9
10#[derive(Debug, Clone)]
11pub struct HttpClientOptions {
12    pub connect_timeout: Duration,
13    pub timeout: Duration,
14    pub max_redirects: usize,
15    pub user_agent: String,
16    pub accept_gzip: bool,
17    pub verify_tls: bool,
18    pub ca_cert_path: Option<String>,
19}
20
21impl Default for HttpClientOptions {
22    fn default() -> Self {
23        Self {
24            connect_timeout: Duration::from_secs(30),
25            timeout: Duration::from_secs(300),
26            max_redirects: 5,
27            user_agent: crate::identity::DEFAULT_USER_AGENT.to_string(),
28            accept_gzip: true,
29            verify_tls: true,
30            ca_cert_path: None,
31        }
32    }
33}
34
35pub struct HttpClient {
36    inner: Client,
37    options: HttpClientOptions,
38}
39
40/// Lazily install the `ring` crypto provider for rustls on first call.
41///
42/// Required when reqwest is built with the `rustls-no-provider` feature;
43/// without a provider, `ClientBuilder::build()` will panic.
44///
45/// Note: `install_default()` returns Err if a provider is already installed
46/// (e.g., by test helpers' `ensure_crypto_provider()`). That is fine — we
47/// only need to ensure a provider is present, not that we installed it.
48fn ensure_ring_provider() {
49    static INIT: Once = Once::new();
50    INIT.call_once(|| {
51        let _ = rustls::crypto::ring::default_provider().install_default();
52    });
53}
54
55impl HttpClient {
56    pub fn new(options: HttpClientOptions) -> Result<Self, String> {
57        // Ensure the TLS crypto provider is installed before constructing
58        // the reqwest Client (required for rustls-no-provider builds).
59        ensure_ring_provider();
60        let mut builder = ClientBuilder::new()
61            .connect_timeout(options.connect_timeout)
62            .timeout(options.timeout)
63            .user_agent(&options.user_agent)
64            .redirect(redirect::Policy::limited(options.max_redirects));
65
66        if options.accept_gzip {
67            builder = builder.gzip(true);
68        }
69
70        if !options.verify_tls {
71            builder = builder.danger_accept_invalid_certs(true);
72        }
73
74        if let Some(ref ca_path) = options.ca_cert_path {
75            match std::fs::read(ca_path) {
76                Ok(cert_bytes) => {
77                    let cert = Certificate::from_pem(&cert_bytes)
78                        .map_err(|e| format!("Failed to load CA certificate: {}", e))?;
79                    builder = builder.add_root_certificate(cert);
80                }
81                Err(e) => return Err(format!("Failed to read CA certificate file: {}", e)),
82            }
83        }
84
85        let inner = builder
86            .build()
87            .map_err(|e| format!("Failed to create HTTP client: {}", e))?;
88
89        info!(
90            "HttpClient initialized (timeout={:?}, max_redirects={}, verify_tls={})",
91            options.timeout, options.max_redirects, options.verify_tls
92        );
93
94        Ok(Self { inner, options })
95    }
96
97    pub fn default_client() -> Result<Self, String> {
98        Self::new(HttpClientOptions::default())
99    }
100
101    pub async fn execute(&self, request: HttpRequest) -> Result<HttpResponse, String> {
102        debug!("Sending HTTP request: {} {}", request.method, request.url);
103
104        let mut reqwest_request = match request.method.to_uppercase().as_str() {
105            "GET" => self.inner.get(&request.url),
106            "POST" => self.inner.post(&request.url),
107            "HEAD" => self.inner.head(&request.url),
108            "PUT" => self.inner.put(&request.url),
109            _ => return Err(format!("Unsupported HTTP method: {}", request.method)),
110        };
111
112        if let Some(ref headers) = request.headers {
113            for (key, value) in headers.iter() {
114                reqwest_request = reqwest_request.header(
115                    key.as_str()
116                        .parse::<reqwest::header::HeaderName>()
117                        .map_err(|e| format!("Invalid header name: {}", e))?,
118                    value.as_str(),
119                );
120            }
121        }
122
123        if let Some(body) = request.body {
124            reqwest_request = reqwest_request.body(body);
125        }
126
127        let response = reqwest_request
128            .send()
129            .await
130            .map_err(|e| format!("HTTP request failed: {}", e))?;
131
132        let status = response.status();
133        let status_code = status.as_u16();
134        debug!("Received HTTP response: status_code={}", status_code);
135
136        let headers_map: Vec<(String, String)> = response
137            .headers()
138            .iter()
139            .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
140            .collect();
141
142        let body_bytes = response
143            .bytes()
144            .await
145            .map_err(|e| format!("Failed to read response body: {}", e))?
146            .to_vec();
147
148        Ok(HttpResponse {
149            status_code,
150            status_text: status.canonical_reason().unwrap_or("Unknown").to_string(),
151            headers: headers_map,
152            body: body_bytes,
153        })
154    }
155
156    pub fn get<U: Into<String>>(&self, url: U) -> HttpRequestBuilder<'_> {
157        HttpRequestBuilder::new(self, "GET", url.into())
158    }
159
160    pub fn post<U: Into<String>>(&self, url: U) -> HttpRequestBuilder<'_> {
161        HttpRequestBuilder::new(self, "POST", url.into())
162    }
163
164    pub fn head<U: Into<String>>(&self, url: U) -> HttpRequestBuilder<'_> {
165        HttpRequestBuilder::new(self, "HEAD", url.into())
166    }
167
168    pub fn options_ref(&self) -> &HttpClientOptions {
169        &self.options
170    }
171}
172
173pub struct HttpRequestBuilder<'a> {
174    client: &'a HttpClient,
175    method: String,
176    url: String,
177    headers: Option<reqwest::header::HeaderMap>,
178    body: Option<Vec<u8>>,
179}
180
181impl<'a> HttpRequestBuilder<'a> {
182    fn new(client: &'a HttpClient, method: &str, url: String) -> Self {
183        Self {
184            client,
185            method: method.to_string(),
186            url,
187            headers: None,
188            body: None,
189        }
190    }
191
192    pub fn header(mut self, name: &str, value: &str) -> Self {
193        let mut headers = self.headers.take().unwrap_or_default();
194        headers.insert(
195            name.parse::<reqwest::header::HeaderName>()
196                .expect("Invalid header name"),
197            value
198                .parse::<reqwest::header::HeaderValue>()
199                .expect("Invalid header value"),
200        );
201        self.headers = Some(headers);
202        self
203    }
204
205    pub fn header_raw<K, V>(mut self, key: K, value: V) -> Self
206    where
207        K: TryInto<reqwest::header::HeaderName>,
208        V: TryInto<reqwest::header::HeaderValue>,
209    {
210        let mut headers = self.headers.take().unwrap_or_default();
211        if let (Ok(k), Ok(v)) = (key.try_into(), value.try_into()) {
212            headers.insert(k, v);
213        }
214        self.headers = Some(headers);
215        self
216    }
217
218    pub fn range(self, start: u64, end: Option<u64>) -> Self {
219        let range_value = match end {
220            Some(e) => format!("bytes={}-{}", start, e),
221            None => format!("bytes={}-", start),
222        };
223        self.header("Range", &range_value)
224    }
225
226    pub fn body<B: Into<Vec<u8>>>(mut self, body: B) -> Self {
227        self.body = Some(body.into());
228        self
229    }
230
231    pub fn user_agent(self, ua: &str) -> Self {
232        self.header("User-Agent", ua)
233    }
234
235    pub fn referer(self, referer: &str) -> Self {
236        self.header("Referer", referer)
237    }
238
239    pub async fn send(self) -> Result<HttpResponse, String> {
240        let headers_map = self.headers.map(|h| {
241            h.iter()
242                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
243                .collect::<Vec<_>>()
244        });
245
246        let request = HttpRequest {
247            method: self.method.clone(),
248            url: self.url.clone(),
249            headers: headers_map,
250            body: self.body,
251        };
252
253        self.client.execute(request).await
254    }
255}
256
257#[derive(Debug, Clone, PartialEq)]
258pub enum RedirectPolicy {
259    Follow,
260    Limit(usize),
261    None,
262}
263
264impl Default for RedirectPolicy {
265    fn default() -> Self {
266        Self::Limit(5)
267    }
268}
269
270pub struct RedirectHandler;
271
272impl RedirectHandler {
273    pub fn should_follow_redirect(
274        status_code: u16,
275        _method: &str,
276        current_redirects: usize,
277        max_redirects: usize,
278    ) -> Option<RedirectAction> {
279        if current_redirects >= max_redirects {
280            debug!("Maximum redirect limit reached: {}", max_redirects);
281            return None;
282        }
283
284        match status_code {
285            301 => Some(RedirectAction::FollowKeepMethod),
286            302 | 303 => Some(RedirectAction::FollowChangeToGet),
287            307 | 308 => Some(RedirectAction::FollowKeepMethod),
288            _ => None,
289        }
290    }
291
292    pub fn resolve_redirect_url(current_url: &str, location: &str) -> Result<String, String> {
293        let location = location.trim();
294        if location.is_empty() {
295            return Err("Location header is empty".to_string());
296        }
297
298        if location.starts_with("http://") || location.starts_with("https://") {
299            return Ok(location.to_string());
300        }
301
302        if location.starts_with("/") {
303            if let Some(base_end) = current_url[8..].find('/') {
304                Ok(format!("{}{}", &current_url[..8 + base_end], location))
305            } else {
306                Ok(format!("{}{}", current_url, location))
307            }
308        } else {
309            let last_slash = current_url.rfind('/').unwrap_or(current_url.len());
310            if last_slash < 8 {
311                Ok(format!("{}/{}", current_url, location))
312            } else {
313                Ok(format!("{}{}", &current_url[..last_slash + 1], location))
314            }
315        }
316    }
317
318    pub fn sanitize_redirect_url(url: &str) -> Result<String, String> {
319        if !url.starts_with("http://") && !url.starts_with("https://") {
320            return Err(format!("Unsafe redirect URL protocol: {}", url));
321        }
322        Ok(url.to_string())
323    }
324}
325
326pub enum RedirectAction {
327    FollowKeepMethod,
328    FollowChangeToGet,
329}
330
331#[cfg(test)]
332mod tests {
333    use super::*;
334
335    #[test]
336    fn test_should_follow_301() {
337        let action = RedirectHandler::should_follow_redirect(301, "GET", 0, 5);
338        assert!(action.is_some());
339
340        let no_action = RedirectHandler::should_follow_redirect(301, "GET", 5, 5);
341        assert!(no_action.is_none());
342    }
343
344    #[test]
345    fn test_should_not_follow_200() {
346        let action = RedirectHandler::should_follow_redirect(200, "GET", 0, 5);
347        assert!(action.is_none());
348    }
349
350    #[test]
351    fn test_resolve_absolute_redirect() {
352        let url = RedirectHandler::resolve_redirect_url(
353            "http://example.com/page",
354            "http://other.example.com/new",
355        )
356        .unwrap();
357        assert_eq!(url, "http://other.example.com/new");
358    }
359
360    #[test]
361    fn test_resolve_relative_redirect() {
362        let url = RedirectHandler::resolve_redirect_url("http://example.com/old/path", "/new/path")
363            .unwrap();
364        assert_eq!(url, "http://example.com/new/path");
365    }
366
367    #[test]
368    fn test_resolve_relative_path_redirect() {
369        let url = RedirectHandler::resolve_redirect_url(
370            "http://example.com/old/page.html",
371            "new-page.html",
372        )
373        .unwrap();
374        assert_eq!(url, "http://example.com/old/new-page.html");
375    }
376
377    #[test]
378    fn test_302_changes_to_get() {
379        let action = RedirectHandler::should_follow_redirect(302, "POST", 0, 5);
380        assert!(action.is_some());
381        if let Some(action) = action {
382            assert!(matches!(action, RedirectAction::FollowChangeToGet));
383        }
384    }
385
386    #[test]
387    fn test_307_keeps_method() {
388        let action = RedirectHandler::should_follow_redirect(307, "POST", 0, 5);
389        assert!(action.is_some());
390        if let Some(action) = action {
391            assert!(matches!(action, RedirectAction::FollowKeepMethod));
392        }
393    }
394}