aria2-protocol 0.3.1

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
Documentation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::sync::Once;
use std::time::Duration;

use reqwest::{Certificate, Client, ClientBuilder, redirect};
use tracing::{debug, info};

use crate::http::request::HttpRequest;
use crate::http::response::{HttpResponse, is_redirect_status};

#[derive(Debug, Clone)]
pub struct HttpClientOptions {
    pub connect_timeout: Duration,
    pub timeout: Duration,
    pub max_redirects: usize,
    pub user_agent: String,
    pub accept_gzip: bool,
    pub verify_tls: bool,
    pub ca_cert_path: Option<String>,
}

impl Default for HttpClientOptions {
    fn default() -> Self {
        Self {
            connect_timeout: Duration::from_secs(30),
            timeout: Duration::from_secs(300),
            max_redirects: 5,
            user_agent: crate::identity::DEFAULT_USER_AGENT.to_string(),
            // aria2_original advertises compressed HTTP responses only when
            // --http-accept-gzip is enabled. Keep the protocol client safe
            // by default; callers can opt in explicitly.
            accept_gzip: false,
            verify_tls: true,
            ca_cert_path: None,
        }
    }
}

pub struct HttpClient {
    inner: Client,
    options: HttpClientOptions,
}

/// Lazily install the `ring` crypto provider for rustls on first call.
///
/// Required when reqwest is built with the `rustls-no-provider` feature;
/// without a provider, `ClientBuilder::build()` will panic.
///
/// Note: `install_default()` returns Err if a provider is already installed
/// (e.g., by test helpers' `ensure_crypto_provider()`). That is fine — we
/// only need to ensure a provider is present, not that we installed it.
pub(crate) fn ensure_ring_provider() {
    static INIT: Once = Once::new();
    INIT.call_once(|| {
        let _ = rustls::crypto::ring::default_provider().install_default();
    });
}

impl HttpClient {
    pub fn new(options: HttpClientOptions) -> Result<Self, String> {
        // Ensure the TLS crypto provider is installed before constructing
        // the reqwest Client (required for rustls-no-provider builds).
        ensure_ring_provider();
        let mut builder = ClientBuilder::new()
            .connect_timeout(options.connect_timeout)
            .timeout(options.timeout)
            .user_agent(&options.user_agent)
            .redirect(redirect::Policy::limited(options.max_redirects));

        builder = builder.gzip(options.accept_gzip);

        if !options.verify_tls {
            builder = builder.danger_accept_invalid_certs(true);
        }

        if let Some(ref ca_path) = options.ca_cert_path {
            match std::fs::read(ca_path) {
                Ok(cert_bytes) => {
                    let cert = Certificate::from_pem(&cert_bytes)
                        .map_err(|e| format!("Failed to load CA certificate: {}", e))?;
                    builder = builder.add_root_certificate(cert);
                }
                Err(e) => return Err(format!("Failed to read CA certificate file: {}", e)),
            }
        }

        let inner = builder
            .build()
            .map_err(|e| format!("Failed to create HTTP client: {}", e))?;

        info!(
            "HttpClient initialized (timeout={:?}, max_redirects={}, verify_tls={})",
            options.timeout, options.max_redirects, options.verify_tls
        );

        Ok(Self { inner, options })
    }

    pub fn default_client() -> Result<Self, String> {
        Self::new(HttpClientOptions::default())
    }

    pub async fn execute(&self, request: HttpRequest) -> Result<HttpResponse, String> {
        debug!("Sending HTTP request: {} {}", request.method, request.url);

        let mut reqwest_request = match request.method.to_uppercase().as_str() {
            "GET" => self.inner.get(&request.url),
            "POST" => self.inner.post(&request.url),
            "HEAD" => self.inner.head(&request.url),
            "PUT" => self.inner.put(&request.url),
            _ => return Err(format!("Unsupported HTTP method: {}", request.method)),
        };

        if let Some(ref headers) = request.headers {
            for (key, value) in headers.iter() {
                reqwest_request = reqwest_request.header(
                    key.as_str()
                        .parse::<reqwest::header::HeaderName>()
                        .map_err(|e| format!("Invalid header name: {}", e))?,
                    value.as_str(),
                );
            }
        }

        if let Some(body) = request.body {
            reqwest_request = reqwest_request.body(body);
        }

        let response = reqwest_request
            .send()
            .await
            .map_err(|e| format!("HTTP request failed: {}", e))?;

        let status = response.status();
        let status_code = status.as_u16();
        debug!("Received HTTP response: status_code={}", status_code);

        let headers_map: Vec<(String, String)> = response
            .headers()
            .iter()
            .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
            .collect();

        let body_bytes = response
            .bytes()
            .await
            .map_err(|e| format!("Failed to read response body: {}", e))?
            .to_vec();

        Ok(HttpResponse {
            status_code,
            status_text: status.canonical_reason().unwrap_or("Unknown").to_string(),
            headers: headers_map,
            body: body_bytes,
        })
    }

    pub fn get<U: Into<String>>(&self, url: U) -> HttpRequestBuilder<'_> {
        HttpRequestBuilder::new(self, "GET", url.into())
    }

    pub fn post<U: Into<String>>(&self, url: U) -> HttpRequestBuilder<'_> {
        HttpRequestBuilder::new(self, "POST", url.into())
    }

    pub fn head<U: Into<String>>(&self, url: U) -> HttpRequestBuilder<'_> {
        HttpRequestBuilder::new(self, "HEAD", url.into())
    }

    pub fn options_ref(&self) -> &HttpClientOptions {
        &self.options
    }
}

pub struct HttpRequestBuilder<'a> {
    client: &'a HttpClient,
    method: String,
    url: String,
    headers: Option<reqwest::header::HeaderMap>,
    body: Option<Vec<u8>>,
}

impl<'a> HttpRequestBuilder<'a> {
    fn new(client: &'a HttpClient, method: &str, url: String) -> Self {
        Self {
            client,
            method: method.to_string(),
            url,
            headers: None,
            body: None,
        }
    }

    pub fn header(mut self, name: &str, value: &str) -> Self {
        let mut headers = self.headers.take().unwrap_or_default();
        headers.insert(
            name.parse::<reqwest::header::HeaderName>()
                .expect("Invalid header name"),
            value
                .parse::<reqwest::header::HeaderValue>()
                .expect("Invalid header value"),
        );
        self.headers = Some(headers);
        self
    }

    pub fn header_raw<K, V>(mut self, key: K, value: V) -> Self
    where
        K: TryInto<reqwest::header::HeaderName>,
        V: TryInto<reqwest::header::HeaderValue>,
    {
        let mut headers = self.headers.take().unwrap_or_default();
        if let (Ok(k), Ok(v)) = (key.try_into(), value.try_into()) {
            headers.insert(k, v);
        }
        self.headers = Some(headers);
        self
    }

    pub fn range(self, start: u64, end: Option<u64>) -> Self {
        let range_value = match end {
            Some(e) => format!("bytes={}-{}", start, e),
            None => format!("bytes={}-", start),
        };
        self.header("Range", &range_value)
    }

    pub fn body<B: Into<Vec<u8>>>(mut self, body: B) -> Self {
        self.body = Some(body.into());
        self
    }

    pub fn user_agent(self, ua: &str) -> Self {
        self.header("User-Agent", ua)
    }

    pub fn referer(self, referer: &str) -> Self {
        self.header("Referer", referer)
    }

    pub async fn send(self) -> Result<HttpResponse, String> {
        let headers_map = self.headers.map(|h| {
            h.iter()
                .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
                .collect::<Vec<_>>()
        });

        let request = HttpRequest {
            method: self.method.clone(),
            url: self.url.clone(),
            headers: headers_map,
            body: self.body,
        };

        self.client.execute(request).await
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum RedirectPolicy {
    Follow,
    Limit(usize),
    None,
}

impl Default for RedirectPolicy {
    fn default() -> Self {
        Self::Limit(5)
    }
}

pub struct RedirectHandler;

impl RedirectHandler {
    pub fn should_follow_redirect(
        status_code: u16,
        _method: &str,
        current_redirects: usize,
        max_redirects: usize,
    ) -> Option<RedirectAction> {
        if current_redirects >= max_redirects {
            debug!("Maximum redirect limit reached: {}", max_redirects);
            return None;
        }

        if !is_redirect_status(status_code) {
            return None;
        }

        match status_code {
            300 | 301 => Some(RedirectAction::FollowKeepMethod),
            302 | 303 => Some(RedirectAction::FollowChangeToGet),
            307 | 308 => Some(RedirectAction::FollowKeepMethod),
            _ => None,
        }
    }

    pub fn resolve_redirect_url(current_url: &str, location: &str) -> Result<String, String> {
        let location = location.trim();
        if location.is_empty() {
            return Err("Location header is empty".to_string());
        }

        if location.starts_with("http://") || location.starts_with("https://") {
            return Ok(location.to_string());
        }

        if location.starts_with("/") {
            if let Some(base_end) = current_url[8..].find('/') {
                Ok(format!("{}{}", &current_url[..8 + base_end], location))
            } else {
                Ok(format!("{}{}", current_url, location))
            }
        } else {
            let last_slash = current_url.rfind('/').unwrap_or(current_url.len());
            if last_slash < 8 {
                Ok(format!("{}/{}", current_url, location))
            } else {
                Ok(format!("{}{}", &current_url[..last_slash + 1], location))
            }
        }
    }

    pub fn sanitize_redirect_url(url: &str) -> Result<String, String> {
        if !url.starts_with("http://") && !url.starts_with("https://") {
            return Err(format!("Unsafe redirect URL protocol: {}", url));
        }
        Ok(url.to_string())
    }
}

pub enum RedirectAction {
    FollowKeepMethod,
    FollowChangeToGet,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_options_do_not_advertise_gzip() {
        assert!(!HttpClientOptions::default().accept_gzip);
    }

    #[test]
    fn test_should_follow_301() {
        let action = RedirectHandler::should_follow_redirect(301, "GET", 0, 5);
        assert!(action.is_some());

        let no_action = RedirectHandler::should_follow_redirect(301, "GET", 5, 5);
        assert!(no_action.is_none());
    }

    #[test]
    fn test_should_follow_300() {
        let action = RedirectHandler::should_follow_redirect(300, "GET", 0, 5);
        assert!(matches!(action, Some(RedirectAction::FollowKeepMethod)));
    }

    #[test]
    fn test_should_not_follow_200() {
        let action = RedirectHandler::should_follow_redirect(200, "GET", 0, 5);
        assert!(action.is_none());
    }

    #[test]
    fn test_resolve_absolute_redirect() {
        let url = RedirectHandler::resolve_redirect_url(
            "http://example.com/page",
            "http://other.example.com/new",
        )
        .unwrap();
        assert_eq!(url, "http://other.example.com/new");
    }

    #[test]
    fn test_resolve_relative_redirect() {
        let url = RedirectHandler::resolve_redirect_url("http://example.com/old/path", "/new/path")
            .unwrap();
        assert_eq!(url, "http://example.com/new/path");
    }

    #[test]
    fn test_resolve_relative_path_redirect() {
        let url = RedirectHandler::resolve_redirect_url(
            "http://example.com/old/page.html",
            "new-page.html",
        )
        .unwrap();
        assert_eq!(url, "http://example.com/old/new-page.html");
    }

    #[test]
    fn test_302_changes_to_get() {
        let action = RedirectHandler::should_follow_redirect(302, "POST", 0, 5);
        assert!(action.is_some());
        if let Some(action) = action {
            assert!(matches!(action, RedirectAction::FollowChangeToGet));
        }
    }

    #[test]
    fn test_307_keeps_method() {
        let action = RedirectHandler::should_follow_redirect(307, "POST", 0, 5);
        assert!(action.is_some());
        if let Some(action) = action {
            assert!(matches!(action, RedirectAction::FollowKeepMethod));
        }
    }
}