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