aria2-core 0.3.13

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! HTTP response parsing module
//!
//! Provides HTTP/1.1 response parsing with multi-value header support,
//! redirect detection, and streaming content decoding.

use std::collections::HashMap;
use url::Url;

use crate::error::{Aria2Error, Result};
use crate::http::request::HttpMethod;

/// Return whether an HTTP status is one of aria2's redirect statuses.
///
/// `304 Not Modified` is intentionally excluded even though it is a 3xx
/// response. It is handled as a conditional-cache result and has no
/// `Location` header requirement.
pub(crate) fn is_redirect_status(status_code: u16) -> bool {
    aria2_protocol::http::response::is_redirect_status(status_code)
}

/// HTTP response struct
///
/// Represents a complete HTTP response, including status code, reason phrase, version, headers, and optional body.
/// Supports multi-value headers (e.g., Set-Cookie).
#[derive(Debug, Clone)]
pub struct HttpResponse {
    /// Status code (e.g., 200, 404, 301)
    pub status_code: u16,
    /// Reason phrase (e.g., OK, Not Found, Moved Permanently)
    pub reason_phrase: String,
    /// HTTP version (e.g., "HTTP/1.1")
    pub version: String,
    /// Response headers (supports multi-value)
    pub headers: HashMap<String, Vec<String>>,
    /// Optional response body
    pub body: Option<Vec<u8>>,
}

impl HttpResponse {
    /// Parse HTTP response from raw bytes
    ///
    /// Parses response data conforming to the HTTP/1.1 specification, including status line, headers, and body.
    /// Supports multi-value headers (via comma separation or multiple headers with the same name).
    ///
    /// # Arguments
    ///
    /// * `data` - Raw HTTP response bytes
    ///
    /// # Returns
    ///
    /// Parsed HttpResponse, or an error message
    ///
    /// # Errors
    ///
    /// Returns an error if the response format is invalid or cannot be parsed
    pub fn from_bytes(data: &[u8]) -> Result<Self> {
        let header_end = data.windows(4).position(|window| window == b"\r\n\r\n");
        let (header_bytes, body_bytes) = match header_end {
            Some(position) => (&data[..position + 4], &data[position + 4..]),
            None => (data, &[][..]),
        };
        let mut complete_header = header_bytes.to_vec();
        if !complete_header.ends_with(b"\r\n\r\n") {
            complete_header.extend_from_slice(b"\r\n\r\n");
        }
        let parsed = aria2_protocol::http::header_processor::parse_response_head(&complete_header)
            .map_err(|error| Aria2Error::Parse(error.to_string()))?;

        let mut headers: HashMap<String, Vec<String>> = HashMap::new();
        for (key, value) in parsed.iter_headers() {
            headers
                .entry(key.to_owned())
                .or_default()
                .push(value.to_owned());
        }
        let body = (!body_bytes.is_empty()).then(|| body_bytes.to_vec());

        Ok(HttpResponse {
            status_code: parsed.status_code,
            reason_phrase: parsed.reason_phrase,
            version: parsed.http_version,
            headers,
            body,
        })
    }

    /// Get the first value of a specified header
    ///
    /// # Arguments
    ///
    /// * `name` - Header name (case-insensitive)
    ///
    /// # Returns
    ///
    /// Reference to the first header value, or None if not found
    pub fn header(&self, name: &str) -> Option<&String> {
        let name_lower = name.to_lowercase();
        for (key, values) in &self.headers {
            if key.to_lowercase() == name_lower {
                return values.first();
            }
        }
        None
    }

    /// Get all values of a specified header
    ///
    /// Particularly useful for headers like Set-Cookie that may appear multiple times.
    ///
    /// # Arguments
    ///
    /// * `name` - Header name (case-insensitive)
    ///
    /// # Returns
    ///
    /// Vector containing all matching values
    pub fn header_all(&self, name: &str) -> Vec<String> {
        let name_lower = name.to_lowercase();
        for (key, values) in &self.headers {
            if key.to_lowercase() == name_lower {
                return values.clone();
            }
        }
        Vec::new()
    }

    /// Get the value of the Content-Length header
    ///
    /// # Returns
    ///
    /// Content length (u64), or None if not present or parsing fails
    pub fn content_length(&self) -> Option<u64> {
        self.header("Content-Length")
            .and_then(|v| v.parse::<u64>().ok())
    }

    /// Check if this is a redirect response (3xx) with a Location header.
    ///
    /// Matches C++ `HttpResponse::isRedirect()` which returns `true` ONLY when
    /// the status code is one of 300/301/302/303/307/308 AND the Location
    /// header is present. A 3xx response without a Location header is NOT
    /// considered a valid redirect.
    ///
    /// # Returns
    ///
    /// true if the status code is a recognized redirect code AND Location exists
    pub fn is_redirect(&self) -> bool {
        is_redirect_status(self.status_code) && self.header("Location").is_some()
    }

    /// Determine the HTTP method to use when following this redirect.
    ///
    /// Matches C++ `HttpRequest::getRequestMethod()` + redirect method change
    /// logic per RFC 7231 §6.4:
    ///
    /// - **303 See Other**: Always change to GET (C++ resets method to GET).
    ///   This is the most common redirect type for form submissions.
    /// - **301/302**: C++ aria2 changes POST→GET for 301/302 redirects,
    ///   matching the historical browser behavior that the C++ code follows.
    ///   Non-POST methods are preserved.
    /// - **307/308**: Preserve the original method (RFC 7538 for 308,
    ///   RFC 7231 §6.4.7 for 307). The request body is also preserved.
    ///
    /// # Arguments
    ///
    /// * `original_method` - The HTTP method of the original (pre-redirect) request
    ///
    /// # Returns
    ///
    /// The HTTP method to use for the redirected request
    pub fn redirect_method(&self, original_method: &HttpMethod) -> HttpMethod {
        match self.status_code {
            // 303 See Other: always switch to GET (RFC 7231 §6.4.4)
            303 => HttpMethod::Get,
            // 301/302: change POST→GET to match historical browser behavior
            // (C++ aria2 does this in HttpRequest state machine)
            301 | 302 => {
                if *original_method == HttpMethod::Post {
                    HttpMethod::Get
                } else {
                    original_method.clone()
                }
            }
            // 307 Temporary Redirect / 308 Permanent Redirect:
            // preserve the original method per RFC 7538 and RFC 7231 §6.4.7
            307 | 308 => original_method.clone(),
            _ => original_method.clone(),
        }
    }

    /// Whether this redirect response requires preserving the request body.
    ///
    /// Per RFC 7231 §6.4, a redirect that preserves the method (307/308)
    /// should also preserve the request body. A redirect that changes the
    /// method to GET (301/302 from POST, 303) should drop the body.
    ///
    /// # Arguments
    ///
    /// * `original_method` - The HTTP method of the original request
    ///
    /// # Returns
    ///
    /// true if the request body should be forwarded to the redirect target
    pub fn redirect_preserves_body(&self, original_method: &HttpMethod) -> bool {
        match self.status_code {
            // 303 always drops body (switches to GET)
            303 => false,
            // 301/302 from POST switches to GET → drops body
            301 | 302 => *original_method != HttpMethod::Post,
            // 307/308 preserve method → preserve body
            307 | 308 => true,
            _ => true,
        }
    }

    /// Get the Location header and parse it as a URL
    ///
    /// Particularly useful for redirect responses. If it is a relative URL, it will be resolved based on the current request URL.
    ///
    /// # Returns
    ///
    /// Parsed absolute URL, or None if not present or parsing fails
    pub fn location(&self) -> Option<Url> {
        self.header("Location").and_then(|loc| Url::parse(loc).ok())
    }

    /// Get the decoded body using streaming decoders
    ///
    /// Automatically selects appropriate decoders based on the HTTP response's Content-Encoding and Transfer-Encoding headers
    /// to decode the response body. Supports GZip, Chunked, BZip2, and other encoding formats.
    ///
    /// Decodes transfer framing before applying the independent content
    /// encoding, matching the response pipeline used by the downloader.
    ///
    /// # Returns
    ///
    /// Decoded raw data, or an error message. Returns an empty vector if no body is present.
    ///
    /// # Errors
    ///
    /// - If the encoding format is invalid or the data is corrupted
    /// - If an I/O error occurs during decoding
    pub fn decoded_body(&self) -> Result<Vec<u8>> {
        use crate::http::stream_filter::{AutoFilterSelector, process_filters};

        let encoding = self.header("Content-Encoding").map(|s| s.as_str());
        let transfer_enc = self.header("Transfer-Encoding").map(|s| s.as_str());

        let mut filters = AutoFilterSelector::select_filters(encoding, transfer_enc)?;

        match &self.body {
            Some(raw_data) => process_filters(&mut filters, raw_data),
            None => Ok(Vec::new()),
        }
    }
}

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

    #[test]
    fn response_parser_delegates_headers_and_preserves_binary_body() {
        let mut response =
            b"HTTP/1.1 200 OK\r\nSet-Cookie: a=1\r\nSet-Cookie: b=2\r\n\r\n".to_vec();
        response.extend_from_slice(&[0, 0xff, b'x']);

        let parsed = HttpResponse::from_bytes(&response).unwrap();
        assert_eq!(parsed.status_code, 200);
        assert_eq!(parsed.header_all("set-cookie").len(), 2);
        assert_eq!(parsed.body.as_deref(), Some(&[0, 0xff, b'x'][..]));
    }

    #[test]
    fn test_response_status_parsing() {
        // Test 200 OK
        let response_200 = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<body>";
        let resp = HttpResponse::from_bytes(response_200.as_bytes()).unwrap();
        assert_eq!(resp.status_code, 200);
        assert_eq!(resp.reason_phrase, "OK");
        assert_eq!(resp.version, "HTTP/1.1");

        // Test 404 Not Found
        let response_404 = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\nNot Found";
        let resp = HttpResponse::from_bytes(response_404.as_bytes()).unwrap();
        assert_eq!(resp.status_code, 404);
        assert_eq!(resp.reason_phrase, "Not Found");

        // Test 301 Moved Permanently
        let response_301 = "HTTP/1.1 301 Moved Permanently\r\nLocation: /new-url\r\n\r\n";
        let resp = HttpResponse::from_bytes(response_301.as_bytes()).unwrap();
        assert_eq!(resp.status_code, 301);
        assert_eq!(resp.reason_phrase, "Moved Permanently");
    }

    #[test]
    fn test_response_multi_value_headers() {
        let response = "HTTP/1.1 200 OK\r\n\
                       Set-Cookie: session=abc123; Path=/\r\n\
                       Set-Cookie: user=john; Domain=example.com\r\n\
                       Content-Type: text/html\r\n\r\n<body>";

        let resp = HttpResponse::from_bytes(response.as_bytes()).unwrap();

        // Test getting all Set-Cookie values
        let all_cookies = resp.header_all("Set-Cookie");
        assert_eq!(all_cookies.len(), 2);
        assert!(all_cookies.contains(&"session=abc123; Path=/".to_string()));
        assert!(all_cookies.contains(&"user=john; Domain=example.com".to_string()));

        // Test getting the first value
        let first_cookie = resp.header("Set-Cookie").unwrap();
        assert_eq!(first_cookie, "session=abc123; Path=/");
    }

    #[test]
    fn test_response_content_length() {
        let response = "HTTP/1.1 200 OK\r\nContent-Length: 1024\r\n\r\n";
        let resp = HttpResponse::from_bytes(response.as_bytes()).unwrap();

        assert_eq!(resp.content_length(), Some(1024));

        // No Content-Length
        let response_no_cl = "HTTP/1.1 200 OK\r\n\r\n";
        let resp_no_cl = HttpResponse::from_bytes(response_no_cl.as_bytes()).unwrap();
        assert_eq!(resp_no_cl.content_length(), None);
    }

    #[test]
    fn test_response_is_redirect() {
        // Redirect status codes with Location header
        let redirect_resp = HttpResponse::from_bytes(
            "HTTP/1.1 301 Moved Permanently\r\nLocation: /new\r\n\r\n".as_bytes(),
        )
        .unwrap();
        assert!(redirect_resp.is_redirect());

        // 302 without Location header is NOT a valid redirect (C++ behavior)
        let redirect_302_no_loc =
            HttpResponse::from_bytes("HTTP/1.1 302 Found\r\n\r\n".as_bytes()).unwrap();
        assert!(!redirect_302_no_loc.is_redirect());

        // 302 WITH Location is a valid redirect
        let redirect_302_with_loc =
            HttpResponse::from_bytes("HTTP/1.1 302 Found\r\nLocation: /other\r\n\r\n".as_bytes())
                .unwrap();
        assert!(redirect_302_with_loc.is_redirect());

        // Non-redirect status codes
        let ok_resp = HttpResponse::from_bytes("HTTP/1.1 200 OK\r\n\r\n".as_bytes()).unwrap();
        assert!(!ok_resp.is_redirect());

        let error_resp =
            HttpResponse::from_bytes("HTTP/1.1 500 Internal Server Error\r\n\r\n".as_bytes())
                .unwrap();
        assert!(!error_resp.is_redirect());

        let not_modified =
            HttpResponse::from_bytes("HTTP/1.1 304 Not Modified\r\n\r\n".as_bytes()).unwrap();
        assert!(!not_modified.is_redirect());
    }

    #[test]
    fn test_redirect_status_set_matches_aria2() {
        for status in [300, 301, 302, 303, 307, 308] {
            assert!(
                is_redirect_status(status),
                "status {status} should redirect"
            );
        }
        for status in [304, 305, 306, 309, 399] {
            assert!(
                !is_redirect_status(status),
                "status {status} is not a redirect"
            );
        }
    }

    #[test]
    fn test_redirect_method_303_see_other() {
        // 303 always changes method to GET
        let resp =
            HttpResponse::from_bytes("HTTP/1.1 303 See Other\r\nLocation: /new\r\n\r\n".as_bytes())
                .unwrap();
        assert_eq!(resp.redirect_method(&HttpMethod::Post), HttpMethod::Get);
        assert_eq!(resp.redirect_method(&HttpMethod::Put), HttpMethod::Get);
        assert_eq!(resp.redirect_method(&HttpMethod::Get), HttpMethod::Get);
        // 303 always drops body
        assert!(!resp.redirect_preserves_body(&HttpMethod::Post));
    }

    #[test]
    fn test_redirect_method_301_302_post_to_get() {
        // 301/302 change POST→GET (C++ historical behavior)
        let resp_301 = HttpResponse::from_bytes(
            "HTTP/1.1 301 Moved Permanently\r\nLocation: /new\r\n\r\n".as_bytes(),
        )
        .unwrap();
        assert_eq!(resp_301.redirect_method(&HttpMethod::Post), HttpMethod::Get);
        assert!(!resp_301.redirect_preserves_body(&HttpMethod::Post));

        let resp_302 =
            HttpResponse::from_bytes("HTTP/1.1 302 Found\r\nLocation: /new\r\n\r\n".as_bytes())
                .unwrap();
        assert_eq!(resp_302.redirect_method(&HttpMethod::Post), HttpMethod::Get);
        // Non-POST methods preserved for 301/302
        assert_eq!(resp_301.redirect_method(&HttpMethod::Get), HttpMethod::Get);
        assert_eq!(
            resp_301.redirect_method(&HttpMethod::Head),
            HttpMethod::Head
        );
        assert!(resp_301.redirect_preserves_body(&HttpMethod::Get));
    }

    #[test]
    fn test_redirect_method_307_308_preserve() {
        // 307/308 preserve original method (RFC 7538, RFC 7231 §6.4.7)
        let resp_307 = HttpResponse::from_bytes(
            "HTTP/1.1 307 Temporary Redirect\r\nLocation: /new\r\n\r\n".as_bytes(),
        )
        .unwrap();
        assert_eq!(
            resp_307.redirect_method(&HttpMethod::Post),
            HttpMethod::Post
        );
        assert!(resp_307.redirect_preserves_body(&HttpMethod::Post));

        let resp_308 = HttpResponse::from_bytes(
            "HTTP/1.1 308 Permanent Redirect\r\nLocation: /new\r\n\r\n".as_bytes(),
        )
        .unwrap();
        assert_eq!(
            resp_308.redirect_method(&HttpMethod::Post),
            HttpMethod::Post
        );
        assert_eq!(resp_308.redirect_method(&HttpMethod::Get), HttpMethod::Get);
        assert!(resp_308.redirect_preserves_body(&HttpMethod::Post));
    }

    #[test]
    fn test_response_location() {
        let response =
            "HTTP/1.1 301 Moved Permanently\r\nLocation: https://example.com/new-page\r\n\r\n";
        let resp = HttpResponse::from_bytes(response.as_bytes()).unwrap();

        let location = resp.location().unwrap();
        assert_eq!(location.as_str(), "https://example.com/new-page");
    }

    #[test]
    fn test_response_body_parsing() {
        let response =
            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"status\":\"success\"}";
        let resp = HttpResponse::from_bytes(response.as_bytes()).unwrap();

        assert!(resp.body.is_some());
        assert_eq!(resp.body.unwrap(), b"{\"status\":\"success\"}");

        // No body
        let response_no_body = "HTTP/1.1 204 No Content\r\n\r\n";
        let resp_no_body = HttpResponse::from_bytes(response_no_body.as_bytes()).unwrap();
        assert!(resp_no_body.body.is_none());
    }

    #[test]
    fn test_response_case_insensitive_headers() {
        let response = "HTTP/1.1 200 OK\r\n\
                       Content-Type: text/html\r\n\
                       content-length: 100\r\n\r\n";

        let resp = HttpResponse::from_bytes(response.as_bytes()).unwrap();

        // Case-insensitive lookup
        assert!(resp.header("content-type").is_some());
        assert!(resp.header("CONTENT-TYPE").is_some());
        assert!(resp.header("Content-Length").is_some());
    }
}